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.

41 lines
1.0 KiB

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