-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConnectionManager.test..ts
More file actions
152 lines (127 loc) · 4.26 KB
/
ConnectionManager.test..ts
File metadata and controls
152 lines (127 loc) · 4.26 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import * as grpc from '@grpc/grpc-js';
import { ConnectionManager } from '../../../client/connectionManager/ConnectionManager';
import { DuplexMessageEvent } from '../../../models/message.model';
// Mock duplex stream
const mockStream = {
on: jest.fn(),
end: jest.fn(),
};
// Mock gRPC client
const mockClient = {
ClientStream: jest.fn(() => mockStream),
};
// Mock CentralSystem constructor
const CentralSystemMock = jest.fn(() => mockClient);
// Mock dispatcher
const mockDispatch = jest.fn();
jest.mock('../../../client/ConnectionManager/EventManagement/CentralCommandDispatcher', () => ({
CentralCommandDispatcher: jest.fn(() => ({
dispatch: mockDispatch,
})),
}));
// Mock logger
jest.mock(
'@aliceo2/web-ui',
() => ({
LogManager: {
getLogger: () => ({
infoMessage: jest.fn(),
}),
},
}),
{ virtual: true }
);
// Mock gRPC proto loader and client
jest.mock('@grpc/proto-loader', () => ({
loadSync: jest.fn(() => {
return {};
}),
}));
jest.mock('@grpc/grpc-js', () => {
const original = jest.requireActual('@grpc/grpc-js');
return {
...original,
credentials: {
createInsecure: jest.fn(),
},
loadPackageDefinition: jest.fn(() => ({
webui: {
tokenization: {
CentralSystem: CentralSystemMock,
},
},
})),
};
});
describe('ConnectionManager', () => {
let conn: ConnectionManager;
beforeEach(() => {
jest.clearAllMocks();
conn = new ConnectionManager('dummy.proto', 'localhost:12345');
});
test('should initialize client with correct address', () => {
const connManager = new ConnectionManager('dummy.proto', 'localhost:12345');
expect(connManager).toBeDefined();
expect(grpc.loadPackageDefinition).toHaveBeenCalled();
expect(CentralSystemMock).toHaveBeenCalledWith('localhost:12345', undefined);
});
test('connectToCentralSystem() should set up stream listeners', () => {
const connManager = new ConnectionManager('dummy.proto', 'localhost:12345');
connManager.connectToCentralSystem();
expect(mockClient.ClientStream).toHaveBeenCalled();
expect(mockStream.on).toHaveBeenCalledWith('data', expect.any(Function));
expect(mockStream.on).toHaveBeenCalledWith('end', expect.any(Function));
expect(mockStream.on).toHaveBeenCalledWith('error', expect.any(Function));
});
test('disconnectFromCentralSystem() should end stream', () => {
conn.connectToCentralSystem();
conn.disconnectFromCentralSystem();
expect(mockStream.end).toHaveBeenCalled();
});
test("should reconnect on stream 'end'", () => {
jest.useFakeTimers();
conn.connectToCentralSystem();
const onEnd = mockStream.on.mock.calls.find(([event]) => event === 'end')?.[1];
onEnd?.(); // simulate 'end'
jest.advanceTimersByTime(2000);
expect(mockClient.ClientStream).toHaveBeenCalledTimes(2);
jest.useRealTimers();
});
test("should reconnect on stream 'error'", () => {
jest.useFakeTimers();
conn.connectToCentralSystem();
const onError = mockStream.on.mock.calls.find(([event]) => event === 'error')?.[1];
onError?.(new Error('Simulated error'));
jest.advanceTimersByTime(2000);
expect(mockClient.ClientStream).toHaveBeenCalledTimes(2);
jest.useRealTimers();
});
test("should dispatch event when 'data' is received", () => {
conn.connectToCentralSystem();
const onData = mockStream.on.mock.calls.find(([event]) => event === 'data')?.[1];
const mockMessage = {
event: DuplexMessageEvent.MESSAGE_EVENT_REVOKE_TOKEN,
data: {
revokeToken: {
token: 'abc123',
targetAddress: 'peer-123',
},
},
};
onData?.(mockMessage);
expect(mockDispatch).toHaveBeenCalledWith(mockMessage);
});
});