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.

78 lines
2.4 KiB

  1. package consensus
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "strings"
  7. "testing"
  8. . "github.com/tendermint/go-common"
  9. )
  10. var testTxt = `{"time":"2016-01-16T04:42:00.390Z","msg":[1,{"height":28219,"round":0,"step":"RoundStepPrevote"}]}
  11. {"time":"2016-01-16T04:42:00.390Z","msg":[2,{"msg":[20,{"ValidatorIndex":0,"Vote":{"height":28219,"round":0,"type":1,"block_hash":"67F9689F15BEC30BF311FB4C0C80C5E661AA44E0","block_parts_header":{"total":1,"hash":"DFFD4409A1E273ED61AC27CAF975F446020D5676"},"signature":"4CC6845A128E723A299B470CCBB2A158612AA51321447F6492F3DA57D135C27FCF4124B3B19446A248252BDA45B152819C76AAA5FD35E1C07091885CE6955E05"}}],"peer_key":""}]}
  12. {"time":"2016-01-16T04:42:00.392Z","msg":[1,{"height":28219,"round":0,"step":"RoundStepPrecommit"}]}
  13. {"time":"2016-01-16T04:42:00.392Z","msg":[2,{"msg":[20,{"ValidatorIndex":0,"Vote":{"height":28219,"round":0,"type":2,"block_hash":"67F9689F15BEC30BF311FB4C0C80C5E661AA44E0","block_parts_header":{"total":1,"hash":"DFFD4409A1E273ED61AC27CAF975F446020D5676"},"signature":"1B9924E010F47E0817695DFE462C531196E5A12632434DE12180BBA3EFDAD6B3960FDB9357AFF085EB61729A7D4A6AD8408555D7569C87D9028F280192FD4E05"}}],"peer_key":""}]}
  14. {"time":"2016-01-16T04:42:00.393Z","msg":[1,{"height":28219,"round":0,"step":"RoundStepCommit"}]}
  15. {"time":"2016-01-16T04:42:00.395Z","msg":[1,{"height":28220,"round":0,"step":"RoundStepNewHeight"}]}`
  16. func TestSeek(t *testing.T) {
  17. f, err := ioutil.TempFile(os.TempDir(), "seek_test_")
  18. if err != nil {
  19. panic(err)
  20. }
  21. stat, _ := f.Stat()
  22. name := stat.Name()
  23. _, err = f.WriteString(testTxt)
  24. if err != nil {
  25. panic(err)
  26. }
  27. f.Close()
  28. wal, err := NewWAL(path.Join(os.TempDir(), name), config.GetBool("cswal_light"))
  29. if err != nil {
  30. panic(err)
  31. }
  32. keyWord := "Precommit"
  33. n, err := wal.SeekFromEnd(func(b []byte) bool {
  34. if strings.Contains(string(b), keyWord) {
  35. return true
  36. }
  37. return false
  38. })
  39. if err != nil {
  40. panic(err)
  41. }
  42. // confirm n
  43. spl := strings.Split(testTxt, "\n")
  44. var i int
  45. var s string
  46. for i, s = range spl {
  47. if strings.Contains(s, keyWord) {
  48. break
  49. }
  50. }
  51. // n is lines from the end.
  52. spl = spl[i:]
  53. if n != len(spl) {
  54. panic(Fmt("Wrong nLines. Got %d, expected %d", n, len(spl)))
  55. }
  56. b, err := ioutil.ReadAll(wal.fp)
  57. if err != nil {
  58. panic(err)
  59. }
  60. // first char is a \n
  61. spl2 := strings.Split(strings.Trim(string(b), "\n"), "\n")
  62. for i, s := range spl {
  63. if s != spl2[i] {
  64. panic(Fmt("Mismatch. Got %s, expected %s", spl2[i], s))
  65. }
  66. }
  67. }