Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/federation/lib/DbHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ public function setServerStatus(string $url, int $status, ?string $token = null)
$query->executeStatement();
}

/**
* Set server sync token without changing the server status
*/
public function setServerSyncToken(string $url, string $token): void {
$hash = $this->hash($url);
$query = $this->connection->getQueryBuilder();
$query->update($this->dbTable)
->set('sync_token', $query->createNamedParameter($token))
->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
$query->executeStatement();
}

/**
* Get server status
*/
Expand Down
4 changes: 4 additions & 0 deletions apps/federation/lib/SyncFederationAddressBooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function syncThemAll(\Closure $callback, bool $full = false) {
$targetPrincipal,
$targetBookProperties
);
if ($truncated && $syncToken !== null) {
// Keep the progress of completed pages in case a later page fails
$this->dbHandler->setServerSyncToken($url, $syncToken);
}
} while ($truncated);

if ($full) {
Expand Down
11 changes: 11 additions & 0 deletions apps/federation/tests/DbHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ public function testSetServerStatus(): void {
$this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
}

public function testSetServerSyncToken(): void {
$this->dbHandler->addServer('server1');
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_FAILURE);

$this->dbHandler->setServerSyncToken('http://server1', 'token1234567890');

$servers = $this->dbHandler->getAllServer();
$this->assertSame('token1234567890', $servers[0]['sync_token']);
$this->assertSame(TrustedServers::STATUS_FAILURE, (int)$servers[0]['status']);
}

public function testGetServerStatus(): void {
$this->dbHandler->addServer('server1');
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
Expand Down
69 changes: 69 additions & 0 deletions apps/federation/tests/SyncFederationAddressbooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,75 @@ public function testException(): void {
$this->assertCount(2, $this->callBacks);
}

public function testTruncatedSyncPersistsTokenAfterEachPage(): void {
/** @var DbHandler&MockObject $dbHandler */
$dbHandler = $this->createMock(DbHandler::class);
$dbHandler->method('getAllServer')
->willReturn([
[
'url' => 'https://cloud.example.org',
'url_hash' => 'sha1',
'shared_secret' => 'ilovenextcloud',
'sync_token' => null
]
]);
$tokenCalls = [];
$dbHandler->expects($this->exactly(2))->method('setServerSyncToken')
->willReturnCallback(function (string $url, string $token) use (&$tokenCalls): void {
$tokenCalls[] = [$url, $token];
});
$dbHandler->expects($this->once())->method('setServerStatus')
->with('https://cloud.example.org', TrustedServers::STATUS_OK, '5');
$syncService = $this->createMock(SyncService::class);
$syncService->expects($this->exactly(3))->method('syncRemoteAddressBook')
->willReturnOnConsecutiveCalls(['init_1_5', true], ['init_2_5', true], ['5', false]);

/** @var SyncService $syncService */
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
$s->syncThemAll(function ($url, $ex): void {
$this->callBacks[] = [$url, $ex];
});
$this->assertCount(1, $this->callBacks);
$this->assertSame([
['https://cloud.example.org', 'init_1_5'],
['https://cloud.example.org', 'init_2_5'],
], $tokenCalls);
}

public function testFailedPageKeepsTokenOfCompletedPages(): void {
/** @var DbHandler&MockObject $dbHandler */
$dbHandler = $this->createMock(DbHandler::class);
$dbHandler->method('getAllServer')
->willReturn([
[
'url' => 'https://cloud.example.org',
'url_hash' => 'sha1',
'shared_secret' => 'ilovenextcloud',
'sync_token' => null
]
]);
$dbHandler->expects($this->once())->method('setServerSyncToken')
->with('https://cloud.example.org', 'init_1_5');
$dbHandler->expects($this->once())->method('setServerStatus')
->with('https://cloud.example.org', TrustedServers::STATUS_FAILURE);
$page = 0;
$syncService = $this->createMock(SyncService::class);
$syncService->expects($this->exactly(2))->method('syncRemoteAddressBook')
->willReturnCallback(function () use (&$page) {
if ($page++ === 0) {
return ['init_1_5', true];
}
throw new \Exception('something did not work out');
});

/** @var SyncService $syncService */
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
$s->syncThemAll(function ($url, $ex): void {
$this->callBacks[] = [$url, $ex];
});
$this->assertCount(2, $this->callBacks);
}

public function testSuccessfulSyncWithoutChangesAfterFailure(): void {
/** @var DbHandler&MockObject $dbHandler */
$dbHandler = $this->createMock(DbHandler::class);
Expand Down
Loading