2024-09-17 06:45:42 -07:00
|
|
|
import type { Redis as SingleNodeClient } from 'ioredis';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
|
|
|
|
import config from '@/config';
|
|
|
|
import { generateNanoId } from '@/databases/utils/generators';
|
2024-09-19 00:52:48 -07:00
|
|
|
import type { RedisClientService } from '@/services/redis-client.service';
|
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', () => {
|
|
|
|
let queueModeId: string;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config.set('executions.mode', 'queue');
|
|
|
|
queueModeId = generateNanoId();
|
|
|
|
config.set('redis.queueModeId', queueModeId);
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = mock<SingleNodeClient>();
|
|
|
|
const redisClientService = mock<RedisClientService>({ createClient: () => client });
|
|
|
|
|
|
|
|
describe('constructor', () => {
|
|
|
|
it('should init Redis client in scaling mode', () => {
|
|
|
|
const publisher = new Publisher(mock(), redisClientService);
|
|
|
|
|
|
|
|
expect(publisher.getClient()).toEqual(client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not init Redis client in regular mode', () => {
|
|
|
|
config.set('executions.mode', 'regular');
|
|
|
|
const publisher = new Publisher(mock(), redisClientService);
|
|
|
|
|
|
|
|
expect(publisher.getClient()).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('shutdown', () => {
|
|
|
|
it('should disconnect Redis client', () => {
|
|
|
|
const publisher = new Publisher(mock(), redisClientService);
|
|
|
|
publisher.shutdown();
|
|
|
|
expect(client.disconnect).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('publishCommand', () => {
|
|
|
|
it('should publish command into `n8n.commands` pubsub channel', async () => {
|
|
|
|
const publisher = new Publisher(mock(), redisClientService);
|
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-08 02:18:12 -07:00
|
|
|
JSON.stringify({ ...msg, senderId: queueModeId, 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 () => {
|
|
|
|
const publisher = new Publisher(mock(), redisClientService);
|
2024-09-27 03:35:01 -07:00
|
|
|
const msg = mock<PubSub.WorkerResponse>({
|
|
|
|
command: 'reload-external-secrets-providers',
|
2024-09-17 06:45:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
await publisher.publishWorkerResponse(msg);
|
|
|
|
|
|
|
|
expect(client.publish).toHaveBeenCalledWith('n8n.worker-response', JSON.stringify(msg));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|