mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-29 06:01:21 +00:00
Merge pull request #64 from silasdavis/event-cache
Fix zeroed buffer getting flushed to the empty event
This commit is contained in:
commit
d4c6a68e58
@ -1,9 +1,5 @@
|
|||||||
package events
|
package events
|
||||||
|
|
||||||
const (
|
|
||||||
eventsBufferSize = 1000
|
|
||||||
)
|
|
||||||
|
|
||||||
// An EventCache buffers events for a Fireable
|
// An EventCache buffers events for a Fireable
|
||||||
// All events are cached. Filtering happens on Flush
|
// All events are cached. Filtering happens on Flush
|
||||||
type EventCache struct {
|
type EventCache struct {
|
||||||
@ -14,8 +10,7 @@ type EventCache struct {
|
|||||||
// Create a new EventCache with an EventSwitch as backend
|
// Create a new EventCache with an EventSwitch as backend
|
||||||
func NewEventCache(evsw Fireable) *EventCache {
|
func NewEventCache(evsw Fireable) *EventCache {
|
||||||
return &EventCache{
|
return &EventCache{
|
||||||
evsw: evsw,
|
evsw: evsw,
|
||||||
events: make([]eventInfo, eventsBufferSize),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +22,7 @@ type eventInfo struct {
|
|||||||
|
|
||||||
// Cache an event to be fired upon finality.
|
// Cache an event to be fired upon finality.
|
||||||
func (evc *EventCache) FireEvent(event string, data EventData) {
|
func (evc *EventCache) FireEvent(event string, data EventData) {
|
||||||
// append to list
|
// append to list (go will grow our backing array exponentially)
|
||||||
evc.events = append(evc.events, eventInfo{event, data})
|
evc.events = append(evc.events, eventInfo{event, data})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,5 +32,6 @@ func (evc *EventCache) Flush() {
|
|||||||
for _, ei := range evc.events {
|
for _, ei := range evc.events {
|
||||||
evc.evsw.FireEvent(ei.event, ei.data)
|
evc.evsw.FireEvent(ei.event, ei.data)
|
||||||
}
|
}
|
||||||
evc.events = make([]eventInfo, eventsBufferSize)
|
// 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
|
||||||
}
|
}
|
||||||
|
35
events/event_cache_test.go
Normal file
35
events/event_cache_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package events
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEventCache_Flush(t *testing.T) {
|
||||||
|
evsw := NewEventSwitch()
|
||||||
|
evsw.Start()
|
||||||
|
evsw.AddListenerForEvent("nothingness", "", func(data EventData) {
|
||||||
|
// Check we are not initialising 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")
|
||||||
|
})
|
||||||
|
evc := NewEventCache(evsw)
|
||||||
|
evc.Flush()
|
||||||
|
// Check after reset
|
||||||
|
evc.Flush()
|
||||||
|
fail := true
|
||||||
|
pass := false
|
||||||
|
evsw.AddListenerForEvent("somethingness", "something", func(data EventData) {
|
||||||
|
if fail {
|
||||||
|
require.FailNow(t, "Shouldn't see a message until flushed")
|
||||||
|
}
|
||||||
|
pass = true
|
||||||
|
})
|
||||||
|
evc.FireEvent("something", struct{ int }{1})
|
||||||
|
evc.FireEvent("something", struct{ int }{2})
|
||||||
|
evc.FireEvent("something", struct{ int }{3})
|
||||||
|
fail = false
|
||||||
|
evc.Flush()
|
||||||
|
assert.True(t, pass)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user