-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathtest-with-validator.js
More file actions
116 lines (94 loc) · 3.02 KB
/
test-with-validator.js
File metadata and controls
116 lines (94 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { spawn } from 'child_process';
import { setTimeout } from 'timers/promises';
import { platform } from 'os';
const config = {
validatorStartupTime: parseInt(process.env.SOLANA_VALIDATOR_STARTUP_TIME) || 3000,
validatorArgs: (process.env.SOLANA_VALIDATOR_ARGS || '-r').split(' '),
maxHealthCheckRetries: 10
};
let validatorProcess = null;
async function checkSolanaCLI() {
try {
const checkProcess = spawn('solana', ['--version'], { stdio: 'pipe' });
const exitCode = await new Promise((resolve) => {
checkProcess.on('close', resolve);
});
if (exitCode !== 0) {
throw new Error();
}
} catch (error) {
console.error('Solana CLI not found. Please install: https://docs.solana.com/cli/install-solana-cli-tools');
process.exit(1);
}
}
async function waitForValidator() {
console.log('Waiting for validator to be ready...');
for (let i = 0; i < config.maxHealthCheckRetries; i++) {
try {
const checkProcess = spawn('solana', ['cluster-version'], { stdio: 'pipe' });
const exitCode = await new Promise((resolve) => {
checkProcess.on('close', resolve);
});
if (exitCode === 0) {
console.log('Validator is ready!');
await setTimeout(2_000);
return;
}
} catch (error) {
// Continue waiting
}
await setTimeout(1000);
}
throw new Error('Validator failed to start within timeout period');
}
async function runTestsWithValidator() {
try {
await checkSolanaCLI();
console.log('Starting Solana test validator...');
validatorProcess = spawn('solana-test-validator', config.validatorArgs, {
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
});
// Handle validator errors
validatorProcess.on('error', (error) => {
console.error('Failed to start validator:', error.message);
process.exit(1);
});
await waitForValidator();
console.log('Running tests...');
const testProcess = spawn('jest', ['test/integration.test.ts'], {
stdio: 'inherit',
shell: platform() === 'win32' // Windows compatibility
});
const testExitCode = await new Promise((resolve) => {
testProcess.on('close', resolve);
});
console.log(`Tests completed with exit code: ${testExitCode}`);
process.exit(testExitCode);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
function cleanup() {
if (validatorProcess && !validatorProcess.killed) {
console.log('Shutting down Solana test validator...');
// Graceful shutdown
validatorProcess.kill('SIGTERM');
// Force kill after 5 seconds
setTimeout(() => {
if (validatorProcess && !validatorProcess.killed) {
validatorProcess.kill('SIGKILL');
}
}, 5000);
}
}
process.on('exit', cleanup);
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
cleanup();
process.exit(1);
});
runTestsWithValidator();