Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: be able to specify configuration when creating topic [sc-14009] #44

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Sources/Kafka/ForTesting/RDKafkaClient+Topic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ extension RDKafkaClient {
/// Create a topic with a unique name (`UUID`).
/// Blocks for a maximum of `timeout` milliseconds.
/// - Parameter partitions: Partitions in topic (default: -1 - default for broker)
/// - Parameter config: Topic configuration, if empty broker defaults are used. See https://kafka.apache.org/documentation.html#topicconfigs
/// - Parameter timeout: Timeout in milliseconds.
/// - Returns: Name of newly created topic.
/// - Throws: A ``KafkaError`` if the topic creation failed.
public func _createUniqueTopic(partitions: Int32 = -1, timeout: Int32) throws -> String {
public func _createUniqueTopic(partitions: Int32 = -1, config: [String: String] = [:], timeout: Int32) throws -> String {
let uniqueTopicName = UUID().uuidString

try _createTopic(topicName: uniqueTopicName, partitions: partitions, timeout: timeout)
try _createTopic(topicName: uniqueTopicName, partitions: partitions, config: config, timeout: timeout)

return uniqueTopicName
}

/// Create a topic with specified name
/// Blocks for a maximum of `timeout` milliseconds.
/// - Parameter partitions: Partitions in topic (default: -1 - default for broker)
/// - Parameter config: Topic configuration, if empty broker defaults are used. See https://kafka.apache.org/documentation.html#topicconfigs
/// - Parameter timeout: Timeout in milliseconds.
/// - Returns: Name of newly created topic.
/// - Throws: A ``KafkaError`` if the topic creation failed.
public func _createTopic(topicName: String, partitions: Int32 = -1, timeout: Int32) throws {
public func _createTopic(topicName: String, partitions: Int32 = -1, config: [String: String] = [:], timeout: Int32) throws {
let errorChars = UnsafeMutablePointer<CChar>.allocate(capacity: RDKafkaClient.stringSize)
defer { errorChars.deallocate() }

Expand All @@ -54,6 +56,13 @@ extension RDKafkaClient {
}
defer { rd_kafka_NewTopic_destroy(newTopic) }

for (key, value) in config {
let resultCode = rd_kafka_NewTopic_set_config(newTopic, key, value)
guard resultCode == RD_KAFKA_RESP_ERR_NO_ERROR else {
throw KafkaError.rdKafkaError(wrapping: resultCode)
}
}

try self.withKafkaHandlePointer { kafkaHandle in
let resultQueue = rd_kafka_queue_new(kafkaHandle)
defer { rd_kafka_queue_destroy(resultQueue) }
Expand Down
Loading