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.

49 lines
1.3 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. logger := log.NewTestingLogger(t)
  13. evsw := NewEventSwitch(logger)
  14. err := evsw.Start(ctx)
  15. require.NoError(t, err)
  16. err = evsw.AddListenerForEvent("nothingness", "", func(_ context.Context, data EventData) error {
  17. // Check we are not initializing an empty buffer full of zeroed eventInfos in the EventCache
  18. require.FailNow(t, "We should never receive a message on this switch since none are fired")
  19. return nil
  20. })
  21. require.NoError(t, err)
  22. evc := NewEventCache(evsw)
  23. evc.Flush(ctx)
  24. // Check after reset
  25. evc.Flush(ctx)
  26. fail := true
  27. pass := false
  28. err = evsw.AddListenerForEvent("somethingness", "something", func(_ context.Context, data EventData) error {
  29. if fail {
  30. require.FailNow(t, "Shouldn't see a message until flushed")
  31. }
  32. pass = true
  33. return nil
  34. })
  35. require.NoError(t, err)
  36. evc.FireEvent("something", struct{ int }{1})
  37. evc.FireEvent("something", struct{ int }{2})
  38. evc.FireEvent("something", struct{ int }{3})
  39. fail = false
  40. evc.Flush(ctx)
  41. assert.True(t, pass)
  42. }