Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.0 kB
3
Indexable
const kafka = require('kafka-node');
const chai = require('chai');
const expect = chai.expect;
const Producer = kafka.Producer;
const Consumer = kafka.Consumer;
const Client = kafka.KafkaClient;

describe('Kafka Producer and Consumer Test', function() {
  const client = new Client({ kafkaHost: 'localhost:9092' });
  const producer = new Producer(client);
  const consumer = new Consumer(
    client,
    [{ topic: 'test-topic', partition: 0 }],
    { autoCommit: true }
  );

  it('should send and receive a message', function(done) {
    this.timeout(10000);
    const testMessage = 'Hello, Kafka!';

    consumer.on('message', function(message) {
      expect(message.value).to.equal(testMessage);
      done();
    });

    producer.on('ready', function() {
      producer.send([{ topic: 'test-topic', messages: [testMessage] }], function(err, data) {
        if (err) {
          done(err);
        }
      });
    });

    producer.on('error', function(err) {
      done(err);
    });
  });
});
Leave a Comment