diff --git a/pkg/logger/asynclogger_test.go b/pkg/logger/asynclogger_test.go index 9c8adc7..fecff78 100644 --- a/pkg/logger/asynclogger_test.go +++ b/pkg/logger/asynclogger_test.go @@ -6,22 +6,43 @@ import ( "github.com/dl1998/go-logging/pkg/logger/logrecord" "sync" "testing" - "time" ) var ( - name = "test" messageQueueSize = 1 - testTimeFormat = time.RFC3339 ) +// TestBaseAsyncLogger_startListeningMessages tests that +// baseAsyncLogger.startListeningMessages method processes logs from the message +// queue. +func TestBaseAsyncLogger_startListeningMessages(t *testing.T) { + mockHandler := &MockHandler{} + newBaseAsyncLogger := &baseAsyncLogger{ + baseLogger: &baseLogger{ + name: loggerName, + handlers: []handler.Interface{mockHandler}, + }, + messageQueue: make(chan logrecord.Interface, messageQueueSize), + waitGroup: sync.WaitGroup{}, + } + + record := logrecord.New(loggerName, 0, timeFormat, message, parameters, 3) + newBaseAsyncLogger.messageQueue <- record + + go newBaseAsyncLogger.startListeningMessages() + newBaseAsyncLogger.waitGroup.Add(1) + newBaseAsyncLogger.waitGroup.Wait() + + testutils.AssertEquals(t, record, mockHandler.Parameters[0].(*logrecord.LogRecord)) +} + // TestBaseAsyncLogger_WaitToFinishLogging tests // baseAsyncLogger.WaitToFinishLogging method of the baseAsyncLogger. func TestBaseAsyncLogger_WaitToFinishLogging(t *testing.T) { mockHandler := &MockHandler{} newBaseAsyncLogger := &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -47,7 +68,7 @@ func TestBaseAsyncLogger_Open(t *testing.T) { mockHandler := &MockHandler{} newBaseAsyncLogger := &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -65,7 +86,7 @@ func BenchmarkBaseAsyncLogger_Open(b *testing.B) { mockHandler := &MockHandler{} newBaseAsyncLogger := &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -93,7 +114,7 @@ func TestBaseAsyncLogger_Close(t *testing.T) { mockHandler := &MockHandler{} newBaseAsyncLogger := &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -111,7 +132,7 @@ func BenchmarkBaseAsyncLogger_Close(b *testing.B) { loggers := make([]*baseAsyncLogger, b.N) mockHandler := &MockHandler{} newBaseLogger := &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, } @@ -135,7 +156,7 @@ func TestBaseAsyncLogger_Log(t *testing.T) { mockHandler := &MockHandler{} newBaseAsyncLogger := &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -156,7 +177,7 @@ func BenchmarkBaseAsyncLogger_Log(b *testing.B) { mockHandler := &MockHandler{} newBaseAsyncLogger := &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, b.N), @@ -172,10 +193,10 @@ func BenchmarkBaseAsyncLogger_Log(b *testing.B) { // TestNewAsyncLogger tests NewAsyncLogger function of the baseAsyncLogger. func TestNewAsyncLogger(t *testing.T) { - newAsyncLogger := NewAsyncLogger(name, testTimeFormat, messageQueueSize) + newAsyncLogger := NewAsyncLogger(loggerName, timeFormat, messageQueueSize) testutils.AssertNotNil(t, newAsyncLogger) - testutils.AssertEquals(t, name, newAsyncLogger.Name()) + testutils.AssertEquals(t, loggerName, newAsyncLogger.Name()) testutils.AssertEquals(t, messageQueueSize, cap(newAsyncLogger.baseLogger.(*baseAsyncLogger).messageQueue)) } @@ -183,7 +204,7 @@ func TestNewAsyncLogger(t *testing.T) { // baseAsyncLogger. func BenchmarkNewAsyncLogger(b *testing.B) { for index := 0; index < b.N; index++ { - NewAsyncLogger(name, testTimeFormat, messageQueueSize) + NewAsyncLogger(loggerName, timeFormat, messageQueueSize) } } @@ -195,7 +216,7 @@ func TestAsyncLogger_WaitToFinishLogging(t *testing.T) { Logger: &Logger{ baseLogger: &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -224,7 +245,7 @@ func BenchmarkAsyncLogger_WaitToFinishLogging(b *testing.B) { Logger: &Logger{ baseLogger: &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -245,7 +266,7 @@ func TestAsyncLogger_Open(t *testing.T) { Logger: &Logger{ baseLogger: &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -267,7 +288,7 @@ func BenchmarkAsyncLogger_Open(b *testing.B) { Logger: &Logger{ baseLogger: &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -288,7 +309,7 @@ func TestAsyncLogger_Close(t *testing.T) { Logger: &Logger{ baseLogger: &baseAsyncLogger{ baseLogger: &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, }, messageQueue: make(chan logrecord.Interface, messageQueueSize), @@ -308,7 +329,7 @@ func BenchmarkAsyncLogger_Close(b *testing.B) { loggers := make([]*AsyncLogger, b.N) mockHandler := &MockHandler{} newBaseLogger := &baseLogger{ - name: name, + name: loggerName, handlers: []handler.Interface{mockHandler}, } diff --git a/pkg/logger/common_test.go b/pkg/logger/common_test.go index 8ab77af..729ad96 100644 --- a/pkg/logger/common_test.go +++ b/pkg/logger/common_test.go @@ -9,6 +9,17 @@ import ( "github.com/dl1998/go-logging/pkg/logger/logrecord" "io" "testing" + "time" +) + +var ( + loggerTemplate = "%(level):%(name):%(message)" + loggerName = "test" + message = "Test Message: %s." + parameters = []any{ + "test", + } + timeFormat = time.RFC3339 ) // MockLogger is used to mock baseLogger. @@ -186,7 +197,7 @@ func BenchmarkBaseLogger_Log(b *testing.B) { } } -// TestBaseLogger_Name tests that baseLogger.Name returns name of the logger. +// TestBaseLogger_Name tests that baseLogger.Name returns loggerName of the logger. func TestBaseLogger_Name(t *testing.T) { newBaseLogger := &baseLogger{ name: loggerName, @@ -212,7 +223,7 @@ func BenchmarkBaseLogger_Name(b *testing.B) { } } -// TestBaseLogger_SetName tests that baseLogger.SetName set a new name for the +// TestBaseLogger_SetName tests that baseLogger.SetName set a new loggerName for the // logger. func TestBaseLogger_SetName(t *testing.T) { newBaseLogger := &baseLogger{ @@ -222,7 +233,7 @@ func TestBaseLogger_SetName(t *testing.T) { }, } - newName := "new-name" + newName := "new-loggerName" newBaseLogger.SetName(newName) @@ -238,7 +249,7 @@ func BenchmarkBaseLogger_SetName(b *testing.B) { }, } - newName := "new-name" + newName := "new-loggerName" for index := 0; index < b.N; index++ { newBaseLogger.SetName(newName) diff --git a/pkg/logger/logger_test.go b/pkg/logger/logger_test.go index 82c43df..cc0d665 100644 --- a/pkg/logger/logger_test.go +++ b/pkg/logger/logger_test.go @@ -9,16 +9,6 @@ import ( "time" ) -var ( - loggerTemplate = "%(level):%(name):%(message)" - loggerName = "test" - message = "Test Message: %s." - parameters = []any{ - "test", - } - timeFormat = time.RFC3339 -) - // TestNew tests that New creates a new logger. func TestNew(t *testing.T) { newLogger := New(loggerName, timeFormat) @@ -37,7 +27,7 @@ func BenchmarkNew(b *testing.B) { } } -// TestLogger_Name tests that Logger.Name returns name of the logger. +// TestLogger_Name tests that Logger.Name returns loggerName of the logger. func TestLogger_Name(t *testing.T) { mockLogger := &MockLogger{} @@ -420,7 +410,7 @@ func TestWithFromLevel(t *testing.T) { option(configuration) - testutils.AssertEquals(t, configuration.fromLevel, level.Trace) + testutils.AssertEquals(t, level.Trace, configuration.fromLevel) } // BenchmarkWithFromLevel perform benchmarking of the WithFromLevel(). @@ -442,7 +432,7 @@ func TestWithToLevel(t *testing.T) { option(configuration) - testutils.AssertEquals(t, configuration.toLevel, level.Trace) + testutils.AssertEquals(t, level.Trace, configuration.toLevel) } // BenchmarkWithToLevel perform benchmarking of the WithToLevel(). @@ -461,22 +451,18 @@ func BenchmarkWithToLevel(b *testing.B) { func TestWithTemplate(t *testing.T) { configuration := NewConfiguration() - template := "%(message):%(name):%(level)" - - option := WithTemplate(template) + option := WithTemplate(loggerTemplate) option(configuration) - testutils.AssertEquals(t, configuration.template, template) + testutils.AssertEquals(t, loggerTemplate, configuration.template) } // BenchmarkWithTemplate perform benchmarking of the WithTemplate(). func BenchmarkWithTemplate(b *testing.B) { configuration := NewConfiguration() - template := "%(message):%(name):%(level)" - - option := WithTemplate(template) + option := WithTemplate(loggerTemplate) for index := 0; index < b.N; index++ { option(configuration) @@ -493,7 +479,7 @@ func TestWithFile(t *testing.T) { option(configuration) - testutils.AssertEquals(t, configuration.file, file) + testutils.AssertEquals(t, file, configuration.file) } // BenchmarkWithFile perform benchmarking of the WithFile(). @@ -509,7 +495,7 @@ func BenchmarkWithFile(b *testing.B) { } } -// TestWithName tests that WithName sets the name in the Configuration. +// TestWithName tests that WithName sets the loggerName in the Configuration. func TestWithName(t *testing.T) { configuration := NewConfiguration() @@ -519,7 +505,7 @@ func TestWithName(t *testing.T) { option(configuration) - testutils.AssertEquals(t, configuration.name, name) + testutils.AssertEquals(t, name, configuration.name) } // BenchmarkWithName perform benchmarking of the WithName(). @@ -546,7 +532,7 @@ func TestWithTimeFormat(t *testing.T) { option(configuration) - testutils.AssertEquals(t, configuration.timeFormat, timeFormat) + testutils.AssertEquals(t, timeFormat, configuration.timeFormat) } // BenchmarkWithTimeFormat perform benchmarking of the WithTimeFormat(). @@ -577,7 +563,7 @@ func TestNewConfiguration(t *testing.T) { options: []Option{}, expectedFromLevel: level.Warning, expectedToLevel: level.Null, - expectedTemplate: "%(level):%(name):%(message)", + expectedTemplate: loggerTemplate, expectedFile: "", expectedName: "root", expectedTimeFormat: time.RFC3339, @@ -669,7 +655,7 @@ func BenchmarkConfigure(b *testing.B) { } } -// TestName tests that Name returns name of the default logger. +// TestName tests that Name returns loggerName of the default logger. func TestName(t *testing.T) { Configure(NewConfiguration()) @@ -689,7 +675,7 @@ func BenchmarkName(b *testing.B) { func TestTemplate(t *testing.T) { Configure(NewConfiguration()) - testutils.AssertEquals(t, "%(level):%(name):%(message)", Template()) + testutils.AssertEquals(t, loggerTemplate, Template()) } // BenchmarkTemplate perform benchmarking of the Template().