You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
803 B

  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/libs/log"
  8. )
  9. type testService struct {
  10. BaseService
  11. }
  12. func (testService) OnStop() {}
  13. func (testService) OnStart(context.Context) error {
  14. return nil
  15. }
  16. func TestBaseServiceWait(t *testing.T) {
  17. ctx, cancel := context.WithCancel(context.Background())
  18. defer cancel()
  19. ts := &testService{}
  20. ts.BaseService = *NewBaseService(log.TestingLogger(), "TestService", ts)
  21. err := ts.Start(ctx)
  22. require.NoError(t, err)
  23. waitFinished := make(chan struct{})
  24. go func() {
  25. ts.Wait()
  26. waitFinished <- struct{}{}
  27. }()
  28. go cancel()
  29. select {
  30. case <-waitFinished:
  31. // all good
  32. case <-time.After(100 * time.Millisecond):
  33. t.Fatal("expected Wait() to finish within 100 ms.")
  34. }
  35. }