File: inflight_test.go

package info (click to toggle)
golang-github-hashicorp-raft 0.0~git20160317.0.3359516-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 428 kB
  • sloc: makefile: 16
file content (150 lines) | stat: -rw-r--r-- 3,057 bytes parent folder | download | duplicates (3)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package raft

import (
	"fmt"
	"testing"
)

func TestInflight_StartCommit(t *testing.T) {
	commitCh := make(chan struct{}, 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)
	if in.Committed().Len() != 0 {
		t.Fatalf("should not be commited")
	}

	in.Commit(1)
	if in.Committed().Len() != 1 {
		t.Fatalf("should be commited")
	}

	// Already committed but should work anyways
	in.Commit(1)
}

func TestInflight_Cancel(t *testing.T) {
	commitCh := make(chan struct{}, 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_StartAll(t *testing.T) {
	commitCh := make(chan struct{}, 1)
	in := newInflight(commitCh)

	// Commit a few transaction as being in flight
	l1 := &logFuture{log: Log{Index: 2}}
	l1.policy = newMajorityQuorum(5)
	l2 := &logFuture{log: Log{Index: 3}}
	l2.policy = newMajorityQuorum(5)
	l3 := &logFuture{log: Log{Index: 4}}
	l3.policy = newMajorityQuorum(5)

	// Start all the entries
	in.StartAll([]*logFuture{l1, l2, l3})

	// Commit ranges
	in.CommitRange(1, 5)
	in.CommitRange(1, 4)
	in.CommitRange(1, 10)

	// Should get 3 back
	if in.Committed().Len() != 3 {
		t.Fatalf("expected all 3 to commit")
	}
}

func TestInflight_CommitRange(t *testing.T) {
	commitCh := make(chan struct{}, 1)
	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)
	in.CommitRange(1, 4)
	in.CommitRange(1, 10)

	// Should get 3 back
	if in.Committed().Len() != 3 {
		t.Fatalf("expected all 3 to commit")
	}
}

// Should panic if we commit non contiguously!
func TestInflight_NonContiguous(t *testing.T) {
	commitCh := make(chan struct{}, 1)
	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)
	in.Commit(3)
	in.Commit(3) // panic!

	if in.Committed().Len() != 0 {
		t.Fatalf("should not commit")
	}

	in.Commit(2)
	in.Commit(2)
	in.Commit(2) // panic!

	committed := in.Committed()
	if committed.Len() != 2 {
		t.Fatalf("should commit both")
	}

	current := committed.Front()
	l := current.Value.(*logFuture)
	if l.log.Index != 2 {
		t.Fatalf("bad: %v", *l)
	}

	current = current.Next()
	l = current.Value.(*logFuture)
	if l.log.Index != 3 {
		t.Fatalf("bad: %v", *l)
	}
}