Skip to content

Commit

Permalink
Merge pull request #533 from LIT-Protocol/feature/lit-3598-js-sdk-mak…
Browse files Browse the repository at this point in the history
…e-tinny-test-timeout

feat: add `TEST_TIMEOUT` for tests
  • Loading branch information
MaximusHaximus authored Jul 15, 2024
2 parents c92dc39 + 28323eb commit fb77647
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PRIVATE_KEYS="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
NO_SETUP=false
USE_SHIVA=false
NETWORK_CONFIG=./networkContext.json
TEST_TIMEOUT=45000

#Shiva Client ENV Vars
STOP_TESTNET=false
Expand Down
1 change: 1 addition & 0 deletions local-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). |
Expand Down
8 changes: 8 additions & 0 deletions local-tests/setup/tinny-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ export const RPC_MAP = {
[LIT_TESTNET.DATIL_TEST]: 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;

/**
* The maximum number of milliseconds to wait for a test to complete.
*/
TEST_TIMEOUT: number;

/**
* The network to use for testing. This can be one of the following:
* - `LIT_TESTNET.LOCALCHAIN`
Expand Down
1 change: 1 addition & 0 deletions local-tests/setup/tinny-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']) || 45000,
NETWORK: (process.env['NETWORK'] as LIT_TESTNET) || LIT_TESTNET.LOCALCHAIN,
DEBUG: process.env['DEBUG'] === 'true',
REQUEST_PER_KILOSECOND:
Expand Down
24 changes: 19 additions & 5 deletions local-tests/setup/tinny-operations.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -154,6 +155,8 @@ export const runTestsParallel = async ({
testIndex: number
): Promise<string> => {
const maxAttempts = devEnv.processEnvs.MAX_ATTEMPTS;
const testTimeout = devEnv.processEnvs.TEST_TIMEOUT;

let attempts = 0;
let testPassed = false;

Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -214,7 +226,9 @@ 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) {
Expand Down
18 changes: 18 additions & 0 deletions local-tests/setup/tinny-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ 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<T>(
promise: Promise<T>,
ms: number
): Promise<T | void> {
const timeout = new Promise<T>((_, reject) =>
setTimeout(() => reject(new Error('Timed out')), ms)
);
return Promise.race([promise, timeout]);
}
2 changes: 2 additions & 0 deletions local-tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ import { testSignTransactionWithSolanaEncryptedKey } from './tests/wrapped-keys/
const relayerTests = {
testRelayer,
};

// --filter=WrappedKey
const wrappedKeysTests = {
// -- valid cases
testEthereumSignMessageGeneratedKey,
Expand Down
5 changes: 3 additions & 2 deletions local-tests/tests/testRelayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -112,5 +113,5 @@ export const testRelayer = async (devEnv: TinnyEnvironment) => {
}
});

log('✅ testRelayer');
log('✅ 2. [testRelayer] Claim works');
};

0 comments on commit fb77647

Please sign in to comment.