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.

47 lines
815 B

  1. package sync_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. tmsync "github.com/tendermint/tendermint/libs/sync"
  6. )
  7. func TestWaker(t *testing.T) {
  8. // A new waker should block when sleeping.
  9. waker := tmsync.NewWaker()
  10. select {
  11. case <-waker.Sleep():
  12. require.Fail(t, "unexpected wakeup")
  13. default:
  14. }
  15. // Wakeups should not block, and should cause the next sleeper to awaken.
  16. waker.Wake()
  17. select {
  18. case <-waker.Sleep():
  19. default:
  20. require.Fail(t, "expected wakeup, but sleeping instead")
  21. }
  22. // Multiple wakeups should only wake a single sleeper.
  23. waker.Wake()
  24. waker.Wake()
  25. waker.Wake()
  26. select {
  27. case <-waker.Sleep():
  28. default:
  29. require.Fail(t, "expected wakeup, but sleeping instead")
  30. }
  31. select {
  32. case <-waker.Sleep():
  33. require.Fail(t, "unexpected wakeup")
  34. default:
  35. }
  36. }