forked from tulios/kafkajs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into concurrent-message-processing
- Loading branch information
Showing
11 changed files
with
239 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const createAdmin = require('../index') | ||
|
||
const { secureRandom, createCluster, newLogger, createTopic } = require('testHelpers') | ||
|
||
describe('Admin', () => { | ||
let existingTopicName, numPartitions, admin, consumer | ||
|
||
beforeEach(async () => { | ||
existingTopicName = `test-topic-${secureRandom()}` | ||
numPartitions = 4 | ||
|
||
await createTopic({ topic: existingTopicName, partitions: numPartitions }) | ||
}) | ||
|
||
afterEach(async () => { | ||
await admin.disconnect() | ||
consumer && (await consumer.disconnect()) | ||
}) | ||
|
||
describe('fetchTopicMetadata', () => { | ||
test('throws an error if the topic name is not a valid string', async () => { | ||
admin = createAdmin({ cluster: createCluster(), logger: newLogger() }) | ||
await expect(admin.fetchTopicMetadata({ topics: [null] })).rejects.toHaveProperty( | ||
'message', | ||
'Invalid topic null' | ||
) | ||
}) | ||
|
||
test('retrieves metadata for each partition in the topic', async () => { | ||
const cluster = createCluster() | ||
admin = createAdmin({ cluster, logger: newLogger() }) | ||
|
||
await admin.connect() | ||
const { topics: topicsMetadata } = await admin.fetchTopicMetadata({ | ||
topics: [existingTopicName], | ||
}) | ||
|
||
expect(topicsMetadata).toHaveLength(1) | ||
const topicMetadata = topicsMetadata[0] | ||
expect(topicMetadata).toHaveProperty('name', existingTopicName) | ||
expect(topicMetadata.partitions).toHaveLength(numPartitions) | ||
|
||
topicMetadata.partitions.forEach(partition => { | ||
expect(partition).toHaveProperty('partitionId') | ||
expect(partition).toHaveProperty('leader') | ||
expect(partition).toHaveProperty('replicas') | ||
expect(partition).toHaveProperty('partitionErrorCode') | ||
expect(partition).toHaveProperty('isr') | ||
}) | ||
}) | ||
|
||
test('by default retrieves metadata for all topics', async () => { | ||
const cluster = createCluster() | ||
admin = createAdmin({ cluster, logger: newLogger() }) | ||
|
||
await admin.connect() | ||
const { topics } = await admin.fetchTopicMetadata() | ||
expect(topics.length).toBeGreaterThanOrEqual(1) | ||
}) | ||
|
||
test('creates a new topic if the topic does not exist and "allowAutoTopicCreation" is true', async () => { | ||
admin = createAdmin({ | ||
cluster: createCluster({ allowAutoTopicCreation: true }), | ||
logger: newLogger(), | ||
}) | ||
const newTopicName = `test-topic-${secureRandom()}` | ||
|
||
await admin.connect() | ||
const { topics: topicsMetadata } = await admin.fetchTopicMetadata({ | ||
topics: [existingTopicName, newTopicName], | ||
}) | ||
|
||
expect(topicsMetadata[0]).toHaveProperty('name', existingTopicName) | ||
expect(topicsMetadata[1]).toHaveProperty('name', newTopicName) | ||
expect(topicsMetadata[1].partitions).toHaveLength(1) | ||
}) | ||
|
||
test('throws an error if the topic does not exist and "allowAutoTopicCreation" is false', async () => { | ||
admin = createAdmin({ | ||
cluster: createCluster({ allowAutoTopicCreation: false }), | ||
logger: newLogger(), | ||
}) | ||
const newTopicName = `test-topic-${secureRandom()}` | ||
|
||
await admin.connect() | ||
await expect( | ||
admin.fetchTopicMetadata({ | ||
topics: [existingTopicName, newTopicName], | ||
}) | ||
).rejects.toHaveProperty('message', 'This server does not host this topic-partition') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { createCluster, secureRandom, createTopic } = require('testHelpers') | ||
|
||
describe('Cluster > metadata', () => { | ||
let cluster, topic1, topic2, topic3 | ||
|
||
beforeEach(async () => { | ||
topic1 = `test-topic-${secureRandom()}` | ||
topic2 = `test-topic-${secureRandom()}` | ||
topic3 = `test-topic-${secureRandom()}` | ||
|
||
await createTopic({ topic: topic1 }) | ||
await createTopic({ topic: topic2 }) | ||
await createTopic({ topic: topic3 }) | ||
|
||
cluster = createCluster() | ||
await cluster.connect() | ||
}) | ||
|
||
afterEach(async () => { | ||
cluster && (await cluster.disconnect()) | ||
}) | ||
|
||
test('returns metadata for a set of topics', async () => { | ||
const response = await cluster.metadata({ topics: [topic1, topic2] }) | ||
expect(response.topicMetadata.length).toEqual(2) | ||
|
||
const topics = response.topicMetadata.map(({ topic }) => topic).sort() | ||
expect(topics).toEqual([topic1, topic2].sort()) | ||
}) | ||
|
||
test('returns metadata for all topics', async () => { | ||
const response = await cluster.metadata({ topics: [] }) | ||
expect(response.topicMetadata.length).toBeGreaterThanOrEqual(3) | ||
|
||
const topics = response.topicMetadata.map(({ topic }) => topic).sort() | ||
expect(topics).toEqual(expect.arrayContaining([topic1, topic2, topic3].sort())) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters