From dff8a90712905e6601c0fa04b9b8ea031d38ea5b Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 14 May 2023 18:12:49 +0200 Subject: [PATCH 1/2] separate test cases into separate files --- .taprc | 2 +- test/communication.test.js | 66 +++++++ test/consume-callback.test.js | 68 +++++++ test/{consumer.js => consumer.test.js} | 3 +- test/export.test.js | 25 +++ test/multiple-topics.test.js | 73 ++++++++ test/{producer.js => producer.test.js} | 3 +- test/test.js | 246 ------------------------- test/utils.js | 39 ++++ 9 files changed, 274 insertions(+), 251 deletions(-) create mode 100644 test/communication.test.js create mode 100644 test/consume-callback.test.js rename test/{consumer.js => consumer.test.js} (93%) create mode 100644 test/export.test.js create mode 100644 test/multiple-topics.test.js rename test/{producer.js => producer.test.js} (92%) delete mode 100644 test/test.js create mode 100644 test/utils.js diff --git a/.taprc b/.taprc index aba3f1b..96c2151 100644 --- a/.taprc +++ b/.taprc @@ -1,4 +1,4 @@ check-coverage: false files: - - test/*.js \ No newline at end of file + - test/**/*.test.js \ No newline at end of file diff --git a/test/communication.test.js b/test/communication.test.js new file mode 100644 index 0000000..1b7273d --- /dev/null +++ b/test/communication.test.js @@ -0,0 +1,66 @@ +'use strict' + +const { test } = require('tap') +const Fastify = require('fastify') +const fastifyKafka = require('..') +const { + generateTopicName, + generateGroupId, + getDefaultOptions +} = require('./utils') + +test('communication', t => { + t.plan(7) + const options = getDefaultOptions() + const group = generateGroupId() + options.consumer['group.id'] = group + + const topicName = generateTopicName() + + const producerFastify = Fastify({ logger: true }) + const consumerFastify = Fastify({ logger: true }) + + t.teardown(() => producerFastify.close()) + t.teardown(() => consumerFastify.close()) + + consumerFastify + .register(fastifyKafka, { ...options, producer: undefined }) + .after(err => { + t.error(err) + + consumerFastify.kafka.consumer.on('error', t.fail) + consumerFastify.kafka.subscribe(topicName) + + consumerFastify.kafka.on(topicName, (msg, commit) => { + t.equal(msg.value.toString(), 'hello world!') + commit() + + t.ok(true) + }) + + consumerFastify.kafka.consume() + }) + + producerFastify + .register(fastifyKafka, { ...options, consumer: undefined }) + .after(err => { + t.error(err) + + producerFastify.kafka.producer.on('error', t.fail) + producerFastify.kafka.push({ + topic: topicName, + payload: 'hello world!', + key: 'testKey' + }) + + t.ok(true) + }) + + producerFastify.ready(err => { + t.error(err) + + consumerFastify.ready(err => { + t.error(err) + }) + }) +}) diff --git a/test/consume-callback.test.js b/test/consume-callback.test.js new file mode 100644 index 0000000..156cff1 --- /dev/null +++ b/test/consume-callback.test.js @@ -0,0 +1,68 @@ +'use strict' + +const { test } = require('tap') +const Fastify = require('fastify') +const fastifyKafka = require('..') +const { getDefaultOptions, generateGroupId, generateTopicName } = require('./utils') + +test('consume callback', t => { + const options = getDefaultOptions() + const group = generateGroupId() + options.consumer['group.id'] = group + options.consumer.event_cb = true + + const topicName = generateTopicName() + + const producerFastify = Fastify() + const consumerFastify = Fastify() + + t.teardown(() => producerFastify.close()) + t.teardown(() => consumerFastify.close()) + + consumerFastify + .register(fastifyKafka, { ...options, producer: undefined }) + .after(err => { + t.error(err) + + consumerFastify.kafka.consumer.on('error', t.fail) + consumerFastify.kafka.subscribe(topicName) + + consumerFastify.kafka.on(topicName, t.fail) + + function onConsume (err, message) { + t.error(err) + t.match(message, { + topic: topicName, + value: Buffer.from('hello world!'), + key: Buffer.from('testKey') + }) + + t.end() + } + + consumerFastify.kafka.consume(onConsume) + }) + + producerFastify + .register(fastifyKafka, { ...options, consumer: undefined }) + .after(err => { + t.error(err) + + producerFastify.kafka.producer.on('error', t.fail) + producerFastify.kafka.push({ + topic: topicName, + payload: 'hello world!', + key: 'testKey' + }) + + t.ok(true) + }) + + producerFastify.ready(err => { + t.error(err) + + consumerFastify.ready(err => { + t.error(err) + }) + }) +}) diff --git a/test/consumer.js b/test/consumer.test.js similarity index 93% rename from test/consumer.js rename to test/consumer.test.js index 517c606..b1905ee 100644 --- a/test/consumer.js +++ b/test/consumer.test.js @@ -1,8 +1,7 @@ 'use strict' -const t = require('tap') +const { test } = require('tap') const log = require('abstract-logging') -const test = t.test const Consumer = require('../lib/consumer') diff --git a/test/export.test.js b/test/export.test.js new file mode 100644 index 0000000..f754fb8 --- /dev/null +++ b/test/export.test.js @@ -0,0 +1,25 @@ +'use strict' + +const { test } = require('tap') +const fastifyKafka = require('..') +const defaultExport = require('..').default +const { fastifyKafka: namedExport } = require('..') + +test('export', function (t) { + t.plan(3) + + t.test('module export', function (t) { + t.plan(1) + t.equal(typeof fastifyKafka, 'function') + }) + + t.test('default export', function (t) { + t.plan(1) + t.equal(typeof defaultExport, 'function') + }) + + t.test('named export', function (t) { + t.plan(1) + t.equal(typeof namedExport, 'function') + }) +}) diff --git a/test/multiple-topics.test.js b/test/multiple-topics.test.js new file mode 100644 index 0000000..23f75ea --- /dev/null +++ b/test/multiple-topics.test.js @@ -0,0 +1,73 @@ +'use strict' + +const { test } = require('tap') +const Fastify = require('fastify') +const fastifyKafka = require('..') +const { getDefaultOptions, generateGroupId, generateTopicName } = require('./utils') + +test('multiple topics', t => { + t.plan(9) + const options = getDefaultOptions() + const group = generateGroupId() + options.consumer['group.id'] = group + + const topicName1 = generateTopicName() + const topicName2 = generateTopicName() + + const producerFastify = Fastify() + const consumerFastify = Fastify() + + t.teardown(() => producerFastify.close()) + t.teardown(() => consumerFastify.close()) + + consumerFastify + .register(fastifyKafka, { ...options, producer: undefined }) + .after(err => { + t.error(err) + + consumerFastify.kafka.consumer.on('error', t.fail) + consumerFastify.kafka.subscribe([topicName1, topicName2]) + + consumerFastify.kafka.on(topicName1, (msg, commit) => { + t.equal(msg.value.toString(), 'topic1') + commit() + t.ok(true) + }) + + consumerFastify.kafka.on(topicName2, (msg, commit) => { + t.equal(msg.value.toString(), 'topic2') + commit() + t.ok(true) + }) + + consumerFastify.kafka.consume() + }) + + producerFastify + .register(fastifyKafka, { ...options, consumer: undefined }) + .after(err => { + t.error(err) + + producerFastify.kafka.producer.on('error', t.fail) + producerFastify.kafka.push({ + topic: topicName1, + payload: 'topic1', + key: 'testKey' + }) + producerFastify.kafka.push({ + topic: topicName2, + payload: 'topic2', + key: 'kafkaKey' + }) + + t.ok(true) + }) + + producerFastify.ready(err => { + t.error(err) + + consumerFastify.ready(err => { + t.error(err) + }) + }) +}) diff --git a/test/producer.js b/test/producer.test.js similarity index 92% rename from test/producer.js rename to test/producer.test.js index b701583..9f11705 100644 --- a/test/producer.js +++ b/test/producer.test.js @@ -1,8 +1,7 @@ 'use strict' -const t = require('tap') +const { test } = require('tap') const log = require('abstract-logging') -const test = t.test const Producer = require('../lib/producer') diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 9a64492..0000000 --- a/test/test.js +++ /dev/null @@ -1,246 +0,0 @@ -'use strict' - -const t = require('tap') -const test = t.test -const crypto = require('crypto') -const Fastify = require('fastify') -const fastifyKafka = require('..') -const defaultExport = require('..').default -const { fastifyKafka: namedExport } = require('..') - -const defaultOptions = { - producer: { - 'metadata.broker.list': '127.0.0.1:9092', - 'allow.auto.create.topics': true, - dr_cb: true - }, - consumer: { - 'metadata.broker.list': '127.0.0.1:9092', - 'fetch.wait.max.ms': 10, - 'fetch.error.backoff.ms': 50, - 'topic.metadata.refresh.interval.ms': 100, - 'allow.auto.create.topics': true - }, - consumerTopicConf: { - 'auto.offset.reset': 'beginning' - }, - metadataOptions: { - timeout: 2000 - } -} - -test('export', function (t) { - t.plan(3) - - t.test('module export', function (t) { - t.plan(1) - t.equal(typeof fastifyKafka, 'function') - }) - - t.test('default export', function (t) { - t.plan(1) - t.equal(typeof defaultExport, 'function') - }) - - t.test('named export', function (t) { - t.plan(1) - t.equal(typeof namedExport, 'function') - }) -}) - -test('communication', t => { - t.plan(7) - const options = copyPlainObject(defaultOptions) - const group = generateGroupId() - options.consumer['group.id'] = group - - const topicName = generateTopicName() - - const producerFastify = Fastify() - const consumerFastify = Fastify() - - t.teardown(() => producerFastify.close()) - t.teardown(() => consumerFastify.close()) - - consumerFastify - .register(fastifyKafka, { ...options, producer: undefined }) - .after(err => { - t.error(err) - - consumerFastify.kafka.consumer.on('error', t.fail) - consumerFastify.kafka.subscribe(topicName) - - consumerFastify.kafka.on(topicName, (msg, commit) => { - t.equal(msg.value.toString(), 'hello world!') - commit() - - t.ok(true) - }) - - consumerFastify.kafka.consume() - }) - - producerFastify - .register(fastifyKafka, { ...options, consumer: undefined }) - .after(err => { - t.error(err) - - producerFastify.kafka.producer.on('error', t.fail) - producerFastify.kafka.push({ - topic: topicName, - payload: 'hello world!', - key: 'testKey' - }) - - t.ok(true) - }) - - producerFastify.ready(err => { - t.error(err) - - consumerFastify.ready(err => { - t.error(err) - }) - }) -}) - -test('multiple topics', t => { - t.plan(9) - const options = copyPlainObject(defaultOptions) - const group = generateGroupId() - options.consumer['group.id'] = group - - const topicName1 = generateTopicName() - const topicName2 = generateTopicName() - - const producerFastify = Fastify() - const consumerFastify = Fastify() - - t.teardown(() => producerFastify.close()) - t.teardown(() => consumerFastify.close()) - - consumerFastify - .register(fastifyKafka, { ...options, producer: undefined }) - .after(err => { - t.error(err) - - consumerFastify.kafka.consumer.on('error', t.fail) - consumerFastify.kafka.subscribe([topicName1, topicName2]) - - consumerFastify.kafka.on(topicName1, (msg, commit) => { - t.equal(msg.value.toString(), 'topic1') - commit() - t.ok(true) - }) - - consumerFastify.kafka.on(topicName2, (msg, commit) => { - t.equal(msg.value.toString(), 'topic2') - commit() - t.ok(true) - }) - - consumerFastify.kafka.consume() - }) - - producerFastify - .register(fastifyKafka, { ...options, consumer: undefined }) - .after(err => { - t.error(err) - - producerFastify.kafka.producer.on('error', t.fail) - producerFastify.kafka.push({ - topic: topicName1, - payload: 'topic1', - key: 'testKey' - }) - producerFastify.kafka.push({ - topic: topicName2, - payload: 'topic2', - key: 'kafkaKey' - }) - - t.ok(true) - }) - - producerFastify.ready(err => { - t.error(err) - - consumerFastify.ready(err => { - t.error(err) - }) - }) -}) - -test('consume callback', t => { - const options = copyPlainObject(defaultOptions) - const group = generateGroupId() - options.consumer['group.id'] = group - options.consumer.event_cb = true - - const topicName = generateTopicName() - - const producerFastify = Fastify() - const consumerFastify = Fastify() - - t.teardown(() => producerFastify.close()) - t.teardown(() => consumerFastify.close()) - - consumerFastify - .register(fastifyKafka, { ...options, producer: undefined }) - .after(err => { - t.error(err) - - consumerFastify.kafka.consumer.on('error', t.fail) - consumerFastify.kafka.subscribe(topicName) - - consumerFastify.kafka.on(topicName, t.fail) - - function onConsume (err, message) { - t.error(err) - t.match(message, { - topic: topicName, - value: Buffer.from('hello world!'), - key: Buffer.from('testKey') - }) - - t.end() - } - - consumerFastify.kafka.consume(onConsume) - }) - - producerFastify - .register(fastifyKafka, { ...options, consumer: undefined }) - .after(err => { - t.error(err) - - producerFastify.kafka.producer.on('error', t.fail) - producerFastify.kafka.push({ - topic: topicName, - payload: 'hello world!', - key: 'testKey' - }) - - t.ok(true) - }) - - producerFastify.ready(err => { - t.error(err) - - consumerFastify.ready(err => { - t.error(err) - }) - }) -}) - -function generateGroupId () { - return crypto.randomBytes(20).toString('hex') -} -// Only for test purpose -function copyPlainObject (obj) { - return JSON.parse(JSON.stringify(obj)) -} - -function generateTopicName () { - return crypto.randomBytes(5).toString('hex') -} diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..8e80e3a --- /dev/null +++ b/test/utils.js @@ -0,0 +1,39 @@ +'use strict' + +const crypto = require('crypto') + +module.exports.getDefaultOptions = function getDefaultOptions () { + return { + producer: { + 'metadata.broker.list': '127.0.0.1:9092', + 'allow.auto.create.topics': true, + dr_cb: true + }, + consumer: { + 'metadata.broker.list': '127.0.0.1:9092', + 'fetch.wait.max.ms': 10, + 'fetch.error.backoff.ms': 50, + 'topic.metadata.refresh.interval.ms': 100, + 'allow.auto.create.topics': true + }, + consumerTopicConf: { + 'auto.offset.reset': 'beginning' + }, + metadataOptions: { + timeout: 2000 + } + } +} + +module.exports.generateGroupId = function generateGroupId () { + return crypto.randomBytes(20).toString('hex') +} + +// Only for test purpose +module.exports.copyPlainObject = function copyPlainObject (obj) { + return JSON.parse(JSON.stringify(obj)) +} + +module.exports.generateTopicName = function generateTopicName () { + return crypto.randomBytes(5).toString('hex') +} From 9d7499fcea1f7a39d5a998bae549cec6c5e82b1d Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sun, 14 May 2023 19:04:21 +0200 Subject: [PATCH 2/2] Update .taprc --- .taprc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.taprc b/.taprc index 96c2151..4f1041a 100644 --- a/.taprc +++ b/.taprc @@ -1,4 +1,4 @@ check-coverage: false files: - - test/**/*.test.js \ No newline at end of file + - test/**/*.test.js