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.

48 lines
1.2 KiB

  1. package events
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/libs/log"
  8. )
  9. func TestEventCache_Flush(t *testing.T) {
  10. ctx, cancel := context.WithCancel(context.Background())
  11. defer cancel()
  12. evsw := NewEventSwitch(log.TestingLogger())
  13. err := evsw.Start(ctx)
  14. require.NoError(t, err)
  15. err = evsw.AddListenerForEvent("nothingness", "", func(_ context.Context, data EventData) error {
  16. // Check we are not initializing an empty buffer full of zeroed eventInfos in the EventCache
  17. require.FailNow(t, "We should never receive a message on this switch since none are fired")
  18. return nil
  19. })
  20. require.NoError(t, err)
  21. evc := NewEventCache(evsw)
  22. evc.Flush(ctx)
  23. // Check after reset
  24. evc.Flush(ctx)
  25. fail := true
  26. pass := false
  27. err = evsw.AddListenerForEvent("somethingness", "something", func(_ context.Context, data EventData) error {
  28. if fail {
  29. require.FailNow(t, "Shouldn't see a message until flushed")
  30. }
  31. pass = true
  32. return nil
  33. })
  34. require.NoError(t, err)
  35. evc.FireEvent("something", struct{ int }{1})
  36. evc.FireEvent("something", struct{ int }{2})
  37. evc.FireEvent("something", struct{ int }{3})
  38. fail = false
  39. evc.Flush(ctx)
  40. assert.True(t, pass)
  41. }