github.com/vipernet-xyz/tendermint-core@v0.32.0/libs/service/service_test.go (about) 1 package service 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/require" 8 ) 9 10 type testService struct { 11 BaseService 12 } 13 14 func (testService) OnReset() error { 15 return nil 16 } 17 18 func TestBaseServiceWait(t *testing.T) { 19 ts := &testService{} 20 ts.BaseService = *NewBaseService(nil, "TestService", ts) 21 ts.Start() 22 23 waitFinished := make(chan struct{}) 24 go func() { 25 ts.Wait() 26 waitFinished <- struct{}{} 27 }() 28 29 go ts.Stop() 30 31 select { 32 case <-waitFinished: 33 // all good 34 case <-time.After(100 * time.Millisecond): 35 t.Fatal("expected Wait() to finish within 100 ms.") 36 } 37 } 38 39 func TestBaseServiceReset(t *testing.T) { 40 ts := &testService{} 41 ts.BaseService = *NewBaseService(nil, "TestService", ts) 42 ts.Start() 43 44 err := ts.Reset() 45 require.Error(t, err, "expected cant reset service error") 46 47 ts.Stop() 48 49 err = ts.Reset() 50 require.NoError(t, err) 51 52 err = ts.Start() 53 require.NoError(t, err) 54 }