You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

  1. package consensus
  2. import (
  3. "bytes"
  4. "path"
  5. "testing"
  6. "time"
  7. "github.com/tendermint/tendermint/consensus/types"
  8. tmtypes "github.com/tendermint/tendermint/types"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestWALEncoderDecoder(t *testing.T) {
  14. now := time.Now()
  15. msgs := []TimedWALMessage{
  16. TimedWALMessage{Time: now, Msg: EndHeightMessage{0}},
  17. TimedWALMessage{Time: now, Msg: timeoutInfo{Duration: time.Second, Height: 1, Round: 1, Step: types.RoundStepPropose}},
  18. }
  19. b := new(bytes.Buffer)
  20. for _, msg := range msgs {
  21. b.Reset()
  22. enc := NewWALEncoder(b)
  23. err := enc.Encode(&msg)
  24. require.NoError(t, err)
  25. dec := NewWALDecoder(b)
  26. decoded, err := dec.Decode()
  27. require.NoError(t, err)
  28. assert.Equal(t, msg.Time.Truncate(time.Millisecond), decoded.Time)
  29. assert.Equal(t, msg.Msg, decoded.Msg)
  30. }
  31. }
  32. func TestSearchForEndHeight(t *testing.T) {
  33. wal, err := NewWAL(path.Join(data_dir, "many_blocks.cswal"), false)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. h := 3
  38. gr, found, err := wal.SearchForEndHeight(uint64(h))
  39. assert.NoError(t, err, cmn.Fmt("expected not to err on height %d", h))
  40. assert.True(t, found, cmn.Fmt("expected to find end height for %d", h))
  41. assert.NotNil(t, gr, "expected group not to be nil")
  42. defer gr.Close()
  43. dec := NewWALDecoder(gr)
  44. msg, err := dec.Decode()
  45. assert.NoError(t, err, "expected to decode a message")
  46. rs, ok := msg.Msg.(tmtypes.EventDataRoundState)
  47. assert.True(t, ok, "expected message of type EventDataRoundState")
  48. assert.Equal(t, rs.Height, h+1, cmn.Fmt("wrong height"))
  49. }