Skip to content

Commit 7bbad03

Browse files
authored
fix: concurrency option override order (#1649)
1 parent d2d2e66 commit 7bbad03

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/basic-crawler/src/internals/basic-crawler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
533533
}
534534

535535
const basicCrawlerAutoscaledPoolConfiguration: Partial<AutoscaledPoolOptions> = {
536-
minConcurrency,
537-
maxConcurrency,
536+
minConcurrency: minConcurrency ?? autoscaledPoolOptions?.minConcurrency,
537+
maxConcurrency: maxConcurrency ?? autoscaledPoolOptions?.maxConcurrency,
538538
maxTasksPerMinute: maxRequestsPerMinute ?? autoscaledPoolOptions?.maxTasksPerMinute,
539539
runTaskFunction: this._runTaskFunction.bind(this),
540540
isTaskReadyFunction: async () => {

test/core/crawlers/basic_crawler.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@ import {
1818
CriticalError,
1919
MissingRouteError,
2020
} from '@crawlee/basic';
21+
import {
22+
AutoscaledPool,
23+
} from '@crawlee/core';
2124
import express from 'express';
2225
import type { Dictionary } from '@crawlee/utils';
2326
import { sleep } from '@crawlee/utils';
2427
import { MemoryStorageEmulator } from 'test/shared/MemoryStorageEmulator';
2528
import { startExpressAppPromise } from '../../shared/_helper';
2629

30+
jest.mock('@crawlee/core', () => {
31+
const originalModule = jest.requireActual('@crawlee/core');
32+
const AutoscaledPoolMockConstructor = jest.fn((...args) => new originalModule.AutoscaledPool(...args));
33+
return {
34+
...originalModule,
35+
AutoscaledPool: AutoscaledPoolMockConstructor,
36+
};
37+
});
38+
2739
describe('BasicCrawler', () => {
2840
let logLevel: number;
2941
const localStorageEmulator = new MemoryStorageEmulator();
@@ -91,6 +103,55 @@ describe('BasicCrawler', () => {
91103
expect(await requestList.isEmpty()).toBe(true);
92104
});
93105

106+
test('should correctly combine shorthand and full length options', async () => {
107+
const shorthandOptions = {
108+
options: {
109+
minConcurrency: 123,
110+
maxConcurrency: 456,
111+
maxRequestsPerMinute: 789,
112+
},
113+
compare: {
114+
minConcurrency: 123,
115+
maxConcurrency: 456,
116+
maxTasksPerMinute: 789,
117+
},
118+
};
119+
120+
const autoscaledPoolOptions = {
121+
minConcurrency: 16,
122+
maxConcurrency: 32,
123+
maxTasksPerMinute: 64,
124+
};
125+
126+
const requestList = await RequestList.open(null, []);
127+
const requestHandler = async () => {};
128+
129+
await (new BasicCrawler({
130+
requestList,
131+
requestHandler,
132+
...shorthandOptions.options,
133+
})).run();
134+
135+
expect((AutoscaledPool as any).mock.calls[0][0]).toMatchObject(shorthandOptions.compare);
136+
137+
await (new BasicCrawler({
138+
requestList,
139+
requestHandler,
140+
autoscaledPoolOptions,
141+
})).run();
142+
143+
expect((AutoscaledPool as any).mock.calls[1][0]).toMatchObject(autoscaledPoolOptions);
144+
145+
await (new BasicCrawler({
146+
requestList,
147+
requestHandler,
148+
...shorthandOptions.options,
149+
autoscaledPoolOptions,
150+
})).run();
151+
152+
expect((AutoscaledPool as any).mock.calls[2][0]).toMatchObject(shorthandOptions.compare);
153+
});
154+
94155
test('auto-saved state object', async () => {
95156
const sources = [...Array(50).keys()].map((index) => ({ url: `https://example.com/${index}` }));
96157
const sourcesCopy = JSON.parse(JSON.stringify(sources));

0 commit comments

Comments
 (0)