-
Notifications
You must be signed in to change notification settings - Fork 31
/
connect.php
129 lines (104 loc) · 3.66 KB
/
connect.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
<?php
/**
* This file is part of `prooph/event-store-client`.
* (c) 2018-2020 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2018-2020 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ProophTest\EventStoreClient;
use Amp\Deferred;
use Amp\Delayed;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use Amp\TimeoutException;
use Generator;
use Prooph\EventStore\Async\ClientClosedEventArgs;
use Prooph\EventStore\Async\EventStoreConnection;
use Prooph\EventStore\EndPoint;
use Prooph\EventStore\Exception\InvalidOperationException;
use Prooph\EventStore\ExpectedVersion;
use Prooph\EventStoreClient\ConnectionSettings;
use Prooph\EventStoreClient\EventStoreConnectionFactory;
use ProophTest\EventStoreClient\Helper\TestEvent;
class connect extends AsyncTestCase
{
private EndPoint $blackhole;
protected function setUp(): void
{
parent::setUp();
$this->blackhole = new EndPoint('localhost', 9999);
}
/**
* @test
* @doesNotPerformAssertions
*/
public function should_not_throw_exception_when_server_is_down(): Generator
{
$connection = EventStoreConnectionFactory::createFromEndPoint(
$this->blackhole
);
yield $connection->connectAsync();
yield new Delayed(50); // wait for loop to finish
}
/**
* @test
*/
public function should_throw_exception_when_trying_to_reopen_closed_connection(): Generator
{
$closed = new Deferred();
$settings = ConnectionSettings::create()
->limitReconnectionsTo(0)
->withConnectionTimeoutOf(10000)
->setReconnectionDelayTo(0)
->failOnNoServerResponse()
->build();
$connection = EventStoreConnectionFactory::createFromEndPoint(
$this->blackhole,
$settings
);
$connection->onClosed(function () use ($closed): void {
$closed->resolve(true);
});
yield $connection->connectAsync();
try {
yield Promise\timeout($closed->promise(), 120000);
} catch (TimeoutException $e) {
$this->fail('Connection timeout took too long');
}
$this->expectException(InvalidOperationException::class);
yield $connection->connectAsync();
}
/**
* @test
*/
public function should_close_connection_after_configured_amount_of_failed_reconnections(): Generator
{
$closed = new Deferred();
$settings = ConnectionSettings::create()
->limitReconnectionsTo(1)
->withConnectionTimeoutOf(10000)
->setReconnectionDelayTo(0)
->failOnNoServerResponse()
->build();
$connection = EventStoreConnectionFactory::createFromEndPoint(
$this->blackhole,
$settings
);
$connection->onClosed(function (ClientClosedEventArgs $args) use ($closed): void {
$this->assertInstanceOf(EventStoreConnection::class, $args->connection());
$this->assertSame('Reconnection limit reached', $args->reason());
$closed->resolve(true);
});
yield $connection->connectAsync();
try {
yield Promise\timeout($closed->promise(), 120000);
} catch (TimeoutException $e) {
$this->fail('Connection timeout took too long');
}
$this->expectException(InvalidOperationException::class);
yield $connection->appendToStreamAsync('stream', ExpectedVersion::NO_STREAM, [TestEvent::newTestEvent()]);
}
}