Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(http-middleware-http): test timeout with queue middleware #1686

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/sdk-middleware-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"build:bundles": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js -n CommercetoolsSdkMiddlewareHttp -i ./src/index.js"
},
"devDependencies": {
"@commercetools/sdk-client": "^2.1.2",
"@commercetools/sdk-middleware-queue": "^2.1.4",
"abort-controller": "3.0.0",
"nock": "12.0.3",
"node-fetch": "2.6.1"
Expand Down
43 changes: 43 additions & 0 deletions packages/sdk-middleware-http/test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import nock from 'nock'
import fetch from 'node-fetch'
import AbortController from 'abort-controller'
import { createQueueMiddleware } from '@commercetools/sdk-middleware-queue'
import { createClient } from '@commercetools/sdk-client'
import { createHttpMiddleware } from '../src'

function createTestRequest(options) {
Expand Down Expand Up @@ -1116,4 +1118,45 @@ describe('Http', () => {

httpMiddleware(next)(request, response)
}))

test('queueMiddleware with timeout', async () => {
expect.assertions(20)
const queueMiddleware = createQueueMiddleware({
concurrency: 10,
})

const httpMiddleware = createHttpMiddleware({
host: testHost,
timeout: 1000,
fetch,
getAbortController: () => new AbortController(),
})

const ctpClient = createClient({
middlewares: [queueMiddleware, httpMiddleware],
})

const request = createTestRequest({
uri: '/foo/bar',
})

nock(testHost)
.defaultReplyHeaders({
'Content-Type': 'application/json',
})
.get('/foo/bar')
.times(20)
.delay(1500) // delay response with 1.5 s
.reply(200, { foo: 'bar' })

await Promise.all(
[...Array(20)].map(async () => {
try {
await ctpClient.execute(request)
} catch (err) {
expect(err.message).toEqual('The user aborted a request.')
}
})
)
})
})