-
Notifications
You must be signed in to change notification settings - Fork 83
/
disk_wal_test.go
74 lines (64 loc) · 1.72 KB
/
disk_wal_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
package tstorage
import (
"os"
"path/filepath"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_diskWAL_append_read(t *testing.T) {
var (
op = operationInsert
rows = []Row{
{Metric: "metric-1", DataPoint: DataPoint{Value: 0.1, Timestamp: 1600000000}},
{Metric: "metric-2", DataPoint: DataPoint{Value: 0.2, Timestamp: 1600000001}},
{Metric: "metric-1", DataPoint: DataPoint{Value: 0.1, Timestamp: 1600000001}},
{Metric: "metric-2", DataPoint: DataPoint{Value: 0.2, Timestamp: 1600000003}},
}
)
// Append rows into wal
tmpDir, err := os.MkdirTemp("", "tstorage-test")
defer os.RemoveAll(tmpDir)
require.NoError(t, err)
path := filepath.Join(tmpDir, "wal")
wal, err := newDiskWAL(path, 4096)
require.NoError(t, err)
// Append into two segments
err = wal.append(op, rows[:2])
require.NoError(t, err)
err = wal.punctuate()
require.NoError(t, err)
err = wal.append(op, rows[2:])
require.NoError(t, err)
err = wal.flush()
require.NoError(t, err)
// Recover rows.
reader, err := newDiskWALReader(path)
require.NoError(t, err)
err = reader.readAll()
require.NoError(t, err)
got := reader.rowsToInsert
assert.Equal(t, rows, got)
}
func Test_diskWAL_removeOldest(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "tstorage-test")
require.NoError(t, err)
for i := 0; i < 3; i++ {
err := os.Mkdir(filepath.Join(tmpDir, strconv.Itoa(i)), os.ModePerm)
require.NoError(t, err)
}
w := &diskWAL{
dir: tmpDir,
}
err = w.removeOldest()
require.NoError(t, err)
files, err := os.ReadDir(w.dir)
require.NoError(t, err)
want := []string{"1", "2"}
got := []string{}
for _, f := range files {
got = append(got, f.Name())
}
assert.Equal(t, want, got)
}