-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathpartitionKeyRangeCacheReuse.spec.ts
More file actions
64 lines (57 loc) · 2.06 KB
/
Copy pathpartitionKeyRangeCacheReuse.spec.ts
File metadata and controls
64 lines (57 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { CosmosClient, ResourceType } from "../../../src/index.js";
import type { Container } from "../../../src/index.js";
import { endpoint } from "../common/_testConfig.js";
import { masterKey } from "../common/_fakeTestSecrets.js";
import { getTestContainer, removeAllDatabases } from "../common/TestHelpers.js";
import { describe, it, beforeAll, afterAll, expect } from "vitest";
/**
* The partition key range cache is shared via ClientContext, so cross-partition queries should
* fetch pkranges from the gateway once and reuse the cache on subsequent queries.
*/
describe("Partition key range cache reuse", { timeout: 60000 }, () => {
let pkRangeRequests = 0;
const client = new CosmosClient({
endpoint,
key: masterKey,
plugins: [
{
on: "request",
plugin: async (context, _diagNode, next) => {
if (context.resourceType === ResourceType.pkranges) {
pkRangeRequests++;
}
return next(context);
},
},
],
});
let container: Container;
beforeAll(async () => {
await removeAllDatabases(client);
container = await getTestContainer("pkrange-cache-reuse", client, {
partitionKey: { paths: ["/pk"] },
throughput: 12000,
});
await Promise.all(
Array.from({ length: 30 }, (_, i) =>
container.items.create({ id: `item-${i}`, pk: `pk-${i}` }),
),
);
});
afterAll(async () => {
await removeAllDatabases(client);
});
it("fetches pkranges once and serves later cross-partition queries from cache", async () => {
const options = { forceQueryPlan: true };
await container.items.query("SELECT * FROM c", options).fetchAll();
expect(pkRangeRequests).toBeGreaterThan(0);
const afterFirst = pkRangeRequests;
for (let i = 0; i < 3; i++) {
await container.items.query("SELECT * FROM c", options).fetchAll();
}
// Subsequent queries reuse the cached routing map - no extra pkranges requests.
expect(pkRangeRequests).toBe(afterFirst);
});
});