Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(microservices): Add producer reference to KafkaContext #10272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/microservices/ctx-host/kafka.context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Consumer, KafkaMessage } from '../external/kafka.interface';
import { Consumer, KafkaMessage, Producer } from '../external/kafka.interface';
import { BaseRpcContext } from './base-rpc.context';

type KafkaContextArgs = [
Expand All @@ -7,6 +7,7 @@ type KafkaContextArgs = [
topic: string,
consumer: Consumer,
heartbeat: () => Promise<void>,
producer: Producer,
];

export class KafkaContext extends BaseRpcContext<KafkaContextArgs> {
Expand Down Expand Up @@ -48,4 +49,11 @@ export class KafkaContext extends BaseRpcContext<KafkaContextArgs> {
getHeartbeat() {
return this.args[4];
}

/**
* Returns the Kafka producer reference,
*/
getProducer() {
return this.args[5];
}
}
1 change: 1 addition & 0 deletions packages/microservices/server/server-kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class ServerKafka extends Server implements CustomTransportStrategy {
payload.topic,
this.consumer,
payload.heartbeat,
this.producer,
]);
const handler = this.getHandlerByPattern(packet.pattern);
// if the correlation id or reply topic is not set
Expand Down
21 changes: 19 additions & 2 deletions packages/microservices/test/ctx-host/kafka.context.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { expect } from 'chai';
import { KafkaContext } from '../../ctx-host';
import { Consumer, KafkaMessage } from '../../external/kafka.interface';
import {
Consumer,
KafkaMessage,
Producer,
} from '../../external/kafka.interface';

describe('KafkaContext', () => {
const args = [
Expand All @@ -9,12 +13,20 @@ describe('KafkaContext', () => {
undefined,
{ test: 'consumer' },
() => {},
{ test: 'producer' },
];
let context: KafkaContext;

beforeEach(() => {
context = new KafkaContext(
args as [KafkaMessage, number, string, Consumer, () => Promise<void>],
args as [
KafkaMessage,
number,
string,
Consumer,
() => Promise<void>,
Producer,
],
);
});
describe('getTopic', () => {
Expand Down Expand Up @@ -42,4 +54,9 @@ describe('KafkaContext', () => {
expect(context.getHeartbeat()).to.be.eql(args[4]);
});
});
describe('getProducer', () => {
it('should return producer instance', () => {
expect(context.getProducer()).to.deep.eq({ test: 'producer' });
});
});
});