test('throws an error if the resources array is empty', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) await expect(admin.alterConfigs({ resources: [] })).rejects.toHaveProperty( 'message', 'Resources array cannot be empty' ) })
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) })
describe('Protocol > Requests > Heartbeat > v1', () => { test('request', async () => { const { buffer } = await RequestV0Protocol({ groupId: 'consumer-group-id-4c456000151f094b600d-26762-fd6a6ae7-3f66-408e-802e-d261d6983d0d', groupGenerationId: 1, memberId: 'test-14da1b41ac688a6dcb78-26762-4dac8e12-dc28-4db2-8456-95bc6c1589bb-7bad1e84-c2de-4cc6-8071-badb27c86166', }).encode() expect(buffer).toEqual(Buffer.from(require('../fixtures/v1_request.json'))) }) })
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, }) ) })
test('sendBatch should allow sending an empty list of topicMessages', async () => { const cluster = createCluster({ createPartitioner: createModPartitioner, }) await createTopic({ topic: topicName }) producer = createProducer({ cluster, logger: newLogger(), idempotent }) await producer.connect() await expect(producer.sendBatch({ acks, topicMessages: [] })).toResolve() })
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('Protocol > Requests > SASLHandshake > v0', () => { test('throws KafkaJSProtocolError if the api is not supported', async () => { await expect(decode(unsupportedVersionResponse())).rejects.toThrow( /The version of API is not supported/ ) }) })
test(`throws an error if SASL ${e.name} fails to authenticate`, async () => { const cluster = createCluster(e.wrongOpts(), saslBrokers()) producer = createProducer({ cluster, logger: newLogger() }) await expect(producer.connect()).rejects.toThrow(e.expectedErr) })
test('throws an error if there are blank resource names', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) const resources = [ { type: RESOURCE_TYPES.TOPIC, name: 'abc' }, { type: RESOURCE_TYPES.TOPIC, name: null }, ] await expect(admin.alterConfigs({ resources })).rejects.toHaveProperty( 'message', 'Invalid resource name null: {"type":2,"name":null}' ) })
test('throws an error trying to create partition if topic does not exists', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) await admin.connect() await expect( admin.createPartitions({ topicPartitions: [{ topic: topicName + 'x', count: 2 }], }) ).rejects.toHaveProperty('message', 'This server does not host this topic-partition') })
test('throws an error if the resources array is invalid', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) await expect(admin.describeConfigs({ resources: null })).rejects.toHaveProperty( 'message', 'Invalid resources array null' ) await expect( admin.describeConfigs({ resources: 'this-is-not-an-array' }) ).rejects.toHaveProperty('message', 'Invalid resources array this-is-not-an-array') })
test('create the new topics and return true', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) await admin.connect() await expect( admin.createTopics({ waitForLeaders: false, topics: [{ topic: topicName }], }) ).resolves.toEqual(true) })
test('throws an error if the topic name is not a valid string', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) await expect( admin.createPartitions({ topicPartitions: [{ topic: 123 }] }) ).rejects.toHaveProperty( 'message', 'Invalid topic partitions array, the topic names have to be a valid string' ) })
test('throws an error if there are multiple entries for the same topic', async () => { admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) const topics = [{ topic: 'topic-123' }, { topic: 'topic-123' }] await expect(admin.createTopics({ topics })).rejects.toHaveProperty( 'message', 'Invalid topics array, it cannot have multiple entries for the same topic' ) })