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

chore: Try fix flakey public processor test #11348

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
20 changes: 20 additions & 0 deletions yarn-project/foundation/src/timer/timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { sleep } from '../sleep/index.js';
import { executeTimeout } from './timeout.js';

describe('timeout', () => {
it('execs within timeout', async () => {
await expect(executeTimeout(() => sleep(200).then(() => 'ok'), 300)).resolves.toEqual('ok');
});

it('rejects with custom error on timeout', async () => {
await expect(executeTimeout(() => sleep(500), 200, 'Timed out!')).rejects.toThrow('Timed out!');
});

it('rejects if timeout is zero', async () => {
await expect(executeTimeout(() => sleep(500), 0, 'Timed out!')).rejects.toThrow('Timed out!');
});

it('rejects if timeout is negative', async () => {
await expect(executeTimeout(() => sleep(500), -100, 'Timed out!')).rejects.toThrow('Timed out!');
});
});
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/timer/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TimeoutTask<T> {
* @throws An error with a message indicating the function was interrupted due to exceeding the specified timeout.
*/
public async exec() {
const interruptTimeout = !this.timeout ? 0 : setTimeout(this.interrupt, this.timeout);
const interruptTimeout = setTimeout(this.interrupt, this.timeout);
try {
const start = Date.now();
const result = await Promise.race<T>([this.fn(), this.interruptPromise]);
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/public/public_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ describe('public_processor', () => {

// The simulator will take 400ms to process each tx
publicTxSimulator.simulate.mockImplementation(async () => {
await sleep(400);
await sleep(800);
return mockedEnqueuedCallsResult;
});

// We allocate a deadline of 1s, so only one 2 txs should fit
const deadline = new Date(Date.now() + 1000);
// We allocate a deadline of 2s, so only 2 txs should fit
const deadline = new Date(Date.now() + 2000);
const [processed, failed] = await processor.process(txs, { deadline });

expect(processed.length).toBe(2);
Expand Down
11 changes: 6 additions & 5 deletions yarn-project/simulator/src/public/public_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,17 +372,18 @@ export class PublicProcessor implements Traceable {
return await processFn();
}

const txHash = tx.getTxHash().toString();
const timeout = +deadline - this.dateProvider.now();
if (timeout <= 0) {
throw new PublicProcessorTimeoutError();
}

this.log.debug(`Processing tx ${tx.getTxHash().toString()} within ${timeout}ms`, {
deadline: deadline.toISOString(),
now: new Date(this.dateProvider.now()).toISOString(),
txHash: tx.getTxHash().toString(),
txHash,
});

if (timeout < 0) {
throw new PublicProcessorTimeoutError();
}

return await executeTimeout(
() => processFn(),
timeout,
Expand Down
Loading