5
5
"log"
6
6
"net"
7
7
"net/rpc"
8
+ "os"
9
+ "os/exec"
8
10
"testing"
9
11
"time"
10
12
@@ -69,12 +71,41 @@ func waitForDaemonToShutdown(port int, t *testing.T) {
69
71
}
70
72
}
71
73
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
+
75
106
d := daemon .NewDaemon (svc )
76
107
go d .StartDaemon (port )
77
- return d
108
+ return d , svc
78
109
}
79
110
80
111
// func TestClient_Fail(t *testing.T) {
@@ -89,46 +120,41 @@ func TestRPCClient_List(t *testing.T) {
89
120
waitForPortInTest (port , t )
90
121
defer waitForDaemonToShutdown (port , t )
91
122
client := & PactClient {Port : port }
92
- server := client .StartServer ()
93
123
94
- waitForPortInTest (server . Port , t )
124
+ waitForPortInTest (port , t )
95
125
96
126
s := client .ListServers ()
97
127
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 ))
100
130
}
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
- // }
117
131
}
118
132
119
133
// Integration style test: Can a client hit each endpoint?
120
134
func TestRPCClient_StartServer (t * testing.T ) {
121
135
port , _ := utils .GetFreePort ()
122
- createDaemon (port )
136
+ _ , svc := createDaemon (port )
123
137
waitForPortInTest (port , t )
138
+ defer waitForDaemonToShutdown (port , t )
139
+ client := & PactClient {Port : port }
124
140
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 )
130
146
}
147
+ }
148
+
149
+ // Adapted from http://npf.io/2015/06/testing-exec-command/
150
+ var fakeExecSuccessCommand = func () * exec.Cmd {
151
+ return fakeExecCommand ("" , true , "" )
152
+ }
131
153
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
134
160
}
0 commit comments