This repository has been archived by the owner on Sep 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathaof_test.go
109 lines (103 loc) · 2.2 KB
/
aof_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package resp
import (
"fmt"
"os"
"testing"
"time"
)
func TestAOF(t *testing.T) {
os.RemoveAll("aof.tmp")
if err := os.MkdirAll("aof.tmp", 0700); err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll("aof.tmp")
}()
if _, err := OpenAOF("aof.tmp"); err == nil {
t.Fatal("expecting error, got nil")
}
f, err := OpenAOF("aof.tmp/aof")
if err != nil {
t.Fatal(err)
}
defer func() {
f.Close()
if err := f.Close(); err == nil || err.Error() != "closed" {
t.Fatalf("expected 'closed', got '%v'", err)
}
}()
// Test Setting Sync Policies
f.SetSyncPolicy(Never)
sps := fmt.Sprintf("%s %s %s %s", SyncPolicy(99), Never, Always, EverySecond)
if sps != "unknown never always every second" {
t.Fatalf("expected '%s', got '%s'", "unknown never always every second", sps)
}
f.SetSyncPolicy(99)
f.SetSyncPolicy(Never)
f.SetSyncPolicy(Always)
f.SetSyncPolicy(EverySecond)
f.SetSyncPolicy(EverySecond)
for i := 0; i < 12345; i++ {
if err := f.Append(StringValue(fmt.Sprintf("hello world #%d\n", i))); err != nil {
t.Fatal(err)
}
}
i := 0
if err := f.Scan(func(v Value) {
s := v.String()
e := fmt.Sprintf("hello world #%d\n", i)
if s != e {
t.Fatalf("#%d is '%s', expect '%s'", i, s, e)
}
i++
}); err != nil {
t.Fatal(err)
}
f.Close()
f.Close() // Test closing twice
f, err = OpenAOF("aof.tmp/aof")
if err != nil {
t.Fatal(err)
}
c := i
for i := c; i < c+12345; i++ {
if err := f.Append(StringValue(fmt.Sprintf("hello world #%d\n", i))); err != nil {
t.Fatal(err)
}
}
i = 0
if err := f.Scan(func(v Value) {
s := v.String()
e := fmt.Sprintf("hello world #%d\n", i)
if s != e {
t.Fatalf("#%d is '%s', expect '%s'", i, s, e)
}
i++
}); err != nil {
t.Fatal(err)
}
var multi []Value
for i := 0; i < 50; i++ {
multi = append(multi, StringValue(fmt.Sprintf("hello multi world #%d\n", i)))
}
if err := f.AppendMulti(multi); err != nil {
t.Fatal(err)
}
skip := i
i = 0
j := 0
if err := f.Scan(func(v Value) {
if i >= skip {
s := v.String()
e := fmt.Sprintf("hello multi world #%d\n", j)
if s != e {
t.Fatalf("#%d is '%s', expect '%s'", j, s, e)
}
j++
}
i++
}); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 10)
}