tendermint/consensus/wal_test.go

135 lines
3.0 KiB
Go
Raw Normal View History

2017-10-09 12:10:58 -07:00
package consensus
import (
"bytes"
"crypto/rand"
"fmt"
2018-04-05 07:05:45 -07:00
// "sync"
2017-10-09 12:10:58 -07:00
"testing"
"time"
"github.com/tendermint/tendermint/consensus/types"
2017-10-25 18:54:56 -07:00
tmtypes "github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
2017-10-09 12:10:58 -07:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWALEncoderDecoder(t *testing.T) {
now := tmtime.Now()
2017-10-09 12:10:58 -07:00
msgs := []TimedWALMessage{
TimedWALMessage{Time: now, Msg: EndHeightMessage{0}},
TimedWALMessage{Time: now, Msg: timeoutInfo{Duration: time.Second, Height: 1, Round: 1, Step: types.RoundStepPropose}},
}
b := new(bytes.Buffer)
for _, msg := range msgs {
b.Reset()
enc := NewWALEncoder(b)
err := enc.Encode(&msg)
require.NoError(t, err)
dec := NewWALDecoder(b)
decoded, err := dec.Decode()
require.NoError(t, err)
2018-04-05 08:17:10 -07:00
assert.Equal(t, msg.Time.UTC(), decoded.Time)
2017-10-09 12:10:58 -07:00
assert.Equal(t, msg.Msg, decoded.Msg)
}
}
2017-10-25 18:54:56 -07:00
2018-01-18 17:38:19 -08:00
func TestWALSearchForEndHeight(t *testing.T) {
2017-12-06 16:28:14 -08:00
walBody, err := WALWithNBlocks(6)
2017-12-06 13:57:00 -08:00
if err != nil {
t.Fatal(err)
}
2017-12-06 16:28:14 -08:00
walFile := tempWALWithData(walBody)
2017-12-06 13:57:00 -08:00
2018-04-09 07:32:43 -07:00
wal, err := NewWAL(walFile)
2017-10-25 18:54:56 -07:00
if err != nil {
t.Fatal(err)
}
h := int64(3)
gr, found, err := wal.SearchForEndHeight(h, &WALSearchOptions{})
assert.NoError(t, err, fmt.Sprintf("expected not to err on height %d", h))
assert.True(t, found, fmt.Sprintf("expected to find end height for %d", h))
2017-10-25 18:54:56 -07:00
assert.NotNil(t, gr, "expected group not to be nil")
defer gr.Close()
dec := NewWALDecoder(gr)
msg, err := dec.Decode()
assert.NoError(t, err, "expected to decode a message")
rs, ok := msg.Msg.(tmtypes.EventDataRoundState)
assert.True(t, ok, "expected message of type EventDataRoundState")
assert.Equal(t, rs.Height, h+1, fmt.Sprintf("wrong height"))
2017-10-25 18:54:56 -07:00
}
2018-04-05 07:05:45 -07:00
/*
var initOnce sync.Once
func registerInterfacesOnce() {
initOnce.Do(func() {
var _ = wire.RegisterInterface(
struct{ WALMessage }{},
wire.ConcreteType{[]byte{}, 0x10},
)
})
}
2018-04-05 07:05:45 -07:00
*/
func nBytes(n int) []byte {
buf := make([]byte, n)
n, _ = rand.Read(buf)
return buf[:n]
}
func benchmarkWalDecode(b *testing.B, n int) {
2018-04-05 07:05:45 -07:00
// registerInterfacesOnce()
buf := new(bytes.Buffer)
enc := NewWALEncoder(buf)
data := nBytes(n)
enc.Encode(&TimedWALMessage{Msg: data, Time: time.Now().Round(time.Second).UTC()})
encoded := buf.Bytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.Write(encoded)
dec := NewWALDecoder(buf)
if _, err := dec.Decode(); err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
}
func BenchmarkWalDecode512B(b *testing.B) {
benchmarkWalDecode(b, 512)
}
func BenchmarkWalDecode10KB(b *testing.B) {
benchmarkWalDecode(b, 10*1024)
}
func BenchmarkWalDecode100KB(b *testing.B) {
benchmarkWalDecode(b, 100*1024)
}
func BenchmarkWalDecode1MB(b *testing.B) {
benchmarkWalDecode(b, 1024*1024)
}
func BenchmarkWalDecode10MB(b *testing.B) {
benchmarkWalDecode(b, 10*1024*1024)
}
func BenchmarkWalDecode100MB(b *testing.B) {
benchmarkWalDecode(b, 100*1024*1024)
}
func BenchmarkWalDecode1GB(b *testing.B) {
benchmarkWalDecode(b, 1024*1024*1024)
}