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?
120134func 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