forked from cayleygraph/cayley
/
nosqltest_all.go
161 lines (147 loc) · 3.66 KB
/
nosqltest_all.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
package nosqltest
import (
"bytes"
"context"
"math/rand"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/graphtest"
"github.com/cayleygraph/cayley/graph/graphtest/testutil"
"github.com/cayleygraph/cayley/graph/nosql"
"github.com/cayleygraph/cayley/quad"
)
type DatabaseFunc func(t testing.TB) (nosql.Database, *nosql.Options, graph.Options, func())
type Config struct {
FloatToInt bool // database silently converts all float values to ints, if possible
IntToFloat bool // database always converts all int values to floats
TimeInMs bool
Recreate bool // tests should re-create database instance from scratch on each run
PageSize int // result page size for pagination (large iterator) tests
}
func (c Config) quadStore() *graphtest.Config {
return &graphtest.Config{
NoPrimitives: true,
TimeInMs: c.TimeInMs,
OptimizesComparison: true,
SkipDeletedFromIterator: true,
SkipSizeCheckAfterDelete: true,
}
}
func NewQuadStore(t testing.TB, gen DatabaseFunc) (graph.QuadStore, graph.Options, func()) {
db, nopt, opt, closer := gen(t)
err := nosql.Init(db, opt)
if err != nil {
db.Close()
closer()
require.Fail(t, "init failed", "%v", err)
}
kdb, err := nosql.NewQuadStore(db, nopt, opt)
if err != nil {
db.Close()
closer()
require.Fail(t, "create failed", "%v", err)
}
return kdb, opt, func() {
kdb.Close()
closer()
}
}
const parallel = false
func TestAll(t *testing.T, gen DatabaseFunc, conf *Config) {
if conf == nil {
conf = &Config{}
}
t.Run("nosql", func(t *testing.T) {
TestNoSQL(t, gen, conf)
})
t.Run("qs", func(t *testing.T) {
if parallel {
t.Parallel()
}
graphtest.TestAll(t, func(t testing.TB) (graph.QuadStore, graph.Options, func()) {
return NewQuadStore(t, gen)
}, conf.quadStore())
})
t.Run("concurrent", func(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
t.SkipNow()
if parallel {
t.Parallel()
}
testConcurrent(t, gen)
})
}
func BenchmarkAll(t *testing.B, gen DatabaseFunc, conf *Config) {
t.Run("qs", func(t *testing.B) {
graphtest.BenchmarkAll(t, func(t testing.TB) (graph.QuadStore, graph.Options, func()) {
return NewQuadStore(t, gen)
}, conf.quadStore())
})
}
func randString() string {
const n = 60
b := bytes.NewBuffer(nil)
b.Grow(n)
for i := 0; i < n; i++ {
b.WriteByte(byte('a' + rand.Intn(26)))
}
return b.String()
}
func testConcurrent(t testing.TB, gen DatabaseFunc) {
ctx := context.TODO()
qs, opts, closer := NewQuadStore(t, gen)
defer closer()
if opts == nil {
opts = make(graph.Options)
}
opts["ignore_duplicate"] = true
qw := testutil.MakeWriter(t, qs, opts)
const n = 1000
subjects := make([]string, 0, n/4)
for i := 0; i < cap(subjects); i++ {
subjects = append(subjects, randString())
}
var wg sync.WaitGroup
wg.Add(2)
done := make(chan struct{})
go func() {
defer wg.Done()
defer close(done)
for i := 0; i < n; i++ {
n1 := subjects[rand.Intn(len(subjects))]
n2 := subjects[rand.Intn(len(subjects))]
t := graph.NewTransaction()
t.AddQuad(quad.Make(n1, "link", n2, nil))
t.AddQuad(quad.Make(n2, "link", n1, nil))
if err := qw.ApplyTransaction(t); err != nil {
panic(err)
}
}
}()
go func() {
defer wg.Done()
for {
select {
case <-done:
return
default:
}
n1 := subjects[rand.Intn(len(subjects))]
it := qs.QuadIterator(quad.Subject, qs.ValueOf(quad.String(n1)))
for it.Next(ctx) {
q := qs.Quad(it.Result())
_ = q.Subject.Native()
_ = q.Predicate.Native()
_ = q.Object.Native()
}
if err := it.Close(); err != nil {
panic(err)
}
}
}()
wg.Wait()
}