Skip to content

Commit 71a0fb6

Browse files
committed
tests
1 parent e7f77fd commit 71a0fb6

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

src/Servers/Reverb/Publishing/RedisClient.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function reconnect(): void
9696
if ($this->clientReconnectionTimer >= $this->reconnectionTimeout()) {
9797
Log::error("Failed to reconnect to Redis connection [{$this->name}] within {$this->reconnectionTimeout()} second limit");
9898

99-
exit;
99+
throw new Exception("Failed to reconnect to Redis connection [{$this->name}] within {$this->reconnectionTimeout()} second limit");
100100
}
101101
Log::info("Attempting to reconnect Redis connection [{$this->name}]");
102102
$this->connect();
@@ -170,7 +170,9 @@ protected function configureClientErrorHandler(): void
170170
{
171171
$this->client->on('close', function () {
172172
$this->client = null;
173-
Log::info("Disconnected fromRedis connection [{$this->name}]");
173+
174+
Log::info("Disconnected from Redis connection [{$this->name}]");
175+
174176
$this->reconnect();
175177
});
176178
}

tests/Unit/Servers/Reverb/Publishing/RedisPubSubProviderTest.php

+74-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Laravel\Reverb\Servers\Reverb\Contracts\PubSubIncomingMessageHandler;
55
use Laravel\Reverb\Servers\Reverb\Publishing\RedisClientFactory;
66
use Laravel\Reverb\Servers\Reverb\Publishing\RedisPubSubProvider;
7+
use React\EventLoop\Loop;
78
use React\EventLoop\LoopInterface;
89
use React\Promise\Promise;
910

@@ -45,15 +46,84 @@
4546
$provider->connect(Mockery::mock(LoopInterface::class));
4647
});
4748

48-
it('can successfully reconnect', function () {})->todo();
49+
it('can successfully reconnect', function () {
50+
$clientFactory = Mockery::mock(RedisClientFactory::class);
51+
$loop = Mockery::mock(LoopInterface::class);
52+
53+
$loop->shouldReceive('addTimer')
54+
->once()
55+
->with(1, Mockery::any());
56+
57+
// Publisher client
58+
$clientFactory->shouldReceive('make')
59+
->once()
60+
->andReturn(new Promise(fn () => throw new Exception));
61+
62+
// Subscriber client
63+
$clientFactory->shouldReceive('make')
64+
->once()
65+
->andReturn(new Promise(fn (callable $resolve) => $resolve));
66+
67+
$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb');
68+
$provider->connect($loop);
69+
});
70+
71+
it('can timeout and fail when unable to reconnect', function () {
72+
$clientFactory = Mockery::mock(RedisClientFactory::class);
73+
$loop = Loop::get();
74+
75+
// Publisher client
76+
$clientFactory->shouldReceive('make')
77+
->once()
78+
->andReturn(new Promise(fn () => throw new Exception));
79+
80+
// Subscriber client
81+
$clientFactory->shouldReceive('make')
82+
->once()
83+
->andReturn(new Promise(fn (callable $resolve) => $resolve));
4984

50-
it('can timeout and fail when unable to reconnect', function () {})->todo();
85+
$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb', ['host' => 'localhost', 'port' => 6379, 'timeout' => 1]);
86+
$provider->connect($loop);
5187

52-
it('queues subscription events', function () {})->todo();
88+
$loop->run();
89+
})->throws(Exception::class, 'Failed to reconnect to Redis connection [publisher] within 1 second limit');
90+
91+
it('queues subscription events', function () {
92+
$clientFactory = Mockery::mock(RedisClientFactory::class);
93+
94+
$clientFactory->shouldReceive('make')
95+
->twice()
96+
->andReturn(new Promise(fn (callable $resolve) => $resolve));
97+
98+
$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb');
99+
$provider->connect(Mockery::mock(LoopInterface::class));
100+
$provider->subscribe();
101+
102+
$subscribingClient = (new ReflectionProperty($provider, 'subscribingClient'))->getValue($provider);
103+
$queuedSubscriptionEvents = (new ReflectionProperty($subscribingClient, 'queuedSubscriptionEvents'))->getValue($subscribingClient);
104+
105+
expect(array_keys($queuedSubscriptionEvents))->toBe(['subscribe', 'on']);
106+
});
53107

54108
it('can process queued subscription events', function () {})->todo();
55109

56-
it('queues publish events', function () {})->todo();
110+
it('queues publish events', function () {
111+
$clientFactory = Mockery::mock(RedisClientFactory::class);
112+
113+
$clientFactory->shouldReceive('make')
114+
->twice()
115+
->andReturn(new Promise(fn (callable $resolve) => $resolve));
116+
117+
$provider = new RedisPubSubProvider($clientFactory, Mockery::mock(PubSubIncomingMessageHandler::class), 'reverb');
118+
$provider->connect(Mockery::mock(LoopInterface::class));
119+
$provider->publish(['event' => 'first test']);
120+
$provider->publish(['event' => 'second test']);
121+
122+
$publishingClient = (new ReflectionProperty($provider, 'publishingClient'))->getValue($provider);
123+
$queuedPublishEvents = (new ReflectionProperty($publishingClient, 'queuedPublishEvents'))->getValue($publishingClient);
124+
125+
expect($queuedPublishEvents)->toBe([['event' => 'first test'], ['event' => 'second test']]);
126+
});
57127

58128
it('can process queued publish events', function () {})->todo();
59129

0 commit comments

Comments
 (0)