-
Notifications
You must be signed in to change notification settings - Fork 20
/
race_test.go
73 lines (64 loc) · 1.33 KB
/
race_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
package quamina
import (
"fmt"
"log"
"math/rand"
"sync"
"testing"
)
func TestConcurrencyCore(t *testing.T) {
testConcurrency(t, newCoreMatcher())
}
func testConcurrency(t *testing.T, m matcher) {
t.Helper()
var (
goroutines = 4
n = 500
tasks = 6
)
log.Printf("TestConcurrency %T goroutines: %d, tasks: %d",
m, goroutines, tasks)
populate := func() {
for i := 0; i < n; i++ {
p := fmt.Sprintf(`{"like":["tacos","queso"],"want":[%d]}`, i)
if err := m.addPattern(i, p); err != nil {
t.Fatal(err)
}
}
}
query := func(verify bool) {
f := newJSONFlattener()
for i := 0; i < n; i++ {
e := fmt.Sprintf(`{"like":"tacos","want":%d}`, i)
fs, err := f.Flatten([]byte(e), m.(*coreMatcher).getSegmentsTreeTracker())
if err != nil {
t.Fatal(err)
}
if got, err := m.matchesForFields(fs); err != nil {
t.Fatal(err)
} else if verify && len(got) != 1 {
t.Fatal(got)
}
}
}
wg := sync.WaitGroup{}
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func(i int) {
// We defer to get Done called after a t.Fatal().
defer wg.Done()
for j, k := range rand.Perm(tasks) {
switch k {
case 0, 1:
populate()
// case 1:
// depopulate()
default:
query(false)
}
log.Printf("task %d,%d (%d) complete", i, j, k)
}
}(i)
}
wg.Wait()
}