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.

42 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
  13. // of zeroed eventInfos in the EventCache
  14. require.FailNow(t, "We should never receive a message on this switch since none are fired")
  15. })
  16. require.NoError(t, err)
  17. evc := NewEventCache(evsw)
  18. evc.Flush()
  19. // Check after reset
  20. evc.Flush()
  21. fail := true
  22. pass := false
  23. err = evsw.AddListenerForEvent("somethingness", "something", func(data EventData) {
  24. if fail {
  25. require.FailNow(t, "Shouldn't see a message until flushed")
  26. }
  27. pass = true
  28. })
  29. require.NoError(t, err)
  30. evc.FireEvent("something", struct{ int }{1})
  31. evc.FireEvent("something", struct{ int }{2})
  32. evc.FireEvent("something", struct{ int }{3})
  33. fail = false
  34. evc.Flush()
  35. assert.True(t, pass)
  36. }