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.

46 lines
826 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. logger := log.NewTestingLogger(t)
  20. ts := &testService{}
  21. ts.BaseService = *NewBaseService(logger, "TestService", ts)
  22. err := ts.Start(ctx)
  23. require.NoError(t, err)
  24. waitFinished := make(chan struct{})
  25. go func() {
  26. ts.Wait()
  27. waitFinished <- struct{}{}
  28. }()
  29. go cancel()
  30. select {
  31. case <-waitFinished:
  32. // all good
  33. case <-time.After(100 * time.Millisecond):
  34. t.Fatal("expected Wait() to finish within 100 ms.")
  35. }
  36. }