forked from hashicorp/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inflight_test.go
116 lines (94 loc) · 2.18 KB
/
inflight_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package raft
import (
"fmt"
"testing"
)
func TestInflight_StartCommit(t *testing.T) {
commitCh := make(chan *logFuture, 1)
in := newInflight(commitCh)
// Commit a transaction as being in flight
l := &logFuture{log: Log{Index: 1}}
l.policy = newMajorityQuorum(5)
in.Start(l)
// Commit 3 times
in.Commit(1, nil)
select {
case <-commitCh:
t.Fatalf("should not be commited")
default:
}
in.Commit(1, nil)
select {
case <-commitCh:
t.Fatalf("should not be commited")
default:
}
in.Commit(1, nil)
select {
case <-commitCh:
default:
t.Fatalf("should be commited")
}
// Already commited but should work anyways
in.Commit(1, nil)
}
func TestInflight_Cancel(t *testing.T) {
commitCh := make(chan *logFuture, 1)
in := newInflight(commitCh)
// Commit a transaction as being in flight
l := &logFuture{
log: Log{Index: 1},
}
l.init()
l.policy = newMajorityQuorum(3)
in.Start(l)
// Cancel with an error
err := fmt.Errorf("error 1")
in.Cancel(err)
// Should get an error return
if l.Error() != err {
t.Fatalf("expected error")
}
}
func TestInflight_CommitRange(t *testing.T) {
commitCh := make(chan *logFuture, 3)
in := newInflight(commitCh)
// Commit a few transaction as being in flight
l1 := &logFuture{log: Log{Index: 2}}
l1.policy = newMajorityQuorum(5)
in.Start(l1)
l2 := &logFuture{log: Log{Index: 3}}
l2.policy = newMajorityQuorum(5)
in.Start(l2)
l3 := &logFuture{log: Log{Index: 4}}
l3.policy = newMajorityQuorum(5)
in.Start(l3)
// Commit ranges
in.CommitRange(1, 5, nil)
in.CommitRange(1, 4, nil)
in.CommitRange(1, 10, nil)
// Should get 3 back
if len(commitCh) != 3 {
t.Fatalf("expected all 3 to commit")
}
}
// Should panic if we commit non contiguously!
func TestInflight_NonContiguous(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("should panic")
}
}()
commitCh := make(chan *logFuture, 3)
in := newInflight(commitCh)
// Commit a few transaction as being in flight
l1 := &logFuture{log: Log{Index: 2}}
l1.policy = newMajorityQuorum(5)
in.Start(l1)
l2 := &logFuture{log: Log{Index: 3}}
l2.policy = newMajorityQuorum(5)
in.Start(l2)
in.Commit(3, nil)
in.Commit(3, nil)
in.Commit(3, nil) // panic!
}