forked from evanphx/wal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair_test.go
146 lines (115 loc) · 2.77 KB
/
pair_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package wal
import (
"bytes"
"crypto/rand"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektra/neko"
)
func _TestPair(t *testing.T) {
n := neko.Start(t)
dir, err := ioutil.TempDir("", "wal")
require.NoError(t, err)
defer os.RemoveAll(dir)
path := filepath.Join(dir, "wal")
n.Setup(func() {
os.RemoveAll(path)
})
n.It("accepts write options", func() {
wo := WriteOptions{
SegmentSize: 128,
MaxSegments: 2,
}
_, w, err := NewPair(path, wo)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(wo, w.opts) {
t.Fatalf("\nexpect:\n%v\n\nactual:\n%v", wo, w.opts)
}
})
n.It("exposes writes in the reader", func() {
r, w, err := NewPair(path, DefaultWriteOptions)
require.NoError(t, err)
err = w.Write([]byte("data1"))
require.NoError(t, err)
require.True(t, r.Next())
assert.Equal(t, []byte("data1"), r.Value())
})
n.It("blocks waiting for more data", func() {
r, w, err := NewPair(path, DefaultWriteOptions)
require.NoError(t, err)
go func() {
time.Sleep(1 * time.Second)
w.Write([]byte("data1"))
}()
require.NoError(t, r.BlockingNext())
assert.Equal(t, []byte("data1"), r.Value())
go func() {
time.Sleep(1 * time.Second)
w.Write([]byte("data2"))
}()
require.NoError(t, r.BlockingNext())
assert.Equal(t, []byte("data2"), r.Value())
})
n.It("only blocks when there is no more data", func() {
r, w, err := NewPair(path, DefaultWriteOptions)
require.NoError(t, err)
err = w.Write([]byte("data1"))
require.NoError(t, err)
require.NoError(t, r.BlockingNext())
assert.Equal(t, []byte("data1"), r.Value())
})
n.It("linearizes reads and writes", func() {
r, w, err := NewPair(path, DefaultWriteOptions)
require.NoError(t, err)
// Create a ton of input messages to write
in := make([][]byte, 10240)
for i := 0; i < len(in); i++ {
in[i] = make([]byte, 128)
_, err := rand.Read(in[i])
if err != nil {
t.Fatal(err)
}
}
var wg sync.WaitGroup
wg.Add(2)
// Write all of the messages in the background
go func() {
defer wg.Done()
for _, p := range in {
if err := w.Write(p); err != nil {
t.Fatal(err)
}
}
}()
// In a separate goroutine, try reading a bunch of messages.
// Since we can't really tell how fast the writer is going,
// we'll just make sure that the reads we do get line up
// correctly with what we expect.
go func() {
defer wg.Done()
for i := 0; i < len(in); i++ {
if !r.Next() {
if err := r.Error(); err != nil {
t.Fatal(err)
}
i--
continue
}
if !bytes.Equal(r.Value(), in[i]) {
t.Fatal("mismach found, probably a race")
}
}
}()
wg.Wait()
})
n.Meow()
}