From f2ec99dd9b2805b8ef2d540e84a5eb0604cd761d Mon Sep 17 00:00:00 2001 From: krollins-mdb Date: Wed, 20 Dec 2023 14:03:34 -0600 Subject: [PATCH 1/2] - Add new test for cancellation token - Fix broken test - Generate new snippet - Add new content to upload and download section --- .../dart/test/manage_sync_session_test.dart | 34 +++++++++++++++++-- examples/dart/test/utils.dart | 2 +- ...c_session_test.snippet.cancel-waitfor.dart | 9 +++++ .../sdk/flutter/sync/manage-sync-session.txt | 6 ++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 source/examples/generated/flutter/manage_sync_session_test.snippet.cancel-waitfor.dart diff --git a/examples/dart/test/manage_sync_session_test.dart b/examples/dart/test/manage_sync_session_test.dart index f04112cb40..f6373aca1d 100644 --- a/examples/dart/test/manage_sync_session_test.dart +++ b/examples/dart/test/manage_sync_session_test.dart @@ -34,10 +34,16 @@ main() { await realm.subscriptions.waitForSynchronization(); }); tearDown(() async { - cleanUpRealm(realm, app); + await cleanUpRealm(realm, app); }); group("Manage sync session - ", () { test("Wait for changes to upload and download", () async { + // Ensure there aren't any car objects in the on-device database. + realm.write(() { + realm.deleteAll(); + }); + await realm.syncSession.waitForUpload(); + // :snippet-start: wait-upload-download // Wait to download all pending changes from Atlas await realm.syncSession.waitForDownload(); @@ -45,8 +51,8 @@ main() { // :remove-start: final syncProgress = realm.syncSession.getProgressStream( ProgressDirection.upload, ProgressMode.forCurrentlyOutstandingWork); - var called = false; - var streamListener; + bool called = false; + dynamic streamListener; streamListener = syncProgress.listen((syncProgressEvent) { if (called == false) { expect(syncProgressEvent.transferableBytes > 0, isTrue); @@ -69,6 +75,7 @@ main() { // :snippet-end: expect(called, isTrue); }); + test("Pause and resume sync session", () async { // :snippet-start: pause-resume-sync // Pause the sync session @@ -163,4 +170,25 @@ main() { expect(session.connectionState, ConnectionState.connected); }); }); + + // This test should be at the end of the file. It cancels the sync + // session, which tests above rely on. + test("Cancel waiting for upload or download", () async { + // :snippet-start: cancel-waitfor + final cancellationToken = CancellationToken(); + + final waitForDownloadFuture = + realm.syncSession.waitForDownload(cancellationToken); + cancellationToken.cancel(); + + final waitForUploadFuture = + realm.syncSession.waitForUpload(cancellationToken); + cancellationToken.cancel(); + // :snippet-end: + + expect(() async => await waitForDownloadFuture, + throwsA(isA())); + expect(() async => await waitForUploadFuture, + throwsA(isA())); + }); } diff --git a/examples/dart/test/utils.dart b/examples/dart/test/utils.dart index 434a27b01e..ed245e61d0 100644 --- a/examples/dart/test/utils.dart +++ b/examples/dart/test/utils.dart @@ -8,7 +8,7 @@ Future cleanUpRealm(Realm realm, [App? app]) async { if (!realm.isClosed) { realm.close(); } - sleep(Duration(milliseconds: 250)); + sleep(Duration(milliseconds: 500)); Realm.deleteRealm(realm.config.path); } diff --git a/source/examples/generated/flutter/manage_sync_session_test.snippet.cancel-waitfor.dart b/source/examples/generated/flutter/manage_sync_session_test.snippet.cancel-waitfor.dart new file mode 100644 index 0000000000..ecb8de05a6 --- /dev/null +++ b/source/examples/generated/flutter/manage_sync_session_test.snippet.cancel-waitfor.dart @@ -0,0 +1,9 @@ +final cancellationToken = CancellationToken(); + +final waitForDownloadFuture = + realm.syncSession.waitForDownload(cancellationToken); +cancellationToken.cancel(); + +final waitForUploadFuture = + realm.syncSession.waitForUpload(cancellationToken); +cancellationToken.cancel(); diff --git a/source/sdk/flutter/sync/manage-sync-session.txt b/source/sdk/flutter/sync/manage-sync-session.txt index 0292d3b88e..5667f26daa 100644 --- a/source/sdk/flutter/sync/manage-sync-session.txt +++ b/source/sdk/flutter/sync/manage-sync-session.txt @@ -54,6 +54,12 @@ to download to your synced realm, call :flutter-sdk:`Session.waitForDownload() .. literalinclude:: /examples/generated/flutter/manage_sync_session_test.snippet.wait-upload-download.dart :language: dart +You can add an optional ``CancellationToken`` to both ``waitForUpload()`` and +``waitForDownload()``. + +.. literalinclude:: /examples/generated/flutter/manage_sync_session_test.snippet.cancel-waitfor.dart + :language: dart + .. _flutter-pause-resume-sync: Pause and Resume a Sync Session From 37551dc10d12d5b39fb0a830ffd5a4e68ee51082 Mon Sep 17 00:00:00 2001 From: krollins-mdb Date: Wed, 20 Dec 2023 14:08:53 -0600 Subject: [PATCH 2/2] Add API link --- source/sdk/flutter/sync/manage-sync-session.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/sdk/flutter/sync/manage-sync-session.txt b/source/sdk/flutter/sync/manage-sync-session.txt index 5667f26daa..73209d2f92 100644 --- a/source/sdk/flutter/sync/manage-sync-session.txt +++ b/source/sdk/flutter/sync/manage-sync-session.txt @@ -54,12 +54,13 @@ to download to your synced realm, call :flutter-sdk:`Session.waitForDownload() .. literalinclude:: /examples/generated/flutter/manage_sync_session_test.snippet.wait-upload-download.dart :language: dart -You can add an optional ``CancellationToken`` to both ``waitForUpload()`` and +You can add an optional :flutter-sdk:`CancellationToken +` to ``waitForUpload()`` and ``waitForDownload()``. .. literalinclude:: /examples/generated/flutter/manage_sync_session_test.snippet.cancel-waitfor.dart :language: dart - + .. _flutter-pause-resume-sync: Pause and Resume a Sync Session