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