通过反射,改造钩子机制

This commit is contained in:
李宇翔
2021-07-15 15:05:19 +08:00
parent d3d226f7ee
commit d7893239f2
5 changed files with 29 additions and 4 deletions

25
hook_test.go Normal file
View File

@@ -0,0 +1,25 @@
package engine
import (
"fmt"
"sync"
"testing"
"time"
)
func TestAddHook(t *testing.T) {
t.Run(t.Name(), func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
go AddHook("test", func(a, b int) {
fmt.Printf("on test,%d,%d", a, b)
})
go AddHook("done", wg.Done)
TriggerHook("test", 2, 10)
<-time.After(time.Millisecond * 100)
TriggerHook("test", 1, 12)
<-time.After(time.Millisecond * 100)
TriggerHook("done")
wg.Wait()
})
}