All files / redis-smq-monitor/tests/common consumers.ts

86.36% Statements 19/22
83.33% Branches 5/6
66.66% Functions 4/6
85.71% Lines 18/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 4757x 57x 57x           57x               57x   57x         24x 24x 24x 24x 24x     57x 57x 24x         24x   23x 23x       57x    
import { config } from './config';
import { promisifyAll } from 'bluebird';
import { Consumer, events } from 'redis-smq';
import {
  IConfig,
  TConsumerMessageHandler,
  TQueueParams,
} from 'redis-smq/dist/types';
import { defaultQueue } from './common';
 
type TGetConsumerArgs = {
  queue?: string | TQueueParams;
  messageHandler?: TConsumerMessageHandler;
  cfg?: IConfig;
};
 
let consumersList: Consumer[] = [];
 
export function getConsumer(args: TGetConsumerArgs = {}) {
  const {
    queue = defaultQueue,
    messageHandler = (msg, cb) => cb(),
    cfg = config,
  } = args;
  const consumer = promisifyAll(new Consumer(cfg));
  consumer.consume(queue, messageHandler, () => void 0);
  consumersList.push(consumer);
  return consumer;
}
 
export async function shutdownConsumers() {
  for (const i of consumersList) {
    Iif (i.isGoingUp()) {
      await new Promise((resolve) => {
        i.once(events.UP, resolve);
      });
    }
    if (i.isRunning()) {
      // eslint-disable-next-line no-await-in-loop
      await new Promise((resolve) => {
        i.shutdown(resolve);
      });
    }
  }
  consumersList = [];
}