Skip to content

Commit

Permalink
Error on Registering Activity with Workflow Context (#1093)
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonjmtan authored Apr 24, 2023
1 parent ad1af5e commit a1f826a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/internal_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,15 @@ func validateFnFormat(fnType reflect.Type, isWorkflow bool) error {
if !isWorkflowContext(fnType.In(0)) {
return fmt.Errorf("expected first argument to be workflow.Context but found %s", fnType.In(0))
}
} else {
// For activities, check that workflow context is not accidentally provided
// Activities registered with structs will have their receiver as the first argument so confirm it is not
// in the first two arguments
for i := 0; i < fnType.NumIn() && i < 2; i++ {
if isWorkflowContext(fnType.In(i)) {
return fmt.Errorf("unexpected use of workflow context for an activity")
}
}
}

// Return values
Expand Down
31 changes: 31 additions & 0 deletions internal/internal_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,37 @@ func TestRegisterStructWithInvalidFnsWithoutSkipFails(t *testing.T) {
assert.Panics(t, testRegisterStructWithInvalidFnsWithoutSkipFails)
}

type testActivityStructWithFnWithWorkflowContext struct{}

func (t *testActivityStructWithFnWithWorkflowContext) InvalidActivity(Context) error {
return nil
}

func testRegisterStructWithInvalidWorkflowContextFnFails() {
registry := newRegistry()
registry.RegisterActivityWithOptions(&testActivityStructWithFnWithWorkflowContext{}, RegisterActivityOptions{
Name: "testActivityStructWithFnWithWorkflowContext_",
SkipInvalidStructFunctions: false,
})
}

func TestRegisterStructWithInvalidWorkflowContextFnFails(t *testing.T) {
assert.Panics(t, testRegisterStructWithInvalidWorkflowContextFnFails)
}

func InvalidActivityWithWorkflowContext(Context) error {
return nil
}

func testRegisterStructWithInvalidActivityWithWorkflowContextFails() {
registry := newRegistry()
registry.RegisterActivity(InvalidActivityWithWorkflowContext)
}

func TestRegisterStructWithInvalidActivityWithWorkflowContextFails(t *testing.T) {
assert.Panics(t, testRegisterStructWithInvalidActivityWithWorkflowContextFails)
}

func TestVariousActivitySchedulingOption(t *testing.T) {
w := &activitiesCallingOptionsWorkflow{t: t}

Expand Down

0 comments on commit a1f826a

Please sign in to comment.