forked from elastic/go-txfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing_test.go
160 lines (136 loc) · 3.39 KB
/
testing_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
// 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"
"math/rand"
"sync"
"testing"
"time"
"github.com/urso/qcgen"
"github.com/elastic/go-txfile/internal/mint"
)
// assertions wraps the testing, assert, and quick packages in order to provide
// some unified functionality.
// The Run method pushes a new tracer to the tracer stack, such that trace prints
// will be captured by the current testing context.
type assertions = mint.T
type blocker struct {
blocked bool
mux sync.Mutex
cond *sync.Cond
}
type closeWaiter struct {
wg sync.WaitGroup
duration time.Duration
}
type testPageStore struct {
pageSize uint
pages map[PageID][]byte
}
func newAssertions(t *testing.T) *assertions {
a := mint.NewWith(t, func(sub *mint.T) func() {
pushTracer(mint.NewTestLogTracer(sub, logTracer))
return popTracer
})
a.SetDefaultGenerators(qcMakePageID)
return a
}
func newBlocker() *blocker {
b := &blocker{}
b.cond = sync.NewCond(&b.mux)
return b
}
func (b *blocker) Block() {
b.mux.Lock()
b.blocked = true
b.mux.Unlock()
}
func (b *blocker) Unblock() {
b.mux.Lock()
b.blocked = false
b.mux.Unlock()
b.cond.Signal()
}
func (b *blocker) Wait() {
b.mux.Lock()
defer b.mux.Unlock()
for b.blocked {
b.cond.Wait()
}
}
func makeQuickCheck(fns ...interface{}) func(*assertions) {
return func(assert *assertions) {
assert.QuickCheck(fns...)
}
}
func makeCloseWait(timeout time.Duration) closeWaiter {
return closeWaiter{duration: timeout}
}
func (w *closeWaiter) Add(n int) { w.wg.Add(n) }
func (w *closeWaiter) Done() { w.wg.Done() }
func (w *closeWaiter) Wait() bool {
err := waitFn(w.duration, func() reason {
w.wg.Wait()
return nil
})
return err == nil
}
func newTestPageStore(pageSize uint) *testPageStore {
return &testPageStore{
pages: map[PageID][]byte{},
pageSize: pageSize,
}
}
func (p *testPageStore) Get(id PageID) []byte {
if id < 2 {
panic("must not access file meta region")
}
b := p.pages[id]
if b == nil {
b := make([]byte, p.pageSize)
p.pages[id] = b
}
return b
}
func (p *testPageStore) Set(id PageID, b []byte) reason {
if id < 2 {
panic("must not overwrite file meta region")
}
p.pages[id] = b
return nil
}
func waitFn(timeout time.Duration, fn func() reason) error {
ch := make(chan error)
go func() {
ch <- fn()
}()
select {
case err := <-ch:
return err
case <-time.After(timeout):
return errors.New("wait timed out")
}
}
// quick check generators
func qcMakePageID(rng *rand.Rand) PageID {
max := uint64(1 << (64 - entryBits))
if qcgen.GenBool(rng) {
max = entryOverflow - 1 // generate 'small' id only
}
return PageID(qcgen.GenUint64Range(rng, 2, max))
}