Skip to content

Commit ebdf0fb

Browse files
committed
Add tests to explicit naming of workflows/activities
Revert changes on the anonymous function detection.
1 parent 51b842d commit ebdf0fb

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

workflow/worker.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"log"
2222
"reflect"
23-
"regexp"
2423
"runtime"
2524
"strings"
2625

@@ -93,13 +92,7 @@ func getFunctionName(f interface{}) (string, error) {
9392
callSplit := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")
9493
funcName := callSplit[len(callSplit)-1]
9594

96-
const anonymousFunctionRegexp = "^func[0-9]+$"
97-
isAnonymousFunc, err := regexp.MatchString(anonymousFunctionRegexp, funcName)
98-
if err != nil {
99-
return "", fmt.Errorf("failed to match anonymous function regexp: %w", err)
100-
}
101-
102-
if isAnonymousFunc {
95+
if funcName == "1" {
10396
return "", errors.New("anonymous function name")
10497
}
10598

workflow/worker_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,46 @@ func TestWorkflowRuntime(t *testing.T) {
4343
t.Run("register workflow", func(t *testing.T) {
4444
err := testWorker.RegisterWorkflow(testWorkflow)
4545
require.NoError(t, err)
46+
47+
t.Run("with explicit name", func(t *testing.T) {
48+
err := testWorker.RegisterWorkflow(testWorkflow, RegisterWithName("MyWorkflow"))
49+
require.NoError(t, err)
50+
})
4651
})
4752
t.Run("register workflow - anonymous func", func(t *testing.T) {
4853
err := testWorker.RegisterWorkflow(func(ctx *WorkflowContext) (any, error) {
4954
return nil, nil
5055
})
5156
require.Error(t, err)
57+
58+
t.Run("with explicit name", func(t *testing.T) {
59+
err := testWorker.RegisterWorkflow(func(ctx *WorkflowContext) (any, error) {
60+
return nil, nil
61+
}, RegisterWithName("MyWorkflow2"))
62+
require.NoError(t, err)
63+
})
5264
})
5365
t.Run("register activity", func(t *testing.T) {
5466
err := testWorker.RegisterActivity(testActivity)
5567
require.NoError(t, err)
68+
69+
t.Run("with explicit name", func(t *testing.T) {
70+
err := testWorker.RegisterActivity(testActivity, RegisterWithName("MyActivity"))
71+
require.NoError(t, err)
72+
})
5673
})
5774
t.Run("register activity - anonymous func", func(t *testing.T) {
5875
err := testWorker.RegisterActivity(func(ctx ActivityContext) (any, error) {
5976
return nil, nil
6077
})
6178
require.Error(t, err)
79+
80+
t.Run("with explicit name", func(t *testing.T) {
81+
err := testWorker.RegisterActivity(func(ctx ActivityContext) (any, error) {
82+
return nil, nil
83+
}, RegisterWithName("MyActivity2"))
84+
require.NoError(t, err)
85+
})
6286
})
6387
}
6488

@@ -69,6 +93,16 @@ func TestWorkerOptions(t *testing.T) {
6993
})
7094
}
7195

96+
func TestRegisterOptions(t *testing.T) {
97+
t.Run("with name", func(t *testing.T) {
98+
defaultOpts := registerOptions{}
99+
options, err := processRegisterOptions(defaultOpts, RegisterWithName("testWorkflow"))
100+
require.NoError(t, err)
101+
assert.NotEmpty(t, options.Name)
102+
assert.Equal(t, "testWorkflow", options.Name)
103+
})
104+
}
105+
72106
func returnWorkerOptions(opts ...workerOption) workerOptions {
73107
options := new(workerOptions)
74108
for _, configure := range opts {
@@ -104,6 +138,12 @@ func TestGetFunctionName(t *testing.T) {
104138
require.Error(t, err)
105139
assert.Equal(t, "", name)
106140
})
141+
142+
t.Run("get function name - anonymous", func(t *testing.T) {
143+
name, err := getFunctionName(func() {})
144+
require.Error(t, err)
145+
assert.Equal(t, "", name)
146+
})
107147
}
108148

109149
func testWorkflow(ctx *WorkflowContext) (any, error) {

0 commit comments

Comments
 (0)