github.com/songzhibin97/gkit@v1.2.13/egroup/example_test.go (about) 1 package egroup 2 3 import ( 4 "context" 5 "os" 6 "syscall" 7 "time" 8 ) 9 10 var admin *LifeAdmin 11 12 func mockStart() func(ctx context.Context) error { 13 return nil 14 } 15 16 func mockShutdown() func(ctx context.Context) error { 17 return nil 18 } 19 20 type mockLifeAdminer struct{} 21 22 func (m *mockLifeAdminer) Start(ctx context.Context) error { 23 return nil 24 } 25 26 func (m *mockLifeAdminer) Shutdown(ctx context.Context) error { 27 return nil 28 } 29 30 func ExampleNewLifeAdmin() { 31 // 默认配置 32 // admin = NewLifeAdmin() 33 34 // 可供选择配置选项 35 36 // 设置启动超时时间 37 // <=0 不启动超时时间,注意要在shutdown处理关闭通知 38 // SetStartTimeout(time.Second) 39 40 // 设置关闭超时时间 41 // <=0 不启动超时时间 42 // SetStopTimeout(time.Second) 43 44 // 设置信号集合,和处理信号的函数 45 //SetSignal(func(lifeAdmin *LifeAdmin, signal os.Signal) { 46 // return 47 //}, signal...) 48 49 admin = NewLifeAdmin(SetStartTimeout(time.Second), SetStopTimeout(time.Second), SetSignal(func(a *LifeAdmin, signal os.Signal) { 50 switch signal { 51 case syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT: 52 a.shutdown() 53 default: 54 } 55 })) 56 } 57 58 func ExampleLifeAdmin_Add() { 59 // 通过struct添加 60 admin.Add(Member{ 61 Start: mockStart(), 62 Shutdown: mockShutdown(), 63 }) 64 } 65 66 func ExampleLifeAdmin_AddMember() { 67 // 根据接口适配添加 68 admin.AddMember(&mockLifeAdminer{}) 69 } 70 71 func ExampleLifeAdmin_Start() { 72 defer admin.Shutdown() 73 if err := admin.Start(); err != nil { 74 // 处理错误 75 // 正常启动会hold主 76 } 77 }