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.

76 lines
2.4 KiB

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