Skip to content

Commit

Permalink
Merge pull request #10 from MammatusPHP/no-need-to-double-tap-the-con…
Browse files Browse the repository at this point in the history
…text-close

No need to double tap context close
  • Loading branch information
WyriHaximus authored Jan 6, 2025
2 parents b14a920 + 8d4241b commit f797690
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 94 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"react/async": "^4.3",
"react/event-loop": "^1.5",
"react/promise": "^3.2",
"react/promise-timer": "^1.11",
"roave/better-reflection": "^6.26",
"wyrihaximus/broadcast-contracts": "^1.3",
"wyrihaximus/generative-composer-plugin-tooling": "^1",
Expand Down
160 changes: 80 additions & 80 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function React\Async\async;
use function React\Async\await;
use function React\Promise\all;
use function React\Promise\Timer\sleep;
use function WyriHaximus\React\futurePromise;

final class Consumer extends AbstractList implements Listener
Expand All @@ -37,8 +38,6 @@ public function __construct(
public function close(): void
{
$this->running = false;

$this->context->close();
}

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

$promises = [];
$promises = [
sleep(0.1),
];
for ($i = 0; $i < $worker->concurrency; $i++) {
$this->logger->info('Starting consumer ' . $i . ' of ' . $worker->concurrency . ' for ' . $worker->class);
$promises[] = async(fn () => $this->consume($worker, $workerInstance))();
Expand Down
7 changes: 6 additions & 1 deletion src/LifeCycle/CloseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
use Mammatus\LifeCycleEvents\Shutdown;
use WyriHaximus\Broadcast\Contracts\AsyncListener;

use function React\Async\await;
use function React\Promise\Timer\sleep;

final class CloseContext implements AsyncListener
{
public function __construct(
private readonly Context $context,
) {
}

public function stop(Shutdown $event): void
public function shutdown(Shutdown $event): void
{
await(sleep(0.1));

$this->context->close();
}
}
4 changes: 0 additions & 4 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public function runHappy(): void

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

$context->expects('close')->once();

$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();

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

$context->expects('close')->once();

$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();

$exitCode = (new App($consumer, $logger))->run(Noop::class);
Expand Down
2 changes: 0 additions & 2 deletions tests/ConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function consumeHappy(): void
{
[$consumer, $container, $context, $internalConsumer, $logger] = ConsumerFactory::create(ConsumerFactory::CREATE_CONSUMER_EXPECTED);
$container->expects('get')->with(Noop::class)->once()->andReturn(new Noop());
$context->expects('close')->once();
$logger->expects('info')->with('Starting consumer 0 of 1 for ' . Noop::class)->atLeast()->once();

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

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

$message = new Message();
Expand Down
4 changes: 0 additions & 4 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function runHappy(): void

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

$context->expects('close')->once();

$eventDispatcher = Mockery::mock(EventDispatcherInterface::class);

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

$context->expects('close')->once();

$eventDispatcher = Mockery::mock(EventDispatcherInterface::class);

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

0 comments on commit f797690

Please sign in to comment.