Skip to content

Commit f797690

Browse files
authored
Merge pull request #10 from MammatusPHP/no-need-to-double-tap-the-context-close
No need to double tap context close
2 parents b14a920 + 8d4241b commit f797690

File tree

7 files changed

+91
-94
lines changed

7 files changed

+91
-94
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"react/async": "^4.3",
2323
"react/event-loop": "^1.5",
2424
"react/promise": "^3.2",
25+
"react/promise-timer": "^1.11",
2526
"roave/better-reflection": "^6.26",
2627
"wyrihaximus/broadcast-contracts": "^1.3",
2728
"wyrihaximus/generative-composer-plugin-tooling": "^1",

composer.lock

Lines changed: 80 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Consumer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function React\Async\async;
2121
use function React\Async\await;
2222
use function React\Promise\all;
23+
use function React\Promise\Timer\sleep;
2324
use function WyriHaximus\React\futurePromise;
2425

2526
final class Consumer extends AbstractList implements Listener
@@ -37,8 +38,6 @@ public function __construct(
3738
public function close(): void
3839
{
3940
$this->running = false;
40-
41-
$this->context->close();
4241
}
4342

4443
/** @return PromiseInterface<mixed> */
@@ -49,7 +48,9 @@ public function setupConsumer(Worker $worker): PromiseInterface
4948
throw new RuntimeException('Worker instance must be instance of ' . WorkerContract::class);
5049
}
5150

52-
$promises = [];
51+
$promises = [
52+
sleep(0.1),
53+
];
5354
for ($i = 0; $i < $worker->concurrency; $i++) {
5455
$this->logger->info('Starting consumer ' . $i . ' of ' . $worker->concurrency . ' for ' . $worker->class);
5556
$promises[] = async(fn () => $this->consume($worker, $workerInstance))();

src/LifeCycle/CloseContext.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
use Mammatus\LifeCycleEvents\Shutdown;
99
use WyriHaximus\Broadcast\Contracts\AsyncListener;
1010

11+
use function React\Async\await;
12+
use function React\Promise\Timer\sleep;
13+
1114
final class CloseContext implements AsyncListener
1215
{
1316
public function __construct(
1417
private readonly Context $context,
1518
) {
1619
}
1720

18-
public function stop(Shutdown $event): void
21+
public function shutdown(Shutdown $event): void
1922
{
23+
await(sleep(0.1));
24+
2025
$this->context->close();
2126
}
2227
}

tests/AppTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function runHappy(): void
2727

2828
$container->expects('get')->with(Noop::class)->once()->andReturn(new Noop());
2929

30-
$context->expects('close')->once();
31-
3230
$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();
3331

3432
$exitCode = (new App($consumer, $logger))->run(Noop::class);
@@ -47,8 +45,6 @@ public function runAngry(): void
4745
$exception = new RuntimeException('Ik ben boos!');
4846
$container->expects('get')->with(Noop::class)->once()->andReturn(new Angry($exception));
4947

50-
$context->expects('close')->once();
51-
5248
$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();
5349

5450
$exitCode = (new App($consumer, $logger))->run(Noop::class);

tests/ConsumerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function consumeHappy(): void
2323
{
2424
[$consumer, $container, $context, $internalConsumer, $logger] = ConsumerFactory::create(ConsumerFactory::CREATE_CONSUMER_EXPECTED);
2525
$container->expects('get')->with(Noop::class)->once()->andReturn(new Noop());
26-
$context->expects('close')->once();
2726
$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();
2827

2928
$message = new Message();
@@ -53,7 +52,6 @@ public function invalidJson(): void
5352

5453
[$consumer, $container, $context, $internalConsumer, $logger] = ConsumerFactory::create(ConsumerFactory::CREATE_CONSUMER_EXPECTED);
5554
$container->expects('get')->with(Noop::class)->once()->andReturn(new Noop());
56-
$context->expects('close')->once();
5755
$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();
5856

5957
$message = new Message();

tests/ManagerTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public function runHappy(): void
3232

3333
$container->expects('get')->with(Noop::class)->atLeast()->once()->andReturn(new Noop());
3434

35-
$context->expects('close')->once();
36-
3735
$eventDispatcher = Mockery::mock(EventDispatcherInterface::class);
3836

3937
$logger->expects('debug')->with('Starting queue manager')->once();
@@ -58,8 +56,6 @@ public function runAngry(): void
5856
[$consumer, $container, $context, $internalConsumer, $logger] = ConsumerFactory::create(ConsumerFactory::CREATE_CONSUMER_EXPECTED);
5957
$internalConsumer->expects('receiveNoWait')->between(0, PHP_INT_MAX);
6058

61-
$context->expects('close')->once();
62-
6359
$eventDispatcher = Mockery::mock(EventDispatcherInterface::class);
6460

6561
$exception = new RuntimeException('Ik ben boos!');

0 commit comments

Comments
 (0)