Skip to content

Commit d6e47c5

Browse files
authored
Merge pull request #66 from martinschroeder/master
Test suite now uses socket pairs instead of memory streams
2 parents 55e12c2 + 6b1cc3e commit d6e47c5

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

tests/AbstractLoopTest.php

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,36 @@ public function setUp()
2020

2121
abstract public function createLoop();
2222

23-
public function createStream()
24-
{
25-
return fopen('php://temp', 'r+');
26-
}
27-
28-
public function writeToStream($stream, $content)
29-
{
30-
fwrite($stream, $content);
31-
rewind($stream);
23+
public function createSocketPair()
24+
{
25+
$domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX;
26+
$sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
27+
28+
foreach ($sockets as $socket) {
29+
if (function_exists('stream_set_read_buffer')) {
30+
stream_set_read_buffer($socket, 0);
31+
}
32+
}
33+
34+
return $sockets;
3235
}
3336

3437
public function testAddReadStream()
3538
{
36-
$input = $this->createStream();
39+
list ($input, $output) = $this->createSocketPair();
3740

3841
$this->loop->addReadStream($input, $this->expectCallableExactly(2));
3942

40-
$this->writeToStream($input, "foo\n");
43+
fwrite($output, "foo\n");
4144
$this->loop->tick();
4245

43-
$this->writeToStream($input, "bar\n");
46+
fwrite($output, "bar\n");
4447
$this->loop->tick();
4548
}
4649

4750
public function testAddWriteStream()
4851
{
49-
$input = $this->createStream();
52+
list ($input) = $this->createSocketPair();
5053

5154
$this->loop->addWriteStream($input, $this->expectCallableExactly(2));
5255
$this->loop->tick();
@@ -55,33 +58,33 @@ public function testAddWriteStream()
5558

5659
public function testRemoveReadStreamInstantly()
5760
{
58-
$input = $this->createStream();
61+
list ($input, $output) = $this->createSocketPair();
5962

6063
$this->loop->addReadStream($input, $this->expectCallableNever());
6164
$this->loop->removeReadStream($input);
6265

63-
$this->writeToStream($input, "bar\n");
66+
fwrite($output, "bar\n");
6467
$this->loop->tick();
6568
}
6669

6770
public function testRemoveReadStreamAfterReading()
6871
{
69-
$input = $this->createStream();
72+
list ($input, $output) = $this->createSocketPair();
7073

7174
$this->loop->addReadStream($input, $this->expectCallableOnce());
7275

73-
$this->writeToStream($input, "foo\n");
76+
fwrite($output, "foo\n");
7477
$this->loop->tick();
7578

7679
$this->loop->removeReadStream($input);
7780

78-
$this->writeToStream($input, "bar\n");
81+
fwrite($output, "bar\n");
7982
$this->loop->tick();
8083
}
8184

8285
public function testRemoveWriteStreamInstantly()
8386
{
84-
$input = $this->createStream();
87+
list ($input) = $this->createSocketPair();
8588

8689
$this->loop->addWriteStream($input, $this->expectCallableNever());
8790
$this->loop->removeWriteStream($input);
@@ -90,7 +93,7 @@ public function testRemoveWriteStreamInstantly()
9093

9194
public function testRemoveWriteStreamAfterWriting()
9295
{
93-
$input = $this->createStream();
96+
list ($input) = $this->createSocketPair();
9497

9598
$this->loop->addWriteStream($input, $this->expectCallableOnce());
9699
$this->loop->tick();
@@ -101,60 +104,60 @@ public function testRemoveWriteStreamAfterWriting()
101104

102105
public function testRemoveStreamInstantly()
103106
{
104-
$input = $this->createStream();
105-
107+
list ($input, $output) = $this->createSocketPair();
108+
106109
$this->loop->addReadStream($input, $this->expectCallableNever());
107110
$this->loop->addWriteStream($input, $this->expectCallableNever());
108111
$this->loop->removeStream($input);
109-
110-
$this->writeToStream($input, "bar\n");
112+
113+
fwrite($output, "bar\n");
111114
$this->loop->tick();
112115
}
113116

114117
public function testRemoveStreamForReadOnly()
115118
{
116-
$input = $this->createStream();
119+
list ($input, $output) = $this->createSocketPair();
117120

118121
$this->loop->addReadStream($input, $this->expectCallableNever());
119-
$this->loop->addWriteStream($input, $this->expectCallableOnce());
122+
$this->loop->addWriteStream($output, $this->expectCallableOnce());
120123
$this->loop->removeReadStream($input);
121124

122-
$this->writeToStream($input, "foo\n");
125+
fwrite($output, "foo\n");
123126
$this->loop->tick();
124127
}
125128

126129
public function testRemoveStreamForWriteOnly()
127130
{
128-
$input = $this->createStream();
131+
list ($input, $output) = $this->createSocketPair();
129132

130-
$this->writeToStream($input, "foo\n");
133+
fwrite($output, "foo\n");
131134

132135
$this->loop->addReadStream($input, $this->expectCallableOnce());
133-
$this->loop->addWriteStream($input, $this->expectCallableNever());
134-
$this->loop->removeWriteStream($input);
136+
$this->loop->addWriteStream($output, $this->expectCallableNever());
137+
$this->loop->removeWriteStream($output);
135138

136139
$this->loop->tick();
137140
}
138141

139142
public function testRemoveStream()
140143
{
141-
$input = $this->createStream();
144+
list ($input, $output) = $this->createSocketPair();
142145

143146
$this->loop->addReadStream($input, $this->expectCallableOnce());
144147
$this->loop->addWriteStream($input, $this->expectCallableOnce());
145148

146-
$this->writeToStream($input, "bar\n");
149+
fwrite($output, "bar\n");
147150
$this->loop->tick();
148151

149152
$this->loop->removeStream($input);
150153

151-
$this->writeToStream($input, "bar\n");
154+
fwrite($output, "bar\n");
152155
$this->loop->tick();
153156
}
154157

155158
public function testRemoveInvalid()
156159
{
157-
$stream = $this->createStream();
160+
list ($stream) = $this->createSocketPair();
158161

159162
// remove a valid stream from the event loop that was never added in the first place
160163
$this->loop->removeReadStream($stream);
@@ -171,29 +174,29 @@ public function emptyRunShouldSimplyReturn()
171174
/** @test */
172175
public function runShouldReturnWhenNoMoreFds()
173176
{
174-
$input = $this->createStream();
177+
list ($input, $output) = $this->createSocketPair();
175178

176179
$loop = $this->loop;
177180
$this->loop->addReadStream($input, function ($stream) use ($loop) {
178181
$loop->removeStream($stream);
179182
});
180183

181-
$this->writeToStream($input, "foo\n");
184+
fwrite($output, "foo\n");
182185

183186
$this->assertRunFasterThan($this->tickTimeout * 2);
184187
}
185188

186189
/** @test */
187190
public function stopShouldStopRunningLoop()
188191
{
189-
$input = $this->createStream();
192+
list ($input, $output) = $this->createSocketPair();
190193

191194
$loop = $this->loop;
192195
$this->loop->addReadStream($input, function ($stream) use ($loop) {
193196
$loop->stop();
194197
});
195198

196-
$this->writeToStream($input, "foo\n");
199+
fwrite($output, "foo\n");
197200

198201
$this->assertRunFasterThan($this->tickTimeout * 2);
199202
}
@@ -219,23 +222,33 @@ function () {
219222
public function testIgnoreRemovedCallback()
220223
{
221224
// two independent streams, both should be readable right away
222-
$stream1 = $this->createStream();
223-
$stream2 = $this->createStream();
225+
list ($input1, $output1) = $this->createSocketPair();
226+
list ($input2, $output2) = $this->createSocketPair();
227+
228+
$called = false;
224229

225230
$loop = $this->loop;
226-
$loop->addReadStream($stream1, function ($stream) use ($loop, $stream2) {
231+
$loop->addReadStream($input1, function ($stream) use (& $called, $loop, $input2) {
227232
// stream1 is readable, remove stream2 as well => this will invalidate its callback
228233
$loop->removeReadStream($stream);
229-
$loop->removeReadStream($stream2);
234+
$loop->removeReadStream($input2);
235+
236+
$called = true;
230237
});
231238

232239
// this callback would have to be called as well, but the first stream already removed us
233-
$loop->addReadStream($stream2, $this->expectCallableNever());
234-
235-
$this->writeToStream($stream1, "foo\n");
236-
$this->writeToStream($stream2, "foo\n");
237-
240+
$loop->addReadStream($input2, function () use (& $called) {
241+
if ($called) {
242+
$this->fail('Callback 2 must not be called after callback 1 was called');
243+
}
244+
});
245+
246+
fwrite($output1, "foo\n");
247+
fwrite($output2, "foo\n");
248+
238249
$loop->run();
250+
251+
$this->assertTrue($called);
239252
}
240253

241254
public function testNextTick()
@@ -258,7 +271,7 @@ public function testNextTick()
258271

259272
public function testNextTickFiresBeforeIO()
260273
{
261-
$stream = $this->createStream();
274+
list ($stream) = $this->createSocketPair();
262275

263276
$this->loop->addWriteStream(
264277
$stream,
@@ -280,7 +293,7 @@ function () {
280293

281294
public function testRecursiveNextTick()
282295
{
283-
$stream = $this->createStream();
296+
list ($stream) = $this->createSocketPair();
284297

285298
$this->loop->addWriteStream(
286299
$stream,
@@ -306,7 +319,7 @@ function () {
306319

307320
public function testRunWaitsForNextTickEvents()
308321
{
309-
$stream = $this->createStream();
322+
list ($stream) = $this->createSocketPair();
310323

311324
$this->loop->addWriteStream(
312325
$stream,
@@ -327,7 +340,7 @@ function () {
327340

328341
public function testNextTickEventGeneratedByFutureTick()
329342
{
330-
$stream = $this->createStream();
343+
list ($stream) = $this->createSocketPair();
331344

332345
$this->loop->futureTick(
333346
function () {
@@ -382,7 +395,7 @@ public function testFutureTick()
382395

383396
public function testFutureTickFiresBeforeIO()
384397
{
385-
$stream = $this->createStream();
398+
list ($stream) = $this->createSocketPair();
386399

387400
$this->loop->addWriteStream(
388401
$stream,
@@ -404,7 +417,7 @@ function () {
404417

405418
public function testRecursiveFutureTick()
406419
{
407-
$stream = $this->createStream();
420+
list ($stream) = $this->createSocketPair();
408421

409422
$this->loop->addWriteStream(
410423
$stream,
@@ -432,7 +445,7 @@ function () {
432445

433446
public function testRunWaitsForFutureTickEvents()
434447
{
435-
$stream = $this->createStream();
448+
list ($stream) = $this->createSocketPair();
436449

437450
$this->loop->addWriteStream(
438451
$stream,
@@ -453,7 +466,7 @@ function () {
453466

454467
public function testFutureTickEventGeneratedByNextTick()
455468
{
456-
$stream = $this->createStream();
469+
list ($stream) = $this->createSocketPair();
457470

458471
$this->loop->nextTick(
459472
function () {

tests/ExtEventLoopTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public function testCanUseReadableStreamWithFeatureFds()
8181

8282
$this->loop->addReadStream($input, $this->expectCallableExactly(2));
8383

84-
$this->writeToStream($input, "foo\n");
84+
fwrite($input, "foo\n");
8585
$this->loop->tick();
8686

87-
$this->writeToStream($input, "bar\n");
87+
fwrite($input, "bar\n");
8888
$this->loop->tick();
8989
}
9090
}

tests/StreamSelectLoopTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testSignalInterruptWithStream($sigName, $signal)
8888
$this->loop->addPeriodicTimer(0.01, function() { pcntl_signal_dispatch(); });
8989

9090
// add stream to the loop
91-
list($writeStream, $readStream) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
91+
list($writeStream, $readStream) = $this->createSocketPair();
9292
$this->loop->addReadStream($readStream, function($stream, $loop) {
9393
/** @var $loop LoopInterface */
9494
$read = fgets($stream);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy