Skip to content

Commit

Permalink
feat: Add tests for openMediaHotStandby
Browse files Browse the repository at this point in the history
  • Loading branch information
olzzon committed Oct 28, 2024
1 parent c2b35be commit 5985b19
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
122 changes: 122 additions & 0 deletions packages/connector/src/__tests__/OpenMediaHotStandby.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { MosConnection } from "../MosConnection";
import { getMosConnection, setupMocks } from "./lib";
import { NCSServerConnection } from "../connection/NCSServerConnection";

describe('Hot Standby Feature', () => {
let mosConnection: MosConnection;
let primary: NCSServerConnection | null;
let secondary: NCSServerConnection | null;

beforeAll(() => {
setupMocks();
});

beforeEach(async () => {
mosConnection = await getMosConnection({
'0': true,
'1': true,
}, false);

const device = await mosConnection.connect({
primary: {
id: 'primary',
host: '127.0.0.1',
},
secondary: {
id: 'secondary',
host: '127.0.0.2',
openMediaHotStandby: true
}
});

// Wait for connections to be established
await new Promise(resolve => setTimeout(resolve, 100));

primary = device['_primaryConnection'];
secondary = device['_secondaryConnection'];
});

test('should disable secondary heartbeats when primary is connected', async () => {
expect(primary).toBeTruthy();
expect(secondary).toBeTruthy();

if (primary && secondary) {
expect(primary.isHearbeatEnabled()).toBe(true);
expect(secondary.isHearbeatEnabled()).toBe(false);
}
});

test('should enable secondary heartbeats when primary disconnects', async () => {
expect(primary).toBeTruthy();
expect(secondary).toBeTruthy();

if (primary && secondary) {
// Simulate primary disconnect
await primary.dispose();

// Wait for primary to disconnect
await new Promise(resolve => setTimeout(resolve, 100));

// Verify heartbeat states switched correctly
expect(secondary.isHearbeatEnabled()).toBe(true);
expect(primary.isHearbeatEnabled()).toBe(false);
}
});

test('should disable primary heartbeasts when secondary is connected and primary is disconnected', async () => {
expect(primary).toBeTruthy();
expect(secondary).toBeTruthy();

if (primary && secondary) {
// Simulate primary disconnect
await primary.dispose();

// Wait for primary to disconnect
await new Promise(resolve => setTimeout(resolve, 100));

// Wait for secondary to connect
await new Promise(resolve => setTimeout(resolve, 100));

// Verify heartbeat states switched correctly
expect(secondary.isHearbeatEnabled()).toBe(true);
expect(primary.isHearbeatEnabled()).toBe(false);
}
})

test('should handle rapid primary connection changes', async () => {
expect(primary).toBeTruthy();
expect(secondary).toBeTruthy();

if (primary && secondary) {
const connectionStates: boolean[] = [];

// Rapidly toggle primary connection
for (let i = 0; i < 5; i++) {
await primary.dispose();
await new Promise(resolve => setTimeout(resolve, 50));
primary.connect();
await new Promise(resolve => setTimeout(resolve, 50));

connectionStates.push(
secondary.connected,
primary.connected
);
}

// Verify states remained consistent
connectionStates.forEach((state, i) => {
if (i % 2 === 0) {
expect(state).toBe(false); // Secondary should be disabled
} else {
expect(state).toBe(true); // Primary should be enabled
}
});
}
});

afterEach(async () => {
if (mosConnection) {
await mosConnection.dispose();
}
});
});
10 changes: 9 additions & 1 deletion packages/connector/src/connection/NCSServerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,22 @@ export class NCSServerConnection extends EventEmitter<NCSServerConnectionEvents>
this._clients[i].useHeartbeats = false
}
}

/** */
enableHeartbeats(): void {
for (const i in this._clients) {
this._clients[i].useHeartbeats = true
}
}

/** */
isHearbeatEnabled(): boolean {
for (const i in this._clients) {
if (this._clients[i].useHeartbeats) return true
}
return false
}

connect(): void {
for (const i in this._clients) {
// Connect client
Expand Down

0 comments on commit 5985b19

Please sign in to comment.