Skip to content

Commit 7d8a0fd

Browse files
committed
test(service): remove need for a running pact mock service in tests
1 parent 98b7c57 commit 7d8a0fd

File tree

3 files changed

+60
-33
lines changed

3 files changed

+60
-33
lines changed

daemon/daemon.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func (d *Daemon) StartDaemon(port int) {
9191
panic(err)
9292
}
9393
go http.Serve(l, mux)
94-
fmt.Println("Server started, waiting for stuff!")
9594

9695
// Wait for sigterm
9796
signal.Notify(d.signalChan, os.Interrupt, os.Kill)

daemon/service_mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ServiceMock struct {
1111
ServiceStopError error
1212
ServiceList map[int]*exec.Cmd
1313
ServiceStartCmd *exec.Cmd
14+
ServiceStartCount int
1415
ServicePort int
1516
ServiceStopCount int
1617
ServicesSetupCalled bool
@@ -38,6 +39,7 @@ func (s *ServiceMock) List() map[int]*exec.Cmd {
3839
// Start a Service and log its output.
3940
func (s *ServiceMock) Start() *exec.Cmd {
4041

42+
s.ServiceStartCount++
4143
cmd := s.ExecFunc()
4244
cmd.Start()
4345

dsl/client_test.go

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"log"
66
"net"
77
"net/rpc"
8+
"os"
9+
"os/exec"
810
"testing"
911
"time"
1012

@@ -69,12 +71,41 @@ func waitForDaemonToShutdown(port int, t *testing.T) {
6971
}
7072
}
7173

72-
func createDaemon(port int) *daemon.Daemon {
73-
s := &daemon.PactMockService{}
74-
_, svc := s.NewService()
74+
// This guy mocks out the underlying Service provider in the Daemon,
75+
// but executes actual Daemon code.
76+
//
77+
// Stubbing the exec.Cmd interface is hard, see fakeExec* functions for
78+
// the magic.
79+
func createDaemon(port int) (*daemon.Daemon, *daemon.ServiceMock) {
80+
svc := &daemon.ServiceMock{
81+
Command: "test",
82+
Args: []string{},
83+
ServiceStopResult: true,
84+
ServiceStopError: nil,
85+
ExecFunc: fakeExecSuccessCommand,
86+
ServiceList: map[int]*exec.Cmd{
87+
1: fakeExecCommand("", true, ""),
88+
2: fakeExecCommand("", true, ""),
89+
3: fakeExecCommand("", true, ""),
90+
},
91+
ServiceStartCmd: nil,
92+
}
93+
94+
// Start all processes to get the Pids!
95+
for _, s := range svc.ServiceList {
96+
s.Start()
97+
}
98+
99+
// Cleanup all Processes when we finish
100+
defer func() {
101+
for _, s := range svc.ServiceList {
102+
s.Process.Kill()
103+
}
104+
}()
105+
75106
d := daemon.NewDaemon(svc)
76107
go d.StartDaemon(port)
77-
return d
108+
return d, svc
78109
}
79110

80111
// func TestClient_Fail(t *testing.T) {
@@ -89,46 +120,41 @@ func TestRPCClient_List(t *testing.T) {
89120
waitForPortInTest(port, t)
90121
defer waitForDaemonToShutdown(port, t)
91122
client := &PactClient{Port: port}
92-
server := client.StartServer()
93123

94-
waitForPortInTest(server.Port, t)
124+
waitForPortInTest(port, t)
95125

96126
s := client.ListServers()
97127

98-
if len(s.Servers) != 1 {
99-
t.Fatalf("Expected 1 server to be running, got %d", len(s.Servers))
128+
if len(s.Servers) != 3 {
129+
t.Fatalf("Expected 3 server to be running, got %d", len(s.Servers))
100130
}
101-
102-
// client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port))
103-
// var res daemon.PactMockServer
104-
// err = client.Call("Daemon.StartServer", daemon.PactMockServer{}, &res)
105-
// if err != nil {
106-
// log.Fatal("rpc error:", err)
107-
// }
108-
//
109-
// waitForPortInTest(res.Port, t)
110-
//
111-
// client, err = rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port))
112-
// var res2 daemon.PactListResponse
113-
// err = client.Call("Daemon.ListServers", daemon.PactMockServer{}, &res2)
114-
// if err != nil {
115-
// log.Fatal("rpc error:", err)
116-
// }
117131
}
118132

119133
// Integration style test: Can a client hit each endpoint?
120134
func TestRPCClient_StartServer(t *testing.T) {
121135
port, _ := utils.GetFreePort()
122-
createDaemon(port)
136+
_, svc := createDaemon(port)
123137
waitForPortInTest(port, t)
138+
defer waitForDaemonToShutdown(port, t)
139+
client := &PactClient{Port: port}
124140

125-
client, err := rpc.DialHTTP("tcp", fmt.Sprintf(":%d", port))
126-
var res daemon.PactMockServer
127-
err = client.Call("Daemon.StartServer", daemon.PactMockServer{}, &res)
128-
if err != nil {
129-
log.Fatal("rpc error:", err)
141+
waitForPortInTest(port, t)
142+
143+
client.StartServer()
144+
if svc.ServiceStartCount != 1 {
145+
t.Fatalf("Expected 1 server to have been started, got %d", svc.ServiceStartCount)
130146
}
147+
}
148+
149+
// Adapted from http://npf.io/2015/06/testing-exec-command/
150+
var fakeExecSuccessCommand = func() *exec.Cmd {
151+
return fakeExecCommand("", true, "")
152+
}
131153

132-
<-time.After(10 * time.Second)
133-
waitForDaemonToShutdown(port, t)
154+
func fakeExecCommand(command string, success bool, args ...string) *exec.Cmd {
155+
cs := []string{"-test.run=TestHelperProcess", "--", command}
156+
cs = append(cs, args...)
157+
cmd := exec.Command(os.Args[0], cs...)
158+
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", fmt.Sprintf("GO_WANT_HELPER_PROCESS_TO_SUCCEED=%t", success)}
159+
return cmd
134160
}

0 commit comments

Comments
 (0)