Skip to content

Commit 536c0af

Browse files
committed
Adapt to project style and few minor fixes
1 parent b3a9e7a commit 536c0af

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

src/client.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,7 @@ impl<C: ClientContext> Client<C> {
407407
///
408408
/// [`MockCluster`]: crate::mocking::MockCluster
409409
pub fn mock_cluster(&self) -> Option<MockCluster<'_, C>> {
410-
let mc = unsafe { rdsys::rd_kafka_handle_mock_cluster(self.native.ptr()) };
411-
if !mc.is_null() {
412-
Some(MockCluster::new_ref(mc, self))
413-
} else {
414-
None
415-
}
410+
MockCluster::from_client(self)
416411
}
417412

418413
/// Returns a NativeTopic from the current client. The NativeTopic shouldn't outlive the client

src/mocking.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::error::{IsError, KafkaError, KafkaResult};
2525
use crate::producer::DefaultProducerContext;
2626
use crate::ClientContext;
2727

28-
/// Used internally by `MockCluster` to distinguish whether the mock cluster is owned or referenced.
28+
/// Used internally by `MockCluster` to distinguish whether the mock cluster is owned or borrowed.
2929
///
3030
/// The mock cluster can be created in two ways:
3131
///
@@ -34,13 +34,13 @@ use crate::ClientContext;
3434
///
3535
/// - By setting `test.mock.num.brokers` in a configuration of a producer/consumer client.
3636
/// In this case, the client creates the mock cluster internally and destroys it in its d-tor,
37-
/// and we only hold a reference to the mock cluster obtained with `rd_kafka_handle_mock_cluster()` (cf. `Client::mock_cluser()`).
37+
/// and we only hold a reference to the mock cluster obtained with `rd_kafka_handle_mock_cluster()` (cf. `Client::mock_cluster()`).
3838
///
39-
/// In this case, we **must neither** destroy the mock clsuter in `MockCluster`'s `drop()`,
39+
/// In this case, we **must neither** destroy the mock cluster in `MockCluster`'s `drop()`,
4040
/// **nor** outlive the `Client` from which the reference is obtained, hence the lifetime.
4141
enum MockClusterClient<'c, C: ClientContext> {
4242
Owned(Client<C>),
43-
Ref(&'c Client<C>),
43+
Borrowed(&'c Client<C>),
4444
}
4545

4646
/// Mock Kafka cluster with a configurable number of brokers that support a reasonable subset of
@@ -90,7 +90,7 @@ pub enum MockCoordinator {
9090
}
9191

9292
impl MockCluster<'static, DefaultProducerContext> {
93-
/// Create new mock cluster with a given number of brokers
93+
/// Creates a new mock cluster with the given number of brokers
9494
pub fn new(broker_count: i32) -> KafkaResult<Self> {
9595
let config = ClientConfig::new();
9696
let native_config = config.create_native_config()?;
@@ -103,8 +103,8 @@ impl MockCluster<'static, DefaultProducerContext> {
103103
context,
104104
)?;
105105

106-
let kafka_ptr = client.native_ptr();
107-
let mock_cluster = unsafe { rdsys::rd_kafka_mock_cluster_new(kafka_ptr, broker_count) };
106+
let mock_cluster =
107+
unsafe { rdsys::rd_kafka_mock_cluster_new(client.native_ptr(), broker_count) };
108108
if mock_cluster.is_null() {
109109
return Err(KafkaError::MockCluster(rdsys::RDKafkaErrorCode::Fail));
110110
}
@@ -120,20 +120,24 @@ impl<'c, C> MockCluster<'c, C>
120120
where
121121
C: ClientContext,
122122
{
123-
pub(crate) fn new_ref(mock_cluster: *mut RDKafkaMockCluster, client: &'c Client<C>) -> Self {
124-
Self {
125-
mock_cluster,
126-
client: MockClusterClient::Ref(client),
123+
/// Returns the mock cluster associated with the given client if any
124+
pub(crate) fn from_client(client: &'c Client<C>) -> Option<Self> {
125+
let mock_cluster = unsafe { rdsys::rd_kafka_handle_mock_cluster(client.native_ptr()) };
126+
if mock_cluster.is_null() {
127+
return None;
127128
}
129+
130+
Some(MockCluster {
131+
mock_cluster,
132+
client: MockClusterClient::Borrowed(client),
133+
})
128134
}
129135

130-
/// Obtain the bootstrap address for the mock cluster
136+
/// Returns the mock cluster's bootstrap.servers list
131137
pub fn bootstrap_servers(&self) -> String {
132-
let raw =
138+
let bootstrap =
133139
unsafe { CStr::from_ptr(rdsys::rd_kafka_mock_cluster_bootstraps(self.mock_cluster)) };
134-
raw.to_str()
135-
.expect("Unexpected non-Unicode characters in bootstrap servers")
136-
.to_string()
140+
bootstrap.to_string_lossy().to_string()
137141
}
138142

139143
/// Create a topic
@@ -147,20 +151,20 @@ where
147151
partition_count: i32,
148152
replication_factor: i32,
149153
) -> KafkaResult<()> {
150-
let raw_topic = CString::new(topic).unwrap();
154+
let topic_c = CString::new(topic)?;
151155
return_mock_op! {
152156
unsafe {
153157
rdsys::rd_kafka_mock_topic_create(
154158
self.mock_cluster,
155-
raw_topic.as_ptr(),
159+
topic_c.as_ptr(),
156160
partition_count,
157161
replication_factor,
158162
)
159163
}
160164
}
161165
}
162166

163-
/// Sets the parititon leader
167+
/// Sets the partition leader
164168
///
165169
/// The topic will be created if it does not exist.
166170
///
@@ -171,22 +175,22 @@ where
171175
partition: i32,
172176
broker_id: Option<i32>,
173177
) -> KafkaResult<()> {
174-
let raw_topic = CString::new(topic).unwrap();
178+
let topic_c = CString::new(topic)?;
175179
let broker_id = broker_id.unwrap_or(-1);
176180

177181
return_mock_op! {
178182
unsafe {
179183
rdsys::rd_kafka_mock_partition_set_leader(
180184
self.mock_cluster,
181-
raw_topic.as_ptr(),
185+
topic_c.as_ptr(),
182186
partition,
183187
broker_id,
184188
)
185189
}
186190
}
187191
}
188192

189-
/// Sets the partitions preferred replica / follower.
193+
/// Sets the partition's preferred replica / follower.
190194
///
191195
/// The topic will be created if it does not exist.
192196
///
@@ -197,37 +201,37 @@ where
197201
partition: i32,
198202
broker_id: i32,
199203
) -> KafkaResult<()> {
200-
let raw_topic = CString::new(topic).unwrap();
204+
let topic_c = CString::new(topic)?;
201205

202206
return_mock_op! {
203207
unsafe {
204208
rdsys::rd_kafka_mock_partition_set_follower(
205-
self.mock_cluster, raw_topic.as_ptr(), partition, broker_id)
209+
self.mock_cluster, topic_c.as_ptr(), partition, broker_id)
206210
}
207211
}
208212
}
209213

210-
/// Set the partitions preferred replicate / follower low and high watermarks.
214+
/// Sets the partition's preferred replica / follower low and high watermarks.
211215
///
212216
/// The topic will be created if it does not exist.
213217
///
214-
/// Setting an offset to `Non` will revert back to the leaders corresponding watermark.
218+
/// Setting an offset to `None` will revert back to the leader's corresponding watermark.
215219
pub fn follower_watermarks(
216220
&self,
217221
topic: &str,
218222
partition: i32,
219223
low_watermark: Option<i64>,
220224
high_watermark: Option<i64>,
221225
) -> KafkaResult<()> {
222-
let raw_topic = CString::new(topic).unwrap();
226+
let topic_c = CString::new(topic)?;
223227
let low_watermark = low_watermark.unwrap_or(-1);
224228
let high_watermark = high_watermark.unwrap_or(-1);
225229

226230
return_mock_op! {
227231
unsafe {
228232
rdsys::rd_kafka_mock_partition_set_follower_wmarks(
229233
self.mock_cluster,
230-
raw_topic.as_ptr(),
234+
topic_c.as_ptr(),
231235
partition,
232236
low_watermark,
233237
high_watermark
@@ -237,6 +241,7 @@ where
237241
}
238242

239243
/// Disconnects the broker and disallows any new connections.
244+
/// Use -1 for all brokers, or >= 0 for a specific broker.
240245
///
241246
/// NOTE: This does NOT trigger leader change.
242247
pub fn broker_down(&self, broker_id: i32) -> KafkaResult<()> {
@@ -247,7 +252,8 @@ where
247252
}
248253
}
249254

250-
/// brief Makes the broker accept connections again.
255+
/// Makes the broker accept connections again.
256+
/// Use -1 for all brokers, or >= 0 for a specific broker.
251257
///
252258
/// NOTE: This does NOT trigger leader change.
253259
pub fn broker_up(&self, broker_id: i32) -> KafkaResult<()> {
@@ -259,6 +265,7 @@ where
259265
}
260266

261267
/// Set broker round-trip-time delay in milliseconds.
268+
/// Use -1 for all brokers, or >= 0 for a specific broker.
262269
pub fn broker_round_trip_time(&self, broker_id: i32, delay: Duration) -> KafkaResult<()> {
263270
let rtt_ms = delay.as_millis().try_into().unwrap_or(c_int::MAX);
264271

@@ -274,14 +281,15 @@ where
274281
}
275282

276283
/// Sets the broker's rack as reported in Metadata to the client.
284+
/// Use -1 for all brokers, or >= 0 for a specific broker.
277285
pub fn broker_rack(&self, broker_id: i32, rack: &str) -> KafkaResult<()> {
278-
let raw_rack = CString::new(rack).unwrap();
286+
let rack_c = CString::new(rack)?;
279287
return_mock_op! {
280288
unsafe {
281289
rdsys::rd_kafka_mock_broker_set_rack(
282290
self.mock_cluster,
283291
broker_id,
284-
raw_rack.as_ptr()
292+
rack_c.as_ptr()
285293
)
286294
}
287295
}
@@ -291,22 +299,22 @@ where
291299
///
292300
/// If this API is not a standard hashing scheme will be used.
293301
///
294-
/// `broker_id` does not need to point to an existing broker.`
302+
/// `broker_id` does not need to point to an existing broker.
295303
pub fn coordinator(&self, coordinator: MockCoordinator, broker_id: i32) -> KafkaResult<()> {
296304
let (kind, key) = match coordinator {
297305
MockCoordinator::Transaction(key) => ("transaction", key),
298306
MockCoordinator::Group(key) => ("group", key),
299307
};
300308

301-
let raw_kind = CString::new(kind).unwrap();
302-
let raw_key = CString::new(key).unwrap();
309+
let kind_c = CString::new(kind)?;
310+
let raw_c = CString::new(key)?;
303311

304312
return_mock_op! {
305313
unsafe {
306314
rdsys::rd_kafka_mock_coordinator_set(
307315
self.mock_cluster,
308-
raw_kind.as_ptr(),
309-
raw_key.as_ptr(),
316+
kind_c.as_ptr(),
317+
raw_c.as_ptr(),
310318
broker_id
311319
)
312320
}
@@ -356,7 +364,7 @@ mod tests {
356364
.create()
357365
.expect("Client creation error");
358366

359-
let rec = FutureRecord::to(TOPIC).key(b"msg1").payload(b"test");
367+
let rec = FutureRecord::to(TOPIC).key("msg1").payload("test");
360368
producer.send_result(rec).unwrap().await.unwrap().unwrap();
361369

362370
consumer.subscribe(&[TOPIC]).unwrap();

0 commit comments

Comments
 (0)