2
2
3
3
use PHPUnit \Framework \TestCase ;
4
4
use Ollyxar \WebSockets \{
5
+ Exceptions \ForkException ,
6
+ Exceptions \SocketException ,
5
7
Frame ,
6
8
Server ,
7
- Exceptions \ForkException ,
8
- Logger ,
9
9
Ssl
10
10
};
11
11
@@ -25,7 +25,8 @@ class ServerTest extends TestCase
25
25
'path ' => '/tmp/cert.pem ' ,
26
26
'passPhrase ' => 'qwerty123 '
27
27
];
28
- private $ client ;
28
+
29
+ public static $ serverStarted = false ;
29
30
30
31
/**
31
32
* Starts Server
@@ -34,26 +35,13 @@ class ServerTest extends TestCase
34
35
*/
35
36
private function startServer (): void
36
37
{
37
- Logger::enable ();
38
- Server::$ connector = '/tmp/ws.sock ' ;
39
-
40
38
(new Server ('0.0.0.0 ' , static ::PORT , 2 , true ))
41
39
->setHandler (Handler::class)
42
40
->setCert (static ::CERT ['path ' ])
43
41
->setPassPhrase (static ::CERT ['passPhrase ' ])
44
42
->run ();
45
43
}
46
44
47
- /**
48
- * Stops server
49
- *
50
- * @return void
51
- */
52
- private function stopServer (): void
53
- {
54
- posix_kill ($ this ->serverPid , SIGINT );
55
- }
56
-
57
45
/**
58
46
* Forking process to get properly working Server and separated test
59
47
*
@@ -62,23 +50,30 @@ private function stopServer(): void
62
50
*/
63
51
private function forkProcess (): void
64
52
{
53
+ if (static ::$ serverStarted ) {
54
+ return ;
55
+ }
56
+
65
57
$ pid = pcntl_fork ();
66
58
67
59
if ($ pid == -1 ) {
68
60
throw new ForkException ('Cannot fork process ' );
69
61
} elseif ($ pid ) {
70
62
$ this ->serverPid = $ pid ;
63
+ sleep (3 );
71
64
} else {
72
65
$ this ->startServer ();
73
66
}
67
+
68
+ static ::$ serverStarted = true ;
74
69
}
75
70
76
71
/**
77
72
* Making lightweight WebSocket client
78
73
*
79
- * @return void
74
+ * @return resource
80
75
*/
81
- private function makeClient (): void
76
+ private function makeClient ()
82
77
{
83
78
$ context = stream_context_create ([
84
79
'ssl ' => [
@@ -91,7 +86,9 @@ private function makeClient(): void
91
86
]
92
87
]);
93
88
94
- $ this ->client = stream_socket_client ('ssl://localhost: ' . static ::PORT , $ errorNumber , $ errorString , null , STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT , $ context );
89
+ sleep (3 ); // prevent receiving previous messages from queue
90
+
91
+ return stream_socket_client ('ssl://localhost: ' . static ::PORT , $ errorNumber , $ errorString , null , STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT , $ context );
95
92
}
96
93
97
94
/**
@@ -136,41 +133,81 @@ private function generateHandshakeRequest(): string
136
133
}
137
134
138
135
/**
136
+ * ServerTest constructor.
137
+ *
138
+ * @param null $name
139
+ * @param array $data
140
+ * @param string $dataName
139
141
* @throws ForkException
140
142
*/
141
- public function setUp ( )
143
+ public function __construct ( $ name = null , array $ data = [], $ dataName = '' )
142
144
{
145
+ Server::$ connector = '/tmp/ws.sock ' ;
143
146
Ssl::generateCert (static ::CERT ['path ' ], static ::CERT ['passPhrase ' ]);
144
147
$ this ->forkProcess ();
145
- sleep ( 3 );
146
- $ this -> makeClient ( );
148
+
149
+ parent :: __construct ( $ name , $ data , $ dataName );
147
150
}
148
151
149
152
/**
150
- * Free resources
153
+ * Simple messaging
154
+ *
155
+ * @throws SocketException
151
156
*/
152
- public function __destruct ()
157
+ public function testBasicMessaging ()
153
158
{
154
- //$this->stopServer(); // doesn't work on TravisCI
155
- @unlink (static ::CERT ['path ' ]);
159
+ $ client = $ this ->makeClient ();
160
+
161
+ if (!$ client ) {
162
+ throw new SocketException ('Client socket does not created properly ' );
163
+ }
164
+
165
+ fwrite ($ client , $ this ->generateHandshakeRequest ());
166
+ $ response = fread ($ client , 4096 );
167
+ $ this ->assertContains ('101 Web Socket Protocol Handshake ' , $ response );
168
+
169
+ $ data = Frame::decode ($ client );
170
+ $ this ->assertArrayHasKey ('payload ' , $ data );
171
+ $ this ->assertContains ('connected. ' , $ data ['payload ' ]);
172
+
173
+ fwrite ($ client , Frame::encode ('client:hello ' ));
174
+ $ data = Frame::decode ($ client );
175
+ $ this ->assertArrayHasKey ('payload ' , $ data );
176
+ $ this ->assertEquals ('client:hello ' , $ data ['payload ' ]);
177
+
178
+ fclose ($ client );
156
179
}
157
180
158
181
/**
159
- * Simple messaging
182
+ * Base connector test
183
+ *
184
+ * @throws SocketException
160
185
*/
161
- public function testBasicMessaging ()
186
+ public function testConnector ()
162
187
{
163
- fwrite ($ this ->client , $ this ->generateHandshakeRequest ());
164
- $ response = fread ($ this ->client , 4096 );
188
+ $ client = $ this ->makeClient ();
189
+
190
+ if (!$ client ) {
191
+ throw new SocketException ('Client socket does not created properly ' );
192
+ }
193
+
194
+ fwrite ($ client , $ this ->generateHandshakeRequest ());
195
+ $ response = fread ($ client , 4096 );
165
196
$ this ->assertContains ('101 Web Socket Protocol Handshake ' , $ response );
166
197
167
- $ data = Frame::decode ($ this -> client );
198
+ $ data = Frame::decode ($ client );
168
199
$ this ->assertArrayHasKey ('payload ' , $ data );
169
200
$ this ->assertContains ('connected. ' , $ data ['payload ' ]);
170
201
171
- fwrite ($ this ->client , Frame::encode ('Hello message ' ));
172
- $ data = Frame::decode ($ this ->client );
202
+ $ socket = socket_create (AF_UNIX , SOCK_STREAM , 0 );
203
+ socket_connect ($ socket , Server::$ connector );
204
+ socket_write ($ socket , Frame::encode ('connector:hello ' ));
205
+ socket_close ($ socket );
206
+
207
+ $ data = Frame::decode ($ client );
173
208
$ this ->assertArrayHasKey ('payload ' , $ data );
174
- $ this ->assertEquals ('Hello message ' , $ data ['payload ' ]);
209
+ $ this ->assertEquals ('connector:hello ' , $ data ['payload ' ]);
210
+
211
+ fclose ($ client );
175
212
}
176
213
}
0 commit comments