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.

141 lines
3.2 KiB

  1. package cursor_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/tendermint/tendermint/internal/eventlog/cursor"
  7. )
  8. func mustParse(t *testing.T, s string) cursor.Cursor {
  9. t.Helper()
  10. var c cursor.Cursor
  11. if err := c.UnmarshalText([]byte(s)); err != nil {
  12. t.Fatalf("Unmarshal %q: unexpected error: %v", s, err)
  13. }
  14. return c
  15. }
  16. func TestSource_counter(t *testing.T) {
  17. src := &cursor.Source{
  18. TimeIndex: func() int64 { return 255 },
  19. }
  20. for i := 1; i <= 5; i++ {
  21. want := fmt.Sprintf("00000000000000ff-%04x", i)
  22. got := src.Cursor().String()
  23. if got != want {
  24. t.Errorf("Cursor %d: got %q, want %q", i, got, want)
  25. }
  26. }
  27. }
  28. func TestSource_timeIndex(t *testing.T) {
  29. times := []int64{0, 1, 100, 65535, 0x76543210fecdba98}
  30. src := &cursor.Source{
  31. TimeIndex: func() int64 {
  32. out := times[0]
  33. times = append(times[1:], out)
  34. return out
  35. },
  36. Counter: 160,
  37. }
  38. results := []string{
  39. "0000000000000000-00a1",
  40. "0000000000000001-00a2",
  41. "0000000000000064-00a3",
  42. "000000000000ffff-00a4",
  43. "76543210fecdba98-00a5",
  44. }
  45. for i, want := range results {
  46. if got := src.Cursor().String(); got != want {
  47. t.Errorf("Cursor %d: got %q, want %q", i+1, got, want)
  48. }
  49. }
  50. }
  51. func TestCursor_roundTrip(t *testing.T) {
  52. const text = `0123456789abcdef-fce9`
  53. c := mustParse(t, text)
  54. if got := c.String(); got != text {
  55. t.Errorf("Wrong string format: got %q, want %q", got, text)
  56. }
  57. cmp, err := c.MarshalText()
  58. if err != nil {
  59. t.Fatalf("Marshal %+v failed: %v", c, err)
  60. }
  61. if got := string(cmp); got != text {
  62. t.Errorf("Wrong text format: got %q, want %q", got, text)
  63. }
  64. }
  65. func TestCursor_ordering(t *testing.T) {
  66. // Condition: text1 precedes text2 in time order.
  67. // Condition: text2 has an earlier sequence than text1.
  68. const zero = ""
  69. const text1 = "0000000012345678-0005"
  70. const text2 = "00000000fecdeba9-0002"
  71. zc := mustParse(t, zero)
  72. c1 := mustParse(t, text1)
  73. c2 := mustParse(t, text2)
  74. // Confirm for all pairs that string order respects time order.
  75. pairs := []struct {
  76. t1, t2 string
  77. c1, c2 cursor.Cursor
  78. }{
  79. {zero, zero, zc, zc},
  80. {zero, text1, zc, c1},
  81. {zero, text2, zc, c2},
  82. {text1, zero, c1, zc},
  83. {text1, text1, c1, c1},
  84. {text1, text2, c1, c2},
  85. {text2, zero, c2, zc},
  86. {text2, text1, c2, c1},
  87. {text2, text2, c2, c2},
  88. }
  89. for _, pair := range pairs {
  90. want := pair.t1 < pair.t2
  91. if got := pair.c1.Before(pair.c2); got != want {
  92. t.Errorf("(%s).Before(%s): got %v, want %v", pair.t1, pair.t2, got, want)
  93. }
  94. }
  95. }
  96. func TestCursor_IsZero(t *testing.T) {
  97. tests := []struct {
  98. text string
  99. want bool
  100. }{
  101. {"", true},
  102. {"0000000000000000-0000", true},
  103. {"0000000000000001-0000", false},
  104. {"0000000000000000-0001", false},
  105. {"0000000000000001-0001", false},
  106. }
  107. for _, test := range tests {
  108. c := mustParse(t, test.text)
  109. if got := c.IsZero(); got != test.want {
  110. t.Errorf("IsZero(%q): got %v, want %v", test.text, got, test.want)
  111. }
  112. }
  113. }
  114. func TestCursor_Diff(t *testing.T) {
  115. const time1 = 0x1ac0193001
  116. const time2 = 0x0ac0193001
  117. text1 := fmt.Sprintf("%016x-0001", time1)
  118. text2 := fmt.Sprintf("%016x-0005", time2)
  119. want := time.Duration(time1 - time2)
  120. c1 := mustParse(t, text1)
  121. c2 := mustParse(t, text2)
  122. got := c1.Diff(c2)
  123. if got != want {
  124. t.Fatalf("Diff %q - %q: got %v, want %v", text1, text2, got, want)
  125. }
  126. }