@@ -39,3 +39,56 @@ func TestEntryID(t *testing.T) {
39
39
require .Equal (t , tt .want , pbEntryID (& tt .entry ))
40
40
}
41
41
}
42
+
43
+ func TestLogSlice (t * testing.T ) {
44
+ id := func (index , term uint64 ) entryID {
45
+ return entryID {term : term , index : index }
46
+ }
47
+ e := func (index , term uint64 ) pb.Entry {
48
+ return pb.Entry {Term : term , Index : index }
49
+ }
50
+ for _ , tt := range []struct {
51
+ term uint64
52
+ prev entryID
53
+ entries []pb.Entry
54
+
55
+ notOk bool
56
+ last entryID
57
+ }{
58
+ // Empty "dummy" slice, starting at (0, 0) origin of the log.
59
+ {last : id (0 , 0 )},
60
+ // Empty slice with a given prev ID. Valid only if term >= prev.term.
61
+ {prev : id (123 , 10 ), notOk : true },
62
+ {term : 9 , prev : id (123 , 10 ), notOk : true },
63
+ {term : 10 , prev : id (123 , 10 ), last : id (123 , 10 )},
64
+ {term : 11 , prev : id (123 , 10 ), last : id (123 , 10 )},
65
+ // A single entry.
66
+ {term : 0 , entries : []pb.Entry {e (1 , 1 )}, notOk : true },
67
+ {term : 1 , entries : []pb.Entry {e (1 , 1 )}, last : id (1 , 1 )},
68
+ {term : 2 , entries : []pb.Entry {e (1 , 1 )}, last : id (1 , 1 )},
69
+ // Multiple entries.
70
+ {term : 2 , entries : []pb.Entry {e (2 , 1 ), e (3 , 1 ), e (4 , 2 )}, notOk : true },
71
+ {term : 1 , prev : id (1 , 1 ), entries : []pb.Entry {e (2 , 1 ), e (3 , 1 ), e (4 , 2 )}, notOk : true },
72
+ {term : 2 , prev : id (1 , 1 ), entries : []pb.Entry {e (2 , 1 ), e (3 , 1 ), e (4 , 2 )}, last : id (4 , 2 )},
73
+ // First entry inconsistent with prev.
74
+ {term : 10 , prev : id (123 , 5 ), entries : []pb.Entry {e (111 , 5 )}, notOk : true },
75
+ {term : 10 , prev : id (123 , 5 ), entries : []pb.Entry {e (124 , 4 )}, notOk : true },
76
+ {term : 10 , prev : id (123 , 5 ), entries : []pb.Entry {e (234 , 6 )}, notOk : true },
77
+ {term : 10 , prev : id (123 , 5 ), entries : []pb.Entry {e (124 , 6 )}, last : id (124 , 6 )},
78
+ // Inconsistent entries.
79
+ {term : 10 , prev : id (12 , 2 ), entries : []pb.Entry {e (13 , 2 ), e (12 , 2 )}, notOk : true },
80
+ {term : 10 , prev : id (12 , 2 ), entries : []pb.Entry {e (13 , 2 ), e (15 , 2 )}, notOk : true },
81
+ {term : 10 , prev : id (12 , 2 ), entries : []pb.Entry {e (13 , 2 ), e (14 , 1 )}, notOk : true },
82
+ {term : 10 , prev : id (12 , 2 ), entries : []pb.Entry {e (13 , 2 ), e (14 , 3 )}, last : id (14 , 3 )},
83
+ } {
84
+ t .Run ("" , func (t * testing.T ) {
85
+ s := logSlice {term : tt .term , prev : tt .prev , entries : tt .entries }
86
+ require .Equal (t , tt .notOk , s .valid () != nil )
87
+ if ! tt .notOk {
88
+ last := s .lastEntryID ()
89
+ require .Equal (t , tt .last , last )
90
+ require .Equal (t , last .index , s .lastIndex ())
91
+ }
92
+ })
93
+ }
94
+ }
0 commit comments