// defined in context.go, that supports registering AfterFuncs.
type afterFuncContext struct {
mu sync.Mutex
- afterFuncs map[*struct{}]func()
+ afterFuncs map[*byte]func()
done chan struct{}
err error
}
func (c *afterFuncContext) AfterFunc(f func()) func() bool {
c.mu.Lock()
defer c.mu.Unlock()
- k := &struct{}{}
+ k := new(byte)
if c.afterFuncs == nil {
- c.afterFuncs = make(map[*struct{}]func())
+ c.afterFuncs = make(map[*byte]func())
}
c.afterFuncs[k] = f
return func() bool {
func TestCustomContextAfterFuncUnregisterCancel(t *testing.T) {
ctx0 := &afterFuncContext{}
- _, cancel := context.WithCancel(ctx0)
- if got, want := len(ctx0.afterFuncs), 1; got != want {
+ _, cancel1 := context.WithCancel(ctx0)
+ _, cancel2 := context.WithCancel(ctx0)
+ if got, want := len(ctx0.afterFuncs), 2; got != want {
t.Errorf("after WithCancel(ctx0): ctx0 has %v afterFuncs, want %v", got, want)
}
- cancel()
+ cancel1()
+ cancel2()
if got, want := len(ctx0.afterFuncs), 0; got != want {
t.Errorf("after canceling WithCancel(ctx0): ctx0 has %v afterFuncs, want %v", got, want)
}