it('updates the list of brokers', async () => { expect(brokerPool.brokers).toEqual({}) await brokerPool.refreshMetadata([topicName]) expect(Object.keys(brokerPool.brokers).sort()).toEqual(['0', '1', '2']) expect(Object.values(brokerPool.brokers)).toEqual( expect.arrayContaining([expect.any(Broker), expect.any(Broker), expect.any(Broker)]) ) })
describe('#paused', () => { it('returns an empty array if consumer#run has not been called', () => { expect(consumer.paused()).toEqual([]) }) })
it('includes the seed broker into the broker pool', async () => { await brokerPool.refreshMetadata([topicName]) const seed = brokerPool.seedBroker.connection const brokers = Object.values(brokerPool.brokers) const seedFromBrokerPool = brokers .map(b => b.connection) .find(b => b.host === seed.host && b.port === seed.port) expect(seedFromBrokerPool).toEqual(seed) })
it('crashes on KafkaJSNotImplemented errors', async () => { const notImplementedError = new KafkaJSNotImplemented('not implemented') consumerGroup.fetch.mockImplementationOnce(() => BufferedAsyncIterator([Promise.reject(notImplementedError)]) ) await runner.start() // scheduleFetch in runner#start is async, and we never wait for it, // so we have to wait a bit to give the callback a chance of being executed await sleep(100) expect(onCrash).toHaveBeenCalledWith(notImplementedError) })
it('a triggered rejoin failing should cause a crash', async () => { const unknownError = new KafkaJSProtocolError(createErrorFromCode(UNKNOWN)) consumerGroup.join.mockImplementationOnce(() => { throw unknownError }) consumerGroup.commitOffsets.mockImplementationOnce(() => { throw rebalancingError() }) expect(runner.commitOffsets(offsets)).rejects.toThrow('The group is rebalancing') await sleep(100) expect(onCrash).toHaveBeenCalledWith(unknownError) })
describe('Broker > SASL Authenticator > PLAIN', () => { it('throws KafkaJSSASLAuthenticationError for invalid username', async () => { const plain = new Plain({ sasl: {} }, newLogger()) await expect(plain.authenticate()).rejects.toThrow('Invalid username or password') }) it('throws KafkaJSSASLAuthenticationError for invalid password', async () => { const plain = new Plain({ sasl: { username: '<username>' } }, newLogger()) await expect(plain.authenticate()).rejects.toThrow('Invalid username or password') }) })
it('gives access to its logger', () => { expect( createAdmin({ cluster: createCluster(), logger: newLogger(), }).logger() ).toMatchSnapshot() })
it('should commit offsets while running', async () => { await runner.commitOffsets(offsets) expect(consumerGroup.commitOffsetsIfNecessary).toHaveBeenCalledTimes(0) expect(consumerGroup.commitOffsets.mock.calls.length).toBeGreaterThanOrEqual(1) expect(consumerGroup.commitOffsets).toHaveBeenCalledWith(offsets) })
it('logs INFO', () => { logger.info('<info message>', { extra1: true }) expect(console.info).toHaveBeenCalledWith( JSON.stringify({ level: 'INFO', timestamp: timeNow.toISOString(), logger: 'kafkajs', message: '<info message>', extra1: true, }) ) })
it('should throw when group is rebalancing, while triggering another join', async () => { consumerGroup.commitOffsets.mockImplementationOnce(() => { throw rebalancingError() }) expect(runner.commitOffsets(offsets)).rejects.toThrow('The group is rebalancing') expect(consumerGroup.join).toHaveBeenCalledTimes(0) await sleep(100) expect(consumerGroup.join).toHaveBeenCalledTimes(1) })
describe('Consumer', () => { it('gives access to its logger', () => { expect( createConsumer({ cluster: createCluster(), groupId: 'test-consumer', logger: newLogger(), }).logger() ).toMatchSnapshot() }) })
it('updates the metadata object', async () => { expect(brokerPool.metadata).toEqual(null) await brokerPool.refreshMetadata([topicName]) expect(brokerPool.metadata).not.toEqual(null) })
describe('Utils > websiteUrl', () => { it('generates links to the website', () => { expect(websiteUrl('docs/faq')).toEqual('https://kafka.js.org/docs/faq') }) it('allows specifying a hash', () => { expect( websiteUrl('docs/faq', 'why-am-i-receiving-messages-for-topics-i-m-not-subscribed-to') ).toEqual( 'https://kafka.js.org/docs/faq#why-am-i-receiving-messages-for-topics-i-m-not-subscribed-to' ) }) })
it('logs ERROR', () => { logger.error('<error message>', { extra1: true }) expect(console.error).toHaveBeenCalledWith( JSON.stringify({ level: 'ERROR', timestamp: timeNow.toISOString(), logger: 'kafkajs', message: '<error message>', extra1: true, }) ) })
it('logs DEBUG', () => { logger.debug('<debug message>', { extra1: true }) expect(console.log).toHaveBeenCalledWith( JSON.stringify({ level: 'DEBUG', timestamp: timeNow.toISOString(), logger: 'kafkajs', message: '<debug message>', extra1: true, }) ) })