-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi_test.go
64 lines (56 loc) · 1.16 KB
/
multi_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
package mbbolt
import (
"context"
"strconv"
"sync"
"testing"
"go.oneofone.dev/otk"
)
func TestMultiRace(t *testing.T) {
opts := *DefaultOptions
opts.AutoRetry = true
mdb := NewMultiDB(t.TempDir(), ".db", &opts)
defer mdb.Close()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
mdb.MustGet(context.Background(), "test"+strconv.Itoa(i%3), nil)
}()
}
wg.Wait()
mdb.Close()
}
func TestMultiBackupRestore(t *testing.T) {
opts := *DefaultOptions
opts.AutoRetry = true
tmp := t.TempDir()
mdb := NewMultiDB(tmp, ".db", &opts)
defer mdb.Close()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
mdb.MustGet(context.Background(), "test"+strconv.Itoa(i%3), nil).Put("bucket", "key", i)
}()
}
wg.Wait()
var buf otk.Buffer // need ReadAt
if _, err := mdb.Backup(&buf, nil); err != nil {
t.Fatal(err)
}
mdb.Close()
mdb.m = make(map[string]*DB)
t.Logf("buf size: %d", buf.Len())
if err := mdb.Restore(context.Background(), &buf); err != nil {
t.Fatal(err)
}
if len(mdb.m) != 3 {
t.Fatal("expected 100 dbs, got", len(mdb.m))
}
mdb.Close()
}