|
9 | 9 | use Google\ApiCore\ApiException; |
10 | 10 | use Google\Cloud\Tasks\V2\HttpMethod; |
11 | 11 | use Google\Cloud\Tasks\V2\HttpRequest; |
| 12 | +use Google\Cloud\Tasks\V2\Queue\State; |
12 | 13 | use PHPUnit\Framework\Attributes\Test; |
| 14 | +use Google\Cloud\Tasks\V2\GetQueueRequest; |
13 | 15 | use Google\Cloud\Tasks\V2\Client\CloudTasksClient; |
14 | 16 | use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi; |
15 | 17 |
|
@@ -138,4 +140,88 @@ public function test_delete_task() |
138 | 140 | $this->expectExceptionMessage('NOT_FOUND'); |
139 | 141 | CloudTasksApi::getTask($task->getName()); |
140 | 142 | } |
| 143 | + |
| 144 | + #[Test] |
| 145 | + public function it_can_pause_queues(): void |
| 146 | + { |
| 147 | + $queueName = $this->client->queueName( |
| 148 | + env('CI_CLOUD_TASKS_PROJECT_ID'), |
| 149 | + env('CI_CLOUD_TASKS_LOCATION'), |
| 150 | + env('CI_CLOUD_TASKS_QUEUE').'-pause' |
| 151 | + ); |
| 152 | + |
| 153 | + $this->ensureQueueIs($queueName, State::RUNNING); |
| 154 | + |
| 155 | + // Act |
| 156 | + CloudTasksApi::pause($queueName); |
| 157 | + |
| 158 | + // Assert |
| 159 | + $this->assertEquals(State::PAUSED, $this->waitForQueueState($queueName, State::PAUSED)); |
| 160 | + } |
| 161 | + |
| 162 | + #[Test] |
| 163 | + public function it_can_resume_queues(): void |
| 164 | + { |
| 165 | + $queueName = $this->client->queueName( |
| 166 | + env('CI_CLOUD_TASKS_PROJECT_ID'), |
| 167 | + env('CI_CLOUD_TASKS_LOCATION'), |
| 168 | + env('CI_CLOUD_TASKS_QUEUE').'-pause' |
| 169 | + ); |
| 170 | + |
| 171 | + $this->ensureQueueIs($queueName, State::PAUSED); |
| 172 | + |
| 173 | + // Act |
| 174 | + CloudTasksApi::resume($queueName); |
| 175 | + |
| 176 | + // Assert |
| 177 | + $this->assertEquals(State::RUNNING, $this->waitForQueueState($queueName, State::RUNNING)); |
| 178 | + } |
| 179 | + |
| 180 | + private function getQueueState(string $queue): int |
| 181 | + { |
| 182 | + return $this->client->getQueue(GetQueueRequest::build($queue))->getState(); |
| 183 | + } |
| 184 | + |
| 185 | + private function waitForQueueState(string $queue, int $waitForState): ?int |
| 186 | + { |
| 187 | + $state = null; |
| 188 | + $attempts = 0; |
| 189 | + |
| 190 | + while ($state !== $waitForState) { |
| 191 | + $state = $this->getQueueState($queue); |
| 192 | + |
| 193 | + if ($state === $waitForState) { |
| 194 | + return $state; |
| 195 | + } |
| 196 | + |
| 197 | + $attempts++; |
| 198 | + |
| 199 | + if ($attempts >= 10) { |
| 200 | + break; |
| 201 | + } |
| 202 | + |
| 203 | + sleep(1); |
| 204 | + } |
| 205 | + |
| 206 | + return $state; |
| 207 | + } |
| 208 | + |
| 209 | + private function ensureQueueIs(string $queue, int $desiredState): void |
| 210 | + { |
| 211 | + $currentState = $this->getQueueState($queue); |
| 212 | + |
| 213 | + if ($currentState === $desiredState) { |
| 214 | + return; |
| 215 | + } |
| 216 | + |
| 217 | + if ($currentState === State::RUNNING && $desiredState === State::PAUSED) { |
| 218 | + CloudTasksApi::pause($queue); |
| 219 | + } |
| 220 | + |
| 221 | + if ($currentState === State::PAUSED && $desiredState === State::RUNNING) { |
| 222 | + CloudTasksApi::resume($queue); |
| 223 | + } |
| 224 | + |
| 225 | + $this->assertEquals($desiredState, $this->waitForQueueState($queue, $desiredState)); |
| 226 | + } |
141 | 227 | } |
0 commit comments