@@ -75,56 +75,77 @@ func serverAddress(t *testing.T) string {
75
75
return ln .Addr ().String ()
76
76
}
77
77
78
- func waitForServerReady (t * testing.T , addr string ) {
79
- waitForServer (t ,
78
+ func waitForServerReady (t * testing.T , cmd * exec.Cmd , addr string ) {
79
+ ch := make (chan error , 1 )
80
+ go func () { ch <- fmt .Errorf ("server exited early: %v" , cmd .Wait ()) }()
81
+ go waitForServer (t , ch ,
80
82
fmt .Sprintf ("http://%v/" , addr ),
81
83
"The Go Programming Language" ,
82
84
15 * time .Second ,
83
85
false )
86
+ if err := <- ch ; err != nil {
87
+ t .Fatal (err )
88
+ }
84
89
}
85
90
86
- func waitForSearchReady (t * testing.T , addr string ) {
87
- waitForServer (t ,
91
+ func waitForSearchReady (t * testing.T , cmd * exec.Cmd , addr string ) {
92
+ ch := make (chan error , 1 )
93
+ go func () { ch <- fmt .Errorf ("server exited early: %v" , cmd .Wait ()) }()
94
+ go waitForServer (t , ch ,
88
95
fmt .Sprintf ("http://%v/search?q=FALLTHROUGH" , addr ),
89
96
"The list of tokens." ,
90
97
2 * time .Minute ,
91
98
false )
99
+ if err := <- ch ; err != nil {
100
+ t .Fatal (err )
101
+ }
92
102
}
93
103
94
104
func waitUntilScanComplete (t * testing.T , addr string ) {
95
- waitForServer (t ,
105
+ ch := make (chan error )
106
+ go waitForServer (t , ch ,
96
107
fmt .Sprintf ("http://%v/pkg" , addr ),
97
108
"Scan is not yet complete" ,
98
109
2 * time .Minute ,
110
+ // setting reverse as true, which means this waits
111
+ // until the string is not returned in the response anymore
99
112
true ,
100
113
)
101
- // setting reverse as true, which means this waits
102
- // until the string is not returned in the response anymore
114
+ if err := <- ch ; err != nil {
115
+ t .Fatal (err )
116
+ }
103
117
}
104
118
105
119
const pollInterval = 200 * time .Millisecond
106
120
107
- func waitForServer (t * testing.T , url , match string , timeout time.Duration , reverse bool ) {
108
- // "health check" duplicated from x/tools/cmd/tipgodoc/tip.go
121
+ // waitForServer waits for server to meet the required condition.
122
+ // It sends a single error value to ch, unless the test has failed.
123
+ // The error value is nil if the required condition was met within
124
+ // timeout, or non-nil otherwise.
125
+ func waitForServer (t * testing.T , ch chan <- error , url , match string , timeout time.Duration , reverse bool ) {
109
126
deadline := time .Now ().Add (timeout )
110
127
for time .Now ().Before (deadline ) {
111
- time .Sleep (pollInterval )
128
+ if t .Failed () {
129
+ return
130
+ }
112
131
res , err := http .Get (url )
113
132
if err != nil {
114
133
continue
115
134
}
116
- rbody , err := ioutil .ReadAll (res .Body )
135
+ body , err := ioutil .ReadAll (res .Body )
117
136
res .Body .Close ()
118
- if err == nil && res .StatusCode == http .StatusOK {
119
- if bytes .Contains (rbody , []byte (match )) && ! reverse {
120
- return
121
- }
122
- if ! bytes .Contains (rbody , []byte (match )) && reverse {
123
- return
124
- }
137
+ if err != nil || res .StatusCode != http .StatusOK {
138
+ continue
139
+ }
140
+ switch {
141
+ case ! reverse && bytes .Contains (body , []byte (match )),
142
+ reverse && ! bytes .Contains (body , []byte (match )):
143
+ ch <- nil
144
+ return
125
145
}
146
+ time .Sleep (pollInterval )
126
147
}
127
- t . Fatalf ( "Server failed to respond in %v" , timeout )
148
+ ch <- fmt . Errorf ( "server failed to respond in %v" , timeout )
128
149
}
129
150
130
151
// hasTag checks whether a given release tag is contained in the current version
@@ -140,7 +161,7 @@ func hasTag(t string) bool {
140
161
141
162
func killAndWait (cmd * exec.Cmd ) {
142
163
cmd .Process .Kill ()
143
- cmd .Wait ()
164
+ cmd .Process . Wait ()
144
165
}
145
166
146
167
func TestURL (t * testing.T ) {
@@ -228,9 +249,9 @@ func testWeb(t *testing.T, withIndex bool) {
228
249
defer killAndWait (cmd )
229
250
230
251
if withIndex {
231
- waitForSearchReady (t , addr )
252
+ waitForSearchReady (t , cmd , addr )
232
253
} else {
233
- waitForServerReady (t , addr )
254
+ waitForServerReady (t , cmd , addr )
234
255
waitUntilScanComplete (t , addr )
235
256
}
236
257
@@ -444,7 +465,7 @@ func main() { print(lib.V) }
444
465
t .Fatalf ("failed to start godoc: %s" , err )
445
466
}
446
467
defer killAndWait (cmd )
447
- waitForServerReady (t , addr )
468
+ waitForServerReady (t , cmd , addr )
448
469
449
470
// Wait for type analysis to complete.
450
471
reader := bufio .NewReader (stderr )
0 commit comments