Skip to content

Commit

Permalink
Fix registry already registered check (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
yycptt authored Feb 10, 2021
1 parent e6b493b commit e462133
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
18 changes: 13 additions & 5 deletions internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (r *registry) RegisterWorkflowWithOptions(
defer r.Unlock()

if !options.DisableAlreadyRegisteredCheck {
if _, ok := r.workflowFuncMap[registerName]; ok {
if _, ok := r.getWorkflowNoLock(registerName); ok {
panic(fmt.Sprintf("workflow name \"%v\" is already registered", registerName))
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (r *registry) registerActivityFunction(af interface{}, options RegisterActi
defer r.Unlock()

if !options.DisableAlreadyRegisteredCheck {
if _, ok := r.activityFuncMap[registerName]; ok {
if _, ok := r.getActivityNoLock(registerName); ok {
return fmt.Errorf("activity type \"%v\" is already registered", registerName)
}
}
Expand Down Expand Up @@ -231,6 +231,14 @@ func (r *registry) getWorkflowFn(fnName string) (interface{}, bool) {
return fn, ok
}

func (r *registry) getWorkflowNoLock(registerName string) (interface{}, bool) {
a, ok := r.workflowFuncMap[registerName]
if !ok && r.next != nil {
return r.next.getWorkflowNoLock(registerName)
}
return a, ok
}

func (r *registry) getRegisteredWorkflowTypes() []string {
r.Lock() // do not defer for Unlock to call next.getRegisteredWorkflowTypes without lock
var result []string
Expand Down Expand Up @@ -277,10 +285,10 @@ func (r *registry) GetActivity(fnName string) (activity, bool) {
return a, ok
}

func (r *registry) getActivityNoLock(fnName string) (activity, bool) {
a, ok := r.activityFuncMap[fnName]
func (r *registry) getActivityNoLock(registerName string) (activity, bool) {
a, ok := r.activityFuncMap[registerName]
if !ok && r.next != nil {
return r.next.getActivityNoLock(fnName)
return r.next.getActivityNoLock(registerName)
}
return a, ok
}
Expand Down
63 changes: 63 additions & 0 deletions internal/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestWorkflowRegistration(t *testing.T) {
tests := []struct {
msg string
register func(r *registry)
registerPanic bool
workflowType string
altWorkflowType string
resolveByFunction interface{}
Expand Down Expand Up @@ -66,11 +67,41 @@ func TestWorkflowRegistration(t *testing.T) {
altWorkflowType: "go.uber.org/cadence/internal.(*testWorkflowStruct).Method-fm",
resolveByFunction: w.Method,
},
{
msg: "register duplicated workflow in one registry (should panic)",
register: func(r *registry) {
r.RegisterWorkflow(testWorkflowFunction)
r.RegisterWorkflow(testWorkflowFunction)
},
registerPanic: true,
},
{
msg: "register duplicated workflow with already registered check disabled",
register: func(r *registry) {
r.RegisterWorkflow(testWorkflowFunction)
r.RegisterWorkflowWithOptions(testWorkflowFunction, RegisterWorkflowOptions{DisableAlreadyRegisteredCheck: true})
},
workflowType: "go.uber.org/cadence/internal.testWorkflowFunction",
resolveByFunction: testWorkflowFunction,
},
{
msg: "register duplicated workflow in chained registry (should panic)",
register: func(r *registry) {
r.next.RegisterWorkflow(testWorkflowFunction)
r.RegisterWorkflow(testWorkflowFunction)
},
registerPanic: true,
},
}

for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
r := newRegistry()
if tt.registerPanic {
require.Panics(t, func() { tt.register(r) }, "register should panic")
return
}

tt.register(r)

// Verify registered workflow type
Expand Down Expand Up @@ -104,6 +135,7 @@ func TestActivityRegistration(t *testing.T) {
tests := []struct {
msg string
register func(r *registry)
registerPanic bool
activityType string
altActivityType string
resolveByFunction interface{}
Expand Down Expand Up @@ -156,10 +188,41 @@ func TestActivityRegistration(t *testing.T) {
resolveByFunction: (&testActivityStruct{}).Method,
resolveByAlias: "prefix.Method",
},
{
msg: "register duplicated activity function in one registry (should panic)",
register: func(r *registry) {
duplicatedActivityAlias := "activity.alias"
r.RegisterActivityWithOptions(testActivityFunction, RegisterActivityOptions{Name: duplicatedActivityAlias})
r.RegisterActivityWithOptions(testActivityFunction, RegisterActivityOptions{Name: duplicatedActivityAlias})
},
registerPanic: true,
},
{
msg: "register duplicated activity struct with already registered check disabled",
register: func(r *registry) {
r.RegisterActivity(&testActivityStruct{})
r.RegisterActivityWithOptions(&testActivityStruct{}, RegisterActivityOptions{DisableAlreadyRegisteredCheck: true})
},
activityType: "go.uber.org/cadence/internal.(*testActivityStruct).Method",
resolveByFunction: (&testActivityStruct{}).Method,
},
{
msg: "register duplicated activity function in chained registry (should panic)",
register: func(r *registry) {
r.next.RegisterActivity(testActivityFunction)
r.RegisterActivity(testActivityFunction)
},
registerPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.msg, func(t *testing.T) {
r := newRegistry()
if tt.registerPanic {
require.Panics(t, func() { tt.register(r) }, "register should panic")
return
}

tt.register(r)

// Verify registered activity type
Expand Down

0 comments on commit e462133

Please sign in to comment.