33package testhooks
44
55import (
6- "context"
76 "sync"
87
8+ "go.temporal.io/server/common/log"
99 "go.uber.org/fx"
1010)
1111
1212var Module = fx .Options (
13- fx .Provide (NewTestHooksImpl ),
13+ fx .Provide (NewTestHooks ),
1414)
1515
1616type (
@@ -31,10 +31,12 @@ type (
3131 testHooksImpl struct {
3232 m sync.Map
3333 }
34-
35- contextKey struct {}
3634)
3735
36+ func NewTestHooks (_ log.Logger ) TestHooks {
37+ return & testHooksImpl {}
38+ }
39+
3840// Get gets the value of a test hook from the registry.
3941//
4042// TestHooks should be used sparingly, see comment on TestHooks.
@@ -50,19 +52,6 @@ func Get[T any](th TestHooks, key Key) (T, bool) {
5052 return zero , false
5153}
5254
53- // GetCtx gets the value of a test hook from the registry embedded in the
54- // context chain.
55- //
56- // TestHooks should be used sparingly, see comment on TestHooks.
57- func GetCtx [T any ](ctx context.Context , key Key ) (T , bool ) {
58- hooks := ctx .Value (contextKey {})
59- if hooks , ok := hooks .(TestHooks ); ok {
60- return Get [T ](hooks , key )
61- }
62- var zero T
63- return zero , false
64- }
65-
6655// Call calls a func() hook if present.
6756//
6857// TestHooks should be used sparingly, see comment on TestHooks.
@@ -72,49 +61,13 @@ func Call(th TestHooks, key Key) {
7261 }
7362}
7463
75- // CallCtx calls a func() hook if present and a TestHooks implementation
76- // exists in the context chain.
77- //
78- // TestHooks should be used sparingly, see comment on TestHooks.
79- func CallCtx (ctx context.Context , key Key ) {
80- hooks := ctx .Value (contextKey {})
81- if hooks , ok := hooks .(TestHooks ); ok {
82- Call (hooks , key )
83- }
84- }
85-
8664// Set sets a test hook to a value and returns a cleanup function to unset it.
8765// Calls to Set and the cleanup function should form a stack.
8866func Set [T any ](th TestHooks , key Key , val T ) func () {
8967 th .set (key , val )
9068 return func () { th .del (key ) }
9169}
9270
93- // SetCtx sets a test hook to a value on the provided context and returns a
94- // cleanup function to unset it. Calls to SetCtx and the cleanup function
95- // should form a stack.
96- func SetCtx [T any ](ctx context.Context , key Key , val T ) func () {
97- hooks := ctx .Value (contextKey {})
98- if hooks , ok := hooks .(TestHooks ); ok {
99- return Set (hooks , key , val )
100- }
101- return func () {}
102- }
103-
104- // NewTestHooksImpl returns a new instance of a test hook registry. This is provided and used
105- // in the main "resource" module as a default, but in functional tests, it's overridden by an
106- // explicitly constructed instance.
107- func NewTestHooksImpl () TestHooks {
108- return & testHooksImpl {}
109- }
110-
111- // NewInjectedTestHooksImpl returns a new instance of a test hook registry and a context with the
112- // registry injected.
113- func NewInjectedTestHooksImpl (parent context.Context ) (context.Context , TestHooks ) {
114- hooks := NewTestHooksImpl ()
115- return context .WithValue (parent , contextKey {}, hooks ), hooks
116- }
117-
11871func (th * testHooksImpl ) get (key Key ) (any , bool ) {
11972 return th .m .Load (key )
12073}
0 commit comments