Adjust tests to use new behaviour

This commit is contained in:
Kegan Dougal 2024-09-13 13:45:40 +01:00
parent df7bb61a5b
commit 7fbac10490
2 changed files with 16 additions and 46 deletions

View file

@ -266,14 +266,14 @@ export class SlidingSyncManager {
*/ */
public async startSpidering(batchSize: number, gapBetweenRequestsMs: number): Promise<void> { public async startSpidering(batchSize: number, gapBetweenRequestsMs: number): Promise<void> {
await sleep(gapBetweenRequestsMs); // wait a bit as this is called on first render so let's let things load await sleep(gapBetweenRequestsMs); // wait a bit as this is called on first render so let's let things load
let windowSize = batchSize; let fetchUpTo = batchSize;
let hasMore = true; let hasMore = true;
let firstTime = true; let firstTime = true;
while (hasMore) { while (hasMore) {
try { try {
if (firstTime) { if (firstTime) {
await this.slidingSync!.setList(SlidingSyncManager.ListSearch, { await this.slidingSync!.setList(SlidingSyncManager.ListSearch, {
ranges: [[0, windowSize]], ranges: [[0, fetchUpTo]],
sort: [ sort: [
"by_recency", // this list isn't shown on the UI so just sorting by timestamp is enough "by_recency", // this list isn't shown on the UI so just sorting by timestamp is enough
], ],
@ -296,7 +296,7 @@ export class SlidingSyncManager {
}, },
}); });
} else { } else {
await this.slidingSync!.setListRanges(SlidingSyncManager.ListSearch, ranges); await this.slidingSync!.setListRanges(SlidingSyncManager.ListSearch, [[0,fetchUpTo]]);
} }
} catch (err) { } catch (err) {
// do nothing, as we reject only when we get interrupted but that's fine as the next // do nothing, as we reject only when we get interrupted but that's fine as the next
@ -306,8 +306,8 @@ export class SlidingSyncManager {
await sleep(gapBetweenRequestsMs); await sleep(gapBetweenRequestsMs);
} }
const listData = this.slidingSync!.getListData(SlidingSyncManager.ListSearch)!; const listData = this.slidingSync!.getListData(SlidingSyncManager.ListSearch)!;
hasMore = windowSize < listData.joinedCount; hasMore = fetchUpTo < listData.joinedCount;
windowSize += batchSize; fetchUpTo += batchSize;
firstTime = false; firstTime = false;
} }
} }

View file

@ -79,7 +79,6 @@ describe("SlidingSyncManager", () => {
it("creates a new list based on the key", async () => { it("creates a new list based on the key", async () => {
const listKey = "key"; const listKey = "key";
mocked(slidingSync.getListParams).mockReturnValue(null); mocked(slidingSync.getListParams).mockReturnValue(null);
mocked(slidingSync.setList).mockResolvedValue("yep");
await manager.ensureListRegistered(listKey, { await manager.ensureListRegistered(listKey, {
sort: ["by_recency"], sort: ["by_recency"],
}); });
@ -114,7 +113,6 @@ describe("SlidingSyncManager", () => {
mocked(slidingSync.getListParams).mockReturnValue({ mocked(slidingSync.getListParams).mockReturnValue({
ranges: [[0, 42]], ranges: [[0, 42]],
}); });
mocked(slidingSync.setList).mockResolvedValue("yep");
await manager.ensureListRegistered(listKey, { await manager.ensureListRegistered(listKey, {
ranges: [[0, 52]], ranges: [[0, 52]],
}); });
@ -128,7 +126,6 @@ describe("SlidingSyncManager", () => {
ranges: [[0, 42]], ranges: [[0, 42]],
sort: ["by_recency"], sort: ["by_recency"],
}); });
mocked(slidingSync.setList).mockResolvedValue("yep");
await manager.ensureListRegistered(listKey, { await manager.ensureListRegistered(listKey, {
ranges: [[0, 42]], ranges: [[0, 42]],
sort: ["by_recency"], sort: ["by_recency"],
@ -139,11 +136,9 @@ describe("SlidingSyncManager", () => {
}); });
describe("startSpidering", () => { describe("startSpidering", () => {
it("requests in batchSizes", async () => { it("requests in expanding batchSizes", async () => {
const gapMs = 1; const gapMs = 1;
const batchSize = 10; const batchSize = 10;
mocked(slidingSync.setList).mockResolvedValue("yep");
mocked(slidingSync.setListRanges).mockResolvedValue("yep");
mocked(slidingSync.getListData).mockImplementation((key) => { mocked(slidingSync.getListData).mockImplementation((key) => {
return { return {
joinedCount: 64, joinedCount: 64,
@ -153,12 +148,13 @@ describe("SlidingSyncManager", () => {
await manager.startSpidering(batchSize, gapMs); await manager.startSpidering(batchSize, gapMs);
// we expect calls for 10,19 -> 20,29 -> 30,39 -> 40,49 -> 50,59 -> 60,69 // we expect calls for 10,19 -> 20,29 -> 30,39 -> 40,49 -> 50,59 -> 60,69
const wantWindows = [ const wantWindows = [
[10, 19], [0, 10],
[20, 29], [0, 20],
[30, 39], [0, 30],
[40, 49], [0, 40],
[50, 59], [0, 50],
[60, 69], [0, 60],
[0, 70],
]; ];
expect(slidingSync.getListData).toHaveBeenCalledTimes(wantWindows.length); expect(slidingSync.getListData).toHaveBeenCalledTimes(wantWindows.length);
expect(slidingSync.setList).toHaveBeenCalledTimes(1); expect(slidingSync.setList).toHaveBeenCalledTimes(1);
@ -170,13 +166,12 @@ describe("SlidingSyncManager", () => {
SlidingSyncManager.ListSearch, SlidingSyncManager.ListSearch,
// eslint-disable-next-line jest/no-conditional-expect // eslint-disable-next-line jest/no-conditional-expect
expect.objectContaining({ expect.objectContaining({
ranges: [[0, batchSize - 1], range], ranges: [range],
}), }),
); );
return; return;
} }
expect(slidingSync.setListRanges).toHaveBeenCalledWith(SlidingSyncManager.ListSearch, [ expect(slidingSync.setListRanges).toHaveBeenCalledWith(SlidingSyncManager.ListSearch, [
[0, batchSize - 1],
range, range,
]); ]);
}); });
@ -184,7 +179,6 @@ describe("SlidingSyncManager", () => {
it("handles accounts with zero rooms", async () => { it("handles accounts with zero rooms", async () => {
const gapMs = 1; const gapMs = 1;
const batchSize = 10; const batchSize = 10;
mocked(slidingSync.setList).mockResolvedValue("yep");
mocked(slidingSync.getListData).mockImplementation((key) => { mocked(slidingSync.getListData).mockImplementation((key) => {
return { return {
joinedCount: 0, joinedCount: 0,
@ -198,31 +192,7 @@ describe("SlidingSyncManager", () => {
SlidingSyncManager.ListSearch, SlidingSyncManager.ListSearch,
expect.objectContaining({ expect.objectContaining({
ranges: [ ranges: [
[0, batchSize - 1], [0, batchSize],
[batchSize, batchSize + batchSize - 1],
],
}),
);
});
it("continues even when setList rejects", async () => {
const gapMs = 1;
const batchSize = 10;
mocked(slidingSync.setList).mockRejectedValue("narp");
mocked(slidingSync.getListData).mockImplementation((key) => {
return {
joinedCount: 0,
roomIndexToRoomId: {},
};
});
await manager.startSpidering(batchSize, gapMs);
expect(slidingSync.getListData).toHaveBeenCalledTimes(1);
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
expect(slidingSync.setList).toHaveBeenCalledWith(
SlidingSyncManager.ListSearch,
expect.objectContaining({
ranges: [
[0, batchSize - 1],
[batchSize, batchSize + batchSize - 1],
], ],
}), }),
); );
@ -277,7 +247,7 @@ describe("SlidingSyncManager", () => {
const unstableSpy = jest const unstableSpy = jest
.spyOn(client, "doesServerSupportUnstableFeature") .spyOn(client, "doesServerSupportUnstableFeature")
.mockImplementation(async (feature: string) => { .mockImplementation(async (feature: string) => {
expect(feature).toBe("org.matrix.msc3575"); expect(feature).toBe("org.matrix.simplified_msc3575");
return true; return true;
}); });
const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/"); const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/");