mirror of
https://github.com/nvms/prsm.git
synced 2025-12-16 16:10:54 +00:00
Remove pointless comments and clean up a bit.
This commit is contained in:
parent
7714d71b0a
commit
fc2f2bea12
@ -131,10 +131,8 @@ export class KeepAliveClient extends EventEmitter {
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
// Create a new WebSocket connection
|
|
||||||
this.socket = new WebSocket(this.url);
|
this.socket = new WebSocket(this.url);
|
||||||
|
|
||||||
// Set up a direct onopen handler to ensure we catch the connection event
|
|
||||||
this.socket.onopen = () => {
|
this.socket.onopen = () => {
|
||||||
this._status = Status.ONLINE;
|
this._status = Status.ONLINE;
|
||||||
this.connection.socket = this.socket;
|
this.connection.socket = this.socket;
|
||||||
@ -146,7 +144,6 @@ export class KeepAliveClient extends EventEmitter {
|
|||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set up a direct onerror handler for immediate connection errors
|
|
||||||
this.socket.onerror = (error) => {
|
this.socket.onerror = (error) => {
|
||||||
this._status = Status.OFFLINE;
|
this._status = Status.OFFLINE;
|
||||||
reject(
|
reject(
|
||||||
|
|||||||
@ -2,12 +2,11 @@ import { describe, test, expect, beforeEach, afterEach } from "vitest";
|
|||||||
import { KeepAliveClient, Status } from "../src/client/client";
|
import { KeepAliveClient, Status } from "../src/client/client";
|
||||||
import { KeepAliveServer } from "../src/server/index";
|
import { KeepAliveServer } from "../src/server/index";
|
||||||
|
|
||||||
// Helper to create a WebSocket server for testing
|
|
||||||
const createTestServer = (port: number) => {
|
const createTestServer = (port: number) => {
|
||||||
return new KeepAliveServer({
|
return new KeepAliveServer({
|
||||||
port,
|
port,
|
||||||
pingInterval: 1000, // Faster for testing
|
pingInterval: 1000,
|
||||||
latencyInterval: 500, // Faster for testing
|
latencyInterval: 500,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -19,7 +18,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
server = createTestServer(port);
|
server = createTestServer(port);
|
||||||
|
|
||||||
// Wait for the server to start
|
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
server.on("listening", () => {
|
server.on("listening", () => {
|
||||||
resolve();
|
resolve();
|
||||||
@ -35,12 +33,10 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
// Close connections in order
|
|
||||||
if (client.status === Status.ONLINE) {
|
if (client.status === Status.ONLINE) {
|
||||||
await client.close();
|
await client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the server
|
|
||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((resolve) => {
|
||||||
if (server) {
|
if (server) {
|
||||||
server.close(() => {
|
server.close(() => {
|
||||||
@ -59,7 +55,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
|
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
|
||||||
// Expect it to fail after a short timeout
|
|
||||||
await expect(
|
await expect(
|
||||||
client.command("never-responds", "Should timeout", 500),
|
client.command("never-responds", "Should timeout", 500),
|
||||||
).rejects.toThrow(/timed out/);
|
).rejects.toThrow(/timed out/);
|
||||||
@ -72,13 +67,11 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
|
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
|
||||||
// Expect to receive this error
|
|
||||||
const result = await client.command("throws-error", "Will error", 1000);
|
const result = await client.command("throws-error", "Will error", 1000);
|
||||||
expect(result).toHaveProperty("error", "Custom server error");
|
expect(result).toHaveProperty("error", "Custom server error");
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
|
||||||
test("multiple concurrent commands are handled correctly", async () => {
|
test("multiple concurrent commands are handled correctly", async () => {
|
||||||
// Register commands with different delays
|
|
||||||
await server.registerCommand("fast", async (context) => {
|
await server.registerCommand("fast", async (context) => {
|
||||||
await new Promise((r) => setTimeout(r, 50));
|
await new Promise((r) => setTimeout(r, 50));
|
||||||
return `Fast: ${context.payload}`;
|
return `Fast: ${context.payload}`;
|
||||||
@ -95,14 +88,12 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
|
|
||||||
await client.connect();
|
await client.connect();
|
||||||
|
|
||||||
// Send multiple commands concurrently
|
|
||||||
const results = await Promise.all([
|
const results = await Promise.all([
|
||||||
client.command("fast", "First", 1000),
|
client.command("fast", "First", 1000),
|
||||||
client.command("slow", "Second", 1000),
|
client.command("slow", "Second", 1000),
|
||||||
client.command("echo", "Third", 1000),
|
client.command("echo", "Third", 1000),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Verify all commands completed successfully
|
|
||||||
expect(results).toEqual(["Fast: First", "Slow: Second", "Echo: Third"]);
|
expect(results).toEqual(["Fast: First", "Slow: Second", "Echo: Third"]);
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
||||||
@ -128,7 +119,6 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
|
|
||||||
const result = await client.command("echo", largeData, 5000);
|
const result = await client.command("echo", largeData, 5000);
|
||||||
|
|
||||||
// Verify the response contains the expected data
|
|
||||||
expect(result).toEqual(largeData);
|
expect(result).toEqual(largeData);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
@ -137,25 +127,20 @@ describe("Advanced KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
return `Echo: ${context.payload}`;
|
return `Echo: ${context.payload}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create multiple clients
|
|
||||||
const clients = Array(5)
|
const clients = Array(5)
|
||||||
.fill(0)
|
.fill(0)
|
||||||
.map(() => new KeepAliveClient(`ws://localhost:${port}`));
|
.map(() => new KeepAliveClient(`ws://localhost:${port}`));
|
||||||
|
|
||||||
// Connect all clients
|
|
||||||
await Promise.all(clients.map((client) => client.connect()));
|
await Promise.all(clients.map((client) => client.connect()));
|
||||||
|
|
||||||
// Send a command from each client
|
|
||||||
const results = await Promise.all(
|
const results = await Promise.all(
|
||||||
clients.map((client, i) => client.command("echo", `Client ${i}`, 1000)),
|
clients.map((client, i) => client.command("echo", `Client ${i}`, 1000)),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Verify all commands succeeded
|
|
||||||
results.forEach((result, i) => {
|
results.forEach((result, i) => {
|
||||||
expect(result).toBe(`Echo: Client ${i}`);
|
expect(result).toBe(`Echo: Client ${i}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clean up
|
|
||||||
await Promise.all(clients.map((client) => client.close()));
|
await Promise.all(clients.map((client) => client.close()));
|
||||||
}, 5000);
|
}, 5000);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,14 +1,12 @@
|
|||||||
import { describe, test, expect, beforeEach, afterEach } from "vitest";
|
import { describe, test, expect, beforeEach, afterEach } from "vitest";
|
||||||
import { KeepAliveClient, Status } from "../src/client/client";
|
import { KeepAliveClient, Status } from "../src/client/client";
|
||||||
import { KeepAliveServer } from "../src/server/index";
|
import { KeepAliveServer } from "../src/server/index";
|
||||||
import { WebSocket, WebSocketServer } from "ws";
|
|
||||||
|
|
||||||
// Helper to create a WebSocket server for testing
|
|
||||||
const createTestServer = (port: number) => {
|
const createTestServer = (port: number) => {
|
||||||
return new KeepAliveServer({
|
return new KeepAliveServer({
|
||||||
port,
|
port,
|
||||||
pingInterval: 1000, // Faster for testing
|
pingInterval: 1000,
|
||||||
latencyInterval: 500, // Faster for testing
|
latencyInterval: 500,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -20,13 +18,11 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
server = createTestServer(port);
|
server = createTestServer(port);
|
||||||
|
|
||||||
// Wait for the server to start
|
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
server.on("listening", () => {
|
server.on("listening", () => {
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
// In case the server is already listening
|
|
||||||
if (server.listening) {
|
if (server.listening) {
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
@ -36,12 +32,10 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
// Close connections in order
|
|
||||||
if (client.status === Status.ONLINE) {
|
if (client.status === Status.ONLINE) {
|
||||||
await client.close();
|
await client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the server
|
|
||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((resolve) => {
|
||||||
if (server) {
|
if (server) {
|
||||||
server.close(() => {
|
server.close(() => {
|
||||||
@ -81,7 +75,6 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
await client.connect();
|
await client.connect();
|
||||||
expect(client.status).toBe(Status.ONLINE);
|
expect(client.status).toBe(Status.ONLINE);
|
||||||
|
|
||||||
// Second connect should resolve immediately
|
|
||||||
await client.connect();
|
await client.connect();
|
||||||
expect(client.status).toBe(Status.ONLINE);
|
expect(client.status).toBe(Status.ONLINE);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
@ -90,7 +83,6 @@ describe("Basic KeepAliveClient and KeepAliveServer Tests", () => {
|
|||||||
await client.close();
|
await client.close();
|
||||||
expect(client.status).toBe(Status.OFFLINE);
|
expect(client.status).toBe(Status.OFFLINE);
|
||||||
|
|
||||||
// Second close should resolve immediately
|
|
||||||
await client.close();
|
await client.close();
|
||||||
expect(client.status).toBe(Status.OFFLINE);
|
expect(client.status).toBe(Status.OFFLINE);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user