github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/libs/events/event_cache_test.go (about)

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