From ecdf62b3e02cdb55e84a5417a60c66b4870eddb4 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 12:23:19 +0100 Subject: [PATCH 1/8] feat: add `TEST_TIMEOUT` for tests --- local-tests/setup/tinny-config.ts | 5 +++++ local-tests/setup/tinny-environment.ts | 1 + local-tests/setup/tinny-operations.ts | 22 +++++++++++++++++----- local-tests/setup/tinny-utils.ts | 15 +++++++++++++++ local-tests/test.ts | 2 ++ local-tests/tests/testRelayer.ts | 21 ++++++++++++++++++--- 6 files changed, 58 insertions(+), 8 deletions(-) diff --git a/local-tests/setup/tinny-config.ts b/local-tests/setup/tinny-config.ts index e6bfbd18c0..d92b1cfebf 100644 --- a/local-tests/setup/tinny-config.ts +++ b/local-tests/setup/tinny-config.ts @@ -24,12 +24,17 @@ export const RPC_MAP = { [LIT_TESTNET.DATIL_DEV]: LIT_RPC.VESUVIUS, }; +/** + * Represents the configuration options for the process environment. + */ export interface ProcessEnvs { /** * Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`. */ MAX_ATTEMPTS: number; + TEST_TIMEOUT: number; + /** * The network to use for testing. This can be one of the following: * - `LIT_TESTNET.LOCALCHAIN` diff --git a/local-tests/setup/tinny-environment.ts b/local-tests/setup/tinny-environment.ts index 12fe123137..26acdadcb6 100644 --- a/local-tests/setup/tinny-environment.ts +++ b/local-tests/setup/tinny-environment.ts @@ -30,6 +30,7 @@ export class TinnyEnvironment { */ public processEnvs: ProcessEnvs = { MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 1, + TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 60000, NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN, DEBUG: process.env['DEBUG'] === 'true', REQUEST_PER_KILOSECOND: diff --git a/local-tests/setup/tinny-operations.ts b/local-tests/setup/tinny-operations.ts index d9e9ab9a41..88278d8cf7 100644 --- a/local-tests/setup/tinny-operations.ts +++ b/local-tests/setup/tinny-operations.ts @@ -1,4 +1,5 @@ import { TinnyEnvironment } from './tinny-environment'; +import { withTimeout } from './tinny-utils'; /** * Retrieves filter flags from the command line arguments to determine which tests to run. @@ -154,6 +155,8 @@ export const runTestsParallel = async ({ testIndex: number ): Promise => { const maxAttempts = devEnv.processEnvs.MAX_ATTEMPTS; + const testTimeout = devEnv.processEnvs.TEST_TIMEOUT || 60000; + let attempts = 0; let testPassed = false; @@ -166,8 +169,7 @@ export const runTestsParallel = async ({ }. ${testName}...\x1b[0m` ); - // @ts-ignore - await testFunction(devEnv); + await withTimeout(testFunction(devEnv), testTimeout); testPassed = true; const endTime = performance.now(); @@ -184,9 +186,19 @@ export const runTestsParallel = async ({ } attempts++; + const endTime = performance.now(); + const timeTaken = (endTime - startTime).toFixed(2); + + if (error.message === 'Timed out') { + console.error( + `\x1b[31m✖\x1b[90m ${ + testIndex + 1 + }. ${testName} - Timed out after ${testTimeout}ms (${timeTaken} ms)\x1b[0m` + ); + return `${testName} (Timed out in ${timeTaken} ms)`; + } + if (attempts >= maxAttempts) { - const endTime = performance.now(); - const timeTaken = (endTime - startTime).toFixed(2); console.error( `\x1b[31m✖\x1b[90m ${ testIndex + 1 @@ -214,7 +226,7 @@ export const runTestsParallel = async ({ } const skippedTests = results.filter((result) => result.includes('Skipped')); - const failedTests = results.filter((result) => result.includes('Failed')); + const failedTests = results.filter((result) => result.includes('Failed') || result.includes('Timed out')); const passedTests = results.filter((result) => result.includes('Passed')); if (skippedTests.length > 0) { diff --git a/local-tests/setup/tinny-utils.ts b/local-tests/setup/tinny-utils.ts index f8de46cbc4..35ede2f751 100644 --- a/local-tests/setup/tinny-utils.ts +++ b/local-tests/setup/tinny-utils.ts @@ -45,3 +45,18 @@ export function randomSolanaPrivateKey() { } return result; } + +/** + * Wraps a promise with a timeout. + * If the promise does not resolve or reject within the specified time, it will be rejected with a "Timed out" error. + * + * @param promise - The promise to wrap with a timeout. + * @param ms - The timeout duration in milliseconds. + * @returns A new promise that resolves or rejects based on the original promise or the timeout. + */ +export function withTimeout(promise: Promise, ms: number): Promise { + const timeout = new Promise((_, reject) => + setTimeout(() => reject(new Error('Timed out')), ms) + ); + return Promise.race([promise, timeout]); +} diff --git a/local-tests/test.ts b/local-tests/test.ts index 3007f450ca..c1bd28c844 100644 --- a/local-tests/test.ts +++ b/local-tests/test.ts @@ -115,6 +115,8 @@ import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/ const relayerTests = { testRelayer, }; + + // --filter=WrappedKey const wrappedKeysTests = { // -- valid cases testEthereumSignMessageGeneratedKey, diff --git a/local-tests/tests/testRelayer.ts b/local-tests/tests/testRelayer.ts index 091c647dd2..d474c9035b 100644 --- a/local-tests/tests/testRelayer.ts +++ b/local-tests/tests/testRelayer.ts @@ -10,6 +10,7 @@ import { LitAuthClient, } from '@lit-protocol/lit-auth-client'; import { ProviderType } from '@lit-protocol/constants'; +import { withTimeout } from 'local-tests/setup/tinny-utils'; /** * Test Commands: @@ -40,7 +41,7 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => { if (pkps.length <= 0) { throw new Error('No PKPs found'); } else { - console.log('✅ [testRelayer] /fetch-pkps-by-auth-method works'); + console.log('✅ 1. [testRelayer] /fetch-pkps-by-auth-method works'); } // -- test claims @@ -49,7 +50,21 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => { signer: alice.wallet, }; - const claimRes = await devEnv.litNodeClient.claimKeyId(claimRequest); + let claimRes; + + console.log('Initiating claimKeyId call'); + try { + claimRes = await withTimeout( + devEnv.litNodeClient.claimKeyId(claimRequest), + 10000 + ); // 10 seconds timeout + console.log('claimKeyId call completed'); + // process claimRes as before + } catch (error) { + console.error('❗️ claimKeyId call failed or timed out'); + devEnv.litNodeClient.disconnect(); + throw new Error(error); + } // Expected output: // { @@ -112,5 +127,5 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => { } }); - log('✅ testRelayer'); + log('✅ 2. [testRelayer] Claim works'); }; From 3b182950d1ad3aad51a0ab9b60cd0bb2451958fe Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 12:32:14 +0100 Subject: [PATCH 2/8] doc: add `TEST_TIMEOUT` doc --- local-tests/README.md | 1 + local-tests/setup/tinny-config.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/local-tests/README.md b/local-tests/README.md index 302edf344e..774074767d 100644 --- a/local-tests/README.md +++ b/local-tests/README.md @@ -40,6 +40,7 @@ Below is the API documentation for the `ProcessEnvs` interface, detailing the co | Variable | Description | | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `MAX_ATTEMPTS` | Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`. | +| `TEST_TIMEOUT` | The maximum number of milliseconds to wait for a test to complete. | | `NETWORK` | The network to use for testing, which can be one of the following: `LIT_TESTNET.LOCALCHAIN`, `LIT_TESTNET.MANZANO`, or `LIT_TESTNET.CAYENNE`. | | `DEBUG` | Specifies whether to enable debug mode. | | `REQUEST_PER_KILOSECOND` | To execute a transaction with Lit, you must reserve capacity on the network using Capacity Credits. These allow a set number of requests over a period (default 2 days). | diff --git a/local-tests/setup/tinny-config.ts b/local-tests/setup/tinny-config.ts index d92b1cfebf..b44dcd180e 100644 --- a/local-tests/setup/tinny-config.ts +++ b/local-tests/setup/tinny-config.ts @@ -33,6 +33,9 @@ export interface ProcessEnvs { */ MAX_ATTEMPTS: number; + /** + * The maximum number of milliseconds to wait for a test to complete. + */ TEST_TIMEOUT: number; /** From 5c21795aefc2e4472ae25300d47e876fae6e1c3f Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 12:33:32 +0100 Subject: [PATCH 3/8] chore: revert testRelayer test --- local-tests/tests/testRelayer.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/local-tests/tests/testRelayer.ts b/local-tests/tests/testRelayer.ts index d474c9035b..9cdabd0962 100644 --- a/local-tests/tests/testRelayer.ts +++ b/local-tests/tests/testRelayer.ts @@ -50,21 +50,7 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => { signer: alice.wallet, }; - let claimRes; - - console.log('Initiating claimKeyId call'); - try { - claimRes = await withTimeout( - devEnv.litNodeClient.claimKeyId(claimRequest), - 10000 - ); // 10 seconds timeout - console.log('claimKeyId call completed'); - // process claimRes as before - } catch (error) { - console.error('❗️ claimKeyId call failed or timed out'); - devEnv.litNodeClient.disconnect(); - throw new Error(error); - } + const claimRes = await devEnv.litNodeClient.claimKeyId(claimRequest); // Expected output: // { From 6bfbe413b2614d66e01b3c081e5b062c21831079 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 12:46:13 +0100 Subject: [PATCH 4/8] chore: prettier --- local-tests/setup/tinny-environment.ts | 2 +- local-tests/setup/tinny-operations.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/local-tests/setup/tinny-environment.ts b/local-tests/setup/tinny-environment.ts index 26acdadcb6..bd1b350d12 100644 --- a/local-tests/setup/tinny-environment.ts +++ b/local-tests/setup/tinny-environment.ts @@ -30,7 +30,7 @@ export class TinnyEnvironment { */ public processEnvs: ProcessEnvs = { MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 1, - TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 60000, + TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 60000, NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN, DEBUG: process.env['DEBUG'] === 'true', REQUEST_PER_KILOSECOND: diff --git a/local-tests/setup/tinny-operations.ts b/local-tests/setup/tinny-operations.ts index 88278d8cf7..028b8c6d05 100644 --- a/local-tests/setup/tinny-operations.ts +++ b/local-tests/setup/tinny-operations.ts @@ -226,7 +226,9 @@ export const runTestsParallel = async ({ } const skippedTests = results.filter((result) => result.includes('Skipped')); - const failedTests = results.filter((result) => result.includes('Failed') || result.includes('Timed out')); + const failedTests = results.filter( + (result) => result.includes('Failed') || result.includes('Timed out') + ); const passedTests = results.filter((result) => result.includes('Passed')); if (skippedTests.length > 0) { From e8050d1cfba453f8ab060bd764ac92620ead9709 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 13:01:13 +0100 Subject: [PATCH 5/8] fix: reduce `TEST_TIMEOUT` to 30s --- local-tests/setup/tinny-environment.ts | 2 +- local-tests/setup/tinny-operations.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/local-tests/setup/tinny-environment.ts b/local-tests/setup/tinny-environment.ts index bd1b350d12..8428f1d99b 100644 --- a/local-tests/setup/tinny-environment.ts +++ b/local-tests/setup/tinny-environment.ts @@ -30,7 +30,7 @@ export class TinnyEnvironment { */ public processEnvs: ProcessEnvs = { MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 1, - TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 60000, + TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 30000, NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN, DEBUG: process.env['DEBUG'] === 'true', REQUEST_PER_KILOSECOND: diff --git a/local-tests/setup/tinny-operations.ts b/local-tests/setup/tinny-operations.ts index 028b8c6d05..ce6409d150 100644 --- a/local-tests/setup/tinny-operations.ts +++ b/local-tests/setup/tinny-operations.ts @@ -155,7 +155,7 @@ export const runTestsParallel = async ({ testIndex: number ): Promise => { const maxAttempts = devEnv.processEnvs.MAX_ATTEMPTS; - const testTimeout = devEnv.processEnvs.TEST_TIMEOUT || 60000; + const testTimeout = devEnv.processEnvs.TEST_TIMEOUT; let attempts = 0; let testPassed = false; From 437373915a48c0ce477ed511b12c041c9dcbfbf9 Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 15:50:17 +0100 Subject: [PATCH 6/8] fix: https://github.com/LIT-Protocol/js-sdk/pull/533#pullrequestreview-2175088427 --- .env.sample | 1 + 1 file changed, 1 insertion(+) diff --git a/.env.sample b/.env.sample index 3dd4de07f6..4f6d3eb720 100644 --- a/.env.sample +++ b/.env.sample @@ -11,6 +11,7 @@ PRIVATE_KEYS="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d NO_SETUP=false USE_SHIVA=false NETWORK_CONFIG=./networkContext.json +TEST_TIMEOUT=30000 #Shiva Client ENV Vars STOP_TESTNET=false From 07b2b6691dd544e68a03612efe06d6606285ec2a Mon Sep 17 00:00:00 2001 From: Anson Date: Fri, 12 Jul 2024 16:45:38 +0100 Subject: [PATCH 7/8] fix: https://github.com/LIT-Protocol/js-sdk/pull/533/files/e8050d1cfba453f8ab060bd764ac92620ead9709#r1676070417 --- local-tests/setup/tinny-utils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/local-tests/setup/tinny-utils.ts b/local-tests/setup/tinny-utils.ts index 35ede2f751..2be285519e 100644 --- a/local-tests/setup/tinny-utils.ts +++ b/local-tests/setup/tinny-utils.ts @@ -54,7 +54,10 @@ export function randomSolanaPrivateKey() { * @param ms - The timeout duration in milliseconds. * @returns A new promise that resolves or rejects based on the original promise or the timeout. */ -export function withTimeout(promise: Promise, ms: number): Promise { +export function withTimeout( + promise: Promise, + ms: number +): Promise { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error('Timed out')), ms) ); From 28323eb1d2572aec17834efff98cdcaae5688e4f Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 15 Jul 2024 19:27:44 +0100 Subject: [PATCH 8/8] fix: https://github.com/LIT-Protocol/js-sdk/pull/533#pullrequestreview-2178328957 --- .env.sample | 2 +- local-tests/setup/tinny-environment.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.sample b/.env.sample index 4f6d3eb720..4792e99da9 100644 --- a/.env.sample +++ b/.env.sample @@ -11,7 +11,7 @@ PRIVATE_KEYS="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d NO_SETUP=false USE_SHIVA=false NETWORK_CONFIG=./networkContext.json -TEST_TIMEOUT=30000 +TEST_TIMEOUT=45000 #Shiva Client ENV Vars STOP_TESTNET=false diff --git a/local-tests/setup/tinny-environment.ts b/local-tests/setup/tinny-environment.ts index 8428f1d99b..025a18f576 100644 --- a/local-tests/setup/tinny-environment.ts +++ b/local-tests/setup/tinny-environment.ts @@ -30,7 +30,7 @@ export class TinnyEnvironment { */ public processEnvs: ProcessEnvs = { MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 1, - TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 30000, + TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 45000, NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN, DEBUG: process.env['DEBUG'] === 'true', REQUEST_PER_KILOSECOND: