forked from clue/reactphp-connection-manager-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionManagerTimeoutTest.php
143 lines (105 loc) · 4.96 KB
/
ConnectionManagerTimeoutTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace ConnectionManager\Tests\Extra;
use ConnectionManager\Extra\ConnectionManagerReject;
use ConnectionManager\Extra\ConnectionManagerTimeout;
use React\Promise\Deferred;
use React\Promise\Promise;
class ConnectionManagerTimeoutTest extends TestCase
{
private $loop;
/**
* @before
*/
public function setUpLoop()
{
$this->loop = \React\EventLoop\Loop::get();
}
public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$unused = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$cm = new ConnectionManagerTimeout($unused, 0);
$ref = new \ReflectionProperty($cm, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($cm);
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}
public function testContructorThrowsExceptionForInvalidLoop()
{
$unused = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
new ConnectionManagerTimeout($unused, 0, 'loop');
}
public function testTimeoutOkay()
{
$will = $this->createConnectionManagerMock(true);
$cm = new ConnectionManagerTimeout($will, 0.1, $this->loop);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
$this->loop->run();
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
}
public function testTimeoutWillRejectPromiseWhenConnectorExceedsTimeLimit()
{
$connectionPromise = new Promise(function(){});
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('connect')->willReturn($connectionPromise);
$cm = new ConnectionManagerTimeout($connector, 0.1, $this->loop);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
$this->loop->run();
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
}
public function testTimeoutWillEndConnectiontWhenConnectorResolvesAfterTimeoutFired()
{
$connectionDeferred = new Deferred();
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('connect')->willReturn($connectionDeferred->promise());
$cm = new ConnectionManagerTimeout($connector, 0.1, $this->loop);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
$this->loop->run();
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
$connection = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$connection->expects($this->once())->method('end');
$connectionDeferred->resolve($connection);
}
public function testTimeoutAbort()
{
$wont = new ConnectionManagerReject();
$cm = new ConnectionManagerTimeout($wont, 0.1, $this->loop);
$promise = $cm->connect('www.google.com:80');
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
$this->loop->run();
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
}
public function testWillEndConnectionIfConnectionResolvesDespiteTimeout()
{
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();
$stream->expects($this->once())->method('end');
$loop = $this->loop;
$promise = new Promise(function ($resolve) use ($loop, $stream) {
$loop->addTimer(0.01, function () use ($resolve, $stream) {
$resolve($stream);
});
});
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('connect')->with('www.google.com:80')->willReturn($promise);
$cm = new ConnectionManagerTimeout($connector, 0.001, $this->loop);
$promise = $cm->connect('www.google.com:80');
$this->loop->run();
$this->assertPromiseReject($promise);
}
public function testCancellationOfPromiseWillCancelConnectionAttempt()
{
$promise = new Promise(function () {}, function () {
throw new \RuntimeException();
});
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$connector->expects($this->once())->method('connect')->with('www.google.com:80')->willReturn($promise);
$cm = new ConnectionManagerTimeout($connector, 5.0, $this->loop);
$promise = $cm->connect('www.google.com:80');
$promise->cancel();
$this->loop->run();
$this->assertPromiseReject($promise);
}
}