Skip to content

Commit 51b2d72

Browse files
committed
use panic
1 parent 81df793 commit 51b2d72

File tree

3 files changed

+27
-56
lines changed

3 files changed

+27
-56
lines changed

common/testing/testhooks/noop_impl.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,38 @@
33
package testhooks
44

55
import (
6-
"go.temporal.io/server/common/log"
7-
"go.temporal.io/server/common/softassert"
86
"go.uber.org/fx"
97
)
108

119
var Module = fx.Options(
1210
fx.Provide(NewTestHooks),
1311
)
1412

15-
// TestHooks (in production mode) is an empty struct just so the build works.
16-
// See TestHooks in test_impl.go.
17-
//
18-
// TestHooks are an inherently unclean way of writing tests. They require mixing test-only
19-
// concerns into production code. In general you should prefer other ways of writing tests
20-
// wherever possible, and only use TestHooks sparingly, as a last resort.
21-
type TestHooks struct {
22-
logger log.Logger
23-
}
13+
// noopTestHooks is a noop implementation of TestHooks for production builds.
14+
type noopTestHooks struct{}
2415

25-
func NewTestHooks(logger log.Logger) TestHooks {
26-
return TestHooks{logger: logger}
16+
func NewTestHooks() TestHooks {
17+
return noopTestHooks{}
2718
}
2819

20+
func (noopTestHooks) testHooks() {}
21+
2922
// Get gets the value of a test hook. In production mode it always returns the zero value and
3023
// false, which hopefully the compiler will inline and remove the hook as dead code.
3124
//
3225
// TestHooks should be used very sparingly, see comment on TestHooks.
33-
func Get[T any](_ TestHooks, key Key) (T, bool) {
26+
func Get[T any](_ TestHooks, _ Key) (T, bool) {
3427
var zero T
3528
return zero, false
3629
}
3730

3831
// Call calls a func() hook if present.
3932
//
4033
// TestHooks should be used very sparingly, see comment on TestHooks.
41-
func Call(_ TestHooks, key Key) {
34+
func Call(_ TestHooks, _ Key) {
4235
}
4336

4437
// Set is only to be used by test code together with the test_dep build tag.
45-
func Set[T any](th TestHooks, key Key, val T) func() {
46-
softassert.Fail(th.logger, "testhooks.Set called without test_dep build tag")
47-
return func() {}
38+
func Set[T any](_ TestHooks, _ Key, _ T) func() {
39+
panic("testhooks.Set called but TestHooks are not enabled: use -tags=test_dep when running `go test`")
4840
}

common/testing/testhooks/test_impl.go

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,24 @@ package testhooks
55
import (
66
"sync"
77

8-
"go.temporal.io/server/common/log"
98
"go.uber.org/fx"
109
)
1110

1211
var Module = fx.Options(
1312
fx.Provide(NewTestHooks),
1413
)
1514

16-
type (
17-
// TestHooks holds a registry of active test hooks. It should be obtained through fx and
18-
// used with Get and Set.
19-
//
20-
// TestHooks are an inherently unclean way of writing tests. They require mixing test-only
21-
// concerns into production code. In general you should prefer other ways of writing tests
22-
// wherever possible, and only use TestHooks sparingly, as a last resort.
23-
TestHooks interface {
24-
// private accessors; access must go through package-level Get/Set
25-
get(Key) (any, bool)
26-
set(Key, any)
27-
del(Key)
28-
}
29-
30-
// testHooksImpl is an implementation of TestHooks.
31-
testHooksImpl struct {
32-
m sync.Map
33-
}
34-
)
15+
// testHooksImpl is an implementation of TestHooks.
16+
type testHooksImpl struct {
17+
m sync.Map
18+
}
3519

36-
func NewTestHooks(_ log.Logger) TestHooks {
20+
func NewTestHooks() TestHooks {
3721
return &testHooksImpl{}
3822
}
3923

24+
func (*testHooksImpl) testHooks() {}
25+
4026
// Get gets the value of a test hook from the registry.
4127
//
4228
// TestHooks should be used sparingly, see comment on TestHooks.
@@ -45,9 +31,13 @@ func Get[T any](th TestHooks, key Key) (T, bool) {
4531
if th == nil {
4632
return zero, false
4733
}
48-
if val, ok := th.get(key); ok {
34+
impl, ok := th.(*testHooksImpl)
35+
if !ok {
36+
return zero, false
37+
}
38+
if val, ok := impl.m.Load(key); ok {
4939
// this is only used in test so we want to panic on type mismatch:
50-
return val.(T), ok // nolint:revive
40+
return val.(T), ok //nolint:revive
5141
}
5242
return zero, false
5343
}
@@ -64,18 +54,7 @@ func Call(th TestHooks, key Key) {
6454
// Set sets a test hook to a value and returns a cleanup function to unset it.
6555
// Calls to Set and the cleanup function should form a stack.
6656
func Set[T any](th TestHooks, key Key, val T) func() {
67-
th.set(key, val)
68-
return func() { th.del(key) }
69-
}
70-
71-
func (th *testHooksImpl) get(key Key) (any, bool) {
72-
return th.m.Load(key)
73-
}
74-
75-
func (th *testHooksImpl) set(key Key, val any) {
76-
th.m.Store(key, val)
77-
}
78-
79-
func (th *testHooksImpl) del(key Key) {
80-
th.m.Delete(key)
57+
impl := th.(*testHooksImpl)
58+
impl.m.Store(key, val)
59+
return func() { impl.m.Delete(key) }
8160
}

tests/testcore/onebox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func newTemporal(t *testing.T, params *TemporalParams) *TemporalImpl {
214214
tlsConfigProvider: params.TLSConfigProvider,
215215
captureMetricsHandler: params.CaptureMetricsHandler,
216216
dcClient: dynamicconfig.NewMemoryClient(),
217-
testHooks: testhooks.NewTestHooks(params.Logger),
217+
testHooks: testhooks.NewTestHooks(),
218218
serviceFxOptions: params.ServiceFxOptions,
219219
taskCategoryRegistry: params.TaskCategoryRegistry,
220220
hostsByProtocolByService: params.HostsByProtocolByService,

0 commit comments

Comments
 (0)