@@ -20,33 +20,36 @@ public function setUp()
20
20
21
21
abstract public function createLoop ();
22
22
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 ;
32
35
}
33
36
34
37
public function testAddReadStream ()
35
38
{
36
- $ input = $ this ->createStream ();
39
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
37
40
38
41
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableExactly (2 ));
39
42
40
- $ this -> writeToStream ( $ input , "foo \n" );
43
+ fwrite ( $ output , "foo \n" );
41
44
$ this ->loop ->tick ();
42
45
43
- $ this -> writeToStream ( $ input , "bar \n" );
46
+ fwrite ( $ output , "bar \n" );
44
47
$ this ->loop ->tick ();
45
48
}
46
49
47
50
public function testAddWriteStream ()
48
51
{
49
- $ input = $ this ->createStream ();
52
+ list ( $ input) = $ this ->createSocketPair ();
50
53
51
54
$ this ->loop ->addWriteStream ($ input , $ this ->expectCallableExactly (2 ));
52
55
$ this ->loop ->tick ();
@@ -55,33 +58,33 @@ public function testAddWriteStream()
55
58
56
59
public function testRemoveReadStreamInstantly ()
57
60
{
58
- $ input = $ this ->createStream ();
61
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
59
62
60
63
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableNever ());
61
64
$ this ->loop ->removeReadStream ($ input );
62
65
63
- $ this -> writeToStream ( $ input , "bar \n" );
66
+ fwrite ( $ output , "bar \n" );
64
67
$ this ->loop ->tick ();
65
68
}
66
69
67
70
public function testRemoveReadStreamAfterReading ()
68
71
{
69
- $ input = $ this ->createStream ();
72
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
70
73
71
74
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableOnce ());
72
75
73
- $ this -> writeToStream ( $ input , "foo \n" );
76
+ fwrite ( $ output , "foo \n" );
74
77
$ this ->loop ->tick ();
75
78
76
79
$ this ->loop ->removeReadStream ($ input );
77
80
78
- $ this -> writeToStream ( $ input , "bar \n" );
81
+ fwrite ( $ output , "bar \n" );
79
82
$ this ->loop ->tick ();
80
83
}
81
84
82
85
public function testRemoveWriteStreamInstantly ()
83
86
{
84
- $ input = $ this ->createStream ();
87
+ list ( $ input) = $ this ->createSocketPair ();
85
88
86
89
$ this ->loop ->addWriteStream ($ input , $ this ->expectCallableNever ());
87
90
$ this ->loop ->removeWriteStream ($ input );
@@ -90,7 +93,7 @@ public function testRemoveWriteStreamInstantly()
90
93
91
94
public function testRemoveWriteStreamAfterWriting ()
92
95
{
93
- $ input = $ this ->createStream ();
96
+ list ( $ input) = $ this ->createSocketPair ();
94
97
95
98
$ this ->loop ->addWriteStream ($ input , $ this ->expectCallableOnce ());
96
99
$ this ->loop ->tick ();
@@ -101,60 +104,60 @@ public function testRemoveWriteStreamAfterWriting()
101
104
102
105
public function testRemoveStreamInstantly ()
103
106
{
104
- $ input = $ this ->createStream ();
105
-
107
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
108
+
106
109
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableNever ());
107
110
$ this ->loop ->addWriteStream ($ input , $ this ->expectCallableNever ());
108
111
$ this ->loop ->removeStream ($ input );
109
-
110
- $ this -> writeToStream ( $ input , "bar \n" );
112
+
113
+ fwrite ( $ output , "bar \n" );
111
114
$ this ->loop ->tick ();
112
115
}
113
116
114
117
public function testRemoveStreamForReadOnly ()
115
118
{
116
- $ input = $ this ->createStream ();
119
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
117
120
118
121
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableNever ());
119
- $ this ->loop ->addWriteStream ($ input , $ this ->expectCallableOnce ());
122
+ $ this ->loop ->addWriteStream ($ output , $ this ->expectCallableOnce ());
120
123
$ this ->loop ->removeReadStream ($ input );
121
124
122
- $ this -> writeToStream ( $ input , "foo \n" );
125
+ fwrite ( $ output , "foo \n" );
123
126
$ this ->loop ->tick ();
124
127
}
125
128
126
129
public function testRemoveStreamForWriteOnly ()
127
130
{
128
- $ input = $ this ->createStream ();
131
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
129
132
130
- $ this -> writeToStream ( $ input , "foo \n" );
133
+ fwrite ( $ output , "foo \n" );
131
134
132
135
$ 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 );
135
138
136
139
$ this ->loop ->tick ();
137
140
}
138
141
139
142
public function testRemoveStream ()
140
143
{
141
- $ input = $ this ->createStream ();
144
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
142
145
143
146
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableOnce ());
144
147
$ this ->loop ->addWriteStream ($ input , $ this ->expectCallableOnce ());
145
148
146
- $ this -> writeToStream ( $ input , "bar \n" );
149
+ fwrite ( $ output , "bar \n" );
147
150
$ this ->loop ->tick ();
148
151
149
152
$ this ->loop ->removeStream ($ input );
150
153
151
- $ this -> writeToStream ( $ input , "bar \n" );
154
+ fwrite ( $ output , "bar \n" );
152
155
$ this ->loop ->tick ();
153
156
}
154
157
155
158
public function testRemoveInvalid ()
156
159
{
157
- $ stream = $ this ->createStream ();
160
+ list ( $ stream) = $ this ->createSocketPair ();
158
161
159
162
// remove a valid stream from the event loop that was never added in the first place
160
163
$ this ->loop ->removeReadStream ($ stream );
@@ -171,29 +174,29 @@ public function emptyRunShouldSimplyReturn()
171
174
/** @test */
172
175
public function runShouldReturnWhenNoMoreFds ()
173
176
{
174
- $ input = $ this ->createStream ();
177
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
175
178
176
179
$ loop = $ this ->loop ;
177
180
$ this ->loop ->addReadStream ($ input , function ($ stream ) use ($ loop ) {
178
181
$ loop ->removeStream ($ stream );
179
182
});
180
183
181
- $ this -> writeToStream ( $ input , "foo \n" );
184
+ fwrite ( $ output , "foo \n" );
182
185
183
186
$ this ->assertRunFasterThan ($ this ->tickTimeout * 2 );
184
187
}
185
188
186
189
/** @test */
187
190
public function stopShouldStopRunningLoop ()
188
191
{
189
- $ input = $ this ->createStream ();
192
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
190
193
191
194
$ loop = $ this ->loop ;
192
195
$ this ->loop ->addReadStream ($ input , function ($ stream ) use ($ loop ) {
193
196
$ loop ->stop ();
194
197
});
195
198
196
- $ this -> writeToStream ( $ input , "foo \n" );
199
+ fwrite ( $ output , "foo \n" );
197
200
198
201
$ this ->assertRunFasterThan ($ this ->tickTimeout * 2 );
199
202
}
@@ -219,23 +222,33 @@ function () {
219
222
public function testIgnoreRemovedCallback ()
220
223
{
221
224
// 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 ;
224
229
225
230
$ loop = $ this ->loop ;
226
- $ loop ->addReadStream ($ stream1 , function ($ stream ) use ($ loop , $ stream2 ) {
231
+ $ loop ->addReadStream ($ input1 , function ($ stream ) use (& $ called , $ loop , $ input2 ) {
227
232
// stream1 is readable, remove stream2 as well => this will invalidate its callback
228
233
$ loop ->removeReadStream ($ stream );
229
- $ loop ->removeReadStream ($ stream2 );
234
+ $ loop ->removeReadStream ($ input2 );
235
+
236
+ $ called = true ;
230
237
});
231
238
232
239
// 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
+
238
249
$ loop ->run ();
250
+
251
+ $ this ->assertTrue ($ called );
239
252
}
240
253
241
254
public function testNextTick ()
@@ -258,7 +271,7 @@ public function testNextTick()
258
271
259
272
public function testNextTickFiresBeforeIO ()
260
273
{
261
- $ stream = $ this ->createStream ();
274
+ list ( $ stream) = $ this ->createSocketPair ();
262
275
263
276
$ this ->loop ->addWriteStream (
264
277
$ stream ,
@@ -280,7 +293,7 @@ function () {
280
293
281
294
public function testRecursiveNextTick ()
282
295
{
283
- $ stream = $ this ->createStream ();
296
+ list ( $ stream) = $ this ->createSocketPair ();
284
297
285
298
$ this ->loop ->addWriteStream (
286
299
$ stream ,
@@ -306,7 +319,7 @@ function () {
306
319
307
320
public function testRunWaitsForNextTickEvents ()
308
321
{
309
- $ stream = $ this ->createStream ();
322
+ list ( $ stream) = $ this ->createSocketPair ();
310
323
311
324
$ this ->loop ->addWriteStream (
312
325
$ stream ,
@@ -327,7 +340,7 @@ function () {
327
340
328
341
public function testNextTickEventGeneratedByFutureTick ()
329
342
{
330
- $ stream = $ this ->createStream ();
343
+ list ( $ stream) = $ this ->createSocketPair ();
331
344
332
345
$ this ->loop ->futureTick (
333
346
function () {
@@ -382,7 +395,7 @@ public function testFutureTick()
382
395
383
396
public function testFutureTickFiresBeforeIO ()
384
397
{
385
- $ stream = $ this ->createStream ();
398
+ list ( $ stream) = $ this ->createSocketPair ();
386
399
387
400
$ this ->loop ->addWriteStream (
388
401
$ stream ,
@@ -404,7 +417,7 @@ function () {
404
417
405
418
public function testRecursiveFutureTick ()
406
419
{
407
- $ stream = $ this ->createStream ();
420
+ list ( $ stream) = $ this ->createSocketPair ();
408
421
409
422
$ this ->loop ->addWriteStream (
410
423
$ stream ,
@@ -432,7 +445,7 @@ function () {
432
445
433
446
public function testRunWaitsForFutureTickEvents ()
434
447
{
435
- $ stream = $ this ->createStream ();
448
+ list ( $ stream) = $ this ->createSocketPair ();
436
449
437
450
$ this ->loop ->addWriteStream (
438
451
$ stream ,
@@ -453,7 +466,7 @@ function () {
453
466
454
467
public function testFutureTickEventGeneratedByNextTick ()
455
468
{
456
- $ stream = $ this ->createStream ();
469
+ list ( $ stream) = $ this ->createSocketPair ();
457
470
458
471
$ this ->loop ->nextTick (
459
472
function () {
0 commit comments