-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage_test.go
73 lines (56 loc) · 1.39 KB
/
storage_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
package raft_test
import (
"testing"
"github.com/relab/raft"
"github.com/relab/raft/commonpb"
)
func TestMemory(t *testing.T) {
var firstIndex, nextIndex uint64 = 1, 2
entry := &commonpb.Entry{Index: firstIndex}
m := raft.NewMemory(map[uint64]uint64{
raft.KeyFirstIndex: firstIndex,
raft.KeyNextIndex: nextIndex,
}, map[uint64]*commonpb.Entry{1: entry})
// Get key-value.
first, err := m.Get(raft.KeyFirstIndex)
if err != nil {
t.Errorf("got %+v, want no error", err)
}
if first != firstIndex {
t.Errorf("got %d, want %d", first, firstIndex)
}
// Get entry that exists.
e, err := m.GetEntry(firstIndex)
if err != nil {
t.Errorf("got %+v, want no error", err)
}
if e != entry {
t.Errorf("got %+v, want %+v", e, entry)
}
// Remove entry.
err = m.RemoveEntries(firstIndex, firstIndex)
if err != nil {
t.Errorf("got %+v, want no error", err)
}
// Get entry that doesn't exist.
_, err = m.GetEntry(firstIndex)
if err == nil {
t.Errorf("got nil, want error %v", err)
}
// Get first/next indexes.
first, err = m.FirstIndex()
if err != nil {
t.Errorf("got %+v, want no error", err)
}
next, err := m.NextIndex()
if err != nil {
t.Errorf("got %+v, want no error", err)
}
if first != firstIndex {
t.Errorf("got %d, want %d", first, firstIndex)
}
// Because we remove one entry.
if next != firstIndex {
t.Errorf("got %d, want %d", next, firstIndex)
}
}