-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveldb_test.go
88 lines (71 loc) · 1.92 KB
/
leveldb_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
package raft
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func setupLevelDBLogStorage(t *testing.T) (*LevelDBLogStorage, func()) {
t.Helper()
// 一時的なディレクトリを作成
dir, err := os.MkdirTemp("", "leveldbtest")
assert.NoError(t, err)
ls, err := NewLevelDBLogStorage(dir, 1)
assert.NoError(t, err)
return ls, func() {
ls.Close()
os.RemoveAll(dir)
}
}
func TestAppend(t *testing.T) {
ls, cleanup := setupLevelDBLogStorage(t)
defer cleanup()
ctx := context.Background()
entry := Entry{Command: "cmd1", Index: 1, Term: 1}
index := ls.Append(ctx, entry)
assert.Equal(t, entry.Index, index)
// 追加されたエントリを検証
foundEntry := ls.Find(ctx, index)
assert.Equal(t, entry, foundEntry)
}
func TestExtend(t *testing.T) {
ls, cleanup := setupLevelDBLogStorage(t)
defer cleanup()
ctx := context.Background()
entries := []Entry{
{Command: "cmd1", Index: 1, Term: 1},
{Command: "cmd2", Index: 2, Term: 1},
}
lastIndex := ls.Extend(ctx, entries, 1)
assert.Equal(t, entries[1].Index, lastIndex)
// 拡張されたエントリを検証
for _, entry := range entries {
foundEntry := ls.Find(ctx, entry.Index)
assert.Equal(t, entry, foundEntry)
}
}
func TestFind(t *testing.T) {
ls, cleanup := setupLevelDBLogStorage(t)
defer cleanup()
ctx := context.Background()
entry := Entry{Command: "cmd1", Index: 1, Term: 1}
ls.Append(ctx, entry)
foundEntry := ls.Find(ctx, entry.Index)
assert.Equal(t, entry, foundEntry)
}
func TestSlice(t *testing.T) {
ls, cleanup := setupLevelDBLogStorage(t)
defer cleanup()
ctx := context.Background()
entries := []Entry{
{Command: "cmd1", Index: 1, Term: 1},
{Command: "cmd2", Index: 2, Term: 1},
{Command: "cmd3", Index: 3, Term: 1},
}
for _, entry := range entries {
ls.Append(ctx, entry)
}
slicedEntries := ls.Slice(ctx, 2)
assert.Equal(t, 2, len(slicedEntries))
assert.Equal(t, entries[1:], slicedEntries)
}