Skip to content

Commit ab9db5c

Browse files
committed
test(scheduled task): Replace long waiting time with retries
- Using retries when expecting changes (new running task). - Fixed waiting time when not expecting changes (delete task). Test just has to wait long enough to be sure, the new table won't refresh and appear.
1 parent 723c88c commit ab9db5c

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

tests/Backend/ExternalBuckets/ScheduledTasksTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
use DateTime;
88
use DateTimeInterface;
9+
use Keboola\StorageApi\Client;
910
use Keboola\StorageApi\ClientException;
1011
use Keboola\StorageApi\Workspaces;
1112
use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory;
1213
use Keboola\Test\StorageApiTestCase;
14+
use Retry\BackOff\FixedBackOffPolicy;
15+
use Retry\Policy\SimpleRetryPolicy;
16+
use Retry\RetryProxy;
1317

1418
class ScheduledTasksTest extends StorageApiTestCase
1519
{
@@ -176,10 +180,10 @@ public function testSchedulerWorker(): void
176180
// Schedule external bucket refresh task
177181
$task = $this->_client->scheduleBucketRefresh($bucketId, '* * * * *');
178182

179-
sleep(120); // Wait for the Scheduler restart (every 60 seconds)
183+
sleep(60); // Wait for the Scheduler reload tasks from the database
180184

181185
// Check: Table created in workspace is after auto-refresh available in bucket
182-
$tablesAfterAutoRefresh = $this->_client->listTables($bucketId);
186+
$tablesAfterAutoRefresh = $this->listTablesWithRetry($this->_client, $bucketId);
183187
$this->assertCount(1, $tablesAfterAutoRefresh);
184188
$this->assertSame('TO-AUTO-REFRESH', $tablesAfterAutoRefresh[0]['name']);
185189

@@ -189,7 +193,7 @@ public function testSchedulerWorker(): void
189193
// Create another table in workspace
190194
$db->createTable('TO-MANUAL-REFRESH', ['ID' => 'NUMBER', 'NAME' => 'TEXT']);
191195

192-
sleep(120); // Wait for the Scheduler restart
196+
sleep(90); // Wait for the Scheduler restart
193197

194198
// Check: Second table created in workspace is not available in bucket because of stopped auto-refresh
195199
$tablesOutdated = $this->_client->listTables($bucketId);
@@ -203,4 +207,23 @@ public function testSchedulerWorker(): void
203207
$this->assertSame('TO-AUTO-REFRESH', $tablesAfterManualRefresh[0]['name']);
204208
$this->assertSame('TO-MANUAL-REFRESH', $tablesAfterManualRefresh[1]['name']);
205209
}
210+
211+
private function listTablesWithRetry(Client $client, string $bucketId): array
212+
{
213+
$proxy = new RetryProxy(
214+
new SimpleRetryPolicy(10),
215+
new FixedBackOffPolicy(6000),
216+
);
217+
218+
/** @var array $tables */
219+
$tables = $proxy->call(function () use ($client, $bucketId): array {
220+
$tables = $client->listTables($bucketId);
221+
222+
$this->assertNotEmpty($tables, 'There must be at least one table in the bucket.');
223+
224+
return $tables;
225+
});
226+
227+
return $tables;
228+
}
206229
}

0 commit comments

Comments
 (0)