Skip to content

Commit

Permalink
test(microservices): update server & client redis unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed May 17, 2022
1 parent 40e2755 commit 1e8966b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 58 deletions.
41 changes: 11 additions & 30 deletions packages/microservices/test/client/client-redis.spec.ts
@@ -1,9 +1,7 @@
import { expect } from 'chai';
import { Subject } from 'rxjs';
import * as sinon from 'sinon';
import { ClientRedis } from '../../client/client-redis';
import { ERROR_EVENT } from '../../constants';
import { Client } from '../../external/nats-client.interface';

describe('ClientRedis', () => {
const test = 'test';
Expand Down Expand Up @@ -252,53 +250,39 @@ describe('ClientRedis', () => {
});
});
describe('getClientOptions', () => {
it('should return options object with "retry_strategy" and call "createRetryStrategy"', () => {
it('should return options object with "retryStrategy" and call "createRetryStrategy"', () => {
const createSpy = sinon.spy(client, 'createRetryStrategy');
const { retry_strategy } = client.getClientOptions(new Subject());
const { retryStrategy } = client.getClientOptions();
try {
retry_strategy({} as any);
retryStrategy({} as any);
} catch {}
expect(createSpy.called).to.be.true;
});
});
describe('createRetryStrategy', () => {
const subject = new Subject<Error>();
describe('when is terminated', () => {
it('should return undefined', () => {
(client as any).isExplicitlyTerminated = true;
const result = client.createRetryStrategy({} as any, subject);
const result = client.createRetryStrategy(0);
expect(result).to.be.undefined;
});
});
describe('when "retryAttempts" does not exist', () => {
it('should return an error', () => {
it('should return undefined', () => {
(client as any).isExplicitlyTerminated = false;
(client as any).options.options = {};
(client as any).options.options.retryAttempts = undefined;
const result = client.createRetryStrategy({} as any, subject);
expect(result).to.be.instanceOf(Error);
const result = client.createRetryStrategy(1);
expect(result).to.be.undefined;
});
});
describe('when "attempts" count is max', () => {
it('should return an error', () => {
it('should return undefined', () => {
(client as any).isExplicitlyTerminated = false;
(client as any).options.options = {};
(client as any).options.options.retryAttempts = 3;
const result = client.createRetryStrategy(
{ attempt: 4 } as any,
subject,
);
expect(result).to.be.instanceOf(Error);
});
});
describe('when ECONNREFUSED', () => {
it('should return error', () => {
(client as any).options.options = {};
(client as any).options.options.retryAttempts = 10;

const error = { code: 'ECONNREFUSED' };
const result = client.createRetryStrategy({ error } as any, subject);
expect(result).to.be.instanceOf(Error);
const result = client.createRetryStrategy(4);
expect(result).to.be.undefined;
});
});
describe('otherwise', () => {
Expand All @@ -307,10 +291,7 @@ describe('ClientRedis', () => {
(client as any).isExplicitlyTerminated = false;
(client as any).options.retryAttempts = 3;
(client as any).options.retryDelay = 3;
const result = client.createRetryStrategy(
{ attempt: 2 } as any,
subject,
);
const result = client.createRetryStrategy(2);
expect(result).to.be.eql((client as any).options.retryDelay);
});
});
Expand Down
43 changes: 15 additions & 28 deletions packages/microservices/test/server/server-redis.spec.ts
Expand Up @@ -15,13 +15,17 @@ describe('ServerRedis', () => {
});
describe('listen', () => {
let onSpy: sinon.SinonSpy;
let connectSpy: sinon.SinonSpy;
let client: any;
let callbackSpy: sinon.SinonSpy;

beforeEach(() => {
onSpy = sinon.spy();
connectSpy = sinon.spy();

client = {
on: onSpy,
connect: connectSpy,
};
sinon.stub(server, 'createRedisClient').callsFake(() => client);

Expand All @@ -31,13 +35,9 @@ describe('ServerRedis', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(0).args[0]).to.be.equal('error');
});
it('should bind "connect" event to handler', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(3).args[0]).to.be.equal('connect');
});
it('should bind "message" event to handler', () => {
it('should call "RedisClient#connect()"', () => {
server.listen(callbackSpy);
expect(onSpy.getCall(2).args[0]).to.be.equal('message');
expect(connectSpy.called).to.be.true;
});
describe('when "start" throws an exception', () => {
it('should call callback with a thrown error as an argument', () => {
Expand Down Expand Up @@ -191,11 +191,11 @@ describe('ServerRedis', () => {
});
});
describe('getClientOptions', () => {
it('should return options object with "retry_strategy" and call "createRetryStrategy"', () => {
it('should return options object with "retryStrategy" and call "createRetryStrategy"', () => {
const createSpy = sinon.spy(server, 'createRetryStrategy');
const { retry_strategy } = server.getClientOptions();
const { retryStrategy } = server.getClientOptions();
try {
retry_strategy({} as any);
retryStrategy(0);
} catch {}
expect(createSpy.called).to.be.true;
});
Expand All @@ -204,37 +204,24 @@ describe('ServerRedis', () => {
describe('when is terminated', () => {
it('should return undefined', () => {
(server as any).isExplicitlyTerminated = true;
const result = server.createRetryStrategy({} as any);
const result = server.createRetryStrategy(0);
expect(result).to.be.undefined;
});
});
describe('when "retryAttempts" does not exist', () => {
it('should throw an exception', () => {
it('should return undefined', () => {
(server as any).options.options = {};
(server as any).options.options.retryAttempts = undefined;

expect(() => server.createRetryStrategy({} as any)).to.throw(Error);
expect(server.createRetryStrategy(4)).to.be.undefined;
});
});
describe('when "attempts" count is max', () => {
it('should throw an exception', () => {
it('should return undefined', () => {
(server as any).options.options = {};
(server as any).options.options.retryAttempts = 3;

expect(() =>
server.createRetryStrategy({ attempt: 4 } as any),
).to.throw(Error);
});
});
describe('when ECONNREFUSED', () => {
it('should call logger', () => {
const loggerErrorSpy = sinon.spy((server as any).logger, 'error');
try {
server.createRetryStrategy({
error: { code: 'ECONNREFUSED' },
} as any);
} catch {}
expect(loggerErrorSpy.called).to.be.true;
expect(server.createRetryStrategy(4)).to.be.undefined;
});
});
describe('otherwise', () => {
Expand All @@ -243,7 +230,7 @@ describe('ServerRedis', () => {
(server as any).isExplicitlyTerminated = false;
(server as any).options.retryAttempts = 3;
(server as any).options.retryDelay = 3;
const result = server.createRetryStrategy({ attempt: 2 } as any);
const result = server.createRetryStrategy(2);
expect(result).to.be.eql((server as any).options.retryDelay);
});
});
Expand Down

0 comments on commit 1e8966b

Please sign in to comment.