Skip to content

Commit

Permalink
✅ test: add some new tests case for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 1, 2023
1 parent 8348571 commit 61216e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type testHandler struct {
errOnHandle bool
errOnFlush bool
errOnClose bool
// hooks
callOnFlush func()
}

func newTestHandler() *testHandler {
Expand All @@ -151,6 +153,9 @@ func (h *testHandler) Flush() error {
if h.errOnFlush {
return errorx.Raw("flush error")
}
if h.callOnFlush != nil {
h.callOnFlush()
}

h.Reset()
return nil
Expand Down
20 changes: 20 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ func TestLogger_option_BackupArgs(t *testing.T) {
assert.StrContains(t, s, "field message3")
}

func TestLogger_FlushTimeout(t *testing.T) {
h := newTestHandler()
l := slog.NewWithHandlers(h)

// test flush error
h.errOnFlush = true
l.FlushTimeout(time.Millisecond * 2)

// test flush timeout
h.errOnFlush = false
h.callOnFlush = func() {
time.Sleep(time.Millisecond * 25)
}
l.FlushTimeout(time.Millisecond * 20)

assert.Panics(t, func() {
l.StopDaemon()
})
}

func TestLogger_rewrite_record(t *testing.T) {
h := newTestHandler()
l := slog.NewWithHandlers(h)
Expand Down
4 changes: 4 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func TestInner_parseTemplateToFields(t *testing.T) {
// dump.P(ss, str)
}

func TestUtil_EncodeToString(t *testing.T) {
assert.Eq(t, "{a:1}", EncodeToString(map[string]any{"a": 1}))
}

func TestUtil_formatArgsWithSpaces(t *testing.T) {
// tests for formatArgsWithSpaces
tests := []struct {
Expand Down

0 comments on commit 61216e2

Please sign in to comment.