|
| 1 | +package raft |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAssertValid(t *testing.T) { |
| 10 | + specs := map[string]struct { |
| 11 | + s *RaftBlockState |
| 12 | + next *RaftBlockState |
| 13 | + expErr bool |
| 14 | + }{ |
| 15 | + "ok": { |
| 16 | + s: &RaftBlockState{Height: 10, Timestamp: 100}, |
| 17 | + next: &RaftBlockState{Height: 11, Timestamp: 101}, |
| 18 | + }, |
| 19 | + "all equal": { |
| 20 | + s: &RaftBlockState{Height: 10, Timestamp: 100}, |
| 21 | + next: &RaftBlockState{Height: 10, Timestamp: 100}, |
| 22 | + expErr: true, |
| 23 | + }, |
| 24 | + "equal height": { |
| 25 | + s: &RaftBlockState{Height: 10, Timestamp: 100}, |
| 26 | + next: &RaftBlockState{Height: 10, Timestamp: 101}, |
| 27 | + expErr: true, |
| 28 | + }, |
| 29 | + "equal timestamp": { |
| 30 | + s: &RaftBlockState{Height: 10, Timestamp: 100}, |
| 31 | + next: &RaftBlockState{Height: 11, Timestamp: 100}, |
| 32 | + expErr: true, |
| 33 | + }, |
| 34 | + "lower timestamp": { |
| 35 | + s: &RaftBlockState{Height: 10, Timestamp: 101}, |
| 36 | + next: &RaftBlockState{Height: 11, Timestamp: 100}, |
| 37 | + expErr: true, |
| 38 | + }, |
| 39 | + "lower height": { |
| 40 | + s: &RaftBlockState{Height: 11, Timestamp: 100}, |
| 41 | + next: &RaftBlockState{Height: 10, Timestamp: 101}, |
| 42 | + expErr: true, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + for name, spec := range specs { |
| 47 | + t.Run(name, func(t *testing.T) { |
| 48 | + err := assertValid(spec.s, spec.next) |
| 49 | + if spec.expErr { |
| 50 | + require.Error(t, err) |
| 51 | + return |
| 52 | + } |
| 53 | + require.NoError(t, err) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
0 commit comments