Skip to content

Commit 204ed4f

Browse files
authored
Merge pull request #81 from jsor/update-master-from-0.4
Update master from 0.4
2 parents 7f86af9 + 473c25a commit 204ed4f

File tree

4 files changed

+70
-58
lines changed

4 files changed

+70
-58
lines changed

src/ExtEventLoop.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ public function run()
175175
break;
176176
}
177177

178-
// @-suppression: https://github.com/reactphp/react/pull/234#discussion-diff-7759616R226
179-
@$this->eventBase->loop($flags);
178+
$this->eventBase->loop($flags);
180179
}
181180
}
182181

tests/AbstractLoopTest.php

Lines changed: 58 additions & 45 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()
23+
public function createSocketPair()
2424
{
25-
return fopen('php://temp', 'r+');
26-
}
25+
$domain = (DIRECTORY_SEPARATOR === '\\') ? STREAM_PF_INET : STREAM_PF_UNIX;
26+
$sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
2727

28-
public function writeToStream($stream, $content)
29-
{
30-
fwrite($stream, $content);
31-
rewind($stream);
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->tickLoop($this->loop);
4245

43-
$this->writeToStream($input, "bar\n");
46+
fwrite($output, "bar\n");
4447
$this->tickLoop($this->loop);
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->tickLoop($this->loop);
@@ -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->tickLoop($this->loop);
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->tickLoop($this->loop);
7578

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

78-
$this->writeToStream($input, "bar\n");
81+
fwrite($output, "bar\n");
7982
$this->tickLoop($this->loop);
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->tickLoop($this->loop);
@@ -101,60 +104,60 @@ public function testRemoveWriteStreamAfterWriting()
101104

102105
public function testRemoveStreamInstantly()
103106
{
104-
$input = $this->createStream();
107+
list ($input, $output) = $this->createSocketPair();
105108

106109
$this->loop->addReadStream($input, $this->expectCallableNever());
107110
$this->loop->addWriteStream($input, $this->expectCallableNever());
108111
$this->loop->removeStream($input);
109112

110-
$this->writeToStream($input, "bar\n");
113+
fwrite($output, "bar\n");
111114
$this->tickLoop($this->loop);
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->tickLoop($this->loop);
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->tickLoop($this->loop);
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->tickLoop($this->loop);
148151

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

151-
$this->writeToStream($input, "bar\n");
154+
fwrite($output, "bar\n");
152155
$this->tickLoop($this->loop);
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());
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+
});
234245

235-
$this->writeToStream($stream1, "foo\n");
236-
$this->writeToStream($stream2, "foo\n");
246+
fwrite($output1, "foo\n");
247+
fwrite($output2, "foo\n");
237248

238249
$loop->run();
250+
251+
$this->assertTrue($called);
239252
}
240253

241254
public function testFutureTickEventGeneratedByFutureTick()
@@ -275,7 +288,7 @@ public function testFutureTick()
275288

276289
public function testFutureTickFiresBeforeIO()
277290
{
278-
$stream = $this->createStream();
291+
list ($stream) = $this->createSocketPair();
279292

280293
$this->loop->addWriteStream(
281294
$stream,
@@ -297,7 +310,7 @@ function () {
297310

298311
public function testRecursiveFutureTick()
299312
{
300-
$stream = $this->createStream();
313+
list ($stream) = $this->createSocketPair();
301314

302315
$this->loop->addWriteStream(
303316
$stream,
@@ -325,7 +338,7 @@ function () {
325338

326339
public function testRunWaitsForFutureTickEvents()
327340
{
328-
$stream = $this->createStream();
341+
list ($stream) = $this->createSocketPair();
329342

330343
$this->loop->addWriteStream(
331344
$stream,

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->tickLoop($this->loop);
8686

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

tests/StreamSelectLoopTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function testStreamSelectTimeoutEmulation()
4040
public function signalProvider()
4141
{
4242
return [
43-
['SIGUSR1', SIGUSR1],
44-
['SIGHUP', SIGHUP],
45-
['SIGTERM', SIGTERM],
43+
['SIGUSR1'],
44+
['SIGHUP'],
45+
['SIGTERM'],
4646
];
4747
}
4848

@@ -52,7 +52,7 @@ public function signalProvider()
5252
* Test signal interrupt when no stream is attached to the loop
5353
* @dataProvider signalProvider
5454
*/
55-
public function testSignalInterruptNoStream($sigName, $signal)
55+
public function testSignalInterruptNoStream($signal)
5656
{
5757
if (!extension_loaded('pcntl')) {
5858
$this->markTestSkipped('"pcntl" extension is required to run this test.');
@@ -78,7 +78,7 @@ public function testSignalInterruptNoStream($sigName, $signal)
7878
* Test signal interrupt when a stream is attached to the loop
7979
* @dataProvider signalProvider
8080
*/
81-
public function testSignalInterruptWithStream($sigName, $signal)
81+
public function testSignalInterruptWithStream($signal)
8282
{
8383
if (!extension_loaded('pcntl')) {
8484
$this->markTestSkipped('"pcntl" extension is required to run this test.');
@@ -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);
@@ -116,7 +116,7 @@ public function testSignalInterruptWithStream($sigName, $signal)
116116
protected function setUpSignalHandler($signal)
117117
{
118118
$this->_signalHandled = false;
119-
$this->assertTrue(pcntl_signal($signal, function() { $this->_signalHandled = true; }));
119+
$this->assertTrue(pcntl_signal(constant($signal), function() { $this->_signalHandled = true; }));
120120
}
121121

122122
/**
@@ -125,7 +125,7 @@ protected function setUpSignalHandler($signal)
125125
protected function resetSignalHandlers()
126126
{
127127
foreach($this->signalProvider() as $signal) {
128-
pcntl_signal($signal[1], SIG_DFL);
128+
pcntl_signal(constant($signal[0]), SIG_DFL);
129129
}
130130
}
131131

@@ -141,7 +141,7 @@ protected function forkSendSignal($signal)
141141
} else if ($childPid === 0) {
142142
// this is executed in the child process
143143
usleep(20000);
144-
posix_kill($currentPid, $signal);
144+
posix_kill($currentPid, constant($signal));
145145
die();
146146
}
147147
}

0 commit comments

Comments
 (0)