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.

57 lines
1.0 KiB

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