forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cosmos] Adds timeouts test with sample (Azure#17124)
* Adds timeouts test * Fix lint
- Loading branch information
Showing
2 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sdk/cosmosdb/cosmos/test/public/integration/timeout.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |