From 1fe1b6c0327c50ff9e1660e85673665bd9e4d91e Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Fri, 11 Feb 2022 09:48:37 -0500 Subject: [PATCH] libs/events: remove unused event cache (#7807) --- libs/events/event_cache.go | 39 -------------------------- libs/events/event_cache_test.go | 49 --------------------------------- 2 files changed, 88 deletions(-) delete mode 100644 libs/events/event_cache.go delete mode 100644 libs/events/event_cache_test.go diff --git a/libs/events/event_cache.go b/libs/events/event_cache.go deleted file mode 100644 index 41633cbef..000000000 --- a/libs/events/event_cache.go +++ /dev/null @@ -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 -} diff --git a/libs/events/event_cache_test.go b/libs/events/event_cache_test.go deleted file mode 100644 index fb36fa674..000000000 --- a/libs/events/event_cache_test.go +++ /dev/null @@ -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) -}