-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
115 lines (96 loc) · 2.2 KB
/
pool_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
109
110
111
112
113
114
115
package reactor
import (
"fmt"
"sync"
"testing"
)
func TestPoolRenderEmptyCode(t *testing.T) {
p := NewPool("")
resp, err := p.Render(&Request{})
assertNil(t, resp)
assertNotNil(t, err)
if err != nil {
assertContains(t, err.Error(), "ReferenceError: render is not defined")
}
}
func TestPoolRenderInvalidCode(t *testing.T) {
p := NewPool("throw 'hi';")
resp, err := p.Render(&Request{})
assertNil(t, resp)
assertNotNil(t, err)
if err != nil {
assertContains(t, err.Error(), "Uncaught exception: hi")
}
}
func TestPoolUpdateCode(t *testing.T) {
code1 := `function render() { return '{"html": "<div>1</div>"}'; }`
code2 := `function render() { return '{"html": "<div>2</div>"}'; }`
p := NewPool(code1)
for i := 0; i < 5; i++ {
resp, err := p.Render(&Request{})
assertNil(t, err)
assertNotNil(t, resp)
if resp != nil {
assertContains(t, resp.HTML, "1")
}
}
p.UpdateCode(code2)
for i := 0; i < 5; i++ {
resp, err := p.Render(&Request{})
assertNil(t, err)
assertNotNil(t, resp)
if resp != nil {
assertContains(t, resp.HTML, "2")
}
}
}
func TestPoolRenderTorture(t *testing.T) {
threads := 20
requests := 5000
// create a new pool
pool := NewPool(bundle)
wg := sync.WaitGroup{}
// render components successfully
for i := 0; i < threads; i++ {
wg.Add(1)
go func(i int) {
serial := fmt.Sprintf("N-%d-A", i)
req := &Request{
Name: "Widget",
Props: map[string]interface{}{
"serial": serial,
"date": "2017-10-17",
},
}
for j := 0; j < requests; j++ {
resp, err := pool.Render(req)
assertNil(t, err)
assertNotNil(t, resp)
if resp != nil {
assertContains(t, resp.HTML, serial)
assertContains(t, resp.HTML, "manufactured at")
assertContains(t, resp.HTML, "2017-10-17")
}
}
wg.Done()
}(i)
}
// render components unsuccessfully
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
req := &Request{
Name: "WrongWidget",
}
resp, err := pool.Render(req)
assertNil(t, resp)
assertNotNil(t, err)
if err != nil {
assertContains(t, err.Error(), "Cannot find module './WrongWidget.jsx'")
assertContains(t, err.Error(), "at server.js")
}
wg.Done()
}(i)
}
wg.Wait()
}