Last active
March 15, 2022 21:13
-
-
Save jbreiding/125320415f38b2db7e845925dd1beec0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tests | |
| import ( | |
| "context" | |
| "fmt" | |
| "log" | |
| "os" | |
| "testing" | |
| "go.uber.org/fx" | |
| "go.uber.org/fx/fxtest" | |
| ) | |
| type ServiceName string | |
| type ServiceNameLogger struct { | |
| Name ServiceName | |
| Logger *log.Logger | |
| } | |
| func (s *ServiceNameLogger) LogServiceName() { | |
| s.Logger.Println("ServiceName ", s.Name) | |
| } | |
| func NewServiceNameLogger(name ServiceName, logger *log.Logger, lc fx.Lifecycle) *ServiceNameLogger { | |
| logger.Print("NewServiceNameLogger ", name) | |
| snl := ServiceNameLogger{ | |
| Name: name, | |
| Logger: logger, | |
| } | |
| 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 | |
| } | |
| var Module = fx.Options( | |
| fx.Provide(func() ServiceName { | |
| return ServiceName("test1") | |
| }), | |
| fx.Provide(func(s ServiceName) *log.Logger { | |
| return log.New(os.Stdout, fmt.Sprintf("%s: ", s), 0) | |
| }), | |
| fx.Provide(NewServiceNameLogger), | |
| fx.Invoke(func(snl *ServiceNameLogger) { | |
| snl.LogServiceName() | |
| }), | |
| ) | |
| 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(NewServiceNameLogger), | |
| fx.Decorate(func(s ServiceName) *log.Logger { | |
| return log.New(os.Stdout, fmt.Sprintf("%s: ", s), 0) | |
| }), | |
| fx.Invoke(func(snl *ServiceNameLogger) { | |
| snl.LogServiceName() | |
| }), | |
| ) | |
| test2 := fx.Module( | |
| "test2", | |
| test3, | |
| fx.Decorate(func() ServiceName { | |
| return ServiceName("test2") | |
| }), | |
| fx.Decorate(NewServiceNameLogger), | |
| fx.Decorate(func(s ServiceName) *log.Logger { | |
| return log.New(os.Stdout, fmt.Sprintf("%s: ", s), 0) | |
| }), | |
| fx.Invoke(func(snl *ServiceNameLogger) { | |
| snl.LogServiceName() | |
| }), | |
| ) | |
| app := fxtest.New(t, | |
| test2, | |
| Module, | |
| ) | |
| defer app.RequireStart().RequireStop() | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //go test -v -timeout 20m -run ^TestXxx$ go.temporal.io/server/tests | |
| === RUN TestXxx | |
| === RUN TestXxx/oops | |
| writer.go:40: [Fx] PROVIDE tests.ServiceName <= go.temporal.io/server/tests.glob..func1() | |
| writer.go:40: [Fx] PROVIDE *log.Logger <= go.temporal.io/server/tests.glob..func2() | |
| 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 *tests.ServiceNameLogger <= go.temporal.io/server/tests.NewServiceNameLogger() | |
| writer.go:40: [Fx] DECORATE *log.Logger <= go.temporal.io/server/tests.TestXxx.func1.5() | |
| writer.go:40: [Fx] DECORATE tests.ServiceName <= go.temporal.io/server/tests.TestXxx.func1.1() | |
| writer.go:40: [Fx] DECORATE *tests.ServiceNameLogger <= go.temporal.io/server/tests.NewServiceNameLogger() | |
| writer.go:40: [Fx] DECORATE *log.Logger <= go.temporal.io/server/tests.TestXxx.func1.2() | |
| writer.go:40: [Fx] INVOKE go.temporal.io/server/tests.glob..func3() | |
| test1: NewServiceNameLogger test1 | |
| test1: ServiceName test1 | |
| writer.go:40: [Fx] INVOKE go.temporal.io/server/tests.TestXxx.func1.6() | |
| test1: NewServiceNameLogger test1 | |
| test1: ServiceName test1 | |
| writer.go:40: [Fx] INVOKE go.temporal.io/server/tests.TestXxx.func1.3() | |
| test2: NewServiceNameLogger test2 | |
| test2: ServiceName test2 | |
| writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger) | |
| test1: ServiceName 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 875ns | |
| writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger) | |
| test1: ServiceName 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 583ns | |
| writer.go:40: [Fx] HOOK OnStart go.temporal.io/server/tests.NewServiceNameLogger.func1() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger) | |
| test2: ServiceName 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 542ns | |
| 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) | |
| test2: ServiceName 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 667ns | |
| writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger) | |
| test1: ServiceName 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 542ns | |
| writer.go:40: [Fx] HOOK OnStop go.temporal.io/server/tests.NewServiceNameLogger.func2() executing (caller: go.temporal.io/server/tests.NewServiceNameLogger) | |
| test1: ServiceName 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 500ns | |
| --- PASS: TestXxx (0.00s) | |
| --- PASS: TestXxx/oops (0.00s) | |
| PASS | |
| ok go.temporal.io/server/tests 0.021s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment