Skip to content

Commit 386a6d3

Browse files
authoredMay 9, 2024··
Require update handler to have a context (#1457)
Require update handler to have a context
1 parent d051de6 commit 386a6d3

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed
 

‎internal/internal_update.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func (up *updateProtocol) HasCompleted() bool {
379379
//
380380
// 1. is a function
381381
// 2. has exactly one return parameter
382-
// 3. the one return prarmeter is of type `error`
382+
// 3. the one return parameter is of type `error`
383383
func validateValidatorFn(fn interface{}) error {
384384
fnType := reflect.TypeOf(fn)
385385
if fnType.Kind() != reflect.Func {
@@ -405,13 +405,22 @@ func validateValidatorFn(fn interface{}) error {
405405
// validateUpdateHandlerFn validates that the supplied interface
406406
//
407407
// 1. is a function
408-
// 2. has one or two return parameters, the last of which is of type `error`
409-
// 3. if there are two return parameters, the first is a serializable type
408+
// 2. has at least one parameter, the first of which is of type `workflow.Context`
409+
// 3. has one or two return parameters, the last of which is of type `error`
410+
// 4. if there are two return parameters, the first is a serializable type
410411
func validateUpdateHandlerFn(fn interface{}) error {
411412
fnType := reflect.TypeOf(fn)
412413
if fnType.Kind() != reflect.Func {
413414
return fmt.Errorf("handler must be function but was %s", fnType.Kind())
414415
}
416+
if fnType.NumIn() == 0 {
417+
return errors.New("first parameter of handler must be a workflow.Context")
418+
} else if !isWorkflowContext(fnType.In(0)) {
419+
return fmt.Errorf(
420+
"first parameter of handler must be a workflow.Context but found %v",
421+
fnType.In(0).Kind(),
422+
)
423+
}
415424
switch fnType.NumOut() {
416425
case 1:
417426
if !isError(fnType.Out(0)) {

‎internal/internal_update_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestUpdateHandlerPanicHandling(t *testing.T) {
103103
interceptor, ctx, err := newWorkflowContext(env, nil)
104104
require.NoError(t, err)
105105

106-
panicFunc := func() error { panic("intentional") }
106+
panicFunc := func(ctx Context) error { panic("intentional") }
107107
dispatcher, _ := newDispatcher(
108108
ctx,
109109
interceptor,
@@ -123,7 +123,7 @@ func TestUpdateHandlerPanicHandling(t *testing.T) {
123123
interceptor, ctx, err := newWorkflowContext(env, nil)
124124
require.NoError(t, err)
125125

126-
panicFunc := func() error { panic("intentional") }
126+
panicFunc := func(ctx Context) error { panic("intentional") }
127127
dispatcher, _ := newDispatcher(
128128
ctx,
129129
interceptor,
@@ -151,10 +151,12 @@ func TestUpdateHandlerFnValidation(t *testing.T) {
151151
{require.Error, func() int { return 0 }},
152152
{require.Error, func(Context, int) (int, int, error) { return 0, 0, nil }},
153153
{require.Error, func(int) (chan int, error) { return nil, nil }},
154-
{require.NoError, func() error { return nil }},
154+
{require.Error, func() error { return nil }},
155+
{require.Error, func(int, int, string) error { return nil }},
156+
{require.Error, func(int) error { return nil }},
157+
{require.NoError, func(Context, int) error { return nil }},
158+
{require.NoError, func(Context, int, int, string) error { return nil }},
155159
{require.NoError, func(Context) error { return nil }},
156-
{require.NoError, func(int) error { return nil }},
157-
{require.NoError, func(int, int, string) error { return nil }},
158160
{require.NoError, func(Context, int, int, string) error { return nil }},
159161
} {
160162
t.Run(reflect.TypeOf(tc.fn).String(), func(t *testing.T) {
@@ -219,7 +221,7 @@ func TestDefaultUpdateHandler(t *testing.T) {
219221
t,
220222
ctx,
221223
"unused_handler",
222-
func() error { panic("should not be called") },
224+
func(ctx Context) error { panic("should not be called") },
223225
UpdateHandlerOptions{},
224226
)
225227
},

0 commit comments

Comments
 (0)
Please sign in to comment.