Skip to content

Commit fb77647

Browse files
Merge pull request #533 from LIT-Protocol/feature/lit-3598-js-sdk-make-tinny-test-timeout
feat: add `TEST_TIMEOUT` for tests
2 parents c92dc39 + 28323eb commit fb77647

File tree

8 files changed

+53
-7
lines changed

8 files changed

+53
-7
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PRIVATE_KEYS="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
1111
NO_SETUP=false
1212
USE_SHIVA=false
1313
NETWORK_CONFIG=./networkContext.json
14+
TEST_TIMEOUT=45000
1415

1516
#Shiva Client ENV Vars
1617
STOP_TESTNET=false

local-tests/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Below is the API documentation for the `ProcessEnvs` interface, detailing the co
4040
| Variable | Description |
4141
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4242
| `MAX_ATTEMPTS` | Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`. |
43+
| `TEST_TIMEOUT` | The maximum number of milliseconds to wait for a test to complete. |
4344
| `NETWORK` | The network to use for testing, which can be one of the following: `LIT_TESTNET.LOCALCHAIN`, `LIT_TESTNET.MANZANO`, or `LIT_TESTNET.CAYENNE`. |
4445
| `DEBUG` | Specifies whether to enable debug mode. |
4546
| `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). |

local-tests/setup/tinny-config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ export const RPC_MAP = {
2626
[LIT_TESTNET.DATIL_TEST]: LIT_RPC.VESUVIUS,
2727
};
2828

29+
/**
30+
* Represents the configuration options for the process environment.
31+
*/
2932
export interface ProcessEnvs {
3033
/**
3134
* Each test is executed in a loop with a maximum number of attempts specified by `devEnv.processEnvs.MAX_ATTEMPTS`.
3235
*/
3336
MAX_ATTEMPTS: number;
3437

38+
/**
39+
* The maximum number of milliseconds to wait for a test to complete.
40+
*/
41+
TEST_TIMEOUT: number;
42+
3543
/**
3644
* The network to use for testing. This can be one of the following:
3745
* - `LIT_TESTNET.LOCALCHAIN`

local-tests/setup/tinny-environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class TinnyEnvironment {
3030
*/
3131
public processEnvs: ProcessEnvs = {
3232
MAX_ATTEMPTS: parseInt(process.env['MAX_ATTEMPTS']) || 1,
33+
TEST_TIMEOUT: parseInt(process.env['TEST_TIMEOUT']) || 45000,
3334
NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN,
3435
DEBUG: process.env['DEBUG'] === 'true',
3536
REQUEST_PER_KILOSECOND:

local-tests/setup/tinny-operations.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TinnyEnvironment } from './tinny-environment';
2+
import { withTimeout } from './tinny-utils';
23

34
/**
45
* Retrieves filter flags from the command line arguments to determine which tests to run.
@@ -154,6 +155,8 @@ export const runTestsParallel = async ({
154155
testIndex: number
155156
): Promise<string> => {
156157
const maxAttempts = devEnv.processEnvs.MAX_ATTEMPTS;
158+
const testTimeout = devEnv.processEnvs.TEST_TIMEOUT;
159+
157160
let attempts = 0;
158161
let testPassed = false;
159162

@@ -166,8 +169,7 @@ export const runTestsParallel = async ({
166169
}. ${testName}...\x1b[0m`
167170
);
168171

169-
// @ts-ignore
170-
await testFunction(devEnv);
172+
await withTimeout(testFunction(devEnv), testTimeout);
171173
testPassed = true;
172174

173175
const endTime = performance.now();
@@ -184,9 +186,19 @@ export const runTestsParallel = async ({
184186
}
185187
attempts++;
186188

189+
const endTime = performance.now();
190+
const timeTaken = (endTime - startTime).toFixed(2);
191+
192+
if (error.message === 'Timed out') {
193+
console.error(
194+
`\x1b[31m✖\x1b[90m ${
195+
testIndex + 1
196+
}. ${testName} - Timed out after ${testTimeout}ms (${timeTaken} ms)\x1b[0m`
197+
);
198+
return `${testName} (Timed out in ${timeTaken} ms)`;
199+
}
200+
187201
if (attempts >= maxAttempts) {
188-
const endTime = performance.now();
189-
const timeTaken = (endTime - startTime).toFixed(2);
190202
console.error(
191203
`\x1b[31m✖\x1b[90m ${
192204
testIndex + 1
@@ -214,7 +226,9 @@ export const runTestsParallel = async ({
214226
}
215227

216228
const skippedTests = results.filter((result) => result.includes('Skipped'));
217-
const failedTests = results.filter((result) => result.includes('Failed'));
229+
const failedTests = results.filter(
230+
(result) => result.includes('Failed') || result.includes('Timed out')
231+
);
218232
const passedTests = results.filter((result) => result.includes('Passed'));
219233

220234
if (skippedTests.length > 0) {

local-tests/setup/tinny-utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ export function randomSolanaPrivateKey() {
4545
}
4646
return result;
4747
}
48+
49+
/**
50+
* Wraps a promise with a timeout.
51+
* If the promise does not resolve or reject within the specified time, it will be rejected with a "Timed out" error.
52+
*
53+
* @param promise - The promise to wrap with a timeout.
54+
* @param ms - The timeout duration in milliseconds.
55+
* @returns A new promise that resolves or rejects based on the original promise or the timeout.
56+
*/
57+
export function withTimeout<T>(
58+
promise: Promise<T>,
59+
ms: number
60+
): Promise<T | void> {
61+
const timeout = new Promise<T>((_, reject) =>
62+
setTimeout(() => reject(new Error('Timed out')), ms)
63+
);
64+
return Promise.race([promise, timeout]);
65+
}

local-tests/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/
115115
const relayerTests = {
116116
testRelayer,
117117
};
118+
119+
// --filter=WrappedKey
118120
const wrappedKeysTests = {
119121
// -- valid cases
120122
testEthereumSignMessageGeneratedKey,

local-tests/tests/testRelayer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
LitAuthClient,
1111
} from '@lit-protocol/lit-auth-client';
1212
import { ProviderType } from '@lit-protocol/constants';
13+
import { withTimeout } from 'local-tests/setup/tinny-utils';
1314

1415
/**
1516
* Test Commands:
@@ -40,7 +41,7 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => {
4041
if (pkps.length <= 0) {
4142
throw new Error('No PKPs found');
4243
} else {
43-
console.log('✅ [testRelayer] /fetch-pkps-by-auth-method works');
44+
console.log('✅ 1. [testRelayer] /fetch-pkps-by-auth-method works');
4445
}
4546

4647
// -- test claims
@@ -112,5 +113,5 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => {
112113
}
113114
});
114115

115-
log('✅ testRelayer');
116+
log('✅ 2. [testRelayer] Claim works');
116117
};

0 commit comments

Comments
 (0)