Skip to content

Commit

Permalink
[Cosmos] Adds timeouts test with sample (Azure#17124)
Browse files Browse the repository at this point in the history
* Adds timeouts test

* Fix lint
  • Loading branch information
zfoster authored Aug 26, 2021
1 parent c64d6b1 commit cf25413
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
7 changes: 3 additions & 4 deletions sdk/cosmosdb/cosmos/test/internal/session.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import assert from "assert";
import { Suite } from "mocha";
import { ClientContext, Container, PluginConfig, PluginOn } from "../../src";
Expand Down Expand Up @@ -96,10 +97,8 @@ describe.skip("Session Token", function(this: Suite) {
if (context.headers["x-ms-session-token"]) {
context.headers["x-ms-session-token"] = "0:0#900000#3=8600000#10=-1";
}
console.log(context.method, context.path, context.headers["x-ms-session-token"]);
const repsonse = await next(context);
console.log(repsonse.code, repsonse.substatus);
return repsonse;
const response = await next(context);
return response;
}
}
]
Expand Down
71 changes: 71 additions & 0 deletions sdk/cosmosdb/cosmos/test/public/integration/timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import assert from "assert";
import { Container, CosmosClient } from "../../../src";
import { addEntropy, removeAllDatabases } from "../common/TestHelpers";
import { endpoint, masterKey } from "../common/_testConfig";

describe("Timeout", function() {
beforeEach(async function() {
await removeAllDatabases();
});

it("successfully exits queries after a timeout duration", async function() {
const clientA = new CosmosClient({
endpoint,
key: masterKey,
connectionPolicy: {
enableBackgroundEndpointRefreshing: false,
requestTimeout: 500,
retryOptions: {
maxRetryAttemptCount: 2,
maxWaitTimeInSeconds: 5,
fixedRetryIntervalInMilliseconds: 0
}
},
plugins: [
{
on: "request",
plugin: async (context, next) => {
// Simulate a request longer than our timeout duration
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 1500);
});
const response = await next(context);
return response;
}
}
]
});

const dbId = addEntropy("timeouttest");
const containerId = addEntropy("timeouttest");

// Create Database and Container
try {
const { database } = await clientA.databases.createIfNotExists({
id: dbId
});
const { container } = await database.containers.createIfNotExists({
id: containerId
});

// Create an item using client
await createItem(container);
} catch (e) {
assert.equal(e.code, "TimeoutError");
}
});
});

async function createItem(container: Container) {
const {
resource: { id }
} = await container.items.create({
id: (Math.random() + 1).toString(36).substring(7)
});
return id;
}

0 comments on commit cf25413

Please sign in to comment.