Skip to content

Commit a129b35

Browse files
committed
wip: fixed test & fixing single queue clients type
1 parent 7053bab commit a129b35

File tree

6 files changed

+62
-17
lines changed

6 files changed

+62
-17
lines changed

src/IMQClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export abstract class IMQClient extends EventEmitter {
101101
this.options = { ...DEFAULT_IMQ_CLIENT_OPTIONS, ...options };
102102
this.id = pid(baseName);
103103
this.logger = this.options.logger || /* istanbul ignore next */ console;
104-
this.hostName = `${osUuid()}-${1 || this.id}:client`;
104+
this.hostName = `${osUuid()}-${this.id}:client`;
105105
this.name = `${baseName}-${this.hostName}`;
106106
this.serviceName = serviceName || baseName.replace(/Client$/, '');
107107
this.queueName = this.options.singleQueue ? this.hostName : this.name;

src/cache/RedisCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import * as os from 'os';
2727

2828
export interface IRedisCacheOptions extends Partial<IMQOptions> {
29-
conn?: IRedisClient
29+
conn?: IRedisClient;
3030
}
3131

3232
export const DEFAULT_REDIS_CACHE_OPTIONS = {

test/IMQCache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ describe('IMQCache', () => {
5353

5454
expect(IMQCache.adapters['RedisCache'])
5555
.to.be.instanceOf(RedisCache);
56-
5756
});
5857
});
5958

test/IMQClient.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
IMQDelay,
2525
Description,
2626
expose,
27-
remote
27+
remote,
2828
} from '..';
2929
import * as sinon from 'sinon';
3030
import * as fs from 'fs';
@@ -133,6 +133,24 @@ describe('IMQClient', () => {
133133
expect(new TestServiceClient({}, undefined, 'TestClient').name)
134134
.to.contain('TestClient');
135135
});
136+
137+
it('should re-use existing redis connection if singleQueue option '
138+
+ 'enabled', () => {
139+
const options = { logger, singleQueue: true };
140+
const client1: any = new TestServiceClient(options);
141+
const client2: any = new TestServiceClient(options);
142+
143+
expect(client1.imq).to.equal(client2.imq);
144+
});
145+
146+
it('should not re-use existing redis connection if singleQueue option '
147+
+ 'disabled', () => {
148+
const options = { logger, singleQueue: false };
149+
const client1: any = new TestServiceClient(options);
150+
const client2: any = new TestServiceClient(options);
151+
152+
expect(client1.imq).to.not.equal(client2.imq);
153+
});
136154
});
137155

138156
describe('describe()', () => {

test/cache/RedisCache.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { Redis, logger } from '../mocks';
1919
import { expect } from 'chai';
2020
import { RedisCache } from '../..';
21-
import { uuid } from '@imqueue/core';
21+
import { IRedisClient, uuid } from '@imqueue/core';
2222

2323
describe('cache/RedisCache', () => {
2424
it('should be a class', () => {
@@ -55,7 +55,7 @@ describe('cache/RedisCache', () => {
5555
});
5656

5757
it('should use connection from options if passed', async () => {
58-
const conn = new Redis();
58+
const conn = new Redis() as unknown as IRedisClient;
5959
const cache = new RedisCache();
6060

6161
await cache.init({ conn, logger });
@@ -74,7 +74,6 @@ describe('cache/RedisCache', () => {
7474

7575
after(async () => RedisCache.destroy());
7676

77-
7877
it('should return undefined if nothing found', async () => {
7978
expect(await cache.get(uuid())).to.be.undefined;
8079
});

test/mocks/redis.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,39 @@ export class RedisClientMock extends EventEmitter {
5353
public quit() {}
5454

5555
// noinspection JSMethodCanBeStatic
56-
public set(...args: any[]): number {
57-
const [key, val] = args;
56+
public async set(...args: any[]): Promise<boolean> {
57+
let [key, val, units, expire, nx] = args;
58+
59+
if (
60+
(units === 'NX' || nx === 'NX') &&
61+
RedisClientMock.__keys[key] !== undefined
62+
) {
63+
const cb = args.pop();
64+
typeof cb === 'function' && cb(null, 0);
65+
return true;
66+
}
67+
5868
RedisClientMock.__keys[key] = val;
59-
this.cbExecute(args.pop(), null, 1);
60-
return 1;
69+
70+
if (typeof units === 'string' && typeof expire === 'number') {
71+
if (units === 'EX') {
72+
expire *= 1000;
73+
}
74+
setTimeout(() => { delete RedisClientMock.__keys[key] }, expire);
75+
}
76+
77+
const cb = args.pop();
78+
typeof cb === 'function' && cb(null, 1);
79+
return true;
80+
}
81+
82+
// noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic
83+
public async get(...args: any[]): Promise<string> {
84+
const [key] = args;
85+
const val = RedisClientMock.__keys[key];
86+
const cb = args.pop();
87+
typeof cb === 'function' && cb(null, val);
88+
return val;
6189
}
6290

6391
// noinspection JSUnusedGlobalSymbols,JSMethodCanBeStatic
@@ -288,12 +316,13 @@ export class RedisClientMock extends EventEmitter {
288316
}
289317
}
290318

291-
mock('ioredis', {
292-
default: RedisClientMock,
293-
Redis: RedisClientMock,
294-
});
319+
const Redis = RedisClientMock;
320+
321+
mock('ioredis', { Redis, default: Redis });
295322

296323
// @ts-ignore
297-
export * as Redis from 'ioredis';
324+
export * from 'ioredis';
325+
326+
export { Redis };
298327

299-
export default { Redis: RedisClientMock };
328+
export default Redis;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy