@@ -20,33 +20,36 @@ public function setUp()
20
20
21
21
abstract public function createLoop ();
22
22
23
- public function createStream ()
23
+ public function createSocketPair ()
24
24
{
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 );
27
27
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 ;
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 ->tickLoop ($ this ->loop );
42
45
43
- $ this -> writeToStream ( $ input , "bar \n" );
46
+ fwrite ( $ output , "bar \n" );
44
47
$ this ->tickLoop ($ this ->loop );
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 ->tickLoop ($ this ->loop );
@@ -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 ->tickLoop ($ this ->loop );
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 ->tickLoop ($ this ->loop );
75
78
76
79
$ this ->loop ->removeReadStream ($ input );
77
80
78
- $ this -> writeToStream ( $ input , "bar \n" );
81
+ fwrite ( $ output , "bar \n" );
79
82
$ this ->tickLoop ($ this ->loop );
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 ->tickLoop ($ this ->loop );
@@ -101,60 +104,60 @@ public function testRemoveWriteStreamAfterWriting()
101
104
102
105
public function testRemoveStreamInstantly ()
103
106
{
104
- $ input = $ this ->createStream ();
107
+ list ( $ input, $ output ) = $ this ->createSocketPair ();
105
108
106
109
$ this ->loop ->addReadStream ($ input , $ this ->expectCallableNever ());
107
110
$ this ->loop ->addWriteStream ($ input , $ this ->expectCallableNever ());
108
111
$ this ->loop ->removeStream ($ input );
109
112
110
- $ this -> writeToStream ( $ input , "bar \n" );
113
+ fwrite ( $ output , "bar \n" );
111
114
$ this ->tickLoop ($ this ->loop );
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 ->tickLoop ($ this ->loop );
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 ->tickLoop ($ this ->loop );
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 ->tickLoop ($ this ->loop );
148
151
149
152
$ this ->loop ->removeStream ($ input );
150
153
151
- $ this -> writeToStream ( $ input , "bar \n" );
154
+ fwrite ( $ output , "bar \n" );
152
155
$ this ->tickLoop ($ this ->loop );
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 ());
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
+ });
234
245
235
- $ this -> writeToStream ( $ stream1 , "foo \n" );
236
- $ this -> writeToStream ( $ stream2 , "foo \n" );
246
+ fwrite ( $ output1 , "foo \n" );
247
+ fwrite ( $ output2 , "foo \n" );
237
248
238
249
$ loop ->run ();
250
+
251
+ $ this ->assertTrue ($ called );
239
252
}
240
253
241
254
public function testFutureTickEventGeneratedByFutureTick ()
@@ -275,7 +288,7 @@ public function testFutureTick()
275
288
276
289
public function testFutureTickFiresBeforeIO ()
277
290
{
278
- $ stream = $ this ->createStream ();
291
+ list ( $ stream) = $ this ->createSocketPair ();
279
292
280
293
$ this ->loop ->addWriteStream (
281
294
$ stream ,
@@ -297,7 +310,7 @@ function () {
297
310
298
311
public function testRecursiveFutureTick ()
299
312
{
300
- $ stream = $ this ->createStream ();
313
+ list ( $ stream) = $ this ->createSocketPair ();
301
314
302
315
$ this ->loop ->addWriteStream (
303
316
$ stream ,
@@ -325,7 +338,7 @@ function () {
325
338
326
339
public function testRunWaitsForFutureTickEvents ()
327
340
{
328
- $ stream = $ this ->createStream ();
341
+ list ( $ stream) = $ this ->createSocketPair ();
329
342
330
343
$ this ->loop ->addWriteStream (
331
344
$ stream ,
0 commit comments