diff --git a/test/common/TestHelpers.ts b/test/common/TestHelpers.ts index bd514bf..dc0f940 100644 --- a/test/common/TestHelpers.ts +++ b/test/common/TestHelpers.ts @@ -215,3 +215,22 @@ export function replaceOrUpsertPermission( return user.permission(body.id).replace(body, options); } } + +export function generateDocuments(docSize: number) { + const docs = []; + for (let i = 0; i < docSize; i++) { + const d = { + id: i.toString(), + name: "sample document", + spam: "eggs" + i.toString(), + cnt: i, + key: "value", + spam2: i === 3 ? "eggs" + i.toString() : i, + spam3: `eggs${i % 3}`, + boolVar: i % 2 === 0, + number: 1.1 * i + }; + docs.push(d); + } + return docs; +} diff --git a/test/functional/client.spec.ts b/test/functional/client.spec.ts index 3891a51..8767c50 100644 --- a/test/functional/client.spec.ts +++ b/test/functional/client.spec.ts @@ -2,14 +2,14 @@ import assert from "assert"; import { Agent } from "http"; import { CosmosClient } from "../../dist-esm"; import { endpoint, masterKey } from "../common/_testConfig"; -import { getTestDatabase } from "../common/TestHelpers"; +import { getTestDatabase, getTestContainer, generateDocuments, bulkInsertItems } from "../common/TestHelpers"; import { AbortController } from "abort-controller"; describe("NodeJS CRUD Tests", function() { this.timeout(process.env.MOCHA_TIMEOUT || 20000); describe("Validate client request timeout", function() { - it("nativeApi Client Should throw exception", async function() { + it("timeout occurs within expected timeframe", async function() { // making timeout 1 ms to make sure it will throw // (create database request takes 10ms-15ms to finish on emulator) const client = new CosmosClient({ endpoint, key: masterKey, connectionPolicy: { requestTimeout: 1 } }); @@ -64,5 +64,19 @@ describe("NodeJS CRUD Tests", function() { assert.equal(err.name, "AbortError", "client should throw exception"); } }); + it("should abort a query", async function() { + const container = await getTestContainer("abort query"); + await bulkInsertItems(container, generateDocuments(20)); + try { + const controller = new AbortController(); + const signal = controller.signal; + setTimeout(() => controller.abort(), 5000); + // Setting maxItemCount = 1 to ensure this query take a long time + await container.items.query("SELECT * from c", { abortSignal: signal, maxItemCount: 1 }).fetchAll(); + assert.fail("Must throw"); + } catch (err) { + assert.equal(err.name, "AbortError", "client should throw exception"); + } + }); }); }); diff --git a/test/integration/crossPartition.spec.ts b/test/integration/crossPartition.spec.ts index 44ababc..7ca2bfb 100644 --- a/test/integration/crossPartition.spec.ts +++ b/test/integration/crossPartition.spec.ts @@ -4,7 +4,7 @@ import { Container, ContainerDefinition } from "../../dist-esm/client"; import { DataType, IndexKind } from "../../dist-esm/documents"; import { SqlQuerySpec } from "../../dist-esm/queryExecutionContext"; import { QueryIterator } from "../../dist-esm/queryIterator"; -import { bulkInsertItems, getTestContainer, removeAllDatabases } from "../common/TestHelpers"; +import { bulkInsertItems, getTestContainer, removeAllDatabases, generateDocuments } from "../common/TestHelpers"; import { FeedResponse, FeedOptions } from "../../dist-esm"; function compare(key: string) { @@ -21,24 +21,6 @@ function compare(key: string) { describe("Cross Partition", function() { this.timeout(process.env.MOCHA_TIMEOUT || "30000"); - const generateDocuments = function(docSize: number) { - const docs = []; - for (let i = 0; i < docSize; i++) { - const d = { - id: i.toString(), - name: "sample document", - spam: "eggs" + i.toString(), - cnt: i, - key: "value", - spam2: i === 3 ? "eggs" + i.toString() : i, - spam3: `eggs${i % 3}`, - boolVar: i % 2 === 0, - number: 1.1 * i - }; - docs.push(d); - } - return docs; - }; describe("Validate Query", function() { const documentDefinitions = generateDocuments(20);