Skip to content

Commit bbb92c0

Browse files
committed
Question Service tests
1 parent edccb92 commit bbb92c0

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

services/question/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/question/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"amqplib": "^0.10.4",
1818
"body-parser": "^1.20.3",
1919
"cors": "^2.8.5",
20+
"dotenv": "^16.4.5",
2021
"express": "^4.21.0",
2122
"jsonwebtoken": "^9.0.2",
2223
"mongoose": "^8.6.2",

services/question/src/events/broker.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import config from '../config';
66
* https://hassanfouad.medium.com/using-rabbitmq-with-nodejs-and-typescript-8b33d56a62cc
77
*/
88
class MessageBroker {
9-
connection!: Connection;
10-
channel!: Channel;
9+
connection: Connection | undefined;
10+
channel: Channel | undefined;
1111
private connected = false;
1212

1313
async connect(): Promise<void> {
@@ -32,7 +32,11 @@ class MessageBroker {
3232
await this.connect();
3333
}
3434

35-
this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)));
35+
if (this.channel) {
36+
this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)));
37+
} else {
38+
throw new Error('Channel is not initialized');
39+
}
3640
} catch (error) {
3741
console.error('Failed to produce message:', error);
3842
throw error;
@@ -45,15 +49,23 @@ class MessageBroker {
4549
await this.connect();
4650
}
4751

48-
await this.channel.assertQueue(queue, { durable: true });
52+
if (this.channel) {
53+
await this.channel.assertQueue(queue, { durable: true });
54+
} else {
55+
throw new Error('Channel is not initialized');
56+
}
4957

5058
this.channel.consume(
5159
queue,
5260
msg => {
5361
if (!msg) return console.error('Invalid message from queue', queue);
5462

5563
onMessage(JSON.parse(msg.content.toString()) as T);
56-
this.channel.ack(msg);
64+
if (this.channel) {
65+
this.channel.ack(msg);
66+
} else {
67+
console.error('Channel is not initialized');
68+
}
5769
},
5870
{ noAck: false },
5971
);

services/question/src/events/consumer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import messageBroker from './broker';
44
import { produceMatchFailedEvent, produceQuestionFoundEvent } from './producer';
55
import { Queues } from './queues';
66

7-
async function consumeMatchFound(msg: MatchFoundEvent) {
7+
export async function consumeMatchFound(msg: MatchFoundEvent) {
88
console.log('Attempting to find questions:', msg);
99

1010
const { user1, user2, topics, difficulty } = msg;

0 commit comments

Comments
 (0)