neoneo-go/pkg/consensus/cache_test.go

64 lines
1.5 KiB
Go
Raw Normal View History

2019-11-08 15:40:21 +00:00
package consensus
import (
"testing"
"github.com/nspcc-dev/neo-go/internal/random"
2019-11-08 15:40:21 +00:00
"github.com/stretchr/testify/require"
)
func TestRelayCache_Add(t *testing.T) {
const capacity = 3
payloads := getDifferentPayloads(t, capacity+1)
c := newFIFOCache(capacity)
require.Equal(t, 0, c.queue.Len())
require.Equal(t, 0, len(c.elems))
for i := 1; i < capacity; i++ {
c.Add(&payloads[i])
2019-11-15 10:32:40 +00:00
require.True(t, c.Has(payloads[i].Hash()))
2019-11-08 15:40:21 +00:00
require.Equal(t, i, c.queue.Len())
require.Equal(t, i, len(c.elems))
}
// add already existing payload
c.Add(&payloads[1])
require.Equal(t, capacity-1, c.queue.Len())
require.Equal(t, capacity-1, len(c.elems))
c.Add(&payloads[0])
require.Equal(t, capacity, c.queue.Len())
require.Equal(t, capacity, len(c.elems))
// capacity does not exceed maximum
c.Add(&payloads[capacity])
require.Equal(t, capacity, c.queue.Len())
require.Equal(t, capacity, len(c.elems))
// recent payloads are still present in cache
require.Equal(t, &payloads[0], c.Get(payloads[0].Hash()))
for i := 2; i <= capacity; i++ {
require.Equal(t, &payloads[i], c.Get(payloads[i].Hash()))
}
// oldest payload was removed
2019-11-15 10:32:40 +00:00
require.Equal(t, nil, c.Get(payloads[1].Hash()))
2019-11-08 15:40:21 +00:00
}
func getDifferentPayloads(t *testing.T, n int) (payloads []Payload) {
payloads = make([]Payload, n)
for i := range payloads {
var sign [signatureSize]byte
random.Fill(sign[:])
2019-11-08 15:40:21 +00:00
payloads[i].message.ValidatorIndex = byte(i)
payloads[i].message.Type = commitType
2019-11-08 15:40:21 +00:00
payloads[i].payload = &commit{
2019-11-15 10:32:40 +00:00
signature: sign,
2019-11-08 15:40:21 +00:00
}
payloads[i].encodeData()
2019-11-08 15:40:21 +00:00
}
return
}