forked from CloudyKit/jet
/
exec_test.go
102 lines (85 loc) · 1.96 KB
/
exec_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
package jet
import (
"fmt"
"io/ioutil"
"sync"
"testing"
"time"
)
func TestExecuteConcurrency(t *testing.T) {
l := NewInMemLoader()
l.Set("foo", "{{if true}}Hi {{ .Name }}!{{end}}")
set := NewSet(l)
tpl, err := set.GetTemplate("foo")
if err != nil {
t.Errorf("getting template from set: %v", err)
}
for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("CC_%d", i), func(t *testing.T) {
t.Parallel()
err := tpl.Execute(ioutil.Discard, nil, struct{ Name string }{Name: "John"})
if err != nil {
t.Errorf("executing template: %v", err)
}
})
}
}
func TestSetExecute(t *testing.T) {
l := NewInMemLoader()
var template = "{{if true}}Hi {{ .Name }} {{ .I }}!{{end}}"
l.Set("foo", template)
set := NewSet(l)
tpl, err := set.GetTemplate("foo")
if err != nil {
t.Errorf("getting template from set: %v", err)
}
for i := 0; i < 100000000; i++ {
err := tpl.Execute(ioutil.Discard, nil, struct {
Name string
I int
}{Name: "John", I: i})
if err != nil {
t.Errorf("executing template: %v", err)
}
}
}
func TestExecute(t *testing.T) {
var template = "{{if true}}Hi {{ .Name }} {{ .I }}!{{end}}"
Janitor(10 * time.Millisecond)
for i := 0; i < 100000000; i++ {
_, err := Execute(template, VarMap{}, struct {
Name string
I int
}{Name: "John", I: i})
if err != nil {
t.Errorf("executing template: %v", err)
}
//fmt.Println(rendered)
}
}
func TestRandomExecute(t *testing.T) {
var template = "{{if true}}Hi {{ .Name }} {{ .I }}!{{end}} {{global}}"
Janitor(1 * time.Second)
AddGlobal("global", " Global!!")
var testSize = 10000000
var wg = sync.WaitGroup{}
for i := 0; i < testSize; i++ {
wg.Add(1)
go func() {
var x = ""
if i < 10000000/2 {
x = fmt.Sprint(i % 100)
}
_, err := Execute(template+x, VarMap{}, struct {
Name string
I int
}{Name: "John", I: i})
if err != nil {
t.Errorf("executing template: %v", err)
}
wg.Done()
time.Sleep(10 * time.Second)
}()
}
wg.Wait()
}