@arifwidianto/rpc-agent
Version:
RPC Agent for both client and server, extends more methods easily
113 lines • 4.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const client_node_1 = require("../../library/client.node");
async function makeRandomRequest(client) {
const methods = [
async () => await client.echo("Hello RPC!"),
async () => await client.getCurrentDate(),
async () => await client.getServerInfo(),
async () => await client.listExtensions(),
async () => await client.validatePort(8080, Math.random() > 0.5 ? "TCP" : "UDP"),
];
const randomMethod = methods[Math.floor(Math.random() * methods.length)];
try {
const result = await randomMethod();
console.log("Random request result:", result);
}
catch (error) {
console.error("Random request failed:", error);
}
}
async function runRandomTests() {
const protocols = ["tcp", "udp"];
let requestCount = 0;
const maxRequests = 12;
const startTime = Date.now();
console.log("\n=== Running Random Protocol Tests ===");
const interval = setInterval(async () => {
if (requestCount >= maxRequests || Date.now() - startTime >= 60000) {
clearInterval(interval);
return;
}
const randomProtocol = protocols[Math.floor(Math.random() * protocols.length)];
console.log(`\n[Request ${requestCount + 1}] Using ${randomProtocol.toUpperCase()} protocol`);
const client = new client_node_1.AgentLibrary({
host: "127.0.0.1",
port: 9101,
udpPort: 9102,
protocol: randomProtocol,
}, true);
try {
await client.init();
await makeRandomRequest(client);
}
catch (error) {
console.error(`Test failed with ${randomProtocol.toUpperCase()}:`, error);
}
finally {
await client.close();
requestCount++;
if (requestCount >= maxRequests) {
console.log("\nCompleted all random protocol tests!");
clearInterval(interval);
}
}
}, 5000);
await new Promise((resolve) => setTimeout(resolve, 61000));
}
async function runTests(protocol) {
console.log(`\n=== Running ${protocol.toUpperCase()} Tests ===`);
const client = new client_node_1.AgentLibrary({
host: "127.0.0.1",
port: 9101,
udpPort: 9102,
protocol,
}, true);
try {
await client.init();
console.log(`${protocol.toUpperCase()} client initialized successfully`);
console.log(`\nTesting echo extension (${protocol.toUpperCase()})`);
const echoResult = await client.echo("Hello RPC!");
console.log("Echo result:", echoResult);
console.log(`\nTesting date extension (${protocol.toUpperCase()})`);
const dateResult = await client.getCurrentDate();
console.log("Current date:", dateResult);
console.log(`\nTesting server information (${protocol.toUpperCase()})`);
const serverInfo = await client.getServerInfo();
console.log("Server info:", serverInfo);
console.log(`\nTesting extension management (${protocol.toUpperCase()})`);
const extensions = await client.listExtensions();
console.log("Available extensions:", extensions);
console.log(`\nTesting port validation (${protocol.toUpperCase()})`);
const portValidation = await client.validatePort(8080, protocol === "tcp" ? "TCP" : "UDP");
console.log("Port validation result:", portValidation);
console.log(`\n${protocol.toUpperCase()} tests completed successfully`);
}
catch (error) {
console.error(`${protocol.toUpperCase()} test failed:`, error);
throw error;
}
finally {
await client.close();
await new Promise((resolve) => setTimeout(resolve, 500));
}
}
async function main() {
console.log("Starting test suite...");
try {
await runTests("tcp");
await new Promise((resolve) => setTimeout(resolve, 2000));
await runTests("udp");
await runRandomTests();
console.log("\nAll tests completed successfully!");
}
catch (error) {
console.error("\nTest suite failed:", error);
process.exit(1);
}
}
main().catch((error) => {
console.error("Test suite failed:", error);
process.exit(1);
});
//# sourceMappingURL=test.node.js.map