github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/eventlog/cursor/cursor_test.go (about)

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