fix: remove node_modules
This commit is contained in:
77
test/test-discovery.js
Normal file
77
test/test-discovery.js
Normal file
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Test script for UDP discovery
|
||||
* Sends CLUSTER_DISCOVERY messages to test the backend discovery functionality
|
||||
*/
|
||||
|
||||
const dgram = require('dgram');
|
||||
const client = dgram.createSocket('udp4');
|
||||
|
||||
const DISCOVERY_MESSAGE = 'CLUSTER_DISCOVERY';
|
||||
const TARGET_PORT = 4210;
|
||||
const BROADCAST_ADDRESS = '255.255.255.255';
|
||||
|
||||
// Enable broadcast
|
||||
client.setBroadcast(true);
|
||||
|
||||
function sendDiscoveryMessage() {
|
||||
const message = Buffer.from(DISCOVERY_MESSAGE);
|
||||
|
||||
client.send(message, 0, message.length, TARGET_PORT, BROADCAST_ADDRESS, (err) => {
|
||||
if (err) {
|
||||
console.error('Error sending discovery message:', err);
|
||||
} else {
|
||||
console.log(`Sent CLUSTER_DISCOVERY message to ${BROADCAST_ADDRESS}:${TARGET_PORT}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function sendDiscoveryToSpecificIP(ip) {
|
||||
const message = Buffer.from(DISCOVERY_MESSAGE);
|
||||
|
||||
client.send(message, 0, message.length, TARGET_PORT, ip, (err) => {
|
||||
if (err) {
|
||||
console.error(`Error sending discovery message to ${ip}:`, err);
|
||||
} else {
|
||||
console.log(`Sent CLUSTER_DISCOVERY message to ${ip}:${TARGET_PORT}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Main execution
|
||||
const args = process.argv.slice(2);
|
||||
|
||||
if (args.length === 0) {
|
||||
console.log('Usage: node test-discovery.js [broadcast|ip] [count]');
|
||||
console.log(' broadcast: Send to broadcast address (default)');
|
||||
console.log(' ip: Send to specific IP address');
|
||||
console.log(' count: Number of messages to send (default: 1)');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const target = args[0];
|
||||
const count = parseInt(args[1]) || 1;
|
||||
|
||||
console.log(`Sending ${count} discovery message(s) to ${target === 'broadcast' ? 'broadcast' : target}`);
|
||||
|
||||
if (target === 'broadcast') {
|
||||
for (let i = 0; i < count; i++) {
|
||||
setTimeout(() => {
|
||||
sendDiscoveryMessage();
|
||||
}, i * 1000); // Send one message per second
|
||||
}
|
||||
} else {
|
||||
// Assume it's an IP address
|
||||
for (let i = 0; i < count; i++) {
|
||||
setTimeout(() => {
|
||||
sendDiscoveryToSpecificIP(target);
|
||||
}, i * 1000); // Send one message per second
|
||||
}
|
||||
}
|
||||
|
||||
// Close the client after sending all messages
|
||||
setTimeout(() => {
|
||||
client.close();
|
||||
console.log('Test completed');
|
||||
}, (count + 1) * 1000);
|
||||
Reference in New Issue
Block a user