Skip to content

Commit

Permalink
Callbacks for client reset have been restored (#6214)
Browse files Browse the repository at this point in the history
* Callbacks for client reset have been restored
---------
Co-authored-by: LJ <[email protected]>
  • Loading branch information
kneth authored Nov 2, 2023
1 parent 33835a8 commit 791d4d4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* None

### Fixed
* The `onBefore`, `onAfter`, and `onFallback` client reset callbacks were not called. ([#6201](https://github.com/realm/realm-js/issues/6201), since v12.0.0)
* `Symbol.unscopables` has been implemented on the base class of `Realm.Results`, `Realm.List`, and `Realm.Set`. ([#6215](https://github.com/realm/realm-js/pull/6215))

### Compatibility
Expand All @@ -15,9 +16,7 @@
* File format: generates Realms with format v23 (reads and upgrades file format v5 or later for non-synced Realm, upgrades file format v10 or later for synced Realms).

### Internal
<!-- * Either mention core version or upgrade -->
<!-- * Using Realm Core vX.Y.Z -->
<!-- * Upgraded Realm Core from vX.Y.Z to vA.B.C -->
* Some disabled tests for client reset (partition based sync) have been enabled. ([#6201](https://github.com/realm/realm-js/issues/6201)

## 12.2.1 (2023-10-05)

Expand Down
31 changes: 22 additions & 9 deletions integration-tests/tests/src/tests/sync/client-reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ function getPartitionValue() {
}

async function triggerClientReset(app: App, user: User): Promise<void> {
await user.functions.triggerClientReset(app.id, user.id);
const maxAttempts = 5;
let deleted = false;
let count = maxAttempts;
while (count > 0) {
deleted = (await user.functions.triggerClientReset(app.id, user.id)) as boolean;
if (deleted) {
return;
}
count--;
}
throw new Error(`Cannot trigger client reset in ${maxAttempts} attempts`);
}

async function waitServerSideClientResetDiscardUnsyncedChangesCallbacks(
Expand Down Expand Up @@ -194,8 +204,8 @@ async function waitSimulatedClientResetDiscardUnsyncedChangesCallbacks(
if (useFlexibleSync) {
addSubscriptions(realm);
}

await realm.syncSession?.uploadAllLocalChanges();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore calling undocumented method _simulateError
realm.syncSession?._simulateError(211, "Simulate Client Reset", "realm::sync::ProtocolError", false); // 211 -> diverging histories
Expand Down Expand Up @@ -283,16 +293,18 @@ function getSchema(useFlexibleSync: boolean) {
}

// FIXME: testing flexible sync is currently disabled as it is timing out
[false /*, true*/].forEach((useFlexibleSync) => {
// FIXME: ngrok reports "Syntax Error" when tiggerClientResetFunction() is used.
// Once ngrok behaves nicely, the skipped tests can be enabled.
[false /*, true*/].forEach((useFlexibleSync) => {
describe.skipIf(
environment.missingServer,
`client reset handling (${getPartialTestTitle(useFlexibleSync)} sync)`,
function () {
this.longTimeout(); // client reset with flexible sync can take quite some time
importAppBefore(
useFlexibleSync
? buildAppConfig("with-flx").anonAuth().flexibleSync()
: buildAppConfig("with-pbs").anonAuth().partitionBasedSync(),
? buildAppConfig("with-flx").anonAuth().flexibleSync() /* .triggerClientResetFunction() */
: buildAppConfig("with-pbs").anonAuth().partitionBasedSync() /* .triggerClientResetFunction() */,
);
authenticateUserBefore();

Expand Down Expand Up @@ -417,7 +429,7 @@ function getSchema(useFlexibleSync: boolean) {
});
});

it.skip(`client reset fails, the error handler is called (${getPartialTestTitle(
it(`client reset fails, the error handler is called (${getPartialTestTitle(
useFlexibleSync,
)})`, async function (this: RealmContext) {
// if client reset fails, the error handler is called
Expand Down Expand Up @@ -456,7 +468,7 @@ function getSchema(useFlexibleSync: boolean) {
});
});

it.skip(`handles discard local simulated client reset with ${getPartialTestTitle(
it(`handles discard local simulated client reset with ${getPartialTestTitle(
useFlexibleSync,
)} sync enabled`, async function (this: RealmContext) {
// (i) using a client reset in "DiscardUnsyncedChanges" mode, a fresh copy
Expand All @@ -481,7 +493,7 @@ function getSchema(useFlexibleSync: boolean) {
);
});

it.skip(`handles simulated client reset with recovery with ${getPartialTestTitle(
it(`handles simulated client reset with recovery with ${getPartialTestTitle(
useFlexibleSync,
)} sync enabled`, async function (this: RealmContext) {
const clientResetBefore = (realm: Realm): void => {
Expand Down Expand Up @@ -534,7 +546,8 @@ function getSchema(useFlexibleSync: boolean) {
// of the Realm will be downloaded (resync)
// (ii) two callback will be called, while the sync error handler is not
// (iii) after the reset, the Realm can be used as before
this.longTimeout();
this.timeout(5 * 60 * 1000);
this.retries(3);
const clientResetBefore = (realm: Realm) => {
expect(realm.schema.length).to.equal(2);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class ExtendedAppConfigBuilder extends AppConfigBuilder {
}

triggerClientResetFunction() {
this.function({
return this.function({
name: "triggerClientReset",
private: false,
run_as_system: true,
Expand Down
1 change: 1 addition & 0 deletions packages/realm-app-importer/src/AdminApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export class AdminApiClient {
if (typeof body === "object") {
headers["content-type"] = "application/json";
}
console.log("FISK 111", { ...rest, body: JSON.stringify(body), headers, reactNative: { textStreaming: true } });
const response = await DefaultNetworkTransport.fetch(url, {
...rest,
body: JSON.stringify(body),
Expand Down
3 changes: 3 additions & 0 deletions packages/realm/bindgen/js_opt_in_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ records:
- ssl_verify_callback
- cancel_waits_on_nonfatal_error
- proxy_config
- client_resync_mode
- notify_before_client_reset
- notify_after_client_reset

SyncProxyConfig:
fields:
Expand Down

0 comments on commit 791d4d4

Please sign in to comment.