Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Using client information returned by queue api to finish quickly in a case of a single client

Marek Trunkat1ae5c38c3f87
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f70d3f754..a7a021ded 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+xxxxxxxxxxxxxxxxxxx
+===================
+- Request queue now allows crawlers to finish quickly without waiting in a case that queue was used by a single client.
+
0.13.5 / 2019-03-27
===================
- Fix `RequestQueue` throttling at high concurrency.
@@ -28,7 +32,7 @@
- **BREAKING CHANGE:** Added `handleRequestTimeoutSecs` option to `BasicCrawler` with a 60 second default.
- **DEPRECATED:** `PuppeteerPool` options in the `PuppeteerCrawler` constructor are now deprecated.
Please use the new `puppeteerPoolOptions` argument of type `Object` to pass them. `launchPuppeteerFunction`
- and `launchPuppeteerOptions` are still available as shortcuts for convenience.
+ and `launchPuppeteerOptions` are still available as shortcuts for convenience.
- `CheerioCrawler` and `PuppeteerCrawler` now automatically set `handleRequestTimeoutSecs` to 10 times
their `handlePageTimeoutSecs`. This is a precaution that should keep requests from hanging forever.
- Added `options.prepareRequestFunction()` to `CheerioCrawler` constructor to enable modification
diff --git a/src/request_queue.js b/src/request_queue.js
index 1dac54864..89b94119a 100644
--- a/src/request_queue.js
+++ b/src/request_queue.js
@@ -5,7 +5,7 @@ import { checkParamOrThrow } from 'apify-client/build/utils';
import LruCache from 'apify-shared/lru_cache';
import ListDictionary from 'apify-shared/list_dictionary';
import { ENV_VARS, LOCAL_STORAGE_SUBDIRS, REQUEST_QUEUE_HEAD_MAX_LIMIT } from 'apify-shared/consts';
-import { delayPromise, checkParamPrototypeOrThrow } from 'apify-shared/utilities';
+import { delayPromise, checkParamPrototypeOrThrow, cryptoRandomObjectId } from 'apify-shared/utilities';
import log from 'apify-shared/log';
import Promise from 'bluebird';
import crypto from 'crypto';
@@ -193,10 +193,12 @@ const getRequestId = (uniqueKey) => {
* @hideconstructor
*/
export class RequestQueue {
- constructor(queueId, queueName) {
+ constructor(queueId, queueName, clientId = cryptoRandomObjectId()) {
checkParamOrThrow(queueId, 'queueId', 'String');
checkParamOrThrow(queueName, 'queueName', 'Maybe String');
+ checkParamOrThrow(clientId, 'clientId', 'String');
+ this.clientId = clientId;
this.queueId = queueId;
this.queueName = queueName;
this.queueHeadDict = new ListDictionary();
@@ -204,6 +206,11 @@ export class RequestQueue {
this.inProgressCount = 0;
this.queryQueueHeadPromise = null;
+ // We can trust these numbers only in a case that queue is used by a single client.
+ // This information is returned by getHead() under the hadMultipleClients property.
+ this.assumedTotalCount = 0;
+ this.assumedHandledCount = 0;
+
// Caching requests to avoid duplicite addRequest() calls.
// Key is computed using getRequestId() and value is { id, isHandled }.
this.requestsCache = new LruCache({ maxLength: MAX_CACHED_REQUESTS });
@@ -248,9 +255,10 @@ export class RequestQueue {
request,
queueId: this.queueId,
forefront,
+ clientId: this.clientId,
})
.then((queueOperationInfo) => {
- const { requestId, wasAlreadyHandled } = queueOperationInfo;
+ const { requestId, wasAlreadyHandled, wasAlreadyPresent } = queueOperationInfo;
this._cacheRequest(cacheKey, queueOperationInfo);
@@ -258,6 +266,10 @@ export class RequestQueue {
this.queueHeadDict.add(requestId, requestId, true);
}
+ if (!wasAlreadyPresent) {
+ this.assumedTotalCount++;
+ }
+
// TODO: Why not set request.id to cachedInfo.id???
queueOperationInfo.request = request;
@@ -335,11 +347,16 @@ export class RequestQueue {
.updateRequest({
request,
queueId: this.queueId,
+ clientId: this.clientId,
})
.then((queueOperationInfo) => {
this._removeFromInProgress(request.id);
this._cacheRequest(getRequestId(request.uniqueKey), queueOperationInfo);
+ if (!queueOperationInfo.wasAlreadyHandled) {
+ this.assumedHandledCount++;
+ }
+
queueOperationInfo.request = request;
return queueOperationInfo;
@@ -369,6 +386,7 @@ export class RequestQueue {
request,
queueId: this.queueId,
forefront,
+ clientId: this.clientId,
})
.then((queueOperationInfo) => {
this._removeFromInProgress(request.id);
@@ -405,10 +423,10 @@ export class RequestQueue {
*
* @returns {Promise<Boolean>}
*/
- isFinished() {
- return this
- ._ensureHeadIsNonEmpty(true)
- .then(isHeadConsistent => isHeadConsistent && this.inProgressCount === 0 && this.queueHeadDict.length() === 0);
+ async isFinished() {
+ if (this.inProgressCount > 0 || this.queueHeadDict.length() > 0) return false;
+ const isHeadConsistent = await this._ensureHeadIsNonEmpty(true);
+ return isHeadConsistent && this.inProgressCount === 0 && this.queueHeadDict.length() === 0;
}
/**
@@ -460,13 +478,17 @@ export class RequestQueue {
*
* @ignore
*/
- _ensureHeadIsNonEmpty(checkModifiedAt = false, limit = Math.max(this.inProgressCount * QUERY_HEAD_BUFFER, QUERY_HEAD_MIN_LENGTH), iteration = 0) {
+ async _ensureHeadIsNonEmpty(
+ checkModifiedAt = false,
+ limit = Math.max(this.inProgressCount * QUERY_HEAD_BUFFER, QUERY_HEAD_MIN_LENGTH),
+ iteration = 0,
+ ) {
checkParamOrThrow(checkModifiedAt, 'checkModifiedAt', 'Boolean');
checkParamOrThrow(limit, 'limit', 'Number');
checkParamOrThrow(iteration, 'iteration', 'Number');
// If is nonempty resolve immediately.
- if (this.queueHeadDict.length()) return Promise.resolve(true);
+ if (this.queueHeadDict.length()) return true;
if (!this.queryQueueHeadPromise) {
const queryStartedAt = new Date();
@@ -475,8 +497,9 @@ export class RequestQueue {
.getHead({
limit,
queueId: this.queueId,
+ clientId: this.clientId,
})
- .then(({ items, queueModifiedAt }) => {
+ .then(({ items, queueModifiedAt, hadMultipleClients }) => {
items.forEach(({ id, uniqueKey }) => {
if (!this.requestIdsInProgress[id]) {
this.queueHeadDict.add(id, id, false);
@@ -488,48 +511,54 @@ export class RequestQueue {
this.queryQueueHeadPromise = null;
return {
- limitReached: items.length === limit,
+ wasLimitReached: items.length === limit,
prevLimit: limit,
queueModifiedAt: new Date(queueModifiedAt),
queryStartedAt,
+ hadMultipleClients,
};
});
}
- return this.queryQueueHeadPromise
- .then(({ queueModifiedAt, limitReached, prevLimit, queryStartedAt }) => {
- // If queue is still empty then it's likely because some of the other calls waiting
- // for this promise already consumed all the returned requests or the limit was too
- // low and contained only requests in progress.
- //
- // If limit was not reached in the call then there are no more requests to be returned.
- const shouldRepeatWithHigherLimit = !this.queueHeadDict.length() && limitReached && prevLimit < REQUEST_QUEUE_HEAD_MAX_LIMIT;
- if (prevLimit >= REQUEST_QUEUE_HEAD_MAX_LIMIT) {
- log.warning(`'RequestQueue: We've reached the maximum number of requests in progress: ${REQUEST_QUEUE_HEAD_MAX_LIMIT}.'`);
- }
-
- // If checkModifiedAt=true then we must ensure that queueModifiedAt is older than
- // queryStartedAt for at least API_PROCESSED_REQUESTS_DELAY_MILLIS.
- const shouldRepeatForConsistency = checkModifiedAt && (queryStartedAt - queueModifiedAt < API_PROCESSED_REQUESTS_DELAY_MILLIS);
-
- // If both are false then head is consistent and we may exit.
- if (!shouldRepeatWithHigherLimit && !shouldRepeatForConsistency) return true;
+ const { queueModifiedAt, wasLimitReached, prevLimit, queryStartedAt, hadMultipleClients } = await this.queryQueueHeadPromise;
- // If we are querying for consistency then we limit the number of queries to MAX_QUERIES_FOR_CONSISTENCY.
- // If this is reached then we return false so that empty() and finished() returns possibly false negative.
- if (!shouldRepeatWithHigherLimit && iteration > MAX_QUERIES_FOR_CONSISTENCY) return false;
+ // If queue is still empty then one of the following holds:
+ // - the other calls waiting for this promise already consumed all the returned requests
+ // - the limit was too low and contained only requests in progress
+ // - the whole queue was processed and we are done
- const nextLimit = shouldRepeatWithHigherLimit
- ? prevLimit * 1.5
- : prevLimit;
+ // If limit was not reached in the call then there are no more requests to be returned.
+ const shouldRepeatWithHigherLimit = this.queueHeadDict.length() === 0 && wasLimitReached && prevLimit < REQUEST_QUEUE_HEAD_MAX_LIMIT;
+ if (prevLimit >= REQUEST_QUEUE_HEAD_MAX_LIMIT) {
+ log.warning(`RequestQueue: We've reached the maximum number of requests in progress: ${REQUEST_QUEUE_HEAD_MAX_LIMIT}.`);
+ }
- const delayMillis = shouldRepeatForConsistency
- ? API_PROCESSED_REQUESTS_DELAY_MILLIS
- : 0;
+ // If checkModifiedAt=true then we must ensure that either:
+ // - queueModifiedAt is older than queryStartedAt for at least API_PROCESSED_REQUESTS_DELAY_MILLIS
+ // - hadMultipleClients=false and this.assumedTotalCount=this.assumedHandledCount
+ const isDatabaseConsistent = queryStartedAt - queueModifiedAt >= API_PROCESSED_REQUESTS_DELAY_MILLIS;
+ const isLocallyConsistent = !hadMultipleClients && this.assumedTotalCount === this.assumedHandledCount;
+ const shouldRepeatForConsistency = checkModifiedAt && (!isDatabaseConsistent && !isLocallyConsistent);
+
+ // If both are false then head is consistent and we may exit.
+ if (!shouldRepeatWithHigherLimit && !shouldRepeatForConsistency) return true;
+
+ // If we are querying for consistency then we limit the number of queries to MAX_QUERIES_FOR_CONSISTENCY.
+ // If this is reached then we return false so that empty() and finished() returns possibly false negative.
+ if (!shouldRepeatWithHigherLimit && iteration > MAX_QUERIES_FOR_CONSISTENCY) return false;
+
+ const nextLimit = shouldRepeatWithHigherLimit
+ ? Math.round(prevLimit * 1.5)
+ : prevLimit;
+
+ // If we are repeating for consistency then wait required time.
+ if (shouldRepeatForConsistency) {
+ const delayMillis = API_PROCESSED_REQUESTS_DELAY_MILLIS - (Date.now() - queueModifiedAt);
+ log.info(`RequestQueue: Waiting for ${delayMillis}ms before marking queue as finished to ensure that the data are consistent.`);
+ await delayPromise(delayMillis);
+ }
- return delayPromise(delayMillis)
- .then(() => this._ensureHeadIsNonEmpty(checkModifiedAt, nextLimit, iteration + 1));
- });
+ return this._ensureHeadIsNonEmpty(checkModifiedAt, nextLimit, iteration + 1);
}
/**
diff --git a/test/request_queue.js b/test/request_queue.js
index 776027e99..d2edf15be 100644
--- a/test/request_queue.js
+++ b/test/request_queue.js
@@ -278,7 +278,8 @@ describe('RequestQueue', () => {
const { Request } = Apify;
- const queue = new RequestQueue('some-id');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', undefined, clientId);
const mock = sinon.mock(apifyClient.requestQueues);
const requestA = new Request({ url: 'http://example.com/a' });
@@ -288,6 +289,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestA,
forefront: false,
+ clientId,
})
.returns(Promise.resolve({ requestId: 'a', wasAlreadyHandled: false, wasAlreadyPresent: false, request: requestA }));
await queue.addRequest(requestA);
@@ -299,6 +301,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestB,
forefront: true,
+ clientId,
})
.returns(Promise.resolve({ requestId: 'b', wasAlreadyHandled: false, wasAlreadyPresent: false, request: requestB }));
await queue.addRequest(requestB, { forefront: true });
@@ -336,6 +339,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestB,
forefront: true,
+ clientId,
})
.returns(Promise.resolve({ requestId: requestB.id, wasAlreadyHandled: false, wasAlreadyPresent: true, request: requestB }));
await queue.reclaimRequest(requestB, { forefront: true });
@@ -362,6 +366,7 @@ describe('RequestQueue', () => {
.withArgs({
queueId: 'some-id',
request: requestB,
+ clientId,
})
.returns(Promise.resolve({ requestId: requestB.id, wasAlreadyHandled: false, wasAlreadyPresent: true, request: requestB }));
await queue.markRequestHandled(requestB);
@@ -374,6 +379,7 @@ describe('RequestQueue', () => {
.withArgs({
queueId: 'some-id',
limit: QUERY_HEAD_MIN_LENGTH,
+ clientId,
})
.returns(Promise.resolve({
items: [
@@ -411,7 +417,8 @@ describe('RequestQueue', () => {
const { Request } = Apify;
- const queue = new RequestQueue('some-id');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', undefined, clientId);
const mock = sinon.mock(apifyClient.requestQueues);
const requestA = new Request({ url: 'http://example.com/a' });
@@ -424,6 +431,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestA,
forefront: false,
+ clientId,
})
.returns(Promise.resolve({
requestId: 'a',
@@ -451,7 +459,8 @@ describe('RequestQueue', () => {
const { Request } = Apify;
- const queue = new RequestQueue('some-id');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', undefined, clientId);
const mock = sinon.mock(apifyClient.requestQueues);
const requestX = new Request({ url: 'http://example.com/x' });
@@ -464,6 +473,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestX,
forefront: false,
+ clientId,
})
.returns(Promise.resolve({
requestId: 'x',
@@ -491,7 +501,8 @@ describe('RequestQueue', () => {
const { Request } = Apify;
- const queue = new RequestQueue('some-id', 'some-name');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', 'some-name', clientId);
const mock = sinon.mock(apifyClient.requestQueues);
// Query queue head with request A
@@ -500,6 +511,7 @@ describe('RequestQueue', () => {
.withArgs({
queueId: 'some-id',
limit: QUERY_HEAD_MIN_LENGTH,
+ clientId,
})
.returns(Promise.resolve({
items: [
@@ -527,7 +539,8 @@ describe('RequestQueue', () => {
const { Request } = Apify;
- const queue = new RequestQueue('some-id', 'some-name');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', 'some-name', clientId);
const mock = sinon.mock(apifyClient.requestQueues);
mock.expects('getHead').never();
@@ -540,6 +553,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestA,
forefront: true,
+ clientId,
})
.returns(Promise.resolve({
requestId: 'a',
@@ -578,7 +592,8 @@ describe('RequestQueue', () => {
const { Request } = Apify;
- const queue = new RequestQueue('some-id', 'some-name');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', 'some-name', clientId);
const mock = sinon.mock(apifyClient.requestQueues);
const requestA = new Request({ url: 'http://example.com/a' });
@@ -589,6 +604,7 @@ describe('RequestQueue', () => {
queueId: 'some-id',
request: requestA,
forefront: true,
+ clientId,
})
.returns(Promise.resolve({
requestId: 'a',
@@ -603,6 +619,7 @@ describe('RequestQueue', () => {
.withArgs({
queueId: 'some-id',
limit: QUERY_HEAD_MIN_LENGTH,
+ clientId,
})
.returns(Promise.resolve({ items: [] }));
@@ -615,7 +632,8 @@ describe('RequestQueue', () => {
it('should accept plain object in addRequest()', async () => {
expectNotUsingLocalStorage();
- const queue = new RequestQueue('some-id');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', undefined, clientId);
const mock = sinon.mock(apifyClient.requestQueues);
mock.expects('addRequest')
.once()
@@ -623,6 +641,7 @@ describe('RequestQueue', () => {
requestId: 'xxx',
wasAlreadyHandled: false,
wasAlreadyPresent: false,
+ clientId,
}));
await queue.addRequest({ url: 'http://example.com/a' });
mock.verify();
@@ -642,10 +661,11 @@ describe('RequestQueue', () => {
sinon.restore();
});
- it('should always wait for a queue head to become consistent before marking queue as finished', async () => {
+ it('should always wait for a queue head to become consistent before marking queue as finished (hadMultipleClients = true)', async () => {
expectNotUsingLocalStorage();
- const queue = new RequestQueue('some-id', 'some-name');
+ const clientId = 'my-client';
+ const queue = new RequestQueue('some-id', 'some-name', clientId);
const mock = sinon.mock(apifyClient.requestQueues);
// Return head with modifiedAt = now so it will retry the call.
@@ -654,11 +674,13 @@ describe('RequestQueue', () => {
.withArgs({
queueId: 'some-id',
limit: QUERY_HEAD_MIN_LENGTH,
+ clientId,
})
.returns(Promise.resolve({
limit: 5,
- queueModifiedAt: new Date(),
+ queueModifiedAt: new Date(Date.now() - API_PROCESSED_REQUESTS_DELAY_MILLIS * 0.75),
items: [],
+ hadMultipleClients: true,
}));
// And now return return date which makes the queue consistent.
@@ -667,11 +689,134 @@ describe('RequestQueue', () => {
.withArgs({
queueId: 'some-id',
limit: QUERY_HEAD_MIN_LENGTH,
+ clientId,
})
.returns(Promise.resolve({
limit: 5,
queueModifiedAt: new Date(Date.now() - API_PROCESSED_REQUESTS_DELAY_MILLIS),
items: [],
+ hadMultipleClients: true,
+ }));
+
+ expect(await queue.isFinished()).to.be.eql(true);
+
+ mock.verify();
+ mock.restore();
+ });
+
+ it('should always wait for a queue head to become consistent before marking queue as finished (hadMultipleClients = true)', async () => {
+ expectNotUsingLocalStorage();
+
+ const clientId = 'my-client';
+ const queueId = 'some-id';
+ const queue = new RequestQueue(queueId, 'some-name', clientId);
+ const mock = sinon.mock(apifyClient.requestQueues);
+
+ expect(queue.assumedTotalCount).to.be.eql(0);
+ expect(queue.assumedHandledCount).to.be.eql(0);
+
+ // Add some requests.
+ const requestA = new Apify.Request({ url: 'http://example.com/a' });
+ const requestB = new Apify.Request({ url: 'http://example.com/b' });
+ mock.expects('addRequest').once()
+ .withArgs({ queueId, request: requestA, forefront: true, clientId })
+ .returns(Promise.resolve({ requestId: 'a', wasAlreadyHandled: false, wasAlreadyPresent: false, request: requestA }));
+ mock.expects('addRequest').once()
+ .withArgs({ queueId, request: requestB, forefront: true, clientId })
+ .returns(Promise.resolve({ requestId: 'b', wasAlreadyHandled: false, wasAlreadyPresent: false, request: requestA }));
+ await queue.addRequest(requestA, { forefront: true });
+ await queue.addRequest(requestB, { forefront: true });
+
+ expect(queue.queueHeadDict.length()).to.be.eql(2);
+ expect(queue.inProgressCount).to.be.eql(0);
+ expect(queue.assumedTotalCount).to.be.eql(2);
+ expect(queue.assumedHandledCount).to.be.eql(0);
+
+ // It won't query the head as there is something in progress or pending.
+ mock.expects('getHead').never();
+ expect(await queue.isFinished()).to.be.eql(false);
+
+ // Fetch them from queue.
+ mock.expects('getRequest').once()
+ .withArgs({ queueId: 'some-id', requestId: 'b' })
+ .returns(Promise.resolve(_.extend(requestB, { id: 'b' })));
+ mock.expects('getRequest').once()
+ .withArgs({ queueId: 'some-id', requestId: 'a' })
+ .returns(Promise.resolve(_.extend(requestA, { id: 'a' })));
+ const requestBFromQueue = await queue.fetchNextRequest();
+ expect(requestBFromQueue).to.be.eql(requestB);
+ const requestAFromQueue = await queue.fetchNextRequest();
+ expect(requestAFromQueue).to.be.eql(requestA);
+
+ expect(queue.queueHeadDict.length()).to.be.eql(0);
+ expect(queue.inProgressCount).to.be.eql(2);
+ expect(queue.assumedTotalCount).to.be.eql(2);
+ expect(queue.assumedHandledCount).to.be.eql(0);
+
+ // It won't query the head as there is something in progress or pending.
+ mock.expects('getHead').never();
+ expect(await queue.isFinished()).to.be.eql(false);
+
+ // Reclaim one and mark another one handled.
+ mock.expects('updateRequest').once()
+ .withArgs({ queueId, request: requestB, clientId })
+ .returns(Promise.resolve({ requestId: requestB.id, wasAlreadyHandled: false, wasAlreadyPresent: true, request: requestB }));
+ await queue.markRequestHandled(requestB);
+ mock.expects('updateRequest').once()
+ .withArgs({ queueId, request: requestA, forefront: true, clientId })
+ .returns(Promise.resolve({ requestId: requestA.id, wasAlreadyHandled: false, wasAlreadyPresent: true, request: requestA }));
+ await queue.reclaimRequest(requestA, { forefront: true });
+
+ expect(queue.queueHeadDict.length()).to.be.eql(1);
+ expect(queue.inProgressCount).to.be.eql(0);
+ expect(queue.assumedTotalCount).to.be.eql(2);
+ expect(queue.assumedHandledCount).to.be.eql(1);
+
+ // It won't query the head as there is something in progress or pending.
+ mock.expects('getHead').never();
+ expect(await queue.isFinished()).to.be.eql(false);
+
+ // Fetch again.
+ mock.expects('getRequest').once()
+ .withArgs({ queueId: 'some-id', requestId: 'a' })
+ .returns(Promise.resolve(_.extend(requestA, { id: 'a' })));
+ const requestAFromQueue2 = await queue.fetchNextRequest();
+ expect(requestAFromQueue2).to.be.eql(requestA);
+
+ expect(queue.queueHeadDict.length()).to.be.eql(0);
+ expect(queue.inProgressCount).to.be.eql(1);
+ expect(queue.assumedTotalCount).to.be.eql(2);
+ expect(queue.assumedHandledCount).to.be.eql(1);
+
+ // It won't query the head as there is something in progress or pending.
+ mock.expects('getHead').never();
+ expect(await queue.isFinished()).to.be.eql(false);
+
+ // Mark handled.
+ mock.expects('updateRequest').once()
+ .withArgs({ queueId, request: requestA, clientId })
+ .returns(Promise.resolve({ requestId: requestA.id, wasAlreadyHandled: false, wasAlreadyPresent: true, request: requestA }));
+ await queue.markRequestHandled(requestA);
+
+ expect(queue.queueHeadDict.length()).to.be.eql(0);
+ expect(queue.inProgressCount).to.be.eql(0);
+ expect(queue.assumedTotalCount).to.be.eql(2);
+ expect(queue.assumedHandledCount).to.be.eql(2);
+
+ // Return head with modifiedAt = now so it would retry the query for queue to become consistent but because hadMultipleClients=true
+ // it will finish immediately.
+ mock.expects('getHead')
+ .once()
+ .withArgs({
+ queueId: 'some-id',
+ limit: QUERY_HEAD_MIN_LENGTH,
+ clientId,
+ })
+ .returns(Promise.resolve({
+ limit: 5,
+ queueModifiedAt: new Date(),
+ items: [],
+ hadMultipleClients: false,
}));
expect(await queue.isFinished()).to.be.eql(true);