forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrency_test.go
108 lines (88 loc) · 1.66 KB
/
concurrency_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
package concurrency
import (
"flag"
"os"
"runtime"
"strings"
"sync"
"testing"
"github.com/open-policy-agent/opa/server/types"
"github.com/open-policy-agent/opa/test/e2e"
)
var testRuntime *e2e.TestRuntime
func TestMain(m *testing.M) {
flag.Parse()
testServerParams := e2e.NewAPIServerTestParams()
var err error
testRuntime, err = e2e.NewTestRuntimeWithOpts(e2e.TestRuntimeOpts{}, testServerParams)
if err != nil {
os.Exit(1)
}
os.Exit(testRuntime.RunTests(m))
}
func TestConcurrencyGetV1Data(t *testing.T) {
policy := `
package test
p = true
`
err := testRuntime.UploadPolicy(t.Name(), strings.NewReader(policy))
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
num := runtime.NumCPU()
wg.Add(num)
for i := 0; i < num; i++ {
go func() {
defer wg.Done()
for n := 0; n < 1000; n++ {
dr := struct {
Result bool `json:"result"`
}{}
if err := testRuntime.GetDataWithInputTyped("test/p", nil, &dr); err != nil {
t.Error(err)
return
}
if !dr.Result {
t.Errorf("Unexpected response: %+v", dr)
return
}
}
}()
}
wg.Wait()
}
func TestConcurrencyCompile(t *testing.T) {
policy := `
package test
f(_)
p {
not q
}
q {
not f(input.foo)
}
`
err := testRuntime.UploadPolicy(t.Name(), strings.NewReader(policy))
if err != nil {
t.Fatal(err)
}
req := types.CompileRequestV1{
Query: "data.test.p",
}
var wg sync.WaitGroup
num := runtime.NumCPU()
wg.Add(num)
for i := 0; i < num; i++ {
go func() {
defer wg.Done()
for n := 0; n < 1000; n++ {
if _, err := testRuntime.CompileRequest(req); err != nil {
t.Error(err)
return
}
}
}()
}
wg.Wait()
}