-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathWebSocketHealthMonitoring.test.ts
More file actions
107 lines (79 loc) · 3.1 KB
/
WebSocketHealthMonitoring.test.ts
File metadata and controls
107 lines (79 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { AWSAppSyncRealTimeProvider } from '../src/Providers/AWSAppSyncRealTimeProvider';
import { ConnectionState as CS } from '../src/types/PubSub';
describe('WebSocket Health Monitoring', () => {
let provider: AWSAppSyncRealTimeProvider;
beforeEach(() => {
provider = new AWSAppSyncRealTimeProvider();
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('getConnectionHealth', () => {
test('returns healthy when connected with recent keep-alive', () => {
(provider as any).connectionState = CS.Connected;
(provider as any).keepAliveTimestamp = Date.now();
const health = provider.getConnectionHealth();
expect(health.isHealthy).toBe(true);
expect(health.connectionState).toBe(CS.Connected);
expect(health.lastKeepAliveTime).toBeGreaterThan(0);
expect(health.timeSinceLastKeepAlive).toBeLessThan(1000);
});
test('returns unhealthy when not connected', () => {
(provider as any).connectionState = CS.Disconnected;
(provider as any).keepAliveTimestamp = Date.now();
const health = provider.getConnectionHealth();
expect(health.isHealthy).toBe(false);
expect(health.connectionState).toBe(CS.Disconnected);
});
test('returns unhealthy when keep-alive is stale (>65s)', () => {
(provider as any).connectionState = CS.Connected;
(provider as any).keepAliveTimestamp = Date.now() - 66_000;
const health = provider.getConnectionHealth();
expect(health.isHealthy).toBe(false);
expect(health.connectionState).toBe(CS.Connected);
expect(health.timeSinceLastKeepAlive).toBeGreaterThan(65_000);
});
test('returns unhealthy during connection disruption', () => {
(provider as any).connectionState = CS.ConnectionDisrupted;
(provider as any).keepAliveTimestamp = Date.now();
const health = provider.getConnectionHealth();
expect(health.isHealthy).toBe(false);
expect(health.connectionState).toBe(CS.ConnectionDisrupted);
});
test('defaults connectionState to Disconnected when undefined', () => {
(provider as any).connectionState = undefined;
const health = provider.getConnectionHealth();
expect(health.connectionState).toBe(CS.Disconnected);
});
});
describe('isConnected', () => {
test('returns true when WebSocket readyState is OPEN', () => {
(provider as any).awsRealTimeSocket = {
readyState: WebSocket.OPEN,
};
expect(provider.isConnected()).toBe(true);
});
test('returns false when WebSocket is undefined', () => {
(provider as any).awsRealTimeSocket = undefined;
expect(provider.isConnected()).toBe(false);
});
test('returns false when WebSocket is CONNECTING', () => {
(provider as any).awsRealTimeSocket = {
readyState: WebSocket.CONNECTING,
};
expect(provider.isConnected()).toBe(false);
});
test('returns false when WebSocket is CLOSED', () => {
(provider as any).awsRealTimeSocket = {
readyState: WebSocket.CLOSED,
};
expect(provider.isConnected()).toBe(false);
});
test('returns false when WebSocket is CLOSING', () => {
(provider as any).awsRealTimeSocket = {
readyState: WebSocket.CLOSING,
};
expect(provider.isConnected()).toBe(false);
});
});
});