Skip to content

Commit 6ebdff9

Browse files
Vaibhav  BhallaVaibhav  Bhalla
authored andcommitted
refactor(chore): refactor the code
refactor the code gh-3
1 parent d4b795e commit 6ebdff9

18 files changed

+218
-185
lines changed
Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,92 @@
1-
import {expect, sinon} from '@loopback/testlab';
1+
import {sinon} from '@loopback/testlab';
22
import {ProducerApp} from './fixtures/producers-app';
33
import {TestProducerService} from './fixtures/services/test-producer.service';
44
import {setupProducerApplication} from './helpers/app-builder';
55
import {QueueStub} from './stubs/bullmq-queue.stub';
66
import {Events} from '../test-stream';
7-
import {DEFAULT_SOURCE} from '../../../constants';
87

9-
[
8+
interface TestConfig {
9+
type: string;
10+
queue: QueueStub;
11+
condition: () => boolean;
12+
}
13+
14+
const testConfigs: TestConfig[] = [
1015
{
1116
type: 'Stubbed queue',
1217
queue: new QueueStub(),
1318
condition: () => true,
1419
},
15-
].forEach(({type, queue, condition}) => {
20+
];
21+
22+
for (const {type, queue, condition} of testConfigs) {
23+
runBullMqConnectorTests(type, queue, condition);
24+
}
25+
26+
function runBullMqConnectorTests(
27+
type: string,
28+
queue: QueueStub,
29+
condition: () => boolean,
30+
): void {
1631
describe(`BullMq Connector: With ${type}`, () => {
1732
let producerApp: ProducerApp | undefined;
1833
let producerService: TestProducerService;
1934
let listenerStub: sinon.SinonStub;
35+
2036
before(async function () {
21-
if (!condition()) {
22-
// eslint-disable-next-line @typescript-eslint/no-invalid-this
23-
this.skip();
24-
}
37+
if (!condition()) this.skip();
2538
producerApp = await setupProducerApplication(queue);
2639
producerService = producerApp.getSync<TestProducerService>(
2740
`services.TestProducerService`,
2841
);
2942
listenerStub = sinon.stub().resolves();
30-
if (queue) {
31-
queue.register(listenerStub);
32-
}
43+
queue.register(listenerStub);
3344
});
34-
beforeEach(() => {
35-
listenerStub.reset();
36-
});
37-
after(async () => {
38-
await producerApp?.stop();
45+
46+
beforeEach(() => listenerStub.reset());
47+
after(async () => await producerApp?.stop());
48+
49+
runProducerTests(
50+
() => listenerStub,
51+
() => producerService,
52+
);
53+
});
54+
}
55+
56+
function runProducerTests(
57+
getListenerStub: () => sinon.SinonStub,
58+
getProducerService: () => TestProducerService,
59+
): void {
60+
describe('Producer', () => {
61+
it('should produce an event for a particular topic', async () => {
62+
const producerService = getProducerService();
63+
const listenerStub = getListenerStub();
64+
65+
await producerService.produceEventA('test string');
66+
67+
sinon.assert.calledWithExactly(
68+
listenerStub,
69+
'Event A',
70+
'test string',
71+
{},
72+
);
3973
});
40-
describe('Producer', () => {
41-
it('should produce an event for a particular topic', async () => {
42-
await producerService.produceEventA('test string');
43-
sinon.assert.calledWithExactly(
44-
listenerStub,
45-
'Event A',
46-
'test string',
47-
{},
48-
);
49-
});
50-
it('should produce multiple events for a particular topic', async () => {
51-
const data = ['test string 1', 'test string 2'];
52-
await producerService.produceMultipleA(data);
53-
sinon.assert.calledWithExactly(
54-
listenerStub,
55-
JSON.stringify(
56-
data.map(d => ({name: 'Event A', data: d, type: Events.A})),
57-
),
58-
'',
59-
{},
60-
);
61-
});
74+
75+
it('should produce multiple events for a particular topic', async () => {
76+
const producerService = getProducerService();
77+
const listenerStub = getListenerStub();
78+
79+
const data = ['test string 1', 'test string 2'];
80+
await producerService.produceMultipleA(data);
81+
82+
sinon.assert.calledWithExactly(
83+
listenerStub,
84+
JSON.stringify(
85+
data.map(d => ({name: 'Event A', data: d, type: Events.A})),
86+
),
87+
'',
88+
{},
89+
);
6290
});
6391
});
64-
});
92+
}

src/__tests__/acceptance/bullmq/event-handler-service.acceptance.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
import {expect, sinon} from '@loopback/testlab';
1+
import {sinon} from '@loopback/testlab';
22
import {EventHandlerService} from '../../../services';
33
import {ConsumerApp} from './fixtures/consumer-app';
44
import {Events, TestStream} from '../test-stream';
55
import {setupConsumerApplication} from './helpers/app-builder';
66
import {QueueType} from '../../../types';
77
import {QueueStub} from './stubs/bullmq-queue.stub';
8+
interface MockBullMQConsumerService {
9+
start: sinon.SinonStub;
10+
stop: sinon.SinonStub;
11+
addWorker: sinon.SinonStub;
12+
removeWorker: sinon.SinonStub;
13+
autoScaler: sinon.SinonStub;
14+
startAutoScaler: sinon.SinonStub;
15+
processMessage: sinon.SinonStub;
16+
}
17+
18+
let mockBullMQConsumerService: MockBullMQConsumerService;
819

920
describe('EventHandlerService', () => {
1021
let consumerApp: ConsumerApp;
1122
let consumerStub: sinon.SinonStub;
1223
let handlerService: EventHandlerService<TestStream>;
13-
let mockBullMQConsumerService: any;
24+
let mockBullMQConsumerService: MockBullMQConsumerService;
1425

1526
before(async () => {
1627
const queue = new QueueStub();
@@ -41,8 +52,10 @@ describe('EventHandlerService', () => {
4152
beforeEach(() => {
4253
consumerStub.reset();
4354
// Reset all mock stubs
44-
Object.values(mockBullMQConsumerService).forEach((stub: any) => {
45-
if (stub.reset) stub.reset();
55+
Object.values(mockBullMQConsumerService).forEach(stub => {
56+
if (typeof stub.reset === 'function') {
57+
stub.reset();
58+
}
4659
});
4760
});
4861

src/__tests__/acceptance/bullmq/helpers/app-builder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {expect, sinon} from '@loopback/testlab';
2-
import {givenHttpServerConfig} from '@loopback/testlab';
1+
import {givenHttpServerConfig,sinon} from '@loopback/testlab';
32
import {ProducerApp} from '../fixtures/producers-app';
43
import {BullMQBindings} from '../../../../strategies';
54
import {ConsumerApp} from '../fixtures/consumer-app';
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {producer} from '../../../../../decorators';
22
import {Producer, QueueType} from '../../../../../types';
3-
import {Events, TestStream} from '../../../test-stream';
43
import {AnyObject} from '@loopback/repository';
54

65
export class TestProducerService {
@@ -10,17 +9,14 @@ export class TestProducerService {
109
) {}
1110

1211
produceEventA(data: AnyObject) {
13-
return this.prod.send(data
14-
);
12+
return this.prod.send(data);
1513
}
1614

1715
produceEventB(data: AnyObject) {
18-
return this.prod.send(data
19-
);
16+
return this.prod.send(data);
2017
}
2118

2219
produceMultipleA(data: AnyObject[]) {
23-
return this.prod.sendMultiple(data
24-
);
20+
return this.prod.sendMultiple(data);
2521
}
2622
}

src/__tests__/acceptance/sqs/sqs.acceptance.ts

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {expect, sinon} from '@loopback/testlab';
1+
import {sinon} from '@loopback/testlab';
22
import {ProducerApp} from './fixtures/producers-app';
33
import {TestProducerService} from './fixtures/services/test-producer.service';
44
import {setupProducerApplication} from './helpers/app-builder';
55
import {Events} from '../test-stream';
6-
import {DEFAULT_SOURCE} from '../../../constants';
7-
import { SQSClientStub } from './stubs/sqs-client.stub';
6+
import {SQSClientStub} from './stubs/sqs-client.stub';
87

98
[
109
{
@@ -30,7 +29,6 @@ import { SQSClientStub } from './stubs/sqs-client.stub';
3029
producerService = producerApp.getSync<TestProducerService>(
3130
`services.TestProducerService`,
3231
);
33-
3432
});
3533
beforeEach(() => {
3634
listenerStub.reset();
@@ -42,40 +40,51 @@ import { SQSClientStub } from './stubs/sqs-client.stub';
4240
it('should produce an event for a particular topic', async () => {
4341
const data = {
4442
MessageBody: JSON.stringify({tenantId: '123', action: 'delete'}),
45-
MessageAttributes:{
46-
EventType: {
47-
DataType: 'String',
48-
StringValue: Events.A,
49-
},
50-
}
51-
}
43+
MessageAttributes: {
44+
EventType: {
45+
DataType: 'String',
46+
StringValue: Events.A,
47+
},
48+
},
49+
};
5250
await producerService.produceEventA(data);
53-
sinon.assert.calledWithExactly(listenerStub,'type A', "Source A", data);
51+
sinon.assert.calledWithExactly(
52+
listenerStub,
53+
'type A',
54+
'Source A',
55+
data,
56+
);
5457
});
5558
it('should produce multiple events for a particular topic', async () => {
56-
const data = [{
57-
Id:'1',
58-
MessageBody: JSON.stringify({tenantId: '123', action: 'delete'}),
59-
MessageAttributes:{
60-
EventType: {
61-
DataType: 'String',
62-
StringValue: Events.A,
63-
},
64-
}
65-
},
66-
{
67-
Id:'2',
68-
MessageBody: JSON.stringify({tenantId: '456', action: 'delete'}),
69-
MessageAttributes:{
70-
EventType: {
71-
DataType: 'String',
72-
StringValue: Events.B,
73-
},
74-
}
75-
}];
59+
const data = [
60+
{
61+
Id: '1',
62+
MessageBody: JSON.stringify({tenantId: '123', action: 'delete'}),
63+
MessageAttributes: {
64+
EventType: {
65+
DataType: 'String',
66+
StringValue: Events.A,
67+
},
68+
},
69+
},
70+
{
71+
Id: '2',
72+
MessageBody: JSON.stringify({tenantId: '456', action: 'delete'}),
73+
MessageAttributes: {
74+
EventType: {
75+
DataType: 'String',
76+
StringValue: Events.B,
77+
},
78+
},
79+
},
80+
];
7681
await producerService.produceMultipleA(data);
77-
sinon.assert.calledWithExactly(listenerStub,'type A', "Source A", data);
78-
82+
sinon.assert.calledWithExactly(
83+
listenerStub,
84+
'type A',
85+
'Source A',
86+
data,
87+
);
7988
});
8089
});
8190
});

src/__tests__/unit/providers/bullmq/bullmq-consumer.service.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import {EventHandlerService} from '../../../../services';
1111
import {QueueType} from '../../../../types';
1212
import proxyquire from 'proxyquire';
1313

14+
const HIGH_WAITING_JOB_COUNT=5;
1415
// Mock bullmq Worker instances
15-
const mockWorkerInstances: any[] = [];
16+
const mockWorkerInstances: MockWorker[] = [];
1617

1718
// Create a mock Worker class
1819
class MockWorker {
@@ -143,7 +144,7 @@ describe('BullMQConsumerProvider', () => {
143144
});
144145

145146
it('handles auto-scaling up when queue has more messages', async () => {
146-
queueStub.stubs.getWaitingCount.resolves(5);
147+
queueStub.stubs.getWaitingCount.resolves(HIGH_WAITING_JOB_COUNT);
147148
await consumer['autoScaler']();
148149
// Verify worker count increased
149150
expect(consumer['workers'].length).to.be.greaterThan(0);

src/__tests__/unit/providers/bullmq/bullmq-producer.service.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ describe('BullMQProducerProvider', () => {
7878
const mockJob = {id: 'job-id'};
7979
queueStub.add.resolves(mockJob as Job);
8080

81-
const sqsProvider = new BullMQProducerService(
82-
{QueueName: 'test', redisConfig: {}} as BullMQConfig,
83-
queueStub as unknown as Queue,
84-
loggerStub,
85-
);
86-
8781
const messageMock = {
8882
name: 'test',
8983
data: {foo: 'bar'},

0 commit comments

Comments
 (0)