Skip to content

Commit

Permalink
Refactor tests for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dl1998 committed Mar 31, 2024
1 parent f97e855 commit e8def2f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 50 deletions.
59 changes: 40 additions & 19 deletions pkg/logger/asynclogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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},
}

Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -172,18 +193,18 @@ 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))
}

// BenchmarkNewAsyncLogger benchmarks NewAsyncLogger function of the
// baseAsyncLogger.
func BenchmarkNewAsyncLogger(b *testing.B) {
for index := 0; index < b.N; index++ {
NewAsyncLogger(name, testTimeFormat, messageQueueSize)
NewAsyncLogger(loggerName, timeFormat, messageQueueSize)
}
}

Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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},
}

Expand Down
19 changes: 15 additions & 4 deletions pkg/logger/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand All @@ -222,7 +233,7 @@ func TestBaseLogger_SetName(t *testing.T) {
},
}

newName := "new-name"
newName := "new-loggerName"

newBaseLogger.SetName(newName)

Expand All @@ -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)
Expand Down
40 changes: 13 additions & 27 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{}

Expand Down Expand Up @@ -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().
Expand All @@ -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().
Expand All @@ -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)
Expand All @@ -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().
Expand All @@ -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()

Expand All @@ -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().
Expand All @@ -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().
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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())

Expand All @@ -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().
Expand Down

0 comments on commit e8def2f

Please sign in to comment.