Browse Source

libs/events: remove unused event cache (#7807)

pull/7811/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
1fe1b6c032
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 88 deletions
  1. +0
    -39
      libs/events/event_cache.go
  2. +0
    -49
      libs/events/event_cache_test.go

+ 0
- 39
libs/events/event_cache.go View File

@ -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
}

+ 0
- 49
libs/events/event_cache_test.go View File

@ -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)
}

Loading…
Cancel
Save