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.

54 lines
893 B

  1. package common
  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. ts.Start()
  17. waitFinished := make(chan struct{})
  18. go func() {
  19. ts.Wait()
  20. waitFinished <- struct{}{}
  21. }()
  22. go ts.Stop()
  23. select {
  24. case <-waitFinished:
  25. // all good
  26. case <-time.After(100 * time.Millisecond):
  27. t.Fatal("expected Wait() to finish within 100 ms.")
  28. }
  29. }
  30. func TestBaseServiceReset(t *testing.T) {
  31. ts := &testService{}
  32. ts.BaseService = *NewBaseService(nil, "TestService", ts)
  33. ts.Start()
  34. err := ts.Reset()
  35. require.Error(t, err, "expected cant reset service error")
  36. ts.Stop()
  37. err = ts.Reset()
  38. require.NoError(t, err)
  39. err = ts.Start()
  40. require.NoError(t, err)
  41. }