forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storagetest.go
81 lines (68 loc) · 1.99 KB
/
storagetest.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
// Copyright 2014 Square, Inc
// Author: Ben Darnell (bdarnell@)
/*
Package storagetest is a test suite for raft.Storage implementations.
The otherwise read-only storage interface is augmented with write
methods for use in the tests.
*/
package storagetest
import (
"testing"
"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb"
)
type testFunc func(*testing.T, WriteableStorage)
var testFuncs = []testFunc{
testEmptyLog,
}
// WriteableStorage is a raft.Storage with some additional write methods
// for testing.
type WriteableStorage interface {
raft.Storage
Append(entries []raftpb.Entry) error
SetHardState(st raftpb.HardState) error
}
// RunTests runs the test suite. The setUp and tearDown functions will be called
// at the beginning and end of each test case.
func RunTests(t *testing.T, setUp func(*testing.T) WriteableStorage,
tearDown func(*testing.T, WriteableStorage)) {
for _, f := range testFuncs {
// Use a nested function to create an inline defer scope.
func() {
s := setUp(t)
defer tearDown(t, s)
f(t, s)
}()
}
}
// testEmptyLog calls all the read methods on an empty log and verifies the expected
// state. Note that an empty log need not start from index zero.
func testEmptyLog(t *testing.T, s WriteableStorage) {
hs, _, err := s.InitialState()
if err != nil {
t.Fatal(err)
}
// When the log is empty, FirstIndex = LastIndex + 1 (typically 1 and 0).
// This is because both indices are inclusive so len == 1 + last - first
firstIndex, err := s.FirstIndex()
if err != nil {
t.Fatal(err)
}
if firstIndex < 1 {
t.Errorf("expected FirstIndex to be >= 1, got %d", firstIndex)
}
lastIndex, err := s.LastIndex()
if err != nil {
t.Fatal(err)
}
if lastIndex != firstIndex-1 {
t.Errorf("expected LastIndex to be firstIndex - 1 (%d), got %d", firstIndex-1, lastIndex)
}
term, err := s.Term(firstIndex - 1)
if err != nil {
t.Fatal(err)
}
if term != hs.Term {
t.Errorf("expected Term(firstIndex) to be hs.Term (%d), got %d", hs.Term, term)
}
}