2
2
3
3
namespace React \Tests \Socket ;
4
4
5
+ use Clue \React \Block ;
5
6
use React \Socket \TimeoutConnector ;
6
7
use React \Promise ;
7
8
use React \EventLoop \Factory ;
9
+ use React \Promise \Deferred ;
8
10
9
11
class TimeoutConnectorTest extends TestCase
10
12
{
11
- public function testRejectsOnTimeout ()
13
+ /**
14
+ * @expectedException RuntimeException
15
+ * @expectedExceptionMessage Connection to google.com:80 timed out after 0.01 seconds
16
+ */
17
+ public function testRejectsWithTimeoutReasonOnTimeout ()
12
18
{
13
19
$ promise = new Promise \Promise (function () { });
14
20
@@ -19,17 +25,16 @@ public function testRejectsOnTimeout()
19
25
20
26
$ timeout = new TimeoutConnector ($ connector , 0.01 , $ loop );
21
27
22
- $ timeout ->connect ('google.com:80 ' )->then (
23
- $ this ->expectCallableNever (),
24
- $ this ->expectCallableOnce ()
25
- );
26
-
27
- $ loop ->run ();
28
+ Block \await ($ timeout ->connect ('google.com:80 ' ), $ loop );
28
29
}
29
30
30
- public function testRejectsWhenConnectorRejects ()
31
+ /**
32
+ * @expectedException RuntimeException
33
+ * @expectedExceptionMessage Failed
34
+ */
35
+ public function testRejectsWithOriginalReasonWhenConnectorRejects ()
31
36
{
32
- $ promise = Promise \reject (new \RuntimeException ());
37
+ $ promise = Promise \reject (new \RuntimeException (' Failed ' ));
33
38
34
39
$ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
35
40
$ connector ->expects ($ this ->once ())->method ('connect ' )->with ('google.com:80 ' )->will ($ this ->returnValue ($ promise ));
@@ -38,12 +43,7 @@ public function testRejectsWhenConnectorRejects()
38
43
39
44
$ timeout = new TimeoutConnector ($ connector , 5.0 , $ loop );
40
45
41
- $ timeout ->connect ('google.com:80 ' )->then (
42
- $ this ->expectCallableNever (),
43
- $ this ->expectCallableOnce ()
44
- );
45
-
46
- $ loop ->run ();
46
+ Block \await ($ timeout ->connect ('google.com:80 ' ), $ loop );
47
47
}
48
48
49
49
public function testResolvesWhenConnectorResolves ()
@@ -100,4 +100,51 @@ public function testCancelsPendingPromiseOnCancel()
100
100
101
101
$ out ->then ($ this ->expectCallableNever (), $ this ->expectCallableOnce ());
102
102
}
103
+
104
+ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences ()
105
+ {
106
+ if (class_exists ('React\Promise\When ' )) {
107
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
108
+ }
109
+
110
+ gc_collect_cycles ();
111
+
112
+ $ connection = new Deferred ();
113
+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
114
+ $ connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:80 ' )->willReturn ($ connection ->promise ());
115
+
116
+ $ loop = Factory::create ();
117
+ $ timeout = new TimeoutConnector ($ connector , 0.01 , $ loop );
118
+
119
+ $ promise = $ timeout ->connect ('example.com:80 ' );
120
+ $ connection ->reject (new \RuntimeException ('Connection failed ' ));
121
+ unset($ promise , $ connection );
122
+
123
+ $ this ->assertEquals (0 , gc_collect_cycles ());
124
+ }
125
+
126
+ public function testRejectionDueToTimeoutShouldNotCreateAnyGarbageReferences ()
127
+ {
128
+ if (class_exists ('React\Promise\When ' )) {
129
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
130
+ }
131
+
132
+ gc_collect_cycles ();
133
+
134
+ $ connection = new Deferred (function () {
135
+ throw new \RuntimeException ('Connection cancelled ' );
136
+ });
137
+ $ connector = $ this ->getMockBuilder ('React\Socket\ConnectorInterface ' )->getMock ();
138
+ $ connector ->expects ($ this ->once ())->method ('connect ' )->with ('example.com:80 ' )->willReturn ($ connection ->promise ());
139
+
140
+ $ loop = Factory::create ();
141
+ $ timeout = new TimeoutConnector ($ connector , 0 , $ loop );
142
+
143
+ $ promise = $ timeout ->connect ('example.com:80 ' );
144
+
145
+ $ loop ->run ();
146
+ unset($ promise , $ connection );
147
+
148
+ $ this ->assertEquals (0 , gc_collect_cycles ());
149
+ }
103
150
}
0 commit comments