2024-09-17 06:45:42 -07:00
|
|
|
import type { Redis as SingleNodeClient } from 'ioredis';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
2024-10-15 05:55:13 -07:00
|
|
|
import type { InstanceSettings } from 'n8n-core';
|
2024-09-17 06:45:42 -07:00
|
|
|
|
|
|
|
import config from '@/config';
|
2024-09-19 00:52:48 -07:00
|
|
|
import type { RedisClientService } from '@/services/redis-client.service';
|
2024-10-14 06:15:42 -07:00
|
|
|
import { mockLogger } from '@test/mocking';
|
2024-09-17 06:45:42 -07:00
|
|
|
|
|
|
|
import { Publisher } from '../pubsub/publisher.service';
|
2024-09-27 03:35:01 -07:00
|
|
|
import type { PubSub } from '../pubsub/pubsub.types';
|
2024-09-17 06:45:42 -07:00
|
|
|
|
|
|
|
describe('Publisher', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = mock<SingleNodeClient>();
|
2024-10-14 06:15:42 -07:00
|
|
|
const logger = mockLogger();
|
2024-10-15 05:55:13 -07:00
|
|
|
const hostId = 'main-bnxa1riryKUNHtln';
|
|
|
|
const instanceSettings = mock<InstanceSettings>({ hostId });
|
2024-09-17 06:45:42 -07:00
|
|
|
const redisClientService = mock<RedisClientService>({ createClient: () => client });
|
|
|
|
|
|
|
|
describe('constructor', () => {
|
|
|
|
it('should init Redis client in scaling mode', () => {
|
2024-10-15 05:55:13 -07:00
|
|
|
const publisher = new Publisher(logger, redisClientService, instanceSettings);
|
2024-09-17 06:45:42 -07:00
|
|
|
|
|
|
|
expect(publisher.getClient()).toEqual(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not init Redis client in regular mode', () => {
|
|
|
|
config.set('executions.mode', 'regular');
|
2024-10-15 05:55:13 -07:00
|
|
|
const publisher = new Publisher(logger, redisClientService, instanceSettings);
|
2024-09-17 06:45:42 -07:00
|
|
|
|
|
|
|
expect(publisher.getClient()).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('shutdown', () => {
|
|
|
|
it('should disconnect Redis client', () => {
|
2024-10-15 05:55:13 -07:00
|
|
|
const publisher = new Publisher(logger, redisClientService, instanceSettings);
|
2024-09-17 06:45:42 -07:00
|
|
|
publisher.shutdown();
|
|
|
|
expect(client.disconnect).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('publishCommand', () => {
|
|
|
|
it('should publish command into `n8n.commands` pubsub channel', async () => {
|
2024-10-15 05:55:13 -07:00
|
|
|
const publisher = new Publisher(logger, redisClientService, instanceSettings);
|
2024-09-27 03:35:01 -07:00
|
|
|
const msg = mock<PubSub.Command>({ command: 'reload-license' });
|
2024-09-17 06:45:42 -07:00
|
|
|
|
|
|
|
await publisher.publishCommand(msg);
|
|
|
|
|
|
|
|
expect(client.publish).toHaveBeenCalledWith(
|
|
|
|
'n8n.commands',
|
2024-10-15 05:55:13 -07:00
|
|
|
JSON.stringify({ ...msg, senderId: hostId, selfSend: false, debounce: true }),
|
2024-09-17 06:45:42 -07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('publishWorkerResponse', () => {
|
|
|
|
it('should publish worker response into `n8n.worker-response` pubsub channel', async () => {
|
2024-10-15 05:55:13 -07:00
|
|
|
const publisher = new Publisher(logger, redisClientService, instanceSettings);
|
2024-09-27 03:35:01 -07:00
|
|
|
const msg = mock<PubSub.WorkerResponse>({
|
2024-10-11 01:31:33 -07:00
|
|
|
response: 'response-to-get-worker-status',
|
2024-09-17 06:45:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
await publisher.publishWorkerResponse(msg);
|
|
|
|
|
|
|
|
expect(client.publish).toHaveBeenCalledWith('n8n.worker-response', JSON.stringify(msg));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|