1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package sharding
16
17 import (
18 "testing"
19 )
20
21 func TestVirtualLogIndex(t *testing.T) {
22 tests := []struct {
23 description string
24 leafIndex int64
25 tid int64
26 ranges LogRanges
27 expectedIndex int64
28 }{
29 {
30 description: "no ranges",
31 leafIndex: 5,
32 ranges: LogRanges{},
33 expectedIndex: 5,
34 },
35
36
37 {
38 description: "two shards",
39 leafIndex: 2,
40 tid: 300,
41 ranges: LogRanges{
42 inactive: []LogRange{
43 {
44 TreeID: 100,
45 TreeLength: 5,
46 }},
47 active: 300,
48 },
49 expectedIndex: 7,
50 },
51
52
53
54 {
55 description: "three shards",
56 leafIndex: 1,
57 tid: 300,
58 ranges: LogRanges{
59 inactive: []LogRange{
60 {
61 TreeID: 100,
62 TreeLength: 5,
63 }, {
64 TreeID: 300,
65 TreeLength: 4,
66 }},
67 active: 400,
68 },
69 expectedIndex: 6,
70 },
71
72 {
73 description: "only active tree",
74 leafIndex: 2,
75 tid: 30,
76 ranges: LogRanges{
77 active: 30,
78 },
79 expectedIndex: 2,
80 }, {
81 description: "invalid tid passed in",
82 leafIndex: 2,
83 tid: 4,
84 ranges: LogRanges{
85 active: 30,
86 },
87 expectedIndex: -1,
88 },
89 }
90
91 for _, test := range tests {
92 t.Run(test.description, func(t *testing.T) {
93 got := VirtualLogIndex(test.leafIndex, test.tid, test.ranges)
94 if got != test.expectedIndex {
95 t.Fatalf("expected %v got %v", test.expectedIndex, got)
96 }
97 })
98 }
99 }
100
View as plain text