Remove pointless comments and clean up a bit.

This commit is contained in:
nvms 2025-03-26 21:12:41 -04:00
parent 7714d71b0a
commit fc2f2bea12
5 changed files with 6 additions and 32 deletions

View File

@ -131,10 +131,8 @@ export class KeepAliveClient extends EventEmitter {
return new Promise((resolve, reject) => {
try {
// Create a new WebSocket connection
this.socket = new WebSocket(this.url);
// Set up a direct onopen handler to ensure we catch the connection event
this.socket.onopen = () => {
this._status = Status.ONLINE;
this.connection.socket = this.socket;
@ -146,7 +144,6 @@ export class KeepAliveClient extends EventEmitter {
resolve();
};
// Set up a direct onerror handler for immediate connection errors
this.socket.onerror = (error) => {
this._status = Status.OFFLINE;
reject(

View File

@ -2,12 +2,11 @@ import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { KeepAliveClient, Status } from "../src/client/client";
import { KeepAliveServer } from "../src/server/index";
// Helper to create a WebSocket server for testing
const createTestServer = (port: number) => {
return new KeepAliveServer({
port,
pingInterval: 1000, // Faster for testing
latencyInterval: 500, // Faster for testing
pingInterval: 1000,
latencyInterval: 500,
});
};
@ -19,7 +18,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
beforeEach(async () => {
server = createTestServer(port);
// Wait for the server to start
await new Promise<void>((resolve) => {
server.on("listening", () => {
resolve();
@ -35,12 +33,10 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
});
afterEach(async () => {
// Close connections in order
if (client.status === Status.ONLINE) {
await client.close();
}
// Close the server
return new Promise<void>((resolve) => {
if (server) {
server.close(() => {
@ -59,7 +55,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
await client.connect();
// Expect it to fail after a short timeout
await expect(
client.command("never-responds", "Should timeout", 500),
).rejects.toThrow(/timed out/);
@ -72,13 +67,11 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
await client.connect();
// Expect to receive this error
const result = await client.command("throws-error", "Will error", 1000);
expect(result).toHaveProperty("error", "Custom server error");
}, 2000);
test("multiple concurrent commands are handled correctly", async () => {
// Register commands with different delays
await server.registerCommand("fast", async (context) => {
await new Promise((r) => setTimeout(r, 50));
return `Fast: ${context.payload}`;
@ -95,14 +88,12 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
await client.connect();
// Send multiple commands concurrently
const results = await Promise.all([
client.command("fast", "First", 1000),
client.command("slow", "Second", 1000),
client.command("echo", "Third", 1000),
]);
// Verify all commands completed successfully
expect(results).toEqual(["Fast: First", "Slow: Second", "Echo: Third"]);
}, 3000);
@ -128,7 +119,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
const result = await client.command("echo", largeData, 5000);
// Verify the response contains the expected data
expect(result).toEqual(largeData);
}, 10000);
@ -137,25 +127,20 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
return `Echo: ${context.payload}`;
});
// Create multiple clients
const clients = Array(5)
.fill(0)
.map(() => new KeepAliveClient(`ws://localhost:${port}`));
// Connect all clients
await Promise.all(clients.map((client) => client.connect()));
// Send a command from each client
const results = await Promise.all(
clients.map((client, i) => client.command("echo", `Client ${i}`, 1000)),
);
// Verify all commands succeeded
results.forEach((result, i) => {
expect(result).toBe(`Echo: Client ${i}`);
});
// Clean up
await Promise.all(clients.map((client) => client.close()));
}, 5000);
});

View File

@ -1,14 +1,12 @@
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { KeepAliveClient, Status } from "../src/client/client";
import { KeepAliveServer } from "../src/server/index";
import { WebSocket, WebSocketServer } from "ws";
// Helper to create a WebSocket server for testing
const createTestServer = (port: number) => {
return new KeepAliveServer({
port,
pingInterval: 1000, // Faster for testing
latencyInterval: 500, // Faster for testing
pingInterval: 1000,
latencyInterval: 500,
});
};
@ -20,13 +18,11 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
beforeEach(async () => {
server = createTestServer(port);
// Wait for the server to start
await new Promise<void>((resolve) => {
server.on("listening", () => {
resolve();
});
// In case the server is already listening
if (server.listening) {
resolve();
}
@ -36,12 +32,10 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
});
afterEach(async () => {
// Close connections in order
if (client.status === Status.ONLINE) {
await client.close();
}
// Close the server
return new Promise<void>((resolve) => {
if (server) {
server.close(() => {
@ -81,7 +75,6 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
await client.connect();
expect(client.status).toBe(Status.ONLINE);
// Second connect should resolve immediately
await client.connect();
expect(client.status).toBe(Status.ONLINE);
}, 10000);
@ -90,7 +83,6 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
await client.close();
expect(client.status).toBe(Status.OFFLINE);
// Second close should resolve immediately
await client.close();
expect(client.status).toBe(Status.OFFLINE);
}, 10000);