cluster-integration.js (4168B)
1 const path = require('path'); 2 const { 3 spawnDaemon, 4 killDaemon, 5 killAllDaemons, 6 connectApi, 7 apiCommand, 8 sleep, 9 findFreePort 10 } = require('./helpers'); 11 12 const NODE1_CONFIG = path.join(__dirname, 'config-cluster-node1.ini'); 13 const NODE2_CONFIG = path.join(__dirname, 'config-cluster-node2.ini'); 14 15 const DAEMON_PATH = path.join(__dirname, '..', 'udphole'); 16 const { spawn } = require('child_process'); 17 18 async function runTest() { 19 let daemon1 = null; 20 let daemon2 = null; 21 let cluster = null; 22 let apiSock1 = null; 23 let apiSock2 = null; 24 let clusterSock = null; 25 let returnCode = 0; 26 27 console.log('=== Cluster Integration Test ==='); 28 console.log('Testing: session creation, aggregation, and forwarding\n'); 29 30 try { 31 console.log('1. Spawning node 1 daemon...'); 32 daemon1 = await spawnDaemon(NODE1_CONFIG); 33 await sleep(1000); 34 console.log(' Node 1 started on port 19123'); 35 36 console.log('2. Spawning node 2 daemon...'); 37 daemon2 = await spawnDaemon(NODE2_CONFIG); 38 await sleep(1000); 39 console.log(' Node 2 started on port 19124'); 40 41 console.log('3. Connecting to node 1 API...'); 42 apiSock1 = await connectApi(19123); 43 console.log(' Connected'); 44 45 console.log('4. Connecting to node 2 API...'); 46 apiSock2 = await connectApi(19124); 47 console.log(' Connected'); 48 49 console.log('5. Authenticating with nodes...'); 50 let resp = await apiCommand(apiSock1, 'auth', 'finwo', 'testsecret'); 51 if (resp !== 'OK') throw new Error('Auth failed for node 1'); 52 resp = await apiCommand(apiSock2, 'auth', 'finwo', 'testsecret'); 53 if (resp !== 'OK') throw new Error('Auth failed for node 2'); 54 console.log(' Auth OK on both nodes'); 55 56 console.log('\n6. Creating sessions directly on nodes...'); 57 resp = await apiCommand(apiSock1, 'session.create', 'session-node1'); 58 console.log(` Node 1 session.create: ${JSON.stringify(resp)}`); 59 if (!Array.isArray(resp) || resp[0] !== 'OK') throw new Error(`Failed to create session on node 1: got ${JSON.stringify(resp)}`); 60 61 resp = await apiCommand(apiSock2, 'session.create', 'session-node2'); 62 console.log(` Node 2 session.create: ${JSON.stringify(resp)}`); 63 if (!Array.isArray(resp) || resp[0] !== 'OK') throw new Error('Failed to create session on node 2'); 64 65 console.log('\n7. Verifying session counts on individual nodes...'); 66 resp = await apiCommand(apiSock1, 'session.count'); 67 console.log(` Node 1 session.count: ${resp}`); 68 if (resp !== 1) throw new Error(`Expected 1 session on node 1, got ${resp}`); 69 70 resp = await apiCommand(apiSock2, 'session.count'); 71 console.log(` Node 2 session.count: ${resp}`); 72 if (resp !== 1) throw new Error(`Expected 1 session on node 2, got ${resp}`); 73 74 console.log('\n8. Verifying socket creation returns correct advertise address...'); 75 resp = await apiCommand(apiSock1, 'session.socket.create.listen', 'session-node1', 'socket1'); 76 console.log(` Node 1 socket create listen: ${JSON.stringify(resp)}`); 77 if (!Array.isArray(resp) || resp[1] !== '127.0.0.1') { 78 throw new Error(`Expected advertise address 127.0.0.1 for node 1, got ${JSON.stringify(resp)}`); 79 } 80 console.log(' ā Contains advertise address 127.0.0.1'); 81 82 resp = await apiCommand(apiSock2, 'session.socket.create.listen', 'session-node2', 'socket2'); 83 console.log(` Node 2 socket create listen: ${JSON.stringify(resp)}`); 84 if (!Array.isArray(resp) || resp[1] !== '127.0.0.2') { 85 throw new Error(`Expected advertise address 127.0.0.2 for node 2, got ${JSON.stringify(resp)}`); 86 } 87 console.log(' ā Contains advertise address 127.0.0.2'); 88 89 console.log('\nā PASS: Cluster integration test passed'); 90 91 } catch (err) { 92 console.error(`\nā FAIL: ${err.message}`); 93 console.error(err.stack); 94 returnCode = 1; 95 } finally { 96 if (apiSock1) apiSock1.end(); 97 if (apiSock2) apiSock2.end(); 98 if (clusterSock) clusterSock.end(); 99 if (daemon1) await killDaemon(daemon1); 100 if (daemon2) await killDaemon(daemon2); 101 if (cluster) await killDaemon(cluster); 102 await killAllDaemons(); 103 process.exit(returnCode); 104 } 105 } 106 107 runTest();