mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-05 13:05:09 +00:00
libs/events: remove unused event cache (#7807)
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
package events
|
||||
|
||||
import "context"
|
||||
|
||||
// An EventCache buffers events for a Fireable
|
||||
// All events are cached. Filtering happens on Flush
|
||||
type EventCache struct {
|
||||
evsw Fireable
|
||||
events []eventInfo
|
||||
}
|
||||
|
||||
// Create a new EventCache with an EventSwitch as backend
|
||||
func NewEventCache(evsw Fireable) *EventCache {
|
||||
return &EventCache{
|
||||
evsw: evsw,
|
||||
}
|
||||
}
|
||||
|
||||
// a cached event
|
||||
type eventInfo struct {
|
||||
event string
|
||||
data EventData
|
||||
}
|
||||
|
||||
// Cache an event to be fired upon finality.
|
||||
func (evc *EventCache) FireEvent(event string, data EventData) {
|
||||
// append to list (go will grow our backing array exponentially)
|
||||
evc.events = append(evc.events, eventInfo{event, data})
|
||||
}
|
||||
|
||||
// Fire events by running evsw.FireEvent on all cached events. Blocks.
|
||||
// Clears cached events
|
||||
func (evc *EventCache) Flush(ctx context.Context) {
|
||||
for _, ei := range evc.events {
|
||||
evc.evsw.FireEvent(ctx, ei.event, ei.data)
|
||||
}
|
||||
// Clear the buffer, since we only add to it with append it's safe to just set it to nil and maybe safe an allocation
|
||||
evc.events = nil
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
)
|
||||
|
||||
func TestEventCache_Flush(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
logger := log.NewTestingLogger(t)
|
||||
evsw := NewEventSwitch(logger)
|
||||
err := evsw.Start(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = evsw.AddListenerForEvent("nothingness", "", func(_ context.Context, data EventData) error {
|
||||
// Check we are not initializing an empty buffer full of zeroed eventInfos in the EventCache
|
||||
require.FailNow(t, "We should never receive a message on this switch since none are fired")
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
evc := NewEventCache(evsw)
|
||||
evc.Flush(ctx)
|
||||
// Check after reset
|
||||
evc.Flush(ctx)
|
||||
fail := true
|
||||
pass := false
|
||||
err = evsw.AddListenerForEvent("somethingness", "something", func(_ context.Context, data EventData) error {
|
||||
if fail {
|
||||
require.FailNow(t, "Shouldn't see a message until flushed")
|
||||
}
|
||||
pass = true
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
evc.FireEvent("something", struct{ int }{1})
|
||||
evc.FireEvent("something", struct{ int }{2})
|
||||
evc.FireEvent("something", struct{ int }{3})
|
||||
fail = false
|
||||
evc.Flush(ctx)
|
||||
assert.True(t, pass)
|
||||
}
|
||||
Reference in New Issue
Block a user