@@ -27,32 +27,48 @@ import (
27
27
28
28
func TestFindConflict (t * testing.T ) {
29
29
previousEnts := index (1 ).terms (1 , 2 , 3 )
30
- tests := []struct {
31
- ents []pb.Entry
32
- wconflict uint64
30
+ ids := make ([]entryID , 1 , len (previousEnts )+ 1 ) // dummy (0, 0) at index 0
31
+ for i := range previousEnts {
32
+ ids = append (ids , pbEntryID (& previousEnts [i ]))
33
+ }
34
+ for _ , tt := range []struct {
35
+ prev entryID
36
+ ents []pb.Entry
37
+ notOk bool
38
+ want entryID
33
39
}{
34
- // no conflict, empty ent
35
- {nil , 0 },
40
+ // prev does not match the log
41
+ {prev : entryID {term : 10 , index : 1 }, notOk : true },
42
+ {prev : entryID {term : 4 , index : 1 }, ents : index (2 ).terms (4 , 4 ), notOk : true },
43
+ {prev : entryID {term : 5 , index : 2 }, ents : index (3 ).terms (5 , 6 ), notOk : true },
44
+ // no conflict, empty entries
45
+ {ents : nil , want : ids [0 ]},
36
46
// no conflict
37
- {index (1 ).terms (1 , 2 , 3 ), 0 },
38
- {index (2 ).terms (2 , 3 ), 0 },
39
- {index (3 ).terms (3 ), 0 },
47
+ {prev : ids [ 0 ], ents : index (1 ).terms (1 , 2 , 3 ), want : ids [ 3 ] },
48
+ {prev : ids [ 1 ], ents : index (2 ).terms (2 , 3 ), want : ids [ 3 ] },
49
+ {prev : ids [ 2 ], ents : index (3 ).terms (3 ), want : ids [ 3 ] },
40
50
// no conflict, but has new entries
41
- {index (1 ).terms (1 , 2 , 3 , 4 , 4 ), 4 },
42
- {index (2 ).terms (2 , 3 , 4 , 5 ), 4 },
43
- {index (3 ).terms (3 , 4 , 4 ), 4 },
44
- {index (4 ).terms (4 , 4 ), 4 },
45
- // conflicts with existing entries
46
- {index (1 ).terms (4 , 4 ), 1 },
47
- {index (2 ).terms (1 , 4 , 4 ), 2 },
48
- {index (3 ).terms (1 , 2 , 4 , 4 ), 3 },
49
- }
50
-
51
- for i , tt := range tests {
52
- t .Run (fmt .Sprint (i ), func (t * testing.T ) {
53
- raftLog := newLog (NewMemoryStorage (), raftLogger )
54
- raftLog .append (previousEnts ... )
55
- require .Equal (t , tt .wconflict , raftLog .findConflict (tt .ents ))
51
+ {prev : ids [0 ], ents : index (1 ).terms (1 , 2 , 3 , 4 , 4 ), want : ids [3 ]},
52
+ {prev : ids [1 ], ents : index (2 ).terms (2 , 3 , 4 , 4 ), want : ids [3 ]},
53
+ {prev : ids [2 ], ents : index (3 ).terms (3 , 4 , 4 ), want : ids [3 ]},
54
+ {prev : ids [3 ], ents : index (4 ).terms (4 , 4 ), want : ids [3 ]},
55
+ // passes prev check, but conflicts with existing entries
56
+ {prev : ids [0 ], ents : index (1 ).terms (4 , 4 ), want : ids [0 ]},
57
+ {prev : ids [1 ], ents : index (2 ).terms (1 , 4 , 4 ), want : ids [1 ]},
58
+ {prev : ids [2 ], ents : index (3 ).terms (2 , 2 , 4 , 4 ), want : ids [2 ]},
59
+ // out of bounds
60
+ {prev : entryID {term : 3 , index : 10 }, ents : index (11 ).terms (3 ), notOk : true },
61
+ // just touching the right bound, but still out of bounds
62
+ {prev : entryID {term : 3 , index : 4 }, ents : index (5 ).terms (3 , 3 , 4 ), notOk : true },
63
+ } {
64
+ t .Run ("" , func (t * testing.T ) {
65
+ log := newLog (NewMemoryStorage (), discardLogger )
66
+ log .append (previousEnts ... )
67
+ app := logSlice {term : 100 , prev : tt .prev , entries : tt .ents }
68
+ require .NoError (t , app .valid ())
69
+ match , ok := log .findConflict (app )
70
+ require .Equal (t , ! tt .notOk , ok )
71
+ require .Equal (t , tt .want , match )
56
72
})
57
73
}
58
74
}
0 commit comments