udphole

Basic UDP wormhole proxy
git clone git://git.finwo.net/app/udphole
Log | Files | Refs | README | LICENSE

cluster-basic.js (2807B)


      1 const path = require('path');
      2 const {
      3   spawnDaemon,
      4   killDaemon,
      5   killAllDaemons,
      6   connectApi,
      7   apiCommand,
      8   sleep
      9 } = require('./helpers');
     10 
     11 const NODE1_CONFIG = path.join(__dirname, 'config-cluster-node1.ini');
     12 const NODE2_CONFIG = path.join(__dirname, 'config-cluster-node2.ini');
     13 const CLUSTER_CONFIG = path.join(__dirname, 'config-cluster.ini');
     14 
     15 async function runTest() {
     16   let daemon1 = null;
     17   let daemon2 = null;
     18   let cluster = null;
     19   let apiSock1 = null;
     20   let apiSock2 = null;
     21   let clusterSock = null;
     22   let returnCode = 0;
     23 
     24   console.log('=== Cluster Daemon Test ===');
     25   console.log('Testing: session distribution across clustered nodes\n');
     26 
     27   try {
     28     console.log('1. Spawning node 1 daemon...');
     29     daemon1 = await spawnDaemon(NODE1_CONFIG);
     30     await sleep(1000);
     31     console.log('   Node 1 started on port 19123');
     32 
     33     console.log('2. Spawning node 2 daemon...');
     34     daemon2 = await spawnDaemon(NODE2_CONFIG);
     35     await sleep(1000);
     36     console.log('   Node 2 started on port 19124');
     37 
     38     console.log('3. Connecting to node 1 API...');
     39     apiSock1 = await connectApi(19123);
     40     console.log('   Connected');
     41 
     42     console.log('4. Connecting to node 2 API...');
     43     apiSock2 = await connectApi(19124);
     44     console.log('   Connected');
     45 
     46     console.log('5. Authenticating with node 1...');
     47     let resp = await apiCommand(apiSock1, 'auth', 'finwo', 'testsecret');
     48     console.log(`   Auth response: ${resp}`);
     49     if (resp !== 'OK') throw new Error('Auth failed for node 1');
     50 
     51     console.log('6. Authenticating with node 2...');
     52     resp = await apiCommand(apiSock2, 'auth', 'finwo', 'testsecret');
     53     console.log(`   Auth response: ${resp}`);
     54     if (resp !== 'OK') throw new Error('Auth failed for node 2');
     55 
     56     console.log('\n7. Testing individual nodes have 0 sessions...');
     57     resp = await apiCommand(apiSock1, 'session.count');
     58     console.log(`   Node 1 session.count: ${resp}`);
     59     if (resp !== 0) throw new Error('Expected 0 sessions on node 1');
     60 
     61     resp = await apiCommand(apiSock2, 'session.count');
     62     console.log(`   Node 2 session.count: ${resp}`);
     63     if (resp !== 0) throw new Error('Expected 0 sessions on node 2');
     64 
     65     console.log('\n✓ PASS: Basic cluster setup works');
     66     console.log('\nNote: Full cluster daemon requires cluster config file.');
     67     console.log('This test verified that node daemons can run independently.');
     68 
     69   } catch (err) {
     70     console.error(`\n✗ FAIL: ${err.message}`);
     71     returnCode = 1;
     72   } finally {
     73     if (apiSock1) apiSock1.end();
     74     if (apiSock2) apiSock2.end();
     75     if (clusterSock) clusterSock.end();
     76     if (daemon1) await killDaemon(daemon1);
     77     if (daemon2) await killDaemon(daemon2);
     78     if (cluster) await killDaemon(cluster);
     79     await killAllDaemons();
     80     process.exit(returnCode);
     81   }
     82 }
     83 
     84 runTest();