From 9015ffb920f034fb0467537d55c6585eb3800934 Mon Sep 17 00:00:00 2001 From: "Y.Horie" Date: Fri, 1 Jul 2022 14:47:45 +0900 Subject: [PATCH] test(contrib): add unit test for fluent.go (#2141) --- contrib/log/fluent/fluent_test.go | 123 ++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/contrib/log/fluent/fluent_test.go b/contrib/log/fluent/fluent_test.go index 8558f31c7..5b7b3e171 100644 --- a/contrib/log/fluent/fluent_test.go +++ b/contrib/log/fluent/fluent_test.go @@ -5,6 +5,7 @@ import ( "net" "os" "testing" + "time" "github.com/go-kratos/kratos/v2/log" ) @@ -29,6 +30,96 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestWithTimeout(t *testing.T) { + opts := new(options) + var duration time.Duration = 1000000000 + funcTimeout := WithTimeout(duration) + funcTimeout(opts) + if opts.timeout != duration { + t.Errorf("WithTimeout() = %v, want %v", opts.timeout, duration) + } +} + +func TestWithWriteTimeout(t *testing.T) { + opts := new(options) + var duration time.Duration = 1000000000 + funcWriteTimeout := WithWriteTimeout(duration) + funcWriteTimeout(opts) + if opts.writeTimeout != duration { + t.Errorf("WithWriteTimeout() = %v, want %v", opts.writeTimeout, duration) + } +} + +func TestWithBufferLimit(t *testing.T) { + opts := new(options) + bufferLimit := 1000000000 + funcBufferLimit := WithBufferLimit(bufferLimit) + funcBufferLimit(opts) + if opts.bufferLimit != bufferLimit { + t.Errorf("WithBufferLimit() = %d, want %d", opts.bufferLimit, bufferLimit) + } +} + +func TestWithRetryWait(t *testing.T) { + opts := new(options) + retryWait := 1000000000 + funcRetryWait := WithRetryWait(retryWait) + funcRetryWait(opts) + if opts.retryWait != retryWait { + t.Errorf("WithRetryWait() = %d, want %d", opts.retryWait, retryWait) + } +} + +func TestWithMaxRetry(t *testing.T) { + opts := new(options) + maxRetry := 1000000000 + funcMaxRetry := WithMaxRetry(maxRetry) + funcMaxRetry(opts) + if opts.maxRetry != maxRetry { + t.Errorf("WithMaxRetry() = %d, want %d", opts.maxRetry, maxRetry) + } +} + +func TestWithMaxRetryWait(t *testing.T) { + opts := new(options) + maxRetryWait := 1000000000 + funcMaxRetryWait := WithMaxRetryWait(maxRetryWait) + funcMaxRetryWait(opts) + if opts.maxRetryWait != maxRetryWait { + t.Errorf("WithMaxRetryWait() = %d, want %d", opts.maxRetryWait, maxRetryWait) + } +} + +func TestWithTagPrefix(t *testing.T) { + opts := new(options) + tagPrefix := "tag_prefix" + funcTagPrefix := WithTagPrefix(tagPrefix) + funcTagPrefix(opts) + if opts.tagPrefix != tagPrefix { + t.Errorf("WithTagPrefix() = %s, want %s", opts.tagPrefix, tagPrefix) + } +} + +func TestWithAsync(t *testing.T) { + opts := new(options) + async := true + funcAsync := WithAsync(async) + funcAsync(opts) + if opts.async != async { + t.Errorf("WithAsync() = %t, want %t", opts.async, async) + } +} + +func TestWithForceStopAsyncSend(t *testing.T) { + opts := new(options) + forceStopAsyncSend := true + funcForceStopAsyncSend := WithForceStopAsyncSend(forceStopAsyncSend) + funcForceStopAsyncSend(opts) + if opts.forceStopAsyncSend != forceStopAsyncSend { + t.Errorf("WithForceStopAsyncSend() = %t, want %t", opts.forceStopAsyncSend, forceStopAsyncSend) + } +} + func TestLogger(t *testing.T) { logger, err := NewLogger("tcp://127.0.0.1:24224") if err != nil { @@ -43,6 +134,38 @@ func TestLogger(t *testing.T) { flog.Error("log", "test") } +func TestLoggerWithOpt(t *testing.T) { + var duration time.Duration = 1000000000 + logger, err := NewLogger("tcp://127.0.0.1:24224", WithTimeout(duration)) + if err != nil { + t.Error(err) + } + defer logger.Close() + flog := log.NewHelper(logger) + + flog.Debug("log", "test") + flog.Info("log", "test") + flog.Warn("log", "test") + flog.Error("log", "test") +} + +func TestLoggerError(t *testing.T) { + errCase := []string{ + "foo", + "tcp://127.0.0.1/", + "tcp://127.0.0.1:1234a", + "tcp://127.0.0.1:65535", + "https://127.0.0.1:8080", + "unix://foo/bar", + } + for _, errc := range errCase { + _, err := NewLogger(errc) + if err == nil { + t.Error(err) + } + } +} + func BenchmarkLoggerPrint(b *testing.B) { b.SetParallelism(100) logger, err := NewLogger("tcp://127.0.0.1:24224")