-
Notifications
You must be signed in to change notification settings - Fork 1
/
seg_test.go
70 lines (64 loc) · 1.38 KB
/
seg_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
package mbbolt
import (
"context"
"path/filepath"
"strconv"
"sync"
"testing"
)
func TestSegDB(t *testing.T) {
t.Run("OpenCloseLeak", func(t *testing.T) {
d := t.TempDir()
seg := NewSegDB(context.Background(), d, ".db", nil, 32)
seg.Close()
seg = NewSegDB(context.Background(), d, ".db", nil, 32)
defer seg.Close()
})
t.Run("SegmentFn", func(t *testing.T) {
m := [10]int{}
for i := 0; i < 1000; i++ {
m[DefaultSegmentByKey(strconv.Itoa(i))%10]++
}
for i, v := range &m {
if v < 50 {
t.Errorf("segment %d has %d values", i, v)
}
}
})
}
func TestSegBackupRestore(t *testing.T) {
tmp := t.TempDir()
sdb := NewSegDB(context.Background(), tmp, ".db", nil, 32)
defer sdb.Close()
var wg sync.WaitGroup
for i := 0; i < 10000; i++ {
wg.Add(1)
i := i
go func() {
defer wg.Done()
sdb.Put("bucket", "key"+strconv.Itoa(i), i)
}()
}
wg.Wait()
zfp := filepath.Join(tmp, "segdb.zip")
if _, err := sdb.mdb.BackupToFile(zfp, nil); err != nil {
t.Fatal(err)
}
if err := sdb.Close(); err != nil {
t.Fatal(err)
}
sdb, err := NewSegDBFromFile(context.Background(), zfp, tmp, ".db", nil)
if err != nil {
t.Fatal(err)
}
if ln := len(sdb.dbs); ln != 32 {
t.Fatal("expected 100 dbs, got", ln)
}
for i := 0; i < 10000; i++ {
var v int
sdb.Get("bucket", "key"+strconv.Itoa(i), &v)
if v != i {
t.Fatalf("expected %d, got %d", i, v)
}
}
}