diff --git a/yarn-project/foundation/src/timer/timeout.test.ts b/yarn-project/foundation/src/timer/timeout.test.ts new file mode 100644 index 000000000000..cccb7c196bea --- /dev/null +++ b/yarn-project/foundation/src/timer/timeout.test.ts @@ -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!'); + }); +}); diff --git a/yarn-project/foundation/src/timer/timeout.ts b/yarn-project/foundation/src/timer/timeout.ts index 44205f2d4e68..06b80bacdc5b 100644 --- a/yarn-project/foundation/src/timer/timeout.ts +++ b/yarn-project/foundation/src/timer/timeout.ts @@ -27,7 +27,7 @@ export class TimeoutTask { * @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([this.fn(), this.interruptPromise]); diff --git a/yarn-project/simulator/src/public/public_processor.test.ts b/yarn-project/simulator/src/public/public_processor.test.ts index 0d29f716430f..eb4872cbda00 100644 --- a/yarn-project/simulator/src/public/public_processor.test.ts +++ b/yarn-project/simulator/src/public/public_processor.test.ts @@ -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); diff --git a/yarn-project/simulator/src/public/public_processor.ts b/yarn-project/simulator/src/public/public_processor.ts index 5118d1f4a1d9..e02802e9caf0 100644 --- a/yarn-project/simulator/src/public/public_processor.ts +++ b/yarn-project/simulator/src/public/public_processor.ts @@ -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,