Skip to content

Commit

Permalink
Merge 6663726 into d048faf
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik authored Jun 21, 2024
2 parents d048faf + 6663726 commit 010c737
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 82 deletions.
6 changes: 4 additions & 2 deletions integration-tests/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@types/chai-as-promised": "^7.1.5",
"@types/jsrsasign": "^10.5.4",
"@types/mocha": "^10.0.0",
"@types/sinon": "^17.0.3",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"platform": "^1.3.6",
Expand All @@ -94,9 +95,10 @@
"chai": "4.3.6",
"chai-as-promised": "^7.1.1",
"jsrsasign": "^11.0.0",
"node-fetch": "^3.3.2"
"node-fetch": "^3.3.2",
"sinon": "^18.0.0"
},
"files": [
"/src"
]
}
}
118 changes: 118 additions & 0 deletions integration-tests/tests/src/tests/sync/flexible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { expectClientResetError } from "../../utils/expect-sync-error";
import { createSyncConfig } from "../../utils/open-realm";
import { createPromiseHandle } from "../../utils/promise-handle";
import { buildAppConfig } from "../../utils/build-app-config";
import { spy } from "sinon";

export const PersonSchema: Realm.ObjectSchema = {
name: "Person",
Expand Down Expand Up @@ -437,6 +438,123 @@ describe("Flexible sync", function () {
});
});
});
describe("Progress notification", () => {
openRealmBeforeEach({
schema: [Person, Dog],
sync: {
flexible: true,
},
});
beforeEach(async function (this: RealmContext) {
await this.realm.subscriptions.update((mutableSubs) => {
mutableSubs.add(this.realm.objects(Person));
});
});

it("only estimate callback is allowed", async function (this: RealmContext) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
const callback = spy((estimate: number) => {});
this.realm.syncSession?.addProgressNotification(
Realm.ProgressDirection.Download,
Realm.ProgressMode.ForCurrentlyOutstandingWork,
callback,
);
});

it("old callback style is not allowed", async function (this: RealmContext) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
const callback = spy((transferable: number, transferred: number) => {});
expect(() => {
this.realm.syncSession?.addProgressNotification(
Realm.ProgressDirection.Download,
Realm.ProgressMode.ForCurrentlyOutstandingWork,
callback,
);
}).to.throw();

expect(callback.notCalled).to.be.true;
});

describe("with ProgressDirection.Upload", function () {
this.timeout(5000);

it("should only call the callback once with 1.0 when there is no upload", async function (this: RealmContext) {
const realm = this.realm;
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
const callback = spy((estimate: number) => {});

await realm.syncSession?.uploadAllLocalChanges();

realm.syncSession?.addProgressNotification(
Realm.ProgressDirection.Upload,
Realm.ProgressMode.ReportIndefinitely,
callback,
);

expect(callback.callCount).equals(1);

expect(callback.withArgs(1.0).calledOnce).to.be.true;
});

it("should show progress", async function (this: RealmContext) {
const realm = this.realm;
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
const callback = spy((estimate: number) => {});

realm.syncSession?.addProgressNotification(
Realm.ProgressDirection.Upload,
Realm.ProgressMode.ReportIndefinitely,
callback,
);

realm.write(() => {
for (let i = 0; i < 3; i++) {
realm.create(Person, {
_id: new BSON.ObjectId(),
name: "Person",
age: i,
});
}
});

await realm.syncSession?.uploadAllLocalChanges();

// There should be at least one point where the progress is not yet finished.
expect(callback.args.find(([estimate]) => estimate < 1)).to.not.be.undefined;

expect(callback.withArgs(1.0).called).to.be.true;
});

it("should have a correct start and finish states", async function (this: RealmContext) {
const realm = this.realm;
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
const callback = spy((estimate: number) => {});

realm.syncSession?.addProgressNotification(
Realm.ProgressDirection.Upload,
Realm.ProgressMode.ReportIndefinitely,
callback,
);

realm.write(() => {
for (let i = 0; i < 3; i++) {
realm.create(Person, {
_id: new BSON.ObjectId(),
name: "Person",
age: i,
});
}
});

await realm.syncSession?.uploadAllLocalChanges();

// There should be at least one point where the progress is not yet finished.
expect(callback.args.find(([estimate]) => estimate < 1)).to.not.be.undefined;

expect(callback.calledWithExactly(1.0)).to.be.true;
});
});
});

describe("Sync Errors", () => {
it("compensating writes", async function () {
Expand Down
19 changes: 9 additions & 10 deletions integration-tests/tests/src/tests/sync/sync-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getRegisteredEmailPassCredentials } from "../../utils/credentials";
import { generatePartition } from "../../utils/generators";
import { sleep, throwAfterTimeout } from "../../utils/sleep";
import { buildAppConfig } from "../../utils/build-app-config";
import { spy } from "sinon";

const DogForSyncSchema: Realm.ObjectSchema = {
name: "Dog",
Expand Down Expand Up @@ -286,16 +287,14 @@ describe("SessionTest", () => {
describe("progress notification", () => {
afterEach(() => Realm.clearTestState());
it("is called", async function (this: AppContext) {
this.timeout(5000);
const partition = generatePartition();
const { config } = await getSyncConfWithUser(this.app, partition);
let progressCalled = false;
await Promise.race([
Realm.open(config).progress(() => {
progressCalled = true;
}),
throwAfterTimeout(5000),
]);
expect(progressCalled).to.be.true;

const progressCallback = spy();
await Realm.open(config).progress(progressCallback);

expect(progressCallback.called);
});

it("removing progress notification does not invoke callback again", async function (this: AppContext) {
Expand Down Expand Up @@ -339,8 +338,8 @@ describe("SessionTest", () => {
realm.syncSession?.addProgressNotification("upload", "reportIndefinitely", progressCallback);
writeDataFunc();
});
await realm.close();
user.logOut();
realm.close();
await user.logOut();
});
});

Expand Down
Loading

0 comments on commit 010c737

Please sign in to comment.