File: control_test.go

package info (click to toggle)
golang-github-canonicalltd-raft-test 0.0~git20180628.c3345b5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 284 kB
  • sloc: makefile: 2
file content (239 lines) | stat: -rw-r--r-- 7,461 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2017 Canonical Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rafttest_test

import (
	"strconv"
	"testing"
	"time"

	"github.com/CanonicalLtd/raft-test"
	"github.com/hashicorp/raft"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Elect and depose a leader.
func TestControl_ElectAndDepose(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3), rafttest.DiscardLogger())
	defer control.Close()

	control.Elect("0")

	r := rafts["0"]
	assert.Equal(t, raft.Leader, r.State())

	control.Depose()

	assert.NotEqual(t, raft.Leader, r.State())
}

// Depose a previously elected leader after a certain command log gets
// enqueued.
func TestControl_DeposeAfterCommandEnqueued(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3)) // rafttest.DiscardLogger())
	defer control.Close()

	control.Elect("0").When().Command(2).Enqueued().Depose()

	r := rafts["0"]

	err := r.Apply([]byte{}, time.Second).Error()
	require.NoError(t, err)
	err = r.Apply([]byte{}, time.Second).Error()
	assert.EqualError(t, err, raft.ErrLeadershipLost.Error())
	assert.Equal(t, uint64(1), control.Commands("0"))
}

// Depose a previously elected leader after a certain command log gets
// enqueued and then elect another one.
func TestControl_DeposeAfterCommandEnqueuedThenElect(t *testing.T) {
	n := 3
	for i := 1; i < 1; i++ {
		id := raft.ServerID(strconv.Itoa(i)) // ID of the next leader
		t.Run(string(id), func(t *testing.T) {
			rafts, control := rafttest.Cluster(t, rafttest.FSMs(n), rafttest.DiscardLogger())
			defer control.Close()

			control.Elect("0").When().Command(2).Enqueued().Depose()

			r := rafts["0"]
			err := r.Apply([]byte{}, time.Second).Error()
			require.NoError(t, err)
			err = r.Apply([]byte{}, time.Second).Error()
			assert.EqualError(t, err, raft.ErrLeadershipLost.Error())

			control.Elect(id)
			r = rafts[id]
			err = r.Apply([]byte{}, time.Second).Error()
			require.NoError(t, err)
			assert.Equal(t, uint64(2), control.Commands(id))
		})
	}
}

// Depose a previously elected leader after a certain command log gets enqueued
// and then elect the same one. The recovered leader sends the command logs
// that have failed.
func TestControl_DeposeAfterCommandEnqueuedThenElectSame(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3), rafttest.DiscardLogger())
	defer control.Close()

	control.Elect("0").When().Command(2).Enqueued().Depose()

	r := rafts["0"]
	err := r.Apply([]byte{}, time.Second).Error()
	require.NoError(t, err)
	err = r.Apply([]byte{}, time.Second).Error()
	assert.EqualError(t, err, raft.ErrLeadershipLost.Error())

	control.Elect("0")
	r = rafts["0"]
	err = r.Apply([]byte{}, time.Second).Error()
	require.NoError(t, err)
	assert.Equal(t, uint64(3), control.Commands("0"))
}

// Depose a previously elected leader after a certain command log gets
// appended by all followers.
func TestControl_DeposeAfterCommandAppended(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3), rafttest.DiscardLogger())
	defer control.Close()

	control.Elect("0").When().Command(1).Appended().Depose()

	r := rafts["0"]
	err := r.Apply([]byte{}, time.Second).Error()
	assert.EqualError(t, err, raft.ErrLeadershipLost.Error())
	assert.Equal(t, uint64(0), control.Commands("0"))
}

// Depose a previously elected leader after a certain command log gets
// appended and then elect another one.
func TestControl_DeposeAfterCommandAppendedThenElect(t *testing.T) {
	n := 3
	for i := 0; i < n; i++ {
		id := raft.ServerID(strconv.Itoa(i)) // ID of the next leader
		t.Run(string(id), func(t *testing.T) {
			rafts, control := rafttest.Cluster(t, rafttest.FSMs(n))
			defer control.Close()

			control.Elect("0").When().Command(2).Appended().Depose()

			r := rafts["0"]
			err := r.Apply([]byte{}, time.Second).Error()
			require.NoError(t, err)
			err = r.Apply([]byte{}, time.Second).Error()
			assert.EqualError(t, err, raft.ErrLeadershipLost.Error())

			control.Elect(id)
			r = rafts[id]
			err = r.Apply([]byte{}, time.Second).Error()
			require.NoError(t, err)
			assert.Equal(t, uint64(3), control.Commands(id))
		})
	}
}

// Depose a previously elected leader after a certain command log gets
// committed.
func TestControl_DeposeAfterCommandCommitted(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3)) // rafttest.DiscardLogger())
	defer control.Close()

	control.Elect("0").When().Command(1).Committed().Depose()

	r := rafts["0"]
	err := r.Apply([]byte{}, time.Second).Error()
	require.NoError(t, err)
	err = r.Apply([]byte{}, time.Second).Error()
	assert.EqualError(t, err, raft.ErrNotLeader.Error())
	assert.Equal(t, uint64(1), control.Commands("0"))
}

// Depose a previously elected leader after a certain command log gets
// committed and then elect another one.
func TestControl_DeposeAfterCommandCommittedThenElect(t *testing.T) {
	n := 3
	for i := 0; i < n; i++ {
		id := raft.ServerID(strconv.Itoa(i)) // ID of the next leader
		t.Run(string(id), func(t *testing.T) {
			rafts, control := rafttest.Cluster(t, rafttest.FSMs(n)) // rafttest.DiscardLogger())
			defer control.Close()

			control.Elect("0").When().Command(1).Committed().Depose()

			r := rafts["0"]
			err := r.Apply([]byte{}, time.Second).Error()
			require.NoError(t, err)
			err = r.Apply([]byte{}, time.Second).Error()
			assert.EqualError(t, err, raft.ErrNotLeader.Error())

			control.Elect(id)
			r = rafts[id]
			err = r.Apply([]byte{}, time.Second).Error()
			require.NoError(t, err)
			assert.Equal(t, uint64(2), control.Commands(id))
		})
	}
}

// Take a snapshot on the leader when a certain command log gets committed.
func TestControl_SnapshotAfterCommandCommitted(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3)) // rafttest.DiscardLogger())
	defer control.Close()

	control.Elect("0").When().Command(2).Committed().Snapshot()

	r := rafts["0"]
	err := r.Apply([]byte{}, time.Second).Error()
	require.NoError(t, err)
	err = r.Apply([]byte{}, time.Second).Error()
	require.NoError(t, err)

	control.Barrier()

	assert.Equal(t, uint64(1), control.Snapshots("0"))
}

// Make a follower restore from a snapshot after a disconnection.
func TestControl_RestoreAfterDisconnection(t *testing.T) {
	rafts, control := rafttest.Cluster(t, rafttest.FSMs(3)) // rafttest.DiscardLogger())
	defer control.Close()

	term := control.Elect("0")
	term.When().Command(4).Committed().Snapshot()

	r := rafts["0"]
	for i := 0; i < 6; i++ {
		err := r.Apply([]byte{}, time.Second).Error()
		require.NoError(t, err)
		if i == 0 {
			term.Disconnect("1")
		}
		if i == 4 {
			term.Reconnect("1")
		}
	}

	control.Barrier()

	assert.Equal(t, uint64(1), control.Snapshots("0"))
	assert.Equal(t, uint64(1), control.Restores("1"))

	assert.Equal(t, uint64(6), control.Commands("0"))
	assert.Equal(t, uint64(6), control.Commands("1"))
	assert.Equal(t, uint64(6), control.Commands("2"))
}