1
1
package runner
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"net"
6
7
"os"
8
+ "os/signal"
7
9
"strings"
10
+ "syscall"
8
11
"testing"
9
12
"time"
10
13
@@ -152,8 +155,7 @@ func TestRebuild(t *testing.T) {
152
155
if err != nil {
153
156
t .Fatalf ("Should not be fail: %s." , err )
154
157
}
155
- time .Sleep (2 * time .Second )
156
- err = waitingPortConnectionRefused (port , time .Second * 5 )
158
+ err = waitingPortConnectionRefused (port , time .Second * 10 )
157
159
if err != nil {
158
160
t .Fatalf ("timeout: %s." , err )
159
161
}
@@ -171,24 +173,62 @@ func TestRebuild(t *testing.T) {
171
173
172
174
func waitingPortConnectionRefused (port int , timeout time.Duration ) error {
173
175
t := time .NewTimer (timeout )
176
+ ticker := time .NewTicker (time .Millisecond * 100 )
177
+ defer ticker .Stop ()
174
178
defer t .Stop ()
175
179
for {
176
180
select {
177
181
case <- t .C :
178
182
return fmt .Errorf ("timeout" )
179
- default :
180
- conn , err := net .Dial ("tcp" , fmt .Sprintf ("localhost:%d" , port ))
181
- if err != nil {
182
- time .Sleep (time .Millisecond * 100 )
183
- continue
184
- } else {
185
- _ = conn .Close ()
183
+ case <- ticker .C :
184
+ print ("." )
185
+ _ , err := net .Dial ("tcp" , fmt .Sprintf ("localhost:%d" , port ))
186
+ if errors .Is (err , syscall .ECONNREFUSED ) {
186
187
return nil
187
188
}
189
+ time .Sleep (time .Millisecond * 100 )
188
190
}
189
191
}
190
192
}
191
193
194
+ func TestCtrlCWhenREngineIsRunning (t * testing.T ) {
195
+ // generate a random port
196
+ port , f := GetPort ()
197
+ f ()
198
+ t .Logf ("port: %d" , port )
199
+
200
+ tmpDir := initTestEnv (t , port )
201
+ // change dir to tmpDir
202
+ err := os .Chdir (tmpDir )
203
+ if err != nil {
204
+ t .Fatalf ("Should not be fail: %s." , err )
205
+ }
206
+ engine , err := NewEngine ("" , true )
207
+ if err != nil {
208
+ t .Fatalf ("Should not be fail: %s." , err )
209
+ }
210
+ go func () {
211
+ engine .Run ()
212
+ t .Logf ("engine stopped" )
213
+ }()
214
+ sigs := make (chan os.Signal , 1 )
215
+ signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
216
+ go func () {
217
+ <- sigs
218
+ engine .Stop ()
219
+ t .Logf ("engine stopped" )
220
+ }()
221
+ if err := waitingPortReady (port , time .Second * 5 ); err != nil {
222
+ t .Fatalf ("Should not be fail: %s." , err )
223
+ }
224
+ sigs <- syscall .SIGINT
225
+ time .Sleep (time .Second * 1 )
226
+ err = waitingPortConnectionRefused (port , time .Second * 10 )
227
+ if err != nil {
228
+ t .Fatalf ("Should not be fail: %s." , err )
229
+ }
230
+ }
231
+
192
232
// waitingPortReady waits until the port is ready to be used.
193
233
func waitingPortReady (port int , timeout time.Duration ) error {
194
234
timeoutChan := time .After (timeout )
0 commit comments