Skip to content

Commit 4c33bda

Browse files
committed
panic for MustRegister
1 parent a966264 commit 4c33bda

File tree

2 files changed

+24
-45
lines changed

2 files changed

+24
-45
lines changed

v2/delay/delay.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ To use a deferred function, you must register the function to be
1010
deferred as a top-level var. For example,
1111
1212
```
13-
var laterFunc = delay.Register("key", myFunc)
13+
var laterFunc = delay.MustRegister("key", myFunc)
1414
1515
func myFunc(ctx context.Context, a, b string) {...}
1616
```
1717
1818
You can also inline with a function literal:
1919
2020
```
21-
var laterFunc = delay.Register("key", func(ctx context.Context, a, b string) {...})
21+
var laterFunc = delay.MustRegister("key", func(ctx context.Context, a, b string) {...})
2222
```
2323
2424
In the above example, "key" is a logical name for the function.
@@ -150,7 +150,7 @@ func fileKey(file string) (string, error) {
150150
// This function Func must be called in a global scope to properly
151151
// register the function with the framework.
152152
//
153-
// Deprecated: Use Register instead.
153+
// Deprecated: Use MustRegister instead.
154154
func Func(key string, i interface{}) *Function {
155155
// Derive unique, somewhat stable key for this func.
156156
_, file, _, _ := runtime.Caller(1)
@@ -171,24 +171,22 @@ func Func(key string, i interface{}) *Function {
171171
return f
172172
}
173173

174-
// Register declares a new function that can be called in a deferred fashion.
174+
// MustRegister declares a new function that can be called in a deferred fashion.
175175
// The second argument i must be a function with the first argument of
176176
// type context.Context.
177-
// Register requires the key to be globally unique.
177+
// MustRegister requires the key to be globally unique.
178178
//
179-
// This function Register must be called in a global scope to properly
179+
// This function MustRegister must be called in a global scope to properly
180180
// register the function with the framework.
181181
// See the package notes above for more details.
182-
func Register(key string, i interface{}) *Function {
182+
func MustRegister(key string, i interface{}) *Function {
183183
f, err := registerFunction(key, i)
184184
if err != nil {
185185
return f
186186
}
187187

188188
if old := funcs[f.key]; old != nil {
189-
old.err = fmt.Errorf("multiple functions registered for %s", key)
190-
f.err = fmt.Errorf("multiple functions registered for %s", key)
191-
return f
189+
panic(fmt.Sprintf("multiple functions registered for %s", key))
192190
}
193191
funcs[f.key] = f
194192
return f
@@ -208,9 +206,9 @@ func registerFunction(key string, i interface{}) (*Function, error) {
208206
return f, errFirstArg
209207
}
210208

211-
// Register the function's arguments with the gob package.
209+
// MustRegister the function's arguments with the gob package.
212210
// This is required because they are marshaled inside a []interface{}.
213-
// gob.Register only expects to be called during initialization;
211+
// gob.MustRegister only expects to be called during initialization;
214212
// that's fine because this function expects the same.
215213
for i := 0; i < t.NumIn(); i++ {
216214
// Only concrete types may be registered. If the argument has

v2/delay/delay_test.go

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func init() {
4343

4444
var (
4545
invalidFunc = Func("invalid", func() {})
46-
invalidRegister = Register("invalid", func() {})
46+
invalidRegister = MustRegister("invalid", func() {})
4747

4848
regFRuns = 0
4949
regFMsg = ""
@@ -52,7 +52,7 @@ var (
5252
regFMsg = arg
5353
}
5454
regFunc = Func("regFunc", regF)
55-
regRegister = Register("regRegister", regF)
55+
regRegister = MustRegister("regRegister", regF)
5656

5757
custFTally = 0
5858
custF = func(c context.Context, ct *CustomType, ci CustomInterface) {
@@ -66,7 +66,7 @@ var (
6666
custFTally += a + b
6767
}
6868
custFunc = Func("custFunc", custF)
69-
custRegister = Register("custRegister", custF)
69+
custRegister = MustRegister("custRegister", custF)
7070

7171
anotherCustFunc = Func("custFunc2", func(c context.Context, n int, ct *CustomType, ci CustomInterface) {
7272
})
@@ -81,7 +81,7 @@ var (
8181
varFMsg = fmt.Sprintf(format, as...)
8282
}
8383
varFunc = Func("variadicFunc", varF)
84-
varRegister = Register("variadicRegister", varF)
84+
varRegister = MustRegister("variadicRegister", varF)
8585

8686
errFRuns = 0
8787
errFErr = errors.New("error!")
@@ -93,7 +93,7 @@ var (
9393
return errFErr
9494
}
9595
errFunc = Func("errFunc", errF)
96-
errRegister = Register("errRegister", errF)
96+
errRegister = MustRegister("errRegister", errF)
9797

9898
dupeWhich = 0
9999
dupe1F = func(c context.Context) {
@@ -102,14 +102,12 @@ var (
102102
}
103103
}
104104
dupe1Func = Func("dupe", dupe1F)
105-
dupe1Register = Register("dupe", dupe1F)
106105
dupe2F = func(c context.Context) {
107106
if dupeWhich == 0 {
108107
dupeWhich = 2
109108
}
110109
}
111110
dupe2Func = Func("dupe", dupe2F)
112-
dupe2Register = Register("dupe", dupe2F)
113111

114112
requestFuncRuns = 0
115113
requestFuncHeaders *taskqueue.RequestHeaders
@@ -119,14 +117,14 @@ var (
119117
requestFuncHeaders, requestFuncErr = RequestHeaders(c)
120118
}
121119
requestFunc = Func("requestFunc", requestF)
122-
requestRegister = Register("requestRegister", requestF)
120+
requestRegister = MustRegister("requestRegister", requestF)
123121

124122
stdCtxRuns = 0
125123
stdCtxF = func(c stdctx.Context) {
126124
stdCtxRuns++
127125
}
128126
stdCtxFunc = Func("stdctxFunc", stdCtxF)
129-
stdCtxRegister = Register("stdctxRegister", stdCtxF)
127+
stdCtxRegister = MustRegister("stdctxRegister", stdCtxF)
130128
)
131129

132130
type fakeContext struct {
@@ -424,31 +422,14 @@ func TestFuncDuplicateFunction(t *testing.T) {
424422
}
425423
}
426424

427-
func TestRegisterDuplicateFunction(t *testing.T) {
428-
c := newFakeContext()
429-
430-
// Fake out the adding of a task.
431-
var task *taskqueue.Task
432-
taskqueueAdder = func(_ context.Context, tk *taskqueue.Task, queue string) (*taskqueue.Task, error) {
433-
if queue != "" {
434-
t.Errorf(`Got queue %q, expected ""`, queue)
425+
func TestMustRegisterDuplicateFunction(t *testing.T) {
426+
MustRegister("dupe", dupe1F)
427+
defer func() {
428+
if r := recover(); r == nil {
429+
t.Error("MustRegister did not panic")
435430
}
436-
task = tk
437-
return tk, nil
438-
}
439-
440-
if err := dupe1Register.Call(c.ctx); err == nil {
441-
t.Error("dupe1Register.Call did not return error")
442-
}
443-
if task != nil {
444-
t.Error("dupe1Register.Call posted a task")
445-
}
446-
if err := dupe2Register.Call(c.ctx); err == nil {
447-
t.Error("dupe2Register.Call did not return error")
448-
}
449-
if task != nil {
450-
t.Fatalf("dupe2Register.Call did not post a task")
451-
}
431+
}()
432+
MustRegister("dupe", dupe2F)
452433
}
453434

454435
func TestGetRequestHeadersFromContext(t *testing.T) {

0 commit comments

Comments
 (0)