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.

45 lines
1.1 KiB

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