forked from elastic/go-txfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_test.go
324 lines (267 loc) · 7.8 KB
/
write_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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package txfile
import (
"errors"
"testing"
"time"
"github.com/elastic/go-txfile/internal/vfs"
)
type (
testIOOperations []testIOOp
testIOOp interface {
Type() opType
At() int64
Contents() []byte
}
testWriteAtOP struct {
at int64
contents []byte
}
testSyncOP struct{}
opType uint8
)
type testWriterTarget struct {
writeAt func([]byte, int64) (int, error)
sync func() error
}
const (
opUndefined opType = iota
opWriteAt
opSync
)
func TestFileWriter(t *testing.T) {
assert := newAssertions(t)
checkCause := func(assert *assertions, expected, err error) {
if reason, ok := err.(reason); ok {
assert.Equal(expected, reason.Cause())
} else {
assert.Fail("expected failure reason")
}
}
assert.Run("start stop", func(assert *assertions) {
var ops testIOOperations
_, teardown := newTestWriter(recordWriteOps(&ops), 64)
time.Sleep(10 * time.Millisecond)
if assert.NoError(teardown()) {
assert.Len(ops, 0)
}
})
assert.Run("write and sync", func(assert *assertions) {
var ops testIOOperations
w, teardown := newTestWriter(recordWriteOps(&ops), 10)
defer mustSucceed(assert, teardown)
var tmp [10]byte
sync := newTxWriteSync()
w.Schedule(sync, 0, tmp[:])
w.Sync(sync, 0)
if !assert.NoError(waitFn(1*time.Second, sync.Wait)) {
assert.FailNow("invalid writer state")
}
if assert.Len(ops, 2) {
assert.Equal(opWriteAt, ops[0].Type())
assert.Equal(int64(0), ops[0].At())
assert.Len(ops[0].Contents(), 10)
assert.Equal(syncOp, ops[1])
}
})
assert.Run("write after sync", func(assert *assertions) {
var ops testIOOperations
b, target := blockingTarget(recordWriteOps(&ops))
b.Block()
w, teardown := newTestWriter(target, 10)
defer mustSucceed(assert, teardown)
var tmp [10]byte
sync := newTxWriteSync()
w.Schedule(sync, 0, tmp[:])
w.Schedule(sync, 1, tmp[:])
w.Sync(sync, 0)
w.Schedule(sync, 2, tmp[:])
w.Schedule(sync, 3, tmp[:])
w.Schedule(sync, 4, tmp[:])
w.Sync(sync, 0)
w.Schedule(sync, 5, tmp[:])
w.Sync(sync, 0)
// unblock writer, so scheduled write commands will be executed
b.Unblock()
if !assert.NoError(waitFn(1*time.Second, sync.Wait)) {
assert.FailNow("invalid writer state")
}
expectedOps := []opType{opWriteAt, opWriteAt, opSync, opWriteAt, opWriteAt, opWriteAt, opSync, opWriteAt, opSync}
expectedOffsets := []int64{0, 10, -1, 20, 30, 40, -1, 50, -1}
offsets := make([]int64, len(ops))
actual := make([]opType, len(ops))
for i, op := range ops {
t := op.Type()
off := int64(-1)
if t == opWriteAt {
off = op.At()
}
actual[i] = t
offsets[i] = off
}
if assert.Equal(len(expectedOps), len(actual)) {
assert.Equal(expectedOps, actual)
assert.Equal(expectedOffsets, offsets)
}
})
assert.Run("ordered writes", func(assert *assertions) {
var ops testIOOperations
b, target := blockingTarget(recordWriteOps(&ops))
b.Block()
w, teardown := newTestWriter(target, 10)
defer mustSucceed(assert, teardown)
var tmp [10]byte
sync := newTxWriteSync()
w.Sync(sync, 0) // start with sync, to guarantee writer is really blocked
w.Schedule(sync, 3, tmp[:])
w.Schedule(sync, 2, tmp[:])
w.Schedule(sync, 1, tmp[:])
w.Sync(sync, 0)
// unblock writer, so scheduled write commands will be executed
b.Unblock()
if !assert.NoError(waitFn(1*time.Second, sync.Wait)) {
assert.FailNow("invalid writer state")
}
expected := []int64{10, 20, 30}
var actual []int64
for _, op := range ops {
if op.Type() == opWriteAt {
actual = append(actual, op.At())
assert.Equal(10, len(op.Contents()))
}
}
assert.Equal(expected, actual)
})
assert.Run("fail on sync", func(assert *assertions) {
expectedErr := errors.New("ups")
var ops testIOOperations
target := recordWriteOps(&ops)
recordSync := target.sync
target.sync = func() error {
recordSync()
return expectedErr
}
w, teardown := newTestWriter(target, 10)
defer mustSucceed(assert, teardown)
var tmp [10]byte
sync := newTxWriteSync()
w.Schedule(sync, 1, tmp[:])
w.Sync(sync, 0)
w.Schedule(sync, 2, tmp[:])
w.Sync(sync, 0)
err := waitFn(1*time.Second, sync.Wait)
checkCause(assert, expectedErr, err)
// writer should stop on first error and ignore all following commands
expectedOps := []opType{opWriteAt, opSync}
actual := make([]opType, len(ops))
for i, op := range ops {
actual[i] = op.Type()
}
assert.Equal(expectedOps, actual)
})
assert.Run("fail on write", func(assert *assertions) {
expectedErr := errors.New("ups")
var ops testIOOperations
target := recordWriteOps(&ops)
recordWrites := target.writeAt
target.writeAt = func(p []byte, off int64) (int, error) {
recordWrites(p, off)
return 0, expectedErr
}
w, teardown := newTestWriter(target, 10)
defer mustSucceed(assert, teardown)
var tmp [10]byte
sync := newTxWriteSync()
w.Schedule(sync, 1, tmp[:])
w.Schedule(sync, 2, tmp[:])
w.Sync(sync, 0)
w.Schedule(sync, 3, tmp[:])
w.Schedule(sync, 4, tmp[:])
w.Sync(sync, 0)
err := waitFn(5*time.Second, sync.Wait)
assert.Error(err)
checkCause(assert, expectedErr, err)
// writer should stop on first error and ignore all following commands
expectedOps := []opType{opWriteAt}
actual := make([]opType, len(ops))
for i, op := range ops {
actual[i] = op.Type()
}
assert.Equal(expectedOps, actual)
})
}
func newTestWriter(to writable, pagesize uint) (*writer, func() error) {
w := &writer{}
w.Init(to, pagesize, SyncDefault)
cw := makeCloseWait(1 * time.Second)
cw.Add(1)
go func() {
defer cw.Done()
w.Run()
}()
return w, func() error {
w.Stop()
if !cw.Wait() {
return errors.New("test writer shutdown timeout")
}
return nil
}
}
func (w *testWriterTarget) Sync(_ vfs.SyncFlag) error { return w.sync() }
func (w *testWriterTarget) WriteAt(p []byte, off int64) (n int, err error) {
return w.writeAt(p, off)
}
var ignoreOps = &testWriterTarget{
writeAt: func(p []byte, _ int64) (int, error) { return len(p), nil },
sync: func() error { return nil },
}
func recordWriteOps(ops *testIOOperations) *testWriterTarget {
return &testWriterTarget{
writeAt: func(p []byte, off int64) (n int, err error) {
*ops = append(*ops, &testWriteAtOP{at: off, contents: p})
return len(p), nil
},
sync: func() error {
*ops = append(*ops, syncOp)
return nil
},
}
}
func blockingTarget(w *testWriterTarget) (*blocker, *testWriterTarget) {
b := newBlocker()
return b, &testWriterTarget{
writeAt: func(p []byte, off int64) (n int, err error) {
b.Wait()
return w.writeAt(p, off)
},
sync: func() error {
b.Wait()
return w.sync()
},
}
}
func (op *testWriteAtOP) Type() opType { return opWriteAt }
func (op *testWriteAtOP) At() int64 { return op.at }
func (op *testWriteAtOP) Contents() []byte { return op.contents }
var syncOp = (*testSyncOP)(nil)
func (op *testSyncOP) Type() opType { return opSync }
func (op *testSyncOP) At() int64 { panic("sync op") }
func (op *testSyncOP) Contents() []byte { panic("sync op") }
func mustSucceed(assert *assertions, fn func() error) {
assert.NoError(fn())
}