-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
239 lines (206 loc) · 5.52 KB
/
db_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
package anystore
import (
"context"
"errors"
"fmt"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anyproto/any-store/anyenc"
"github.com/anyproto/any-store/internal/driver"
)
func init() {
go http.ListenAndServe(":6066", nil)
}
var ctx = context.Background()
func TestDb_CreateCollection(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
assert.NotNil(t, coll)
cNames, err := fx.GetCollectionNames(ctx)
require.NoError(t, err)
assert.Equal(t, []string{"test"}, cNames)
_, err = fx.CreateCollection(ctx, "test")
assert.ErrorIs(t, err, ErrCollectionExists)
require.NoError(t, coll.Close())
_, err = fx.CreateCollection(ctx, "test")
assert.ErrorIs(t, err, ErrCollectionExists)
}
func TestDb_OpenCollection(t *testing.T) {
t.Run("err not found", func(t *testing.T) {
fx := newFixture(t)
coll, err := fx.OpenCollection(ctx, "test")
require.Nil(t, coll)
require.ErrorIs(t, err, ErrCollectionNotFound)
})
t.Run("success", func(t *testing.T) {
fx := newFixture(t)
_, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
coll, err := fx.OpenCollection(ctx, "test")
require.NoError(t, err)
assert.NotNil(t, coll)
})
t.Run("with indexes", func(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
indexInfo := IndexInfo{Fields: []string{"a", "-b"}, Sparse: true, Unique: true}
require.NoError(t, coll.EnsureIndex(ctx, indexInfo))
require.NoError(t, coll.Close())
coll, err = fx.OpenCollection(ctx, "test")
require.NoError(t, err)
assert.NotNil(t, coll)
indexes := coll.GetIndexes()
assert.Len(t, indexes, 1)
})
}
func TestDb_GetCollectionNames(t *testing.T) {
fx := newFixture(t)
var collNames = []string{"c1", "c2", "c3"}
for _, collName := range collNames {
coll, err := fx.CreateCollection(ctx, collName)
require.NoError(t, err)
require.NoError(t, coll.Close())
}
names, err := fx.GetCollectionNames(ctx)
require.NoError(t, err)
assert.Equal(t, collNames, names)
}
func TestDb_Stats(t *testing.T) {
fx := newFixture(t)
stats, err := fx.Stats(ctx)
require.NoError(t, err)
assert.Empty(t, 0, stats.IndexesCount)
assert.Empty(t, 0, stats.CollectionsCount)
assert.NotEmpty(t, stats.TotalSizeBytes)
assert.NotEmpty(t, stats.DataSizeBytes)
}
func TestDb_QuickCheck(t *testing.T) {
fx := newFixture(t)
assert.NoError(t, fx.QuickCheck(ctx))
}
func TestDb_Checkpoint(t *testing.T) {
fx := newFixture(t)
assert.NoError(t, fx.Checkpoint(ctx, false))
assert.NoError(t, fx.Checkpoint(ctx, true))
}
func TestDb_Backup(t *testing.T) {
fx := newFixture(t)
coll, err := fx.Collection(ctx, "coll")
require.NoError(t, err)
require.NoError(t, coll.EnsureIndex(ctx, IndexInfo{Fields: []string{"doc"}}))
require.NoError(t, coll.Insert(ctx, anyenc.MustParseJson(`{"id":1, "doc":"a"}`), anyenc.MustParseJson(`{"id":2, "doc":"b"}`)))
tmpDir, err := os.MkdirTemp("", "any-store-backup-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
require.NoError(t, fx.Backup(ctx, filepath.Join(tmpDir, "any-store-test.db")))
fx2 := newFixturePath(t, tmpDir)
coll2, err := fx2.Collection(ctx, "coll")
require.NoError(t, err)
assert.Len(t, coll2.GetIndexes(), 1)
assertCollCount(t, coll2, 2)
}
func TestDb_Close(t *testing.T) {
t.Run("race", func(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
var docs []*anyenc.Value
for i := range 1000 {
docs = append(docs, anyenc.MustParseJson(fmt.Sprintf(`{"id": %d, "value": %d}`, i, rand.Int())))
}
require.NoError(t, coll.Insert(ctx, docs...))
var results = make(chan error, 2)
go func() {
// writing
for {
if pErr := coll.UpsertOne(ctx, anyenc.MustParseJson(fmt.Sprintf(`{"id": %d, "value": %d}`, rand.Int(), rand.Int()))); pErr != nil {
results <- pErr
return
}
}
}()
go func() {
// iterating
for {
iter, pErr := coll.Find(nil).Iter(ctx)
if pErr != nil {
results <- pErr
return
}
for iter.Next() {
if _, pErr = iter.Doc(); pErr != nil {
results <- pErr
return
}
}
if pErr = iter.Close(); pErr != nil {
results <- pErr
return
}
}
}()
time.Sleep(time.Second / 2)
require.NoError(t, fx.Close())
for range len(results) {
rErr := <-results
if !assert.ErrorIs(t, rErr, driver.ErrDBIsClosed) {
t.Logf("%#v", rErr == nil)
}
}
dirEntries, err := os.ReadDir(fx.tmpDir)
require.NoError(t, err)
for _, dirEntry := range dirEntries {
if strings.HasSuffix(dirEntry.Name(), "-wal") {
t.Errorf("wal file is not removed after close")
}
}
})
}
func newFixture(t testing.TB, c ...*Config) *fixture {
tmpDir, err := os.MkdirTemp("", "any-store-*")
require.NoError(t, err)
return newFixturePath(t, tmpDir, c...)
}
func newFixturePath(t testing.TB, tmpDir string, c ...*Config) *fixture {
var conf *Config
if len(c) != 0 {
conf = c[0]
}
db, err := Open(ctx, filepath.Join(tmpDir, "any-store-test.db"), conf)
require.NoError(t, err)
fx := &fixture{
DB: db,
tmpDir: tmpDir,
t: t,
}
t.Cleanup(fx.finish)
return fx
}
type fixture struct {
DB
tmpDir string
t testing.TB
}
func (fx *fixture) finish() {
closeErr := fx.Close()
if !errors.Is(closeErr, ErrDBIsClosed) {
require.NoError(fx.t, closeErr)
}
if fx.tmpDir != "" {
if true {
_ = os.RemoveAll(fx.tmpDir)
} else {
fx.t.Log(fx.tmpDir)
}
}
}