Skip to content

Commit f3aa9ab

Browse files
committed
feat: add the workers fuzz option
EDR v0.26.0 runs each fuzz test on parallel workers and takes a `workers` option, which already reached it untyped through the spread in `resolveFuzzConfig`. We add it to the schema and to the user and resolved fuzz config types. It must be a positive integer, and when it isn't set nothing is passed, so EDR's default (the available threads) applies.
1 parent be6e9b7 commit f3aa9ab

5 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎.changeset/edr-fuzz-parallel-workers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"hardhat": minor
33
---
44

5-
Thanks @popescuoctavian! Added parallel workers to Solidity fuzz tests, which now run on the available threads; inputs for a given fuzz `seed` differ from earlier versions.
5+
Thanks @popescuoctavian! Support parallel workers for Solidity fuzz tests.

‎packages/hardhat/src/internal/builtin-plugins/solidity-test/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const solidityTestProfileUserConfigType = z.object({
9191
includePushBytes: z.boolean().optional(),
9292
showLogs: z.boolean().optional(),
9393
timeout: nonnegativeSafeIntOrSafeBigInt.optional(),
94+
workers: z.number().int().positive().safe().optional(),
9495
})
9596
.optional(),
9697
forking: z

‎packages/hardhat/src/internal/builtin-plugins/solidity-test/type-extensions.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ declare module "../../../types/test.js" {
4646
includePushBytes?: boolean;
4747
showLogs?: boolean;
4848
timeout?: number | bigint;
49+
workers?: number;
4950
}
5051

5152
export interface SolidityTestForkingUserConfig {
@@ -122,6 +123,7 @@ declare module "../../../types/test.js" {
122123
includePushBytes?: boolean;
123124
showLogs?: boolean;
124125
timeout?: number;
126+
workers?: number;
125127
}
126128

127129
export interface SolidityTestForkingConfig {

‎packages/hardhat/test/internal/builtin-plugins/solidity-test/config-resolution.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ describe("config resolution", () => {
5454
assert.equal(result.timeout, 30);
5555
assert.equal(result.seed, DEFAULT_FUZZ_SEED);
5656
});
57+
58+
it("should pass `workers` through when it is set", () => {
59+
const result = resolveFuzzConfig({ workers: 1 });
60+
61+
assert.deepEqual(result, { seed: DEFAULT_FUZZ_SEED, workers: 1 });
62+
});
63+
64+
it("should leave `workers` out when it isn't set, so EDR's default applies", () => {
65+
const result = resolveFuzzConfig({ runs: 500 });
66+
67+
assert.ok(result !== undefined, "result is undefined");
68+
assert.equal("workers" in result, false);
69+
});
5770
});
5871

5972
describe("resolveInvariantConfig", () => {
@@ -182,5 +195,23 @@ describe("config resolution", () => {
182195
assert.equal(ci.forking?.blockNumber, 123n);
183196
assert.equal(await ci.forking?.url?.get(), "https://example.com/ci");
184197
});
198+
199+
it("should resolve `workers` per profile", async () => {
200+
const hre = await createHardhatRuntimeEnvironment({
201+
test: {
202+
solidity: {
203+
profiles: {
204+
default: { fuzz: { runs: 500 } },
205+
ci: { fuzz: { workers: 1 } },
206+
},
207+
},
208+
},
209+
});
210+
211+
const { default: defaultProfile, ci } = hre.config.test.solidity.profiles;
212+
213+
assert.equal(defaultProfile.fuzz.workers, undefined);
214+
assert.equal(ci.fuzz.workers, 1);
215+
});
185216
});
186217
});

‎packages/hardhat/test/internal/builtin-plugins/solidity-test/config-validation.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,42 @@ describe("config validation", () => {
207207
);
208208
});
209209

210+
it("should accept a positive integer `fuzz.workers`", async () => {
211+
const hre = await createHardhatRuntimeEnvironment({
212+
test: {
213+
solidity: {
214+
fuzz: { workers: 1 },
215+
},
216+
},
217+
});
218+
219+
assert.equal(hre.config.test.solidity.profiles.default.fuzz.workers, 1);
220+
});
221+
222+
it("should reject a `fuzz.workers` that isn't a positive integer", async () => {
223+
for (const [workers, message] of [
224+
[0, "Number must be greater than 0"],
225+
[-1, "Number must be greater than 0"],
226+
[1.5, "Expected integer, received float"],
227+
] as const) {
228+
const userConfig: HardhatUserConfig = {
229+
test: {
230+
solidity: {
231+
fuzz: { workers },
232+
},
233+
},
234+
};
235+
236+
await assertRejectsWithHardhatError(
237+
createHardhatRuntimeEnvironment(userConfig),
238+
HardhatError.ERRORS.CORE.GENERAL.INVALID_CONFIG,
239+
{
240+
errors: `\t* Config error in config.test.solidity.fuzz.workers: ${message}`,
241+
},
242+
);
243+
}
244+
});
245+
210246
it("should reject the removed `fuzz.failurePersistFile` field", async () => {
211247
const userConfig: HardhatUserConfig = {
212248
test: {

0 commit comments

Comments
 (0)