-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMemcachedProcessManager.test.ts
More file actions
138 lines (120 loc) · 5.32 KB
/
MemcachedProcessManager.test.ts
File metadata and controls
138 lines (120 loc) · 5.32 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
/*
* Copyright 2026, Polytechnique Montreal and contributors
*
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import MemcachedProcessManager, { MemcachedInstance } from '../MemcachedProcessManager';
import ProcessManager from '../ProcessManager';
jest.mock('../ProcessManager', () => ({
startProcess: jest.fn(),
stopProcess: jest.fn(),
isServiceRunning: jest.fn()
}));
const startProcessMock = ProcessManager.startProcess as jest.MockedFunction<typeof ProcessManager.startProcess>;
const stopProcessMock = ProcessManager.stopProcess as jest.MockedFunction<typeof ProcessManager.stopProcess>;
const isServiceRunningMock = ProcessManager.isServiceRunning as jest.MockedFunction<typeof ProcessManager.isServiceRunning>;
beforeEach(() => {
jest.clearAllMocks();
});
describe('MemcachedProcessManager.start', () => {
test.each([
{
name: 'default port',
options: undefined,
mockReturn: { status: 'started', service: 'memcached', name: 'memcached' },
expectedServer: 'localhost:11212',
expectedServiceName: 'memcached11212',
expectedCommandArgs: ['--port=11212', '--user=nobody', '-vv']
},
{
name: 'custom port',
options: { port: 11300 },
mockReturn: { status: 'started', service: 'memcached', name: 'memcached' },
expectedServer: 'localhost:11300',
expectedServiceName: 'memcached11300',
expectedCommandArgs: ['--port=11300', '--user=nobody', '-vv']
}
])('should start memcached with $name', async ({ options, mockReturn, expectedServer, expectedServiceName, expectedCommandArgs }) => {
startProcessMock.mockResolvedValue(mockReturn);
const instance = await MemcachedProcessManager.start(options);
expect(instance).not.toBeNull();
expect(instance?.getServer()).toBe(expectedServer);
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith(
expect.objectContaining({
serviceName: expectedServiceName,
tagName: 'memcached',
command: 'memcached',
commandArgs: expectedCommandArgs,
waitString: '',
useShell: false,
attemptRestart: false
})
);
});
test.each([
{
name: 'memcached executable not found',
mockReturn: { status: 'error', service: 'memcached', name: 'memcached', error: { code: 'ENOENT' } },
expectedErrorMessage: 'memcached executable does not exist in path'
},
{
name: 'other error',
mockReturn: { status: 'error', service: 'memcached', name: 'memcached', error: { message: 'Some error' } },
expectedErrorMessage: 'cannot start memcached:'
},
{
name: 'memcached is already running',
mockReturn: { status: 'already_running', service: 'memcached', name: 'memcached', error: { message: 'already running' } },
expectedErrorMessage: 'a memcached process already exist for port:'
}
])('should return null when $name', async ({ mockReturn, expectedErrorMessage }) => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
startProcessMock.mockResolvedValue(mockReturn);
const instance = await MemcachedProcessManager.start();
expect(instance).toBeNull();
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(expectedErrorMessage),
...(expectedErrorMessage.includes(':') ? [expect.anything()] : [])
);
consoleErrorSpy.mockRestore();
});
});
describe('MemcachedInstance', () => {
let instance: MemcachedInstance;
beforeEach(async () => {
startProcessMock.mockResolvedValue({
status: 'started',
service: 'memcached',
name: 'memcached'
});
instance = (await MemcachedProcessManager.start())!;
});
test('getServer should return correct server string', () => {
expect(instance.getServer()).toBe('localhost:11212');
});
test.each([
{ isRunning: true, expectedStatus: 'running' },
{ isRunning: false, expectedStatus: 'not_running' }
])('status should return $expectedStatus when process is $expectedStatus', async ({ isRunning, expectedStatus }) => {
isServiceRunningMock.mockResolvedValue(isRunning);
const status = await instance.status();
expect(status).toBe(expectedStatus);
expect(isServiceRunningMock).toHaveBeenCalledWith('memcached11212');
});
test.each([
{ mockStatus: 'stopped', expectedStatus: 'stopped' },
{ mockStatus: 'not_running', expectedStatus: 'not_running' },
{ mockStatus: 'error', expectedStatus: 'error' }
])('stop should handle $mockStatus status', async ({ mockStatus, expectedStatus }) => {
stopProcessMock.mockResolvedValue({
status: mockStatus,
service: 'memcached',
name: 'memcached'
});
const result = await instance.stop();
expect(result.status).toBe(expectedStatus);
expect(stopProcessMock).toHaveBeenCalledWith('memcached11212', 'memcached');
});
});