-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSocketTest.php
More file actions
358 lines (307 loc) · 9.54 KB
/
Copy pathSocketTest.php
File metadata and controls
358 lines (307 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php declare(strict_types=1);
namespace hollodotme\FastCGI\Tests\Unit\Sockets;
use Exception;
use hollodotme\FastCGI\Encoders\NameValuePairEncoder;
use hollodotme\FastCGI\Encoders\PacketEncoder;
use hollodotme\FastCGI\Exceptions\ConnectException;
use hollodotme\FastCGI\Exceptions\ReadFailedException;
use hollodotme\FastCGI\Exceptions\TimedoutException;
use hollodotme\FastCGI\Exceptions\WriteFailedException;
use hollodotme\FastCGI\Interfaces\ProvidesResponseData;
use hollodotme\FastCGI\RequestContents\UrlEncodedFormData;
use hollodotme\FastCGI\Requests\PostRequest;
use hollodotme\FastCGI\SocketConnections\Defaults;
use hollodotme\FastCGI\SocketConnections\NetworkSocket;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;
use hollodotme\FastCGI\Sockets\Socket;
use hollodotme\FastCGI\Sockets\SocketId;
use hollodotme\FastCGI\Tests\Traits\SocketDataProviding;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionException;
use RuntimeException;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Throwable;
use function dirname;
use function http_build_query;
final class SocketTest extends TestCase
{
use SocketDataProviding;
/**
* @throws Exception
*/
public function testCanGetIdAfterConstruction() : void
{
$socket = $this->getSocket();
self::assertGreaterThanOrEqual( 1, $socket->getId() );
self::assertLessThanOrEqual( (1 << 16) - 1, $socket->getId() );
}
/**
* @param int $connectTimeout
* @param int $readWriteTimeout
*
* @return Socket
* @throws Exception
*/
private function getSocket(
int $connectTimeout = Defaults::CONNECT_TIMEOUT,
int $readWriteTimeout = Defaults::READ_WRITE_TIMEOUT
) : Socket
{
$nameValuePairEncoder = new NameValuePairEncoder();
$packetEncoder = new PacketEncoder();
$connection = new UnixDomainSocket(
$this->getUnixDomainSocket(),
$connectTimeout,
$readWriteTimeout
);
return new Socket( SocketId::new(), $connection, $packetEncoder, $nameValuePairEncoder );
}
/**
* @throws Exception
* @throws Throwable
* @throws ConnectException
* @throws TimedoutException
* @throws WriteFailedException
*/
public function testCanSendRequestAndFetchResponse() : void
{
$socket = $this->getSocket();
$data = ['test-key' => 'unit'];
$request = new PostRequest(
dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php',
new UrlEncodedFormData( $data )
);
$socket->sendRequest( $request );
$response = $socket->fetchResponse();
self::assertSame( 'unit', $response->getBody() );
$response2 = $socket->fetchResponse();
self::assertSame( $response, $response2 );
}
/**
* @throws Exception
* @throws AssertionFailedError
* @throws \PHPUnit\Framework\Exception
* @throws ConnectException
* @throws TimedoutException
* @throws WriteFailedException
*/
public function testCanCollectResource() : void
{
$resources = [];
$socket = $this->getSocket();
$data = ['test-key' => 'unit'];
$request = new PostRequest(
dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php',
new UrlEncodedFormData( $data )
);
$socket->collectResource( $resources );
self::assertEmpty( $resources );
$socket->sendRequest( $request );
$socket->collectResource( $resources );
self::assertIsResource( $resources[ $socket->getId() ] );
}
/**
* @throws ConnectException
* @throws TimedoutException
* @throws WriteFailedException
* @throws ReadFailedException
* @throws Exception
*/
public function testCanNotifyResponseCallback() : void
{
$socket = $this->getSocket();
$data = ['test-key' => 'unit'];
$request = new PostRequest(
dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php',
new UrlEncodedFormData( $data )
);
$request->addResponseCallbacks(
static function ( ProvidesResponseData $response )
{
echo $response->getBody();
}
);
$socket->sendRequest( $request );
$response = $socket->fetchResponse();
$socket->notifyResponseCallbacks( $response );
$this->expectOutputString( 'unit' );
}
/**
* @throws ConnectException
* @throws TimedoutException
* @throws WriteFailedException
* @throws Exception
*/
public function testCanNotifyFailureCallback() : void
{
$socket = $this->getSocket();
$data = ['test-key' => 'unit'];
$request = new PostRequest(
dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php',
new UrlEncodedFormData( $data )
);
$request->addFailureCallbacks(
static function ( Throwable $throwable )
{
echo $throwable->getMessage();
}
);
$throwable = new RuntimeException( 'Something went wrong.' );
$socket->sendRequest( $request );
$socket->notifyFailureCallbacks( $throwable );
$this->expectOutputString( 'Something went wrong.' );
}
/**
* @throws AssertionFailedError
* @throws ConnectException
* @throws TimedoutException
* @throws WriteFailedException
* @throws Exception
*/
public function testThrowsExceptionIfRequestIsSentToSocketThatIsNotIdle() : void
{
$socket = $this->getSocket();
$request = new PostRequest( '/some/script.php' );
$socket->sendRequest( $request );
$this->expectException( ConnectException::class );
$this->expectExceptionMessage( 'Trying to connect to a socket that is not idle.' );
$socket->sendRequest( $request );
self::fail( 'Expected ConnectException to be thrown.' );
}
/**
* @param int $flag
* @param class-string<Throwable> $expectedException
* @param string $expectedExceptionMessage
*
* @throws AssertionFailedError
* @throws ReflectionException
* @throws Exception
*
* @dataProvider responseFlagProvider
*/
public function testRequestCompletedGuard(
int $flag,
string $expectedException,
string $expectedExceptionMessage
) : void
{
$socket = $this->getSocket();
$guardMethod = (new ReflectionClass( $socket ))->getMethod( 'guardRequestCompleted' );
$guardMethod->setAccessible( true );
$this->expectException( $expectedException );
$this->expectExceptionMessage( $expectedExceptionMessage );
$guardMethod->invoke( $socket, $flag );
self::fail( 'Expected an Exception to be thrown.' );
}
/**
* @return array<array<string, int|string>>
*/
public function responseFlagProvider() : array
{
return [
[
'flag' => 1,
'expectedException' => WriteFailedException::class,
'expectedExceptionMessage' => 'This app can\'t multiplex [CANT_MPX_CONN]',
],
[
'flag' => 2,
'expectedException' => WriteFailedException::class,
'expectedExceptionMessage' => 'New request rejected; too busy [OVERLOADED]',
],
[
'flag' => 3,
'expectedException' => WriteFailedException::class,
'expectedExceptionMessage' => 'Role value not known [UNKNOWN_ROLE]',
],
[
'flag' => 123,
'expectedException' => ReadFailedException::class,
'expectedExceptionMessage' => 'Unknown content.',
],
];
}
/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws Exception
*/
public function testIsUsableWhenInInitialState() : void
{
$socket = $this->getSocket();
self::assertTrue( $socket->isUsable() );
}
/**
* @throws ConnectException
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws ReadFailedException
* @throws TimedoutException
* @throws WriteFailedException
* @throws Exception
*/
public function testIsNotUsableWhenTimedOut() : void
{
$socket = $this->getSocket();
$content = new UrlEncodedFormData( ['sleep' => 1, 'test-key' => 'unit'] );
$request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php', $content );
$socket->sendRequest( $request );
try
{
/** @noinspection UnusedFunctionResultInspection */
$socket->fetchResponse( 100 );
}
catch ( TimedoutException $e )
{
}
self::assertFalse( $socket->isUsable() );
}
/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws ReflectionException
* @throws Exception
*/
public function testIsNotUsableWhenSocketWasClosed() : void
{
$socket = $this->getSocket();
$connectMethod = (new ReflectionClass( $socket ))->getMethod( 'connect' );
$connectMethod->setAccessible( true );
$connectMethod->invoke( $socket );
$disconnectMethod = (new ReflectionClass( $socket ))->getMethod( 'disconnect' );
$disconnectMethod->setAccessible( true );
$disconnectMethod->invoke( $socket );
self::assertFalse( $socket->isUsable() );
}
/**
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws Exception
*/
public function testCanCheckIfSocketUsesConnection() : void
{
$unixDomainConnection = new UnixDomainSocket( $this->getUnixDomainSocket() );
$networkConnection = new NetworkSocket( $this->getNetworkSocketHost(), $this->getNetworkSocketPort() );
$packetEncoder = new PacketEncoder();
$nameValuePairEncoder = new NameValuePairEncoder();
$unixDomainSocket = new Socket(
SocketId::new(),
$unixDomainConnection,
$packetEncoder,
$nameValuePairEncoder
);
$networkSocket = new Socket(
SocketId::new(),
$networkConnection,
$packetEncoder,
$nameValuePairEncoder
);
self::assertTrue( $unixDomainSocket->usesConnection( $unixDomainConnection ) );
self::assertFalse( $unixDomainSocket->usesConnection( $networkConnection ) );
self::assertTrue( $networkSocket->usesConnection( $networkConnection ) );
self::assertFalse( $networkSocket->usesConnection( $unixDomainConnection ) );
}
}