github.com/simpleiot/simpleiot@v0.18.3/client/run_group_test.go (about) 1 package client 2 3 import ( 4 "errors" 5 "fmt" 6 "sync" 7 "testing" 8 ) 9 10 type testClient struct { 11 stop chan struct{} 12 stopOnce sync.Once 13 } 14 15 func newTestClient() *testClient { 16 return &testClient{stop: make(chan struct{})} 17 } 18 19 func (tc *testClient) Run() error { 20 <-tc.stop 21 return errors.New("client stopped") 22 } 23 24 func (tc *testClient) Stop(_ error) { 25 tc.stopOnce.Do(func() { close(tc.stop) }) 26 } 27 28 func TestGroup(_ *testing.T) { 29 g := NewRunGroup("testGroup") 30 testC := newTestClient() 31 g.Add(testC) 32 33 groupErr := make(chan error) 34 35 // first try to stop everything by stopping client 36 go func() { 37 groupErr <- g.Run() 38 }() 39 40 testC.Stop(nil) 41 fmt.Println("group returned: ", <-groupErr) 42 43 // now try stopping everything by stopping group 44 go func() { 45 groupErr <- g.Run() 46 }() 47 48 g.Stop(nil) 49 fmt.Println("group returned: ", <-groupErr) 50 }