Files
tasks/common/testing/testlogger/testlogger_test.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

149 lines
4.0 KiB
Go

package testlogger_test
import (
"fmt"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/testing/testlogger"
)
// mockT wraps a testing.T such that we can know if a test is failed.
type mockT struct {
*testing.T
failure atomic.Pointer[string]
}
// Errorf implements testlogger.TestingT.
func (m *mockT) Errorf(msg string, args ...any) {
s := fmt.Sprintf("ERROR: "+msg, args...)
m.failure.Store(&s)
}
// Fail implements testlogger.TestingT.
func (m *mockT) Fail() {
s := "Fail() called"
m.failure.Store(&s)
}
// FailNow implements testlogger.TestingT.
func (m *mockT) FailNow() {
s := "FailNow() called"
m.failure.Store(&s)
}
// Failed implements testlogger.TestingT.
func (m *mockT) Failed() bool {
return m.failure.Load() != nil
}
// Fatal implements testlogger.TestingT.
func (m *mockT) Fatal(args ...any) {
s := fmt.Sprintf("FATAL: %v", args...)
m.failure.Store(&s)
}
// Fatalf implements testlogger.TestingT.
func (m *mockT) Fatalf(format string, args ...any) {
s := fmt.Sprintf(format, args...)
m.failure.Store(&s)
}
var _ testlogger.TestingT = (*mockT)(nil)
func assertMatches(t *testing.T, level testlogger.Level, msg string, tags []tag.Tag) {
tl := testlogger.NewTestLogger(t, testlogger.FailOnAnyUnexpectedError)
tl.Expect(level, msg, tags...)
switch level {
case testlogger.Error:
tl.Error(msg, tags...)
case testlogger.Fatal:
require.Panics(t, func() {
tl.Fatal(msg, tags...)
})
case testlogger.DPanic:
require.NotPanics(t, func() {
tl.DPanic(msg, tags...)
})
case testlogger.Panic:
require.Panics(t, func() {
tl.Panic(msg, tags...)
})
default:
t.Fatalf("Unknown log level %s", level)
}
}
func TestTestLogger_ExpectationsMatch(t *testing.T) {
for _, level := range []testlogger.Level{testlogger.Error, testlogger.DPanic, testlogger.Panic, testlogger.Fatal} {
t.Run(level.String()+" with tags", func(t *testing.T) {
assertMatches(t, level, "message with tags", []tag.Tag{tag.String("key", "value")})
})
t.Run(level.String()+" without tags", func(t *testing.T) {
assertMatches(t, level, "message without tags", nil)
})
t.Run(level.String()+" no message only tags", func(t *testing.T) {
assertMatches(t, level, "", []tag.Tag{tag.String("key", "value")})
})
}
}
func assertFails(t *testing.T, level testlogger.Level, msg string, tags []tag.Tag) {
mt := &mockT{T: t}
tl := testlogger.NewTestLogger(mt, testlogger.FailOnAnyUnexpectedError)
switch level {
case testlogger.Error:
tl.Error(msg, tags...)
require.True(t, mt.Failed())
case testlogger.Fatal:
tl.Fatal(msg, tags...)
require.True(t, mt.Failed())
case testlogger.DPanic:
tl.DPanic(msg, tags...)
require.True(t, mt.Failed())
case testlogger.Panic:
tl.Panic(msg, tags...)
require.True(t, mt.Failed())
default:
t.Fatalf("Unknown log level %s", level)
}
}
func TestTestLogger_Uncaught(t *testing.T) {
// Non-panicking levels
for _, level := range []testlogger.Level{testlogger.Error, testlogger.DPanic} {
t.Run(level.String()+" with tags", func(t *testing.T) {
assertFails(t, level, "message with tags", []tag.Tag{tag.String("key", "value")})
})
t.Run(level.String()+" without tags", func(t *testing.T) {
assertFails(t, level, "message without tags", nil)
})
t.Run(level.String()+" no message only tags", func(t *testing.T) {
assertFails(t, level, "", []tag.Tag{tag.String("key", "value")})
})
}
// Panicking levels
for _, level := range []testlogger.Level{testlogger.Panic, testlogger.Fatal} {
t.Run(level.String()+" with tags", func(t *testing.T) {
require.Panics(t, func() {
assertFails(t, level, "message with tags", []tag.Tag{tag.String("key", "value")})
})
})
t.Run(level.String()+" without tags", func(t *testing.T) {
require.Panics(t, func() {
assertFails(t, level, "message without tags", nil)
})
})
t.Run(level.String()+" no message only tags", func(t *testing.T) {
require.Panics(t, func() {
assertFails(t, level, "", []tag.Tag{tag.String("key", "value")})
})
})
}
}