4
4
5
5
use Wrench \Protocol \Rfc6455Protocol ;
6
6
use Wrench \Socket \ClientSocket ;
7
+ use Wrench \Tests \ServerTestHelper ;
7
8
use \Exception ;
8
9
use \stdClass ;
9
10
10
11
class ClientSocketTest extends UriSocketTest
11
12
{
12
- const TEST_SERVER_PORT_MIN = 16666 ;
13
- const TEST_SERVER_PORT_MAX = 52222 ;
14
-
15
- public static $ nextPort = null ;
16
-
17
- protected $ port = null ;
18
- protected $ process = null ;
19
- protected $ pipes = array ();
20
-
21
- /**
22
- * Gets the next available port number to start a server on
23
- */
24
- public static function getNextPort ()
25
- {
26
- if (self ::$ nextPort === null ) {
27
- self ::$ nextPort = mt_rand (self ::TEST_SERVER_PORT_MIN , self ::TEST_SERVER_PORT_MAX );
28
- }
29
- return self ::$ nextPort ++;
30
- }
31
-
32
13
/**
33
14
* @see Wrench\Tests.Test::getClass()
34
15
*/
@@ -37,87 +18,6 @@ public function getClass()
37
18
return 'Wrench\Socket\ClientSocket ' ;
38
19
}
39
20
40
- /**
41
- * Gets the server command
42
- *
43
- * @return string
44
- */
45
- protected function getCommand ()
46
- {
47
- return sprintf ('/usr/bin/env php %s/../server.php %d ' , __DIR__ , $ this ->port );
48
- }
49
-
50
- /**
51
- * Starts a listening server
52
- */
53
- protected function startProcess ()
54
- {
55
- $ this ->port = self ::getNextPort ();
56
-
57
- $ this ->process = proc_open (
58
- $ this ->getCommand (),
59
- array (
60
- 0 => array ('file ' , '/dev/null ' , 'r ' ),
61
- 1 => array ('file ' , __DIR__ . '/../../../../build/server.log ' , 'a+ ' ),
62
- 2 => array ('file ' , __DIR__ . '/../../../../build/server.err.log ' , 'a+ ' )
63
- ),
64
- $ this ->pipes ,
65
- __DIR__ . '../ '
66
- );
67
-
68
- sleep (3 );
69
- }
70
-
71
- /**
72
- * Stops the listening server
73
- */
74
- protected function stopProcess ()
75
- {
76
- if ($ this ->process ) {
77
- foreach ($ this ->pipes as &$ pipe ) {
78
- fclose ($ pipe );
79
- }
80
- $ this ->pipes = null ;
81
-
82
- // Sigh
83
- $ status = proc_get_status ($ this ->process );
84
-
85
- if ($ status && isset ($ status ['pid ' ]) && $ status ['pid ' ]) {
86
- // More sigh, this is the pid of the parent sh process, we want
87
- // to terminate the server directly
88
- $ this ->log ('Command: /bin/ps -ao pid,ppid | /usr/bin/col | /usr/bin/tail -n +2 | /bin/grep \' ' . $ status ['pid ' ] . "' " , 'info ' );
89
- exec ('/bin/ps -ao pid,ppid | /usr/bin/col | /usr/bin/tail -n +2 | /bin/grep \' ' . $ status ['pid ' ] . "' " , $ processes , $ return );
90
-
91
- if ($ return === 0 ) {
92
- foreach ($ processes as $ process ) {
93
- list ($ pid , $ ppid ) = explode (' ' , str_replace (' ' , ' ' , $ process ));
94
- if ($ pid ) {
95
- $ this ->log ('Killing ' . $ pid , 'info ' );
96
- exec ('/bin/kill ' . $ pid . ' > /dev/null 2>&1 ' );
97
- }
98
- }
99
- } else {
100
- $ this ->log ('Unable to find child processes ' , 'warning ' );
101
- }
102
-
103
- sleep (1 );
104
-
105
- $ this ->log ('Killing ' . $ status ['pid ' ], 'info ' );
106
- exec ('/bin/kill ' . $ status ['pid ' ] . ' > /dev/null 2>&1 ' );
107
-
108
- sleep (1 );
109
- }
110
-
111
- proc_close ($ this ->process );
112
- unset($ this ->process );
113
- }
114
- }
115
-
116
- public function log ($ message , $ priority = 'info ' )
117
- {
118
- //echo $message . "\n";
119
- }
120
-
121
21
/**
122
22
* Overriden to use with the depends annotation
123
23
*
@@ -236,26 +136,32 @@ public function testSendTooEarly($instance)
236
136
*/
237
137
public function testConnect ()
238
138
{
239
- $ this ->startProcess ();
139
+ try {
140
+ $ helper = new ServerTestHelper ();
141
+ $ helper ->setUp ();
240
142
241
- // Wait for server to come up
242
- $ instance = $ this ->getInstance ('ws://localhost: ' . $ this ->port );
243
- $ success = $ instance ->connect ();
143
+ $ instance = $ this ->getInstance ($ helper ->getConnectionString ());
144
+ $ success = $ instance ->connect ();
244
145
245
- $ this ->assertTrue ($ success , 'Client socket can connect to test server ' );
146
+ $ this ->assertTrue ($ success , 'Client socket can connect to test server ' );
246
147
247
- $ sent = $ instance ->send ("GET /echo HTTP/1.1 \r
148
+ $ sent = $ instance ->send ("GET /echo HTTP/1.1 \r
248
149
Host: localhost \r
249
150
Upgrade: websocket \r
250
151
Connection: Upgrade \r
251
152
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== \r
252
153
Origin: http://localhost \r
253
154
Sec-WebSocket-Version: 13 \r\n\r\n" );
254
- $ this ->assertNotEquals (false , $ sent , 'Client socket can send to test server ' );
155
+ $ this ->assertNotEquals (false , $ sent , 'Client socket can send to test server ' );
255
156
256
- $ response = $ instance ->receive ();
257
- $ this ->assertStringStartsWith ('HTTP ' , $ response , 'Response looks like HTTP handshake response ' );
157
+ $ response = $ instance ->receive ();
158
+ $ this ->assertStringStartsWith ('HTTP ' , $ response , 'Response looks like HTTP handshake response ' );
159
+
160
+ } catch (\Exception $ e ) {
161
+ $ helper ->tearDown ();
162
+ throw $ e ;
163
+ }
258
164
259
- $ this -> stopProcess ();
165
+ $ helper -> tearDown ();
260
166
}
261
167
}
0 commit comments