-
Notifications
You must be signed in to change notification settings - Fork 2
/
persistence_test.go
298 lines (221 loc) · 6.55 KB
/
persistence_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package circular_test
import (
"bytes"
"io"
"io/fs"
"os"
"path/filepath"
"slices"
"strconv"
"testing"
"time"
"github.com/siderolabs/gen/xtesting/must"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"go.uber.org/zap/zaptest"
"github.com/siderolabs/go-circular"
"github.com/siderolabs/go-circular/zstd"
)
func TestLoad(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
options []circular.OptionFunc
chunkSizes []int
numCompressedChunks int
}{
{
name: "no chunks",
numCompressedChunks: 5,
},
{
name: "expected number of chunks",
numCompressedChunks: 5,
chunkSizes: []int{1024, 2048, 1024, 1024, 3072, 4096},
},
{
name: "less chunks than expected",
numCompressedChunks: 5,
chunkSizes: []int{10240, 20480},
},
{
name: "more chunks than expected",
numCompressedChunks: 4,
chunkSizes: []int{10240, 2048, 1024, 1024, 3072, 4096},
},
{
name: "single chunk",
numCompressedChunks: 2,
chunkSizes: []int{10240},
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
logger := zaptest.NewLogger(t)
compressor := must.Value(zstd.NewCompressor())(t)
for i, size := range test.chunkSizes {
chunkData := bytes.Repeat([]byte{byte(i)}, size)
chunkData = must.Value(compressor.Compress(chunkData, nil))(t)
chunkPath := filepath.Join(dir, "chunk."+strconv.Itoa(i))
require.NoError(t, os.WriteFile(chunkPath, chunkData, 0o644))
}
buf, err := circular.NewBuffer(append(test.options,
circular.WithNumCompressedChunks(test.numCompressedChunks, compressor),
circular.WithPersistence(circular.PersistenceOptions{
ChunkPath: filepath.Join(dir, "chunk"),
}),
circular.WithLogger(logger),
)...)
require.NoError(t, err)
actualData, err := io.ReadAll(buf.GetReader())
require.NoError(t, err)
var expectedData []byte
firstIdx := 1
if len(test.chunkSizes) > test.numCompressedChunks+1 {
firstIdx = len(test.chunkSizes) - test.numCompressedChunks
}
for i := firstIdx; i < firstIdx-1+min(len(test.chunkSizes), test.numCompressedChunks+1); i++ {
expectedData = append(expectedData, bytes.Repeat([]byte{byte(i)}, test.chunkSizes[i])...)
}
if len(test.chunkSizes) > 0 {
expectedData = append(expectedData, bytes.Repeat([]byte{0}, test.chunkSizes[0])...)
}
if expectedData == nil {
expectedData = []byte{}
}
require.Equal(t, expectedData, actualData)
require.NoError(t, buf.Close())
})
}
}
func TestPersist(t *testing.T) {
t.Parallel()
for _, test := range []struct { //nolint:govet
name string
numCompressedChunks int
options []circular.OptionFunc
sizes []int64
}{
{
name: "empty",
numCompressedChunks: 5,
sizes: []int64{0, 0},
},
{
name: "some data",
numCompressedChunks: 5,
sizes: []int64{2048, 2048 * 2048, 131072},
},
{
name: "writes close to max capacity",
numCompressedChunks: 6,
options: []circular.OptionFunc{
circular.WithInitialCapacity(128),
circular.WithMaxCapacity(1024),
circular.WithSafetyGap(4),
},
sizes: []int64{1019, 1019},
},
{
name: "uneven writes",
numCompressedChunks: 6,
sizes: []int64{1024*1024 + 1, 1024*1024 - 1, 1024*1024 - 1, 1024*1024 - 1, 1024*1024 - 1, 1024*1024 - 1},
},
{
name: "dropping old chunks",
numCompressedChunks: 3,
options: []circular.OptionFunc{
circular.WithInitialCapacity(128),
circular.WithMaxCapacity(1024),
circular.WithSafetyGap(1),
},
sizes: []int64{2048, 2048 + 256, 1024, 1024},
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
logger := zaptest.NewLogger(t)
compressor := must.Value(zstd.NewCompressor())(t)
expectedContents := []byte{}
for _, size := range append(test.sizes, 0) { // appending zero to run one more iteration verifying previous write
data := make([]byte, size)
for i := range data {
data[i] = byte(i % 256)
}
buf, err := circular.NewBuffer(append(test.options,
circular.WithNumCompressedChunks(test.numCompressedChunks, compressor),
circular.WithPersistence(circular.PersistenceOptions{
ChunkPath: filepath.Join(dir, "chunk"),
}),
circular.WithLogger(logger),
)...)
require.NoError(t, err)
bufferContentsAfterLoad, err := io.ReadAll(buf.GetReader())
require.NoError(t, err)
require.Equal(t, expectedContents, bufferContentsAfterLoad)
_, err = buf.Write(data)
require.NoError(t, err)
expectedContents = append(expectedContents, data...)
for len(expectedContents) > buf.MaxCapacity() {
expectedContents = slices.Delete(expectedContents, 0, buf.Capacity())
}
require.NoError(t, buf.Close())
}
})
}
}
func TestFlush(t *testing.T) {
t.Parallel()
dir := t.TempDir()
logger := zaptest.NewLogger(t)
compressor := must.Value(zstd.NewCompressor())(t)
buf, err := circular.NewBuffer(
circular.WithNumCompressedChunks(5, compressor),
circular.WithPersistence(circular.PersistenceOptions{
ChunkPath: filepath.Join(dir, "chunk"),
FlushInterval: 10 * time.Millisecond,
}),
circular.WithLogger(logger),
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, buf.Close())
})
_, err = buf.Write([]byte("hello"))
require.NoError(t, err)
require.EventuallyWithT(t, func(t *assert.CollectT) {
assert.FileExists(t, filepath.Join(dir, "chunk.0"))
}, time.Second, 10*time.Millisecond)
st1, err := os.Stat(filepath.Join(dir, "chunk.0"))
require.NoError(t, err)
_, err = buf.Write([]byte("world"))
require.NoError(t, err)
require.EventuallyWithT(t, func(t *assert.CollectT) {
var st2 fs.FileInfo
st2, err = os.Stat(filepath.Join(dir, "chunk.0"))
require.NoError(t, err)
assert.Greater(t, st2.Size(), st1.Size())
}, time.Second, 10*time.Millisecond)
// without closing buf, re-open it and check if data is still there
buf2, err := circular.NewBuffer(
circular.WithNumCompressedChunks(5, compressor),
circular.WithPersistence(circular.PersistenceOptions{
ChunkPath: filepath.Join(dir, "chunk"),
}),
circular.WithLogger(logger),
)
require.NoError(t, err)
actualData, err := io.ReadAll(buf2.GetReader())
require.NoError(t, err)
require.Equal(t, []byte("helloworld"), actualData)
require.NoError(t, buf2.Close())
}
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}