|
4 | 4 | use Laravel\Reverb\Servers\Reverb\Contracts\PubSubIncomingMessageHandler;
|
5 | 5 | use Laravel\Reverb\Servers\Reverb\Publishing\RedisClientFactory;
|
6 | 6 | use Laravel\Reverb\Servers\Reverb\Publishing\RedisPubSubProvider;
|
| 7 | +use React\EventLoop\Loop; |
7 | 8 | use React\EventLoop\LoopInterface;
|
8 | 9 | use React\Promise\Promise;
|
9 | 10 |
|
|
45 | 46 | $provider->connect(Mockery::mock(LoopInterface::class));
|
46 | 47 | });
|
47 | 48 |
|
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)); |
49 | 84 |
|
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); |
51 | 87 |
|
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 | +}); |
53 | 107 |
|
54 | 108 | it('can process queued subscription events', function () {})->todo();
|
55 | 109 |
|
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 | +}); |
57 | 127 |
|
58 | 128 | it('can process queued publish events', function () {})->todo();
|
59 | 129 |
|
|
0 commit comments