Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(DOCSP-34476) Add cancellationToken parameter to Session.wait methods [Dart/Flutter] #3122

Merged
merged 2 commits into from
Dec 21, 2023
Merged
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
34 changes: 31 additions & 3 deletions examples/dart/test/manage_sync_session_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,25 @@ 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<Car>();
});
await realm.syncSession.waitForUpload();

// :snippet-start: wait-upload-download
// Wait to download all pending changes from Atlas
await realm.syncSession.waitForDownload();

// :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);
Expand All @@ -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
Expand Down Expand Up @@ -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<CancelledException>()));
expect(() async => await waitForUploadFuture,
throwsA(isA<CancelledException>()));
});
}
2 changes: 1 addition & 1 deletion examples/dart/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Future<void> cleanUpRealm(Realm realm, [App? app]) async {
if (!realm.isClosed) {
realm.close();
}
sleep(Duration(milliseconds: 250));
sleep(Duration(milliseconds: 500));
Realm.deleteRealm(realm.config.path);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
final cancellationToken = CancellationToken();

final waitForDownloadFuture =
realm.syncSession.waitForDownload(cancellationToken);
cancellationToken.cancel();

final waitForUploadFuture =
realm.syncSession.waitForUpload(cancellationToken);
cancellationToken.cancel();
7 changes: 7 additions & 0 deletions source/sdk/flutter/sync/manage-sync-session.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +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 :flutter-sdk:`CancellationToken
<realm/CancellationToken-class.html>` 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
Expand Down
Loading