Skip to content

Commit 51e6cc4

Browse files
committed
RunnableQueue - cancelAll()
1 parent 2ae4619 commit 51e6cc4

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Process/Runnable/RunnableQueue.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,16 @@ private function drainQueue(): void
123123
});
124124
}
125125

126+
public function cancelAll(): void
127+
{
128+
foreach ($this->queue as [$runnable, $size, $deferred]) {
129+
$deferred->promise()->cancel(); // @phpstan-ignore-line
130+
}
131+
132+
foreach ($this->running as $running) { // phpcs:ignore
133+
[, $deferred] = $this->running->getInfo();
134+
$deferred->promise()->cancel(); // @phpstan-ignore-line
135+
}
136+
}
137+
126138
}

tests/PHPStan/Process/Runnable/RunnableQueueTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,43 @@ public function testCancel(): void
122122
], $logger->getMessages());
123123
}
124124

125+
public function testCancelAll(): void
126+
{
127+
$logger = new RunnableQueueLoggerStub();
128+
$queue = new RunnableQueue($logger, 4);
129+
$one = new RunnableStub('1');
130+
$two = new RunnableStub('2');
131+
$three = new RunnableStub('3');
132+
$queue->queue($one, 3);
133+
$queue->queue($two, 2);
134+
$queue->queue($three, 3);
135+
136+
$this->assertSame(5, $queue->getQueueSize());
137+
$this->assertSame(3, $queue->getRunningSize());
138+
139+
$queue->cancelAll();
140+
$this->assertSame(0, $queue->getQueueSize());
141+
$this->assertSame(0, $queue->getRunningSize());
142+
143+
$this->assertSame([
144+
0 => 'Queue not full - looking at first item in the queue',
145+
1 => 'Removing top item from queue - new size is 3',
146+
2 => 'Running process 1',
147+
3 => 'Queue not full - looking at first item in the queue',
148+
4 => 'Canot remote first item from the queue - it has size 2, current queue size is 3, new size would be 5',
149+
5 => 'Queue not full - looking at first item in the queue',
150+
6 => 'Canot remote first item from the queue - it has size 2, current queue size is 3, new size would be 5',
151+
7 => 'Process 1 finished unsuccessfully: Runnable 1 canceled',
152+
8 => 'Queue not full - looking at first item in the queue',
153+
9 => 'Removing top item from queue - new size is 2',
154+
10 => 'Running process 2',
155+
11 => 'Process 2 finished unsuccessfully: Runnable 2 canceled',
156+
12 => 'Queue not full - looking at first item in the queue',
157+
13 => 'Removing top item from queue - new size is 3',
158+
14 => 'Running process 3',
159+
15 => 'Process 3 finished unsuccessfully: Runnable 3 canceled',
160+
16 => 'Queue empty',
161+
], $logger->getMessages());
162+
}
163+
125164
}

0 commit comments

Comments
 (0)