-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathutils.rs
230 lines (200 loc) · 6.91 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#![allow(dead_code)]
use std::collections::HashMap;
use std::env::{self, VarError};
use std::time::Duration;
use rand::Rng;
use regex::Regex;
use rdkafka::admin::{AdminClient, AdminOptions, NewTopic, TopicReplication};
use rdkafka::client::ClientContext;
use rdkafka::config::ClientConfig;
use rdkafka::consumer::ConsumerContext;
use rdkafka::error::KafkaResult;
use rdkafka::message::ToBytes;
use rdkafka::producer::{FutureProducer, FutureRecord};
use rdkafka::statistics::Statistics;
use rdkafka::TopicPartitionList;
pub fn rand_test_topic(test_name: &str) -> String {
let id = rand::thread_rng()
.gen_ascii_chars()
.take(20)
.collect::<String>();
format!("__{}_{}", test_name, id)
}
pub fn rand_test_group() -> String {
let id = rand::thread_rng()
.gen_ascii_chars()
.take(10)
.collect::<String>();
format!("__test_{}", id)
}
pub fn rand_test_transactional_id() -> String {
let id = rand::thread_rng()
.gen_ascii_chars()
.take(10)
.collect::<String>();
format!("__test_{}", id)
}
pub fn get_bootstrap_server() -> String {
env::var("KAFKA_HOST").unwrap_or_else(|_| "localhost:9092".to_owned())
}
pub fn get_broker_version() -> KafkaVersion {
// librdkafka doesn't expose this directly, sadly.
match env::var("KAFKA_VERSION") {
Ok(v) => {
let regex = Regex::new(r"^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?$").unwrap();
match regex.captures(&v) {
Some(captures) => {
let extract = |i| {
captures
.get(i)
.map(|m| m.as_str().parse().unwrap())
.unwrap_or(0)
};
KafkaVersion(extract(1), extract(2), extract(3), extract(4))
}
None => panic!("KAFKA_VERSION env var was not in expected [n[.n[.n[.n]]]] format"),
}
}
Err(VarError::NotUnicode(_)) => {
panic!("KAFKA_VERSION env var contained non-unicode characters")
}
// If the environment variable is unset, assume we're running the latest version.
Err(VarError::NotPresent) => {
KafkaVersion(std::u32::MAX, std::u32::MAX, std::u32::MAX, std::u32::MAX)
}
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct KafkaVersion(pub u32, pub u32, pub u32, pub u32);
pub struct ProducerTestContext {
_some_data: i64, // Add some data so that valgrind can check proper allocation
}
impl ClientContext for ProducerTestContext {
fn stats(&self, _: Statistics) {} // Don't print stats
}
pub async fn create_topic(name: &str, partitions: i32) {
let client: AdminClient<_> = consumer_config("create_topic", None).create().unwrap();
client
.create_topics(
&[NewTopic::new(name, partitions, TopicReplication::Fixed(1))],
&AdminOptions::new(),
)
.await
.unwrap();
}
/// Produce the specified count of messages to the topic and partition specified. A map
/// of (partition, offset) -> message id will be returned. It panics if any error is encountered
/// while populating the topic.
pub async fn populate_topic<P, K, J, Q>(
topic_name: &str,
count: i32,
value_fn: &P,
key_fn: &K,
partition: Option<i32>,
timestamp: Option<i64>,
) -> HashMap<(i32, i64), i32>
where
P: Fn(i32) -> J,
K: Fn(i32) -> Q,
J: ToBytes,
Q: ToBytes,
{
let prod_context = ProducerTestContext { _some_data: 1234 };
// Produce some messages
let producer = &ClientConfig::new()
.set("bootstrap.servers", get_bootstrap_server().as_str())
.set("statistics.interval.ms", "500")
.set("api.version.request", "true")
.set("debug", "all")
.set("message.timeout.ms", "30000")
.create_with_context::<ProducerTestContext, FutureProducer<_>>(prod_context)
.expect("Producer creation error");
let futures = (0..count)
.map(|id| {
let future = async move {
producer
.send(
FutureRecord {
topic: topic_name,
payload: Some(&value_fn(id)),
key: Some(&key_fn(id)),
partition,
timestamp,
headers: None,
},
Duration::from_secs(1),
)
.await
};
(id, future)
})
.collect::<Vec<_>>();
let mut message_map = HashMap::new();
for (id, future) in futures {
match future.await {
Ok((partition, offset)) => message_map.insert((partition, offset), id),
Err((kafka_error, _message)) => panic!("Delivery failed: {}", kafka_error),
};
}
message_map
}
pub fn value_fn(id: i32) -> String {
format!("Message {}", id)
}
pub fn key_fn(id: i32) -> String {
format!("Key {}", id)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_populate_topic() {
let topic_name = rand_test_topic("test_populate_topic");
let message_map = populate_topic(&topic_name, 100, &value_fn, &key_fn, Some(0), None).await;
let total_messages = message_map
.iter()
.filter(|&(&(partition, _), _)| partition == 0)
.count();
assert_eq!(total_messages, 100);
let mut ids = message_map.iter().map(|(_, id)| *id).collect::<Vec<_>>();
ids.sort();
assert_eq!(ids, (0..100).collect::<Vec<_>>());
}
}
pub struct ConsumerTestContext {
pub _n: i64, // Add data for memory access validation
}
impl ClientContext for ConsumerTestContext {
// Access stats
fn stats(&self, stats: Statistics) {
let stats_str = format!("{:?}", stats);
println!("Stats received: {} bytes", stats_str.len());
}
}
impl ConsumerContext for ConsumerTestContext {
fn commit_callback(&self, result: KafkaResult<()>, _offsets: &TopicPartitionList) {
println!("Committing offsets: {:?}", result);
}
}
pub fn consumer_config(
group_id: &str,
config_overrides: Option<HashMap<&str, &str>>,
) -> ClientConfig {
let mut config = ClientConfig::new();
config.set("group.id", group_id);
config.set("client.id", "rdkafka_integration_test_client");
config.set("bootstrap.servers", get_bootstrap_server().as_str());
config.set("enable.partition.eof", "false");
config.set("session.timeout.ms", "6000");
config.set("enable.auto.commit", "false");
config.set("statistics.interval.ms", "500");
config.set("api.version.request", "true");
config.set("debug", "all");
config.set("auto.offset.reset", "earliest");
if let Some(overrides) = config_overrides {
for (key, value) in overrides {
config.set(key, value);
}
}
config
}