Skip to content

Instantly share code, notes, and snippets.

@jbreiding
Last active March 15, 2022 21:13
Show Gist options
  • Save jbreiding/125320415f38b2db7e845925dd1beec0 to your computer and use it in GitHub Desktop.
Save jbreiding/125320415f38b2db7e845925dd1beec0 to your computer and use it in GitHub Desktop.
package tests
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"go.uber.org/zap"
)
type ServiceName string
type ServiceNameLogger struct {
Name ServiceName
Logger *zap.Logger
}
func (s *ServiceNameLogger) LogServiceName() {
s.Logger.Info("LogServiceName ", zap.String("svc_name", string(s.Name)))
}
func NewServiceNameLogger(name ServiceName, l *zap.Logger, lc fx.Lifecycle) *ServiceNameLogger {
snl := ServiceNameLogger{
Name: name,
Logger: l,
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
snl.LogServiceName()
return nil
},
OnStop: func(ctx context.Context) error {
snl.LogServiceName()
return nil
},
})
return &snl
}
func TestXxx(t *testing.T) {
t.Run("oops", func(t *testing.T) {
test3 := fx.Module(
"test3",
fx.Decorate(func() ServiceName {
return ServiceName("test3")
}),
fx.Decorate(func(s ServiceName, l *zap.Logger) *zap.Logger {
assert.Equal(t, "test3", string(s))
return l.With(zap.String("name", string(s)))
}),
fx.Decorate(NewServiceNameLogger),
fx.Invoke(func(snl *ServiceNameLogger) {
assert.Equal(t, "test3", string(snl.Name))
snl.LogServiceName()
}),
)
test2 := fx.Module(
"test2",
test3,
fx.Decorate(func() ServiceName {
return ServiceName("test2")
}),
fx.Decorate(func(s ServiceName, l *zap.Logger) *zap.Logger {
assert.Equal(t, "test2", string(s))
return l.With(zap.String("name", string(s)))
}),
fx.Decorate(NewServiceNameLogger),
fx.Invoke(func(snl *ServiceNameLogger) {
assert.Equal(t, "test2", string(snl.Name))
snl.LogServiceName()
}),
)
app := fxtest.New(t,
test2,
fx.Provide(func() ServiceName {
return ServiceName("test1")
}),
fx.Provide(func(s ServiceName) (*zap.Logger, error) {
assert.Equal(t, "test1", string(s))
return zap.NewDevelopment()
}),
fx.Provide(NewServiceNameLogger),
fx.Invoke(func(snl *ServiceNameLogger) {
assert.Equal(t, "test1", string(snl.Name))
snl.LogServiceName()
}),
)
defer app.RequireStart().RequireStop()
})
}
=== RUN TestXxx
=== RUN TestXxx/oops
writer.go:40: [Fx] PROVIDE tests.ServiceName <= go.temporal.io/server/tests.TestXxx.func1.7()
writer.go:40: [Fx] PROVIDE *zap.Logger <= go.temporal.io/server/tests.TestXxx.func1.8()
writer.go:40: [Fx] PROVIDE *tests.ServiceNameLogger <= go.temporal.io/server/tests.NewServiceNameLogger()
writer.go:40: [Fx] PROVIDE fx.Lifecycle <= go.uber.org/fx.New.func1()
writer.go:40: [Fx] PROVIDE fx.Shutdowner <= go.uber.org/fx.(*App).shutdowner-fm()
writer.go:40: [Fx] PROVIDE fx.DotGraph <= go.uber.org/fx.(*App).dotGraph-fm()
writer.go:40: [Fx] LOGGER Initialized custom logger from go.uber.org/fx/fxtest.New.func1()
writer.go:40: [Fx] DECORATE tests.ServiceName <= go.temporal.io/server/tests.TestXxx.func1.4()
writer.go:40: [Fx] DECORATE *zap.Logger <= go.temporal.io/server/tests.TestXxx.func1.5()
writer.go:40: [Fx] DECORATE *tests.ServiceNameLogger <= go.temporal.io/server/tests.NewServiceNameLogger()
writer.go:40: [Fx] DECORATE tests.ServiceName <= go.temporal.io/server/tests.TestXxx.func1.1()
writer.go:40: [Fx] DECORATE *zap.Logger <= go.temporal.io/server/tests.TestXxx.func1.2()
writer.go:40: [Fx] DECORATE *tests.ServiceNameLogger <= go.temporal.io/server/tests.NewServiceNameLogger()
writer.go:40: [Fx] INVOKE go.temporal.io/server/tests.TestXxx.func1.9()
2022-03-15T21:13:09.891Z INFO tests/fx_module_test.go:21 LogServiceName {"svc_name": "test1"}
writer.go:40: [Fx] INVOKE go.temporal.io/server/tests.TestXxx.func1.6()
fx_module_test.go:74:
Error Trace: fx_module_test.go:74
value.go:556
value.go:339
container.go:220
invoke.go:92
invoke.go:93
module.go:171
module.go:153
module.go:159
app.go:534
app.go:44
fx_module_test.go:79
Error: Not equal:
expected: "test2"
actual : "test1"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-test2
+test1
Test: TestXxx/oops
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"svc_name": "test1"}
writer.go:40: [Fx] INVOKE go.temporal.io/server/tests.TestXxx.func1.3()
fx_module_test.go:57:
Error Trace: fx_module_test.go:57
value.go:556
value.go:339
container.go:220
invoke.go:92
invoke.go:93
module.go:171
module.go:153
module.go:159
module.go:159
app.go:534
app.go:44
fx_module_test.go:79
Error: Not equal:
expected: "test3"
actual : "test2"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-test3
+test2
Test: TestXxx/oops
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"name": "test2", "svc_name": "test2"}
writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger)
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"svc_name": "test1"}
writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() called by go.temporal.io/server/tests.NewServiceNameLogger ran successfully in 6.292µs
writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger)
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"svc_name": "test1"}
writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() called by go.temporal.io/server/tests.NewServiceNameLogger ran successfully in 2.834µs
writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger)
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"name": "test2", "svc_name": "test2"}
writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() called by go.temporal.io/server/tests.NewServiceNameLogger ran successfully in 2.209µs
writer.go:40: [Fx] RUNNING
writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger)
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"name": "test2", "svc_name": "test2"}
writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() called by go.temporal.io/server/tests.NewServiceNameLogger ran successfully in 5.084µs
writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger)
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"svc_name": "test1"}
writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() called by go.temporal.io/server/tests.NewServiceNameLogger ran successfully in 2.125µs
writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger)
2022-03-15T21:13:09.892Z INFO tests/fx_module_test.go:21 LogServiceName {"svc_name": "test1"}
writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() called by go.temporal.io/server/tests.NewServiceNameLogger ran successfully in 2.667µs
--- FAIL: TestXxx (0.00s)
--- FAIL: TestXxx/oops (0.00s)
FAIL
FAIL go.temporal.io/server/tests 0.021s
FAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment