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 4d70620
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 44 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):%(loggerName):%(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
32 changes: 11 additions & 21 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 @@ -461,7 +451,7 @@ func BenchmarkWithToLevel(b *testing.B) {
func TestWithTemplate(t *testing.T) {
configuration := NewConfiguration()

template := "%(message):%(name):%(level)"
template := "%(message):%(loggerName):%(level)"

option := WithTemplate(template)

Expand All @@ -474,7 +464,7 @@ func TestWithTemplate(t *testing.T) {
func BenchmarkWithTemplate(b *testing.B) {
configuration := NewConfiguration()

template := "%(message):%(name):%(level)"
template := "%(message):%(loggerName):%(level)"

option := WithTemplate(template)

Expand Down Expand Up @@ -509,7 +499,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 Down Expand Up @@ -577,7 +567,7 @@ func TestNewConfiguration(t *testing.T) {
options: []Option{},
expectedFromLevel: level.Warning,
expectedToLevel: level.Null,
expectedTemplate: "%(level):%(name):%(message)",
expectedTemplate: "%(level):%(loggerName):%(message)",
expectedFile: "",
expectedName: "root",
expectedTimeFormat: time.RFC3339,
Expand All @@ -586,14 +576,14 @@ func TestNewConfiguration(t *testing.T) {
options: []Option{
WithFromLevel(level.All),
WithToLevel(level.Emergency),
WithTemplate("%(message):%(name):%(level)"),
WithTemplate("%(message):%(loggerName):%(level)"),
WithFile("file.log"),
WithName("test"),
WithTimeFormat(time.DateTime),
},
expectedFromLevel: level.All,
expectedToLevel: level.Emergency,
expectedTemplate: "%(message):%(name):%(level)",
expectedTemplate: "%(message):%(loggerName):%(level)",
expectedFile: "file.log",
expectedName: "test",
expectedTimeFormat: time.DateTime,
Expand Down Expand Up @@ -626,7 +616,7 @@ func TestConfigure(t *testing.T) {
configuration := NewConfiguration(
WithFromLevel(level.All),
WithToLevel(level.Emergency),
WithTemplate("%(message):%(name):%(level)"),
WithTemplate("%(message):%(loggerName):%(level)"),
WithFile(""),
WithName("test"),
)
Expand Down Expand Up @@ -659,7 +649,7 @@ func BenchmarkConfigure(b *testing.B) {
configuration := NewConfiguration(
WithFromLevel(level.All),
WithToLevel(level.Emergency),
WithTemplate("%(message):%(name):%(level)"),
WithTemplate("%(message):%(loggerName):%(level)"),
WithFile(""),
WithName("test"),
)
Expand All @@ -669,7 +659,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 +679,7 @@ func BenchmarkName(b *testing.B) {
func TestTemplate(t *testing.T) {
Configure(NewConfiguration())

testutils.AssertEquals(t, "%(level):%(name):%(message)", Template())
testutils.AssertEquals(t, "%(level):%(loggerName):%(message)", Template())
}

// BenchmarkTemplate perform benchmarking of the Template().
Expand Down

0 comments on commit 4d70620

Please sign in to comment.