@@ -5,38 +5,24 @@ package testhooks
55import (
66 "sync"
77
8- "go.temporal.io/server/common/log"
98 "go.uber.org/fx"
109)
1110
1211var 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.
6656func 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}
0 commit comments