@@ -114,6 +114,143 @@ public function testConnectingFailsIfDnsUsesInvalidResolver()
114
114
Block \await ($ connector ->connect ('google.com:80 ' ), $ loop , self ::TIMEOUT );
115
115
}
116
116
117
+ public function testCancellingPendingConnectionShouldNotCreateAnyGarbageReferences ()
118
+ {
119
+ if (class_exists ('React\Promise\When ' )) {
120
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
121
+ }
122
+
123
+ $ loop = Factory::create ();
124
+ $ connector = new Connector ($ loop , array ('timeout ' => false ));
125
+
126
+ gc_collect_cycles ();
127
+ $ promise = $ connector ->connect ('8.8.8.8:80 ' );
128
+ $ promise ->cancel ();
129
+ unset($ promise );
130
+
131
+ $ this ->assertEquals (0 , gc_collect_cycles ());
132
+ }
133
+
134
+ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferences ()
135
+ {
136
+ if (class_exists ('React\Promise\When ' )) {
137
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
138
+ }
139
+
140
+ $ loop = Factory::create ();
141
+ $ connector = new Connector ($ loop , array ('timeout ' => false ));
142
+
143
+ gc_collect_cycles ();
144
+
145
+ $ wait = true ;
146
+ $ promise = $ connector ->connect ('127.0.0.1:1 ' )->then (
147
+ null ,
148
+ function ($ e ) use (&$ wait ) {
149
+ $ wait = false ;
150
+ throw $ e ;
151
+ }
152
+ );
153
+
154
+ // run loop for short period to ensure we detect connection refused error
155
+ Block \sleep (0.01 , $ loop );
156
+ if ($ wait ) {
157
+ Block \sleep (0.2 , $ loop );
158
+ if ($ wait ) {
159
+ $ this ->fail ('Connection attempt did not fail ' );
160
+ }
161
+ }
162
+ unset($ promise );
163
+
164
+ $ this ->assertEquals (0 , gc_collect_cycles ());
165
+ }
166
+
167
+ public function testWaitingForConnectionTimeoutShouldNotCreateAnyGarbageReferences ()
168
+ {
169
+ if (class_exists ('React\Promise\When ' )) {
170
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
171
+ }
172
+
173
+ $ loop = Factory::create ();
174
+ $ connector = new Connector ($ loop , array ('timeout ' => 0.001 ));
175
+
176
+ gc_collect_cycles ();
177
+
178
+ $ wait = true ;
179
+ $ promise = $ connector ->connect ('google.com:80 ' )->then (
180
+ null ,
181
+ function ($ e ) use (&$ wait ) {
182
+ $ wait = false ;
183
+ throw $ e ;
184
+ }
185
+ );
186
+
187
+ // run loop for short period to ensure we detect connection timeout error
188
+ Block \sleep (0.01 , $ loop );
189
+ if ($ wait ) {
190
+ Block \sleep (0.2 , $ loop );
191
+ if ($ wait ) {
192
+ $ this ->fail ('Connection attempt did not fail ' );
193
+ }
194
+ }
195
+ unset($ promise );
196
+
197
+ $ this ->assertEquals (0 , gc_collect_cycles ());
198
+ }
199
+
200
+ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageReferences ()
201
+ {
202
+ if (class_exists ('React\Promise\When ' )) {
203
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
204
+ }
205
+
206
+ $ loop = Factory::create ();
207
+ $ connector = new Connector ($ loop , array ('timeout ' => false ));
208
+
209
+ gc_collect_cycles ();
210
+
211
+ $ wait = true ;
212
+ $ promise = $ connector ->connect ('example.invalid:80 ' )->then (
213
+ null ,
214
+ function ($ e ) use (&$ wait ) {
215
+ $ wait = false ;
216
+ throw $ e ;
217
+ }
218
+ );
219
+
220
+ // run loop for short period to ensure we detect DNS error
221
+ Block \sleep (0.01 , $ loop );
222
+ if ($ wait ) {
223
+ Block \sleep (0.2 , $ loop );
224
+ if ($ wait ) {
225
+ $ this ->fail ('Connection attempt did not fail ' );
226
+ }
227
+ }
228
+ unset($ promise );
229
+
230
+ $ this ->assertEquals (0 , gc_collect_cycles ());
231
+ }
232
+
233
+ public function testWaitingForSuccessfullyClosedConnectionShouldNotCreateAnyGarbageReferences ()
234
+ {
235
+ if (class_exists ('React\Promise\When ' )) {
236
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
237
+ }
238
+
239
+ $ loop = Factory::create ();
240
+ $ connector = new Connector ($ loop , array ('timeout ' => false ));
241
+
242
+ gc_collect_cycles ();
243
+ $ promise = $ connector ->connect ('google.com:80 ' )->then (
244
+ function ($ conn ) {
245
+ $ conn ->close ();
246
+ }
247
+ );
248
+ Block \await ($ promise , $ loop , self ::TIMEOUT );
249
+ unset($ promise );
250
+
251
+ $ this ->assertEquals (0 , gc_collect_cycles ());
252
+ }
253
+
117
254
public function testConnectingFailsIfTimeoutIsTooSmall ()
118
255
{
119
256
if (!function_exists ('stream_socket_enable_crypto ' )) {
0 commit comments