-
Notifications
You must be signed in to change notification settings - Fork 9
/
testarator.go
190 lines (160 loc) · 3.81 KB
/
testarator.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package testerator
import (
"context"
"os"
"sync"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
)
// Helper uses for setup hooks to Setup struct.
type Helper func(s *Setup) error
// Setup contains aetest.Instance and other environment for setup and clean up.
type Setup struct {
Instance aetest.Instance
Disable1stGen bool
RaisePanic bool
Options *aetest.Options
Context context.Context
counter int
Setuppers []Helper
Cleaners []Helper
total int
ResetThreshold int
SpinDowns []chan struct{}
sync.Mutex
}
// DefaultSetup uses from bare functions.
var DefaultSetup *Setup
func init() {
// runtime.GOMAXPROCS(runtime.NumCPU())
DefaultSetup = &Setup{}
}
// SpinUp dispatch spin up request to DefaultSetup.SpinUp.
func SpinUp() (aetest.Instance, context.Context, error) {
err := DefaultSetup.SpinUp()
return DefaultSetup.Instance, DefaultSetup.Context, err
}
// SpinDown dispatch spin down request to DefaultSetup.SpinDown.
func SpinDown() error {
return DefaultSetup.SpinDown()
}
// IsCI returns this execution environment is Continuous Integration server or not.
// Deprecated.
func IsCI() bool {
return os.Getenv("CI") != ""
}
// SpinUp dev server.
//
// If you call this function twice. launch dev server and increment internal counter twice.
// 1st time then dev server is up and increment internal counter.
// 2nd time then dev server is increment internal counter only.
// see document for SpinDown function.
func (s *Setup) SpinUp() error {
s.Lock()
defer s.Unlock()
if s.ResetThreshold == 0 {
// NOTE
// https://cloud.google.com/appengine/docs/standard/go/release-notes
// August 9, 2017 Updated Go SDK to version 1.9.57.
// The aetest package now reuses HTTP connections, fixing a bug that exhausted file descriptors when running tests.
s.ResetThreshold = 1000
}
s.total++
s.counter++
if s.Instance != nil {
return nil
}
opt := s.Options
if opt == nil {
opt = &aetest.Options{
AppID: "unittest",
StronglyConsistentDatastore: true,
SuppressDevAppServerLog: true,
}
}
if s.Disable1stGen {
s.Instance = &mockAEInstance{}
s.Context = context.Background()
} else {
inst, err := aetest.NewInstance(opt)
if err != nil {
return err
}
req, err := inst.NewRequest("GET", "/", nil)
if err != nil {
return err
}
c := appengine.NewContext(req)
s.Instance = inst
s.Context = c
}
for _, setupper := range s.Setuppers {
err := setupper(s)
if err != nil {
return err
}
}
return nil
}
// SpinDown dev server.
//
// This function clean up dev server environment.
// call each DefaultSetup.Cleaners. usually, it means cleanup Datastore and Search APIs and miscs.
// see document for SpinUp function.
func (s *Setup) SpinDown() error {
s.Lock()
defer s.Unlock()
defer func() {
if s.counter == 0 {
for _, sd := range s.SpinDowns {
<-sd
}
s.SpinDowns = nil
}
}()
defer func() {
if s.Instance == nil {
return
}
closeInstance := func() {
ch := make(chan struct{})
s.SpinDowns = append(s.SpinDowns, ch)
go func(inst aetest.Instance) {
defer func() {
ch <- struct{}{}
}()
inst.Close()
}(s.Instance)
}
if s.counter == 0 {
closeInstance()
s.Instance = nil
} else if s.total%s.ResetThreshold == 0 {
// Sometimes spin down causes. avoid to saturate file descriptor.
closeInstance()
s.Instance = nil
}
}()
// clean up environment
for _, c := range s.Cleaners {
err := c(s)
if err != nil {
if s.RaisePanic {
panic(err)
}
return err
}
}
s.counter--
return nil
}
func (s *Setup) AppendSetuppers(h Helper) {
s.Lock()
defer s.Unlock()
s.Setuppers = append(s.Setuppers, h)
}
func (s *Setup) AppendCleanup(h Helper) {
s.Lock()
defer s.Unlock()
s.Cleaners = append(s.Cleaners, h)
}