-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon_integration_test.go
45 lines (38 loc) · 1.16 KB
/
common_integration_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
// Copyright 2016 struktur AG. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package phoenix
import (
"os"
"os/signal"
"testing"
"time"
)
func withTestServer(t *testing.T, server Server, runFunc RunFunc, runTest func()) {
self, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatalf("Failed to get process handle for self: %v", err)
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
defer signal.Stop(sig)
go func() {
if err := server.Run(runFunc); err != nil {
t.Fatalf("Unexpected error starting server: %v", err)
} else {
t.Log("Server shutdown cleanly")
}
}()
// TODO(lcooper): If and when we can signal that we've bound all of our
// sockets, use that instead of waiting.
time.Sleep(1000 * time.Millisecond)
runTest()
if err := self.Signal(os.Interrupt); err == nil {
<-sig
// NOTE(lcooper): Yield the test goroutine while the signal gets delivered
// elsewhere, otherwise the server doesn't shut down cleanly.
time.Sleep(1 * time.Millisecond)
} else {
t.Fatalf("Failed to kill server, inconsistant state will result: %v", err)
}
}