-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCentralSystemWrapper.test.ts
More file actions
123 lines (102 loc) · 3.63 KB
/
CentralSystemWrapper.test.ts
File metadata and controls
123 lines (102 loc) · 3.63 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
/**
* @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.
*/
const mockAddService = jest.fn();
const mockBindAsync = jest.fn();
const mockServerInstance = {
addService: mockAddService,
bindAsync: mockBindAsync,
};
const logger = {
infoMessage: jest.fn(),
};
jest.mock(
'@aliceo2/web-ui',
() => ({
LogManager: {
getLogger: () => logger,
},
}),
{ virtual: true }
);
jest.mock('@grpc/proto-loader', () => ({
loadSync: jest.fn(() => {
return {};
}),
}));
jest.mock('@grpc/grpc-js', () => {
const original = jest.requireActual('@grpc/grpc-js');
return {
...original,
Server: jest.fn(() => mockServerInstance),
ServerCredentials: {
createInsecure: jest.fn(() => 'mock-credentials'),
},
loadPackageDefinition: jest.fn(() => ({
webui: {
tokenization: {
CentralSystem: {
service: 'mock-service',
},
},
},
})),
};
});
import { CentralSystemWrapper } from '../../central/CentralSystemWrapper';
import * as grpc from '@grpc/grpc-js';
describe('CentralSystemWrapper', () => {
let wrapper: CentralSystemWrapper;
beforeEach(() => {
jest.clearAllMocks();
wrapper = new CentralSystemWrapper('dummy.proto', 12345);
});
test('should set up gRPC service and add it to the server', () => {
const testWrapper = new CentralSystemWrapper('dummy.proto', 12345);
expect(grpc.Server).toHaveBeenCalled();
expect(grpc.loadPackageDefinition).toHaveBeenCalled();
expect(grpc.ServerCredentials.createInsecure).not.toHaveBeenCalled();
expect(testWrapper).toBeDefined();
});
test('should call listen and bind the server', () => {
mockBindAsync.mockImplementation((_addr, _creds, cb) => cb(null, 12345));
wrapper.listen();
expect(mockBindAsync).toHaveBeenCalledWith('localhost:12345', 'mock-credentials', expect.any(Function));
});
test('should log error if bind fails', () => {
const error = new Error('bind failed');
mockBindAsync.mockImplementation((_addr, _creds, cb) => cb(error, null));
wrapper.listen();
expect(logger.infoMessage).toHaveBeenCalledWith('Server bind error:', error);
});
test('should handle client stream events', () => {
const logger = require('@aliceo2/web-ui').LogManager.getLogger();
const mockCall = {
getPeer: jest.fn(() => 'client123'),
on: jest.fn((event, cb) => {
if (event === 'end') cb();
if (event === 'error') cb(new Error('stream error'));
}),
end: jest.fn(),
};
const handler = (wrapper as any).clientStreamHandler.bind(wrapper);
handler(mockCall);
expect(mockCall.on).toHaveBeenCalledWith('data', expect.any(Function));
expect(mockCall.on).toHaveBeenCalledWith('end', expect.any(Function));
expect(mockCall.on).toHaveBeenCalledWith('error', expect.any(Function));
expect(mockCall.end).toHaveBeenCalled();
expect(logger.infoMessage).toHaveBeenCalledWith(expect.stringContaining('Client client123'));
expect(logger.infoMessage).toHaveBeenCalledWith('Client client123 ended stream.');
expect(logger.infoMessage).toHaveBeenCalledWith('Stream error from client client123:', expect.any(Error));
});
});