github.com/sandwich-go/boost@v1.3.29/module/module_test.go (about) 1 package module 2 3 import ( 4 "context" 5 "fmt" 6 . "github.com/smartystreets/goconvey/convey" 7 "testing" 8 "time" 9 ) 10 11 type testPlugin struct { 12 } 13 14 func (*testPlugin) AfterRunModule(context.Context, Master) { 15 fmt.Println("after_run_module") 16 } 17 func (*testPlugin) BeforeCloseModule(context.Context, Master) { 18 fmt.Println("before_close_module") 19 } 20 21 type testModule struct { 22 } 23 24 func (*testModule) OnInit() {} 25 func (*testModule) OnClose() {} 26 func (*testModule) Run(chan struct{}) { time.Sleep(time.Millisecond * 10) } 27 func (*testModule) Name() string { return "exit_after_10_millisecond" } 28 29 type testModuleNotStop struct { 30 } 31 32 func (*testModuleNotStop) OnInit() {} 33 func (*testModuleNotStop) OnClose() {} 34 func (*testModuleNotStop) Run(closeChan chan struct{}) { <-closeChan } 35 func (*testModuleNotStop) Name() string { return "testModuleNotStop" } 36 37 func TestModuleStop(t *testing.T) { 38 Convey("module should stop when no sub module in running state", t, func(c C) { 39 m := New() 40 m.Register(&testModule{}) 41 m.Run() 42 }) 43 Convey("module should not stop when still have sub module in running state", t, func(c C) { 44 Register(&testModule{}, &testModuleNotStop{}) 45 AttachPlugin(&testPlugin{}) 46 closed := make(chan struct{}) 47 go func() { 48 Run() 49 close(closed) 50 }() 51 timeout := false 52 select { 53 case <-time.After(time.Millisecond * 20): 54 timeout = true 55 case <-closed: 56 } 57 So(timeout, ShouldBeTrue) 58 Stop() 59 <-ShutdownNotify() 60 }) 61 }