Skip to main content

confluent_kafka/producer/
mock_producer.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Mock producer for testing code that uses Kafka.
16//!
17//! Corresponds to Java's `org.apache.kafka.clients.producer.MockProducer`.
18//!
19//! By default this mock will synchronously complete each send call successfully.
20//! However it can be configured to allow the user to control the completion of
21//! the call and supply an optional error for the producer to throw.
22//!
23//! # Transactional API
24//!
25//! The [`Producer`] trait's transactional methods are pure in-memory state:
26//! there is no coordinator, no `TransactionManager` and no network, matching
27//! Java's `MockProducer`. While a transaction is in flight, sends are staged in
28//! `uncommitted_sends` (Java `MockProducer`) and offsets in
29//! `uncommitted_consumer_group_offsets` (`MockProducer#sendOffsetsToTransaction`) rather than published;
30//! [`commit_transaction`](Producer::commit_transaction) moves both into the
31//! visible [`history()`](MockProducer::history) and
32//! [`consumer_group_offsets_history()`](MockProducer::consumer_group_offsets_history),
33//! while [`abort_transaction`](Producer::abort_transaction) discards them.
34//!
35//! Misuse returns `Err` where Java throws: `IllegalStateException` becomes
36//! [`Error::local_illegal_state`] and `ProducerFencedException` becomes a
37//! [`Error`] carrying [`Errors::ProducerFenced`], with Java's message text
38//! preserved verbatim (CLAUDE.md §12.2).
39
40use std::collections::{HashMap, VecDeque};
41use std::sync::{Arc, Mutex};
42use std::time::Duration;
43
44use super::Partitioner;
45use super::Producer;
46use super::ProducerRecord;
47use super::RecordMetadata;
48use super::internals::FutureRecordMetadata;
49use super::internals::ProduceRequestResult;
50use crate::common::Cluster;
51use crate::common::Error;
52use crate::common::KafkaFuture;
53use crate::common::MetricName;
54use crate::common::PartitionInfo;
55use crate::common::TopicPartition;
56use crate::common::header::internals::RecordHeaders;
57use crate::common::metrics::metrics_view::SharedMetricMap;
58use crate::common::metrics::{KafkaMetric, MetricsView};
59use crate::common::protocol::Errors;
60use crate::common::record::internal::RecordBatch;
61use crate::common::serialization::{DynSerializer, Serializer};
62use crate::common::utils::SystemTime;
63use crate::consumer::ConsumerGroupMetadata;
64use crate::consumer::OffsetAndMetadata;
65
66use super::Callback;
67
68// =========================================================================
69// PHASE-7 METHOD ACCOUNTING (`definition-of-done.md` §2)
70//
71// `MockProducer.java` declares **40** distinct method names at class level.
72// **33** have a Rust `fn` in this file; the **7** absent are named below with
73// what each is blocked on.
74//
75// Constructors are excluded by construction rather than by assertion: the
76// negative lookahead after the modifier run forces a return type *and* a name,
77// and a constructor has only a name. (Critic 45 issue 4 was the failure of
78// asserting that instead — with the modifier group free to match zero times the
79// engine reads `public` as the return type and counts the constructor anyway.)
80// The Rust file is cut at its `#[cfg(test)]` module before the `fn` scan, so no
81// test-only item can satisfy a production-method claim, and the `assert` on the
82// cut's length is not decoration: a rename of that attribute would otherwise
83// leave an empty corpus reporting all 40 as absent.
84//
85// Derivation (real output below it):
86//
87//   python3 - <<'PY'
88//   import re
89//   J = ("kafka/clients/src/main/java/org/apache/kafka/clients/producer/"
90//        "MockProducer.java")
91//   R = "src/producer/mock_producer.rs"
92//   MODS = r'(?:public|private|protected|synchronized|static|final|abstract)'
93//   decl = re.compile(rf'^    (?:{MODS}\s+)*(?!{MODS}[\s(])'
94//                     rf'[A-Za-z_][A-Za-z0-9_\.\[\]]*(?:<.*>)?\s+'
95//                     rf'([a-zA-Z_][A-Za-z0-9_]*)\s*\(')
96//   names = {}
97//   for i, line in enumerate(open(J), 1):
98//       if any(k in line for k in ('class ', 'enum ', 'interface ')): continue
99//       m = decl.match(line)
100//       if m: names.setdefault(m.group(1), i)
101//   snake = lambda n: re.sub(r'(?<!^)(?=[A-Z])', '_', n).lower()
102//   production = open(R).read().split("\n#[cfg(test)]\n")[0]
103//   assert len(production) > 20000, len(production)
104//   defs = set(re.findall(r'\bfn ([a-z_0-9]+)\s*[(<]', production))
105//   missing = sorted((ln, n) for n, ln in names.items() if snake(n) not in defs)
106//   print(f"{len(names)} declared, {len(names)-len(missing)} present, "
107//         f"{len(missing)} absent")
108//   for ln, n in missing: print(f"  {n}")
109//   PY
110//
111//   40 declared, 33 present, 7 absent
112//     disableTelemetry
113//     injectTimeoutException
114//     setClientInstanceId
115//     clientInstanceId
116//     addedMetrics
117//     registerMetricForSubscription
118//     unregisterMetricFromSubscription
119//
120// All seven predate Phase 7 and none is in its scope — PLAN §Phase-7 enumerates
121// the transactional surface, and these are two other features:
122//
123//   `clientInstanceId`, `registerMetricForSubscription`,
124//     `unregisterMetricFromSubscription` — `Producer`-interface methods
125//     the *Rust trait does not declare*. The gap is in `producer.rs`, not
126//     here: a `MockProducer` impl would have nothing to override. Tracked as
127//     PLAN §9.23.
128//   `disableTelemetry`, `injectTimeoutException`,
129//     `setClientInstanceId`, `addedMetrics` — the mock-only knobs
130//     that exist to drive those same two features. They follow whenever the
131//     three above land. Same §9.23.
132//
133// Three names the Phase-7 paste listed as absent are now present, which is why
134// this reads 33/7 where Phase 7 read 31/9:
135//   - `partition` — the pluggable `Partitioner` phase gave the mock a
136//     `partitioner` field plus the two `Serializer`s and translated
137//     `partition()` onto `MockProducerInner`.
138//   - `metrics` and `setMockMetrics` — landed with the
139//     producer-metrics milestone (which post-dates the Phase-7 paste), as
140//     `fn metrics` / `fn set_mock_metrics`.
141//
142// What that costs the test parity, precisely: all seven appear **zero** times in
143// `MockProducerTest.java`, so they block nothing —
144//
145//   J=kafka/clients/src/test/java/org/apache/kafka/clients/producer/MockProducerTest.java
146//   for m in disableTelemetry injectTimeoutException setClientInstanceId \
147//            clientInstanceId addedMetrics registerMetricForSubscription \
148//            unregisterMetricFromSubscription; do
149//     printf '%s %s\n' "$m" "$(grep -c "$m" $J)"; done
150//
151// prints `0` seven times. `partition` is driven by the `RoundRobinPartitioner`
152// that `testPartitioner` passes; that test is now translated
153// faithfully (it was previously present but weakened). The serializers
154// `shouldThrowClassCastException` passes still have no Rust analogue —
155// static typing rules the mis-apply out — so that test remains not applicable.
156// Both are stated at their entries in the test accounting block at the end of
157// this file.
158//
159// Two Java names cover four Rust `fn`s, so "33 present" is not "33 signatures":
160// `send` → `send` / `send_with_callback`, and `close` →
161// `close` / `close_with_timeout`. The nested `Completion` class sits at 8-space
162// indent and so is outside the scan; its one method, `complete`, is
163// translated as `Completion::complete`.
164//
165// Java's nine public `RuntimeException` *fields* are not methods and are
166// invisible to the derivation. All nine are present as `set_*_error` setters;
167// the five transactional ones landed in Phase 7, covered by
168// `test_set_transactional_errors`.
169// =========================================================================
170
171/// A mock of the producer interface for testing code that uses Kafka.
172///
173/// By default this mock will synchronously complete each send call successfully.
174/// However it can be configured to allow the user to control the completion of
175/// the call and supply an optional error for the producer to return.
176//
177// All methods use interior mutability via `Mutex`, matching
178// Java's `synchronized` methods. The struct is `Send + Sync` so it can be
179// shared via `Arc`.
180#[doc(alias = "org.apache.kafka.clients.producer.MockProducer")]
181pub struct MockProducer<K, V> {
182    inner: Mutex<MockProducerInner<K, V>>,
183    /// Java `keySerializer`, or `None`. Outside the
184    /// mutex: Java assigns it once in the constructor and only reads it, and a
185    /// serializer is awaited, which must not happen with the `inner` guard held
186    /// (CLAUDE.md §11.6).
187    key_serializer: Option<Box<dyn DynSerializer<K>>>,
188    /// Java `valueSerializer`, or `None`. Outside the
189    /// mutex, as `key_serializer`.
190    value_serializer: Option<Box<dyn DynSerializer<V>>>,
191    /// Java `mockMetrics`: the user-supplied metrics returned by
192    /// [`metrics()`](Producer::metrics), seeded via
193    /// [`set_mock_metrics`](MockProducer::set_mock_metrics). Outside the mutex:
194    /// a view returned by `metrics()` reads it, as Java returns the field.
195    mock_metrics: SharedMetricMap,
196}
197
198impl<K, V> std::fmt::Debug for MockProducer<K, V> {
199    // The mock's flags and how many records it holds, not the records.
200    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
201        let inner = self.inner.lock().unwrap();
202        f.debug_struct("MockProducer")
203            .field("auto_complete", &inner.auto_complete)
204            .field("closed", &inner.closed)
205            .field("sent", &inner.sent.len())
206            .finish_non_exhaustive()
207    }
208}
209
210/// The offsets one transaction contributed, grouped by consumer group id.
211///
212/// Spells Java's `Map<String, Map<TopicPartition, OffsetAndMetadata>>`
213/// (`MockProducer#consumerGroupOffsetsHistory`); an alias rather than a new type, so it adds
214/// no struct absent from the Java source (`definition-of-done.md` §7).
215type ConsumerGroupOffsets = HashMap<String, HashMap<TopicPartition, OffsetAndMetadata>>;
216
217struct MockProducerInner<K, V> {
218    cluster: Cluster,
219    /// Java `partitioner` — the partition strategy, or
220    /// `None` when the mock was created without one.
221    partitioner: Option<Box<dyn Partitioner<K, V>>>,
222    auto_complete: bool,
223    /// Java `sent`.
224    sent: Vec<ProducerRecord<K, V>>,
225    /// Java `uncommittedSends` — records sent inside the in-flight
226    /// transaction, published to `sent` only on commit.
227    uncommitted_sends: Vec<ProducerRecord<K, V>>,
228    completions: VecDeque<Completion>,
229    offsets: HashMap<TopicPartition, i64>,
230    /// Java `consumerGroupOffsets` — one entry per committed
231    /// transaction that carried offsets.
232    consumer_group_offsets: Vec<ConsumerGroupOffsets>,
233    /// Java `uncommittedConsumerGroupOffsets`.
234    uncommitted_consumer_group_offsets: ConsumerGroupOffsets,
235    closed: bool,
236    /// Java `transactionInitialized`.
237    transaction_initialized: bool,
238    /// Java `transactionInFlight`.
239    transaction_in_flight: bool,
240    /// Java `transactionCommitted`.
241    transaction_committed: bool,
242    /// Java `transactionAborted`.
243    transaction_aborted: bool,
244    /// Java `producerFenced`.
245    producer_fenced: bool,
246    /// Java `sentOffsets`.
247    sent_offsets: bool,
248    /// Java `commitCount`.
249    commit_count: i64,
250    /// Java `initTransactionException`.
251    init_transaction_error: Option<Error>,
252    /// Java `beginTransactionException`.
253    begin_transaction_error: Option<Error>,
254    /// Java `sendOffsetsToTransactionException`.
255    send_offsets_to_transaction_error: Option<Error>,
256    /// Java `commitTransactionException`.
257    commit_transaction_error: Option<Error>,
258    /// Java `abortTransactionException`.
259    abort_transaction_error: Option<Error>,
260    /// Java `sendException`.
261    send_error: Option<Error>,
262    /// Java `flushException`.
263    flush_error: Option<Error>,
264    /// Java `partitionsForException`.
265    partitions_for_error: Option<Error>,
266    /// Java `closeException`.
267    close_error: Option<Error>,
268}
269
270impl<K, V> MockProducerInner<K, V> {
271    /// Corresponds to Java's `verifyNotClosed()`.
272    fn verify_not_closed(&self) -> Result<(), Error> {
273        if self.closed {
274            return Err(Error::local_illegal_state("MockProducer is already closed."));
275        }
276        Ok(())
277    }
278
279    /// Corresponds to Java's `verifyNotFenced()`,
280    /// which throws `ProducerFencedException`.
281    fn verify_not_fenced(&self) -> Result<(), Error> {
282        if self.producer_fenced {
283            return Err(Error::with_message(Errors::ProducerFenced, "MockProducer is fenced."));
284        }
285        Ok(())
286    }
287
288    /// Corresponds to Java's `verifyTransactionsInitialized()`
289    /// (`MockProducer#verifyTransactionsInitialized`).
290    fn verify_transactions_initialized(&self) -> Result<(), Error> {
291        if !self.transaction_initialized {
292            return Err(Error::local_illegal_state(
293                "MockProducer hasn't been initialized for transactions.",
294            ));
295        }
296        Ok(())
297    }
298
299    /// Corresponds to Java's `verifyTransactionInFlight()`
300    /// (`MockProducer#verifyTransactionInFlight`).
301    fn verify_transaction_in_flight(&self) -> Result<(), Error> {
302        if !self.transaction_in_flight {
303            return Err(Error::local_illegal_state("There is no open transaction."));
304        }
305        Ok(())
306    }
307
308    /// The checks of Java's `flush()`, before it completes the pending sends
309    /// ([`MockProducer::flush_pending`]). Note Java's `flush()` deliberately does
310    /// *not* `verifyNotFenced()` — see `shouldNotThrowOnFlushProducerIfProducerIsFenced`.
311    fn verify_can_flush(&self) -> Result<(), Error> {
312        self.verify_not_closed()?;
313
314        if let Some(err) = self.flush_error.as_ref() {
315            return Err(err.clone());
316        }
317        Ok(())
318    }
319
320    /// The explicit-partition branch of Java's `partition(ProducerRecord,
321    /// Cluster)` (`MockProducer#partition`): the record's own partition,
322    /// validated against the topic's partition count, or `None` when the record
323    /// has none and the key/value must be serialized first.
324    fn explicit_partition(&self, record: &ProducerRecord<K, V>) -> Result<Option<i32>, Error> {
325        let Some(partition) = record.partition() else {
326            return Ok(None);
327        };
328        let num_partitions = self.cluster.partitions_for_topic(record.topic()).len() as i32;
329        // they have given us a partition, use it
330        if partition < 0 || partition >= num_partitions {
331            return Err(Error::local_illegal_argument(format!(
332                "Invalid partition given with record: {partition} is not in the range [0...{num_partitions}]."
333            )));
334        }
335        Ok(Some(partition))
336    }
337
338    /// The rest of Java's `partition(ProducerRecord, Cluster)`
339    /// (`MockProducer#partition`), once the key and value are serialized:
340    /// the partitioner's choice or, when there is none, the first partition of
341    /// the topic (`MockProducer#partition`), exactly as Java's `partitioner == null` branch.
342    fn partition(&self, record: &ProducerRecord<K, V>, key_bytes: Option<&[u8]>, value_bytes: Option<&[u8]>) -> i32 {
343        let topic = record.topic();
344        match self.partitioner.as_ref() {
345            None => self.cluster.partitions_for_topic(topic)[0].partition(),
346            Some(partitioner) => {
347                partitioner.partition(topic, record.key(), key_bytes, record.value(), value_bytes, &self.cluster)
348            },
349        }
350    }
351
352    /// The checks Java's `send` makes before it partitions the record
353    /// (`MockProducer#send`).
354    fn verify_can_send(&self) -> Result<(), Error> {
355        if self.closed {
356            return Err(Error::local_illegal_state("MockProducer is already closed."));
357        }
358
359        // Java `MockProducer#send` throws `KafkaException("MockProducer is fenced.", new
360        // ProducerFencedException("Fenced"))` — deliberately a DIFFERENT value from
361        // `verifyNotFenced`'s bare `ProducerFencedException("MockProducer is fenced.")`
362        // (`MockProducer#verifyNotFenced`): here the fenced error is the *cause* of a bare `KafkaException`,
363        // which is what `shouldThrowOnSendIfProducerGotFenced` asserts
364        // (`assertThrows(KafkaException.class, ..)` plus
365        // `assertInstanceOf(ProducerFencedException.class, e.getCause())`). The outer
366        // error must therefore be a bare `KafkaError` — `is_api_error()` is `false`
367        // for it and `true` for `ProducerFencedError`.
368        if self.producer_fenced {
369            return Err(Error::kafka_message_source(
370                "MockProducer is fenced.",
371                Error::with_message(Errors::ProducerFenced, "Fenced"),
372            ));
373        }
374
375        if let Some(err) = self.send_error.as_ref() {
376            return Err(err.clone());
377        }
378        Ok(())
379    }
380}
381
382/// Internal completion record that holds the state needed to fulfill a
383/// [`FutureRecordMetadata`].
384///
385/// Corresponds to Java's `MockProducer.Completion` inner class.
386#[doc(alias = "org.apache.kafka.clients.producer.MockProducer$Completion")]
387struct Completion {
388    offset: i64,
389    metadata: RecordMetadata,
390    result: Arc<ProduceRequestResult>,
391    callback: Option<Box<dyn Callback>>,
392    topic_partition: TopicPartition,
393}
394
395impl Completion {
396    /// Complete this send with either a success or an error.
397    ///
398    /// Corresponds to Java's `Completion.complete(RuntimeException)`
399    /// (`MockProducer.Completion#complete`), whose three steps run in this order: set the
400    /// result, fire the callback, then `done()`. The `done()` last is load-bearing
401    /// in Java — it is the latch a `Future.get()` waits on, so a waiter cannot
402    /// observe the send as complete before the callback has returned. It is not
403    /// observable from the single task that calls `complete`, since both happen
404    /// before the call returns, but it is from a concurrent one.
405    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer$Completion#complete")]
406    fn complete(self, error: Option<Error>) {
407        let Completion { offset, metadata, result, callback, topic_partition } = self;
408        if let Some(e) = error {
409            let error_fn: Arc<dyn Fn(i32) -> Option<Error> + Send + Sync> = {
410                let e = e.clone();
411                Arc::new(move |_| Some(e.clone()))
412            };
413            result.set(-1, RecordBatch::NO_TIMESTAMP, Some(error_fn));
414            // `MockProducer.Completion#complete`: the callback still receives metadata on the error path,
415            // carrying -1 for every unknown field, exactly as `KafkaProducer`'s own
416            // error path does (`KafkaProducer::handle_api_error`).
417            if let Some(cb) = callback {
418                let null_metadata = RecordMetadata::new(topic_partition, -1, -1, RecordBatch::NO_TIMESTAMP, -1, -1);
419                cb.on_completion(Some(&null_metadata), Some(&e));
420            }
421        } else {
422            result.set(offset, RecordBatch::NO_TIMESTAMP, None);
423            // `MockProducer.Completion#complete`: fire the callback with the record's metadata.
424            if let Some(cb) = callback {
425                cb.on_completion(Some(&metadata), None);
426            }
427        }
428        result.done();
429    }
430}
431
432/// Parameters for [`MockProducer::with_options`].
433///
434/// This struct has **no Java counterpart**. It exists because a method taking
435/// more than three parameters beyond the overload group's intersection takes a
436/// single options struct instead: Java's widest
437/// `MockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)`
438/// constructor (`MockProducer#MockProducer`) differs from the group's parameter-name
439/// intersection — which is EMPTY, since the no-arg `MockProducer` is in the group — by
440/// five parameters, so this struct becomes the method's *only*
441/// parameter, carrying every Java parameter.
442///
443/// It deliberately has **no** `Default`. `auto_complete` is what both
444/// parameter-taking Java constructors (`MockProducer#MockProducer(Cluster,boolean,Partitioner,Serializer,Serializer)`, `MockProducer#MockProducer(boolean,Partitioner,Serializer,Serializer)`) take from their caller, so
445/// it has no Java-derived default; the no-arg `MockProducer#MockProducer()`, which does default it to
446/// `false`, is translated as [`Default for MockProducer`](MockProducer) instead.
447/// Construct it with [`MockProducerOptionsBuilder::new`] and set it:
448/// [`MockProducerOptionsBuilder::build`] returns an error if `auto_complete` was
449/// not set.
450#[non_exhaustive]
451// the options of MockProducer's constructor overloads (CLAUDE.md §2)
452#[doc(alias = "rust-only")]
453pub struct MockProducerOptions<K, V> {
454    /// The cluster holding metadata for this producer. Java's `cluster`; starts
455    /// as [`Cluster::empty`], the value `MockProducer#MockProducer(boolean,Partitioner,Serializer,Serializer)` passes on the caller's behalf.
456    pub(crate) cluster: Cluster,
457    /// If `true`, automatically complete all requests successfully and execute
458    /// the callback. Java's `autoComplete`.
459    pub(crate) auto_complete: bool,
460    /// The partition strategy, or `None`. Java's `partitioner`, which `MockProducer#MockProducer()`
461    /// passes as `null`.
462    pub(crate) partitioner: Option<Box<dyn Partitioner<K, V>>>,
463    /// The key serializer, or `None`. Java's `keySerializer`, which `MockProducer#MockProducer()`
464    /// passes as `null`.
465    pub(crate) key_serializer: Option<Box<dyn DynSerializer<K>>>,
466    /// The value serializer, or `None`. Java's `valueSerializer`, which `MockProducer#MockProducer()`
467    /// passes as `null`.
468    pub(crate) value_serializer: Option<Box<dyn DynSerializer<V>>>,
469}
470
471impl<K, V> std::fmt::Debug for MockProducerOptions<K, V> {
472    // The partitioner and the serializers are trait objects, so only whether
473    // they are set is printed.
474    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
475        f.debug_struct("MockProducerOptions")
476            .field("cluster", &self.cluster)
477            .field("auto_complete", &self.auto_complete)
478            .field("has_partitioner", &self.partitioner.is_some())
479            .field("has_key_serializer", &self.key_serializer.is_some())
480            .field("has_value_serializer", &self.value_serializer.is_some())
481            .finish()
482    }
483}
484
485/// Fluent builder for [`MockProducerOptions`].
486///
487/// [`Self::new`] takes no parameters, every parameter has a fluent setter, and
488/// [`Self::build`] validates the mandatory ones — returning
489/// [`Error::LocalIllegalArgument`] if they were not set. Like
490/// [`MockProducerOptions`] it has no Java counterpart.
491// builds MockProducerOptions (CLAUDE.md §2)
492#[doc(alias = "rust-only")]
493pub struct MockProducerOptionsBuilder<K, V> {
494    cluster: Option<Cluster>,
495    auto_complete: Option<bool>,
496    partitioner: Option<Box<dyn Partitioner<K, V>>>,
497    key_serializer: Option<Box<dyn DynSerializer<K>>>,
498    value_serializer: Option<Box<dyn DynSerializer<V>>>,
499}
500
501impl<K, V> std::fmt::Debug for MockProducerOptionsBuilder<K, V> {
502    // The partitioner and the serializers are trait objects, so only whether
503    // they are set is printed.
504    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
505        f.debug_struct("MockProducerOptionsBuilder")
506            .field("cluster", &self.cluster)
507            .field("auto_complete", &self.auto_complete)
508            .field("has_partitioner", &self.partitioner.is_some())
509            .field("has_key_serializer", &self.key_serializer.is_some())
510            .field("has_value_serializer", &self.value_serializer.is_some())
511            .finish()
512    }
513}
514
515impl<K, V> Default for MockProducerOptionsBuilder<K, V> {
516    fn default() -> Self {
517        Self::new()
518    }
519}
520
521impl<K, V> MockProducerOptionsBuilder<K, V> {
522    /// Creates a builder with every mandatory parameter unset and every other
523    /// parameter at the value Java passes on the caller's behalf.
524    pub fn new() -> Self {
525        Self {
526            cluster: None,
527            auto_complete: None,
528            partitioner: None,
529            key_serializer: None,
530            value_serializer: None,
531        }
532    }
533
534    /// Sets `MockProducerOptions::cluster`.
535    ///
536    /// # Arguments
537    ///
538    /// - `cluster` - the cluster holding metadata for this producer
539    ///
540    /// # Returns
541    ///
542    /// This builder.
543    pub fn set_cluster(mut self, cluster: Cluster) -> Self {
544        self.cluster = Some(cluster);
545        self
546    }
547    /// Sets `MockProducerOptions::auto_complete`, a mandatory parameter:
548    /// [`Self::build`] returns an error if it was not set.
549    ///
550    /// # Arguments
551    ///
552    /// - `auto_complete` - whether to complete every request automatically
553    ///
554    /// # Returns
555    ///
556    /// This builder.
557    pub fn set_auto_complete(mut self, auto_complete: bool) -> Self {
558        self.auto_complete = Some(auto_complete);
559        self
560    }
561    /// Sets `MockProducerOptions::partitioner`.
562    ///
563    /// # Arguments
564    ///
565    /// - `partitioner` - the partition strategy, `None` for Java's `null`
566    ///
567    /// # Returns
568    ///
569    /// This builder.
570    pub fn set_partitioner(mut self, partitioner: Option<Box<dyn Partitioner<K, V>>>) -> Self {
571        self.partitioner = partitioner;
572        self
573    }
574    /// Sets `MockProducerOptions::key_serializer`, any [`Serializer<K>`];
575    /// unset is Java's `null`, which `MockProducer()` passes.
576    ///
577    /// # Arguments
578    ///
579    /// - `key_serializer` - the serializer for key
580    ///
581    /// # Returns
582    ///
583    /// This builder.
584    pub fn set_key_serializer(mut self, key_serializer: impl Serializer<K> + 'static) -> Self {
585        self.key_serializer = Some(Box::new(key_serializer));
586        self
587    }
588    /// Sets `MockProducerOptions::value_serializer`, any [`Serializer<V>`];
589    /// unset is Java's `null`.
590    ///
591    /// # Arguments
592    ///
593    /// - `value_serializer` - the serializer for value
594    ///
595    /// # Returns
596    ///
597    /// This builder.
598    pub fn set_value_serializer(mut self, value_serializer: impl Serializer<V> + 'static) -> Self {
599        self.value_serializer = Some(Box::new(value_serializer));
600        self
601    }
602
603    /// Returns the built options.
604    ///
605    /// The mandatory parameters are validated here rather than being named in
606    /// the constructor, so a later Java version that makes one of them
607    /// optional changes the set this accepts instead of adding a second
608    /// constructor. Today there is one mandatory set: `auto_complete`. `cluster`
609    /// is not in it because Java's `Cluster#Cluster` supplies `Cluster.empty()` itself.
610    ///
611    /// # Errors
612    ///
613    /// - `Error::is_local_illegal_argument_error`, naming the first parameter
614    ///   of that set which was not given a setter call. Only presence is
615    ///   checked here; semantic validation belongs to the method the options
616    ///   are passed to.
617    pub fn build(self) -> Result<MockProducerOptions<K, V>, Error> {
618        Ok(MockProducerOptions {
619            cluster: self.cluster.unwrap_or_else(Cluster::empty),
620            auto_complete: self.auto_complete.ok_or_else(|| Self::missing("auto_complete"))?,
621            partitioner: self.partitioner,
622            key_serializer: self.key_serializer,
623            value_serializer: self.value_serializer,
624        })
625    }
626
627    /// Builds the [`Error::LocalIllegalArgument`] naming a mandatory parameter
628    /// [`Self::build`] found unset.
629    fn missing(parameter: &str) -> Error {
630        Error::local_illegal_argument(format!(
631            "MockProducerOptionsBuilder::build: mandatory parameter `{parameter}` was not set"
632        ))
633    }
634}
635
636impl<K, V> MockProducer<K, V> {
637    // Java's three `MockProducer` constructors (`MockProducer#MockProducer`, `MockProducer`)
638    // have an EMPTY parameter-name intersection, and the no-arg `MockProducer` matches
639    // it — so that one owns the plain name and every sibling is suffixed with
640    // its full Rust parameter list (CLAUDE.md §2). Rust translates `MockProducer#MockProducer()` as
641    // [`Default`] rather than an inherent `new`, which is why no constructor
642    // here is called `new`. The other two take the partitioner and the two
643    // serializers as values, so they DO contribute name tokens: five and four
644    // respectively, past §2's cap of three, which is why both are served by
645    // [`Self::with_options`] and [`MockProducerOptions`] rather than by a name
646    // listing every parameter.
647
648    /// Create a mock producer
649    ///
650    /// # Arguments
651    ///
652    /// - `cluster` - The cluster holding metadata for this producer
653    /// - `auto_complete` - If true automatically complete all requests
654    ///   successfully and execute the callback. Otherwise the user must call
655    ///   [`complete_next`](Self::complete_next) or
656    ///   [`error_next`](Self::error_next) after [`send`](Producer::send) to
657    ///   complete the call and resolve the returned future.
658    ///
659    /// # Translation notes
660    ///
661    /// Java's `MockProducer(Cluster, boolean, Partitioner, Serializer,
662    /// Serializer)` with a `null` partitioner and `null` serializers.
663    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#MockProducer")]
664    pub fn with_cluster_auto_complete(cluster: Cluster, auto_complete: bool) -> Self {
665        Self::with_options(
666            MockProducerOptionsBuilder::new()
667                .set_cluster(cluster)
668                .set_auto_complete(auto_complete)
669                .build()
670                .expect("MockProducerOptionsBuilder::build: every mandatory parameter is set above"),
671        )
672    }
673
674    /// Create a mock producer
675    ///
676    /// # Arguments
677    ///
678    /// - `options` - The cluster holding metadata for this producer (unset:
679    ///   an empty cluster), whether to automatically complete all requests
680    ///   successfully and execute the callback (otherwise the user must call
681    ///   [`complete_next`](Self::complete_next) or
682    ///   [`error_next`](Self::error_next) after [`send`](Producer::send)),
683    ///   the partition strategy, and the serializers for key and value.
684    ///
685    /// # Translation notes
686    ///
687    /// Java's `MockProducer(Cluster, boolean, Partitioner, Serializer,
688    /// Serializer)` and, with `cluster` unset, `MockProducer(boolean,
689    /// Partitioner, Serializer, Serializer)`, which passes `Cluster.empty()`.
690    // `MockProducerOptions` is this method's only parameter because the
691    // derived name would list five parameters, past CLAUDE.md §2's cap of
692    // three.
693    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#MockProducer")]
694    pub fn with_options(options: MockProducerOptions<K, V>) -> Self {
695        let MockProducerOptions { cluster, auto_complete, partitioner, key_serializer, value_serializer } = options;
696        Self {
697            inner: Mutex::new(MockProducerInner {
698                cluster,
699                partitioner,
700                auto_complete,
701                sent: Vec::new(),
702                uncommitted_sends: Vec::new(),
703                completions: VecDeque::new(),
704                offsets: HashMap::new(),
705                consumer_group_offsets: Vec::new(),
706                uncommitted_consumer_group_offsets: HashMap::new(),
707                closed: false,
708                transaction_initialized: false,
709                transaction_in_flight: false,
710                transaction_committed: false,
711                transaction_aborted: false,
712                producer_fenced: false,
713                sent_offsets: false,
714                commit_count: 0,
715                init_transaction_error: None,
716                begin_transaction_error: None,
717                send_offsets_to_transaction_error: None,
718                commit_transaction_error: None,
719                abort_transaction_error: None,
720                send_error: None,
721                flush_error: None,
722                partitions_for_error: None,
723                close_error: None,
724            }),
725            key_serializer,
726            value_serializer,
727            mock_metrics: SharedMetricMap::default(),
728        }
729    }
730
731    /// Create a new mock producer with invented metadata and the given
732    /// `auto_complete` setting.
733    ///
734    /// # Arguments
735    ///
736    /// - `auto_complete` - If true automatically complete all requests
737    ///   successfully and execute the callback. Otherwise the user must call
738    ///   [`complete_next`](Self::complete_next) or
739    ///   [`error_next`](Self::error_next) after [`send`](Producer::send) to
740    ///   complete the call and resolve the returned future.
741    // Java's `MockProducer(boolean, Partitioner, Serializer, Serializer)` with
742    // a `null` partitioner and `null` serializers: equivalent to
743    // `with_cluster_auto_complete(Cluster::empty(), auto_complete)`.
744    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#MockProducer")]
745    pub fn with_auto_complete(auto_complete: bool) -> Self {
746        Self::with_cluster_auto_complete(Cluster::empty(), auto_complete)
747    }
748
749    /// Returns the list of sent records since the last call to
750    /// [`clear`](Self::clear).
751    // A clone of the internal sent list.
752    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#history")]
753    pub fn history(&self) -> Vec<ProducerRecord<K, V>>
754    where
755        K: Clone,
756        V: Clone,
757    {
758        let inner = self.inner.lock().unwrap();
759        inner.sent.clone()
760    }
761
762    /// Gets the list of records sent in the current transaction that have not
763    /// yet been committed.
764    ///
765    /// # Returns
766    ///
767    /// A list of uncommitted producer records
768    // A clone of the internal uncommitted-sends list.
769    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#uncommittedRecords")]
770    pub fn uncommitted_records(&self) -> Vec<ProducerRecord<K, V>>
771    where
772        K: Clone,
773        V: Clone,
774    {
775        let inner = self.inner.lock().unwrap();
776        inner.uncommitted_sends.clone()
777    }
778
779    /// Returns the list of committed consumer group offsets since the last
780    /// call to [`clear`](Self::clear).
781    // One entry per committed transaction that carried offsets.
782    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#consumerGroupOffsetsHistory")]
783    pub fn consumer_group_offsets_history(&self) -> Vec<ConsumerGroupOffsets> {
784        let inner = self.inner.lock().unwrap();
785        inner.consumer_group_offsets.clone()
786    }
787
788    /// Gets the consumer group offsets sent in the current transaction that
789    /// have not yet been committed.
790    ///
791    /// # Returns
792    ///
793    /// A map of consumer group IDs to their uncommitted offsets
794    // Java hands back the live map; behind the mutex that is not expressible,
795    // so this returns a snapshot clone. No Java caller mutates the returned
796    // map.
797    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#uncommittedOffsets")]
798    pub fn uncommitted_offsets(&self) -> ConsumerGroupOffsets {
799        let inner = self.inner.lock().unwrap();
800        inner.uncommitted_consumer_group_offsets.clone()
801    }
802
803    /// Clear the stored history of sent records, consumer group offsets
804    // Per-topic-partition offset counters are intentionally preserved, as
805    // Java's `MockProducer.clear()` does not reset the `offsets` map. Nor does
806    // it reset the transaction flags, only `sentOffsets`.
807    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#clear")]
808    pub fn clear(&self) {
809        let mut inner = self.inner.lock().unwrap();
810        inner.sent.clear();
811        inner.uncommitted_sends.clear();
812        inner.sent_offsets = false;
813        inner.completions.clear();
814        inner.consumer_group_offsets.clear();
815        inner.uncommitted_consumer_group_offsets.clear();
816    }
817
818    /// Complete the earliest uncompleted call successfully.
819    ///
820    /// # Returns
821    ///
822    /// true if there was an uncompleted call to complete
823    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#completeNext")]
824    pub fn complete_next(&self) -> bool {
825        self.complete_one(None)
826    }
827
828    /// Complete the earliest uncompleted call with the given error.
829    ///
830    /// # Arguments
831    ///
832    /// - `error` - the error
833    ///
834    /// # Returns
835    ///
836    /// true if there was an uncompleted call to complete
837    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#errorNext")]
838    pub fn error_next(&self, error: Error) -> bool {
839        self.complete_one(Some(error))
840    }
841
842    // Java's `errorNext(RuntimeException)`, `completeNext()` passing `null`.
843    // Java completes under the producer's monitor, which is reentrant; `inner`'s
844    // `std::sync::Mutex` is not, and completing runs the record's callback and the
845    // future's `then_apply` functions, which may call back into this producer
846    // (`history()`, `send`, `flush`). So the completion is popped under the lock
847    // and completed after the guard is dropped (CLAUDE.md §11.6).
848    fn complete_one(&self, error: Option<Error>) -> bool {
849        let completion = self.inner.lock().unwrap().completions.pop_front();
850        match completion {
851            Some(completion) => {
852                completion.complete(error);
853                true
854            },
855            None => false,
856        }
857    }
858
859    // Java's `flush()`: its checks, then `while (completeNext())`. Each
860    // completion takes the lock on its own (`complete_one`), so a send a callback
861    // makes meanwhile is completed by the same loop, as in Java.
862    fn flush_pending(&self) -> Result<(), Error> {
863        self.inner.lock().unwrap().verify_can_flush()?;
864        while self.complete_one(None) {}
865        Ok(())
866    }
867
868    /// Fences this mock producer, causing it to return a producer-fenced error
869    /// on subsequent transactional operations.
870    ///
871    /// # Errors
872    ///
873    /// - `Error::is_local_illegal_state_error` if closed or not initialized
874    ///   for transactions.
875    /// - `Error::is_producer_fenced_error` if already fenced.
876    // Every subsequent `send()` fails too, as in Java's `MockProducer`.
877    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#fenceProducer")]
878    pub fn fence_producer(&self) -> Result<(), Error> {
879        let mut inner = self.inner.lock().unwrap();
880        inner.verify_not_closed()?;
881        inner.verify_not_fenced()?;
882        inner.verify_transactions_initialized()?;
883        inner.producer_fenced = true;
884        Ok(())
885    }
886
887    /// Returns `true` if [`init_transactions()`](Producer::init_transactions) has
888    /// completed successfully.
889    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#transactionInitialized")]
890    pub fn transaction_initialized(&self) -> bool {
891        let inner = self.inner.lock().unwrap();
892        inner.transaction_initialized
893    }
894
895    /// Returns `true` if a transaction has been begun and neither committed nor
896    /// aborted.
897    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#transactionInFlight")]
898    pub fn transaction_in_flight(&self) -> bool {
899        let inner = self.inner.lock().unwrap();
900        inner.transaction_in_flight
901    }
902
903    /// Returns `true` if the most recent transaction was committed.
904    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#transactionCommitted")]
905    pub fn transaction_committed(&self) -> bool {
906        let inner = self.inner.lock().unwrap();
907        inner.transaction_committed
908    }
909
910    /// Returns `true` if the most recent transaction was aborted.
911    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#transactionAborted")]
912    pub fn transaction_aborted(&self) -> bool {
913        let inner = self.inner.lock().unwrap();
914        inner.transaction_aborted
915    }
916
917    /// Checks whether offsets have been sent to the current transaction.
918    ///
919    /// # Returns
920    ///
921    /// `true` if offsets were sent in the current transaction, `false`
922    /// otherwise
923    // Reset only by `begin_transaction()` and `clear()`, not by a commit.
924    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#sentOffsets")]
925    pub fn sent_offsets(&self) -> bool {
926        let inner = self.inner.lock().unwrap();
927        inner.sent_offsets
928    }
929
930    /// Gets the total number of transactions committed by this mock producer.
931    ///
932    /// # Returns
933    ///
934    /// The commit count
935    // Aborted transactions are not counted.
936    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#commitCount")]
937    pub fn commit_count(&self) -> i64 {
938        let inner = self.inner.lock().unwrap();
939        inner.commit_count
940    }
941
942    /// Returns `true` if the producer is closed.
943    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#closed")]
944    pub fn closed(&self) -> bool {
945        let inner = self.inner.lock().unwrap();
946        inner.closed
947    }
948
949    /// Returns `true` if there are no pending completions.
950    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#flushed")]
951    pub fn flushed(&self) -> bool {
952        let inner = self.inner.lock().unwrap();
953        inner.completions.is_empty()
954    }
955
956    /// Error to return when [`send`](Producer::send) or
957    /// [`send_with_callback`](Producer::send_with_callback) is called
958    ///
959    /// # Arguments
960    ///
961    /// - `error` - `None` clears it
962    // Java's public `MockProducer.sendException` field: the error persists across
963    // calls until set back to `None` (Java's `null`).
964    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#sendException")]
965    pub fn set_send_error(&self, error: Option<Error>) {
966        let mut inner = self.inner.lock().unwrap();
967        inner.send_error = error;
968    }
969
970    /// Error to return when [`flush`](Producer::flush) is called
971    ///
972    /// # Arguments
973    ///
974    /// - `error` - `None` clears it
975    // Java's public `MockProducer.flushException` field: the error persists across
976    // calls until set back to `None` (Java's `null`).
977    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#flushException")]
978    pub fn set_flush_error(&self, error: Option<Error>) {
979        let mut inner = self.inner.lock().unwrap();
980        inner.flush_error = error;
981    }
982
983    /// Error to return when [`partitions_for`](Producer::partitions_for) is
984    /// called
985    ///
986    /// # Arguments
987    ///
988    /// - `error` - `None` clears it
989    // Java's public `MockProducer.partitionsForException` field: the error persists across
990    // calls until set back to `None` (Java's `null`).
991    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#partitionsForException")]
992    pub fn set_partitions_for_error(&self, error: Option<Error>) {
993        let mut inner = self.inner.lock().unwrap();
994        inner.partitions_for_error = error;
995    }
996
997    /// Error to return when [`close`](Producer::close) or
998    /// [`close_with_timeout`](Producer::close_with_timeout) is called
999    ///
1000    /// # Arguments
1001    ///
1002    /// - `error` - `None` clears it
1003    // Java's public `MockProducer.closeException` field: the error persists across
1004    // calls until set back to `None` (Java's `null`).
1005    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#closeException")]
1006    pub fn set_close_error(&self, error: Option<Error>) {
1007        let mut inner = self.inner.lock().unwrap();
1008        inner.close_error = error;
1009    }
1010
1011    /// Set a mock metric for testing
1012    ///
1013    /// # Arguments
1014    ///
1015    /// - `name` - key
1016    /// - `metric` - value
1017    // Java's `setMockMetrics(MetricName name, Metric metric)`; the metric is
1018    // returned by `metrics()`.
1019    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#setMockMetrics")]
1020    pub fn set_mock_metrics(&self, name: MetricName, metric: Arc<KafkaMetric>) {
1021        self.mock_metrics.insert(name, metric);
1022    }
1023
1024    /// Error to return when [`init_transactions`](Producer::init_transactions)
1025    /// is called
1026    ///
1027    /// # Arguments
1028    ///
1029    /// - `error` - `None` clears it
1030    // Java's public `MockProducer.initTransactionException` field: the error persists across
1031    // calls until set back to `None` (Java's `null`).
1032    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#initTransactionException")]
1033    pub fn set_init_transaction_error(&self, error: Option<Error>) {
1034        let mut inner = self.inner.lock().unwrap();
1035        inner.init_transaction_error = error;
1036    }
1037
1038    /// Error to return when [`begin_transaction`](Producer::begin_transaction)
1039    /// is called
1040    ///
1041    /// # Arguments
1042    ///
1043    /// - `error` - `None` clears it
1044    // Java's public `MockProducer.beginTransactionException` field: the error persists across
1045    // calls until set back to `None` (Java's `null`).
1046    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#beginTransactionException")]
1047    pub fn set_begin_transaction_error(&self, error: Option<Error>) {
1048        let mut inner = self.inner.lock().unwrap();
1049        inner.begin_transaction_error = error;
1050    }
1051
1052    /// Error to return when
1053    /// [`send_offsets_to_transaction`](Producer::send_offsets_to_transaction)
1054    /// is called
1055    ///
1056    /// # Arguments
1057    ///
1058    /// - `error` - `None` clears it
1059    // Java's public `MockProducer.sendOffsetsToTransactionException` field: the error persists across
1060    // calls until set back to `None` (Java's `null`).
1061    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#sendOffsetsToTransactionException")]
1062    pub fn set_send_offsets_to_transaction_error(&self, error: Option<Error>) {
1063        let mut inner = self.inner.lock().unwrap();
1064        inner.send_offsets_to_transaction_error = error;
1065    }
1066
1067    /// Error to return when
1068    /// [`commit_transaction`](Producer::commit_transaction) is called
1069    ///
1070    /// # Arguments
1071    ///
1072    /// - `error` - `None` clears it
1073    // Java's public `MockProducer.commitTransactionException` field: the error persists across
1074    // calls until set back to `None` (Java's `null`).
1075    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#commitTransactionException")]
1076    pub fn set_commit_transaction_error(&self, error: Option<Error>) {
1077        let mut inner = self.inner.lock().unwrap();
1078        inner.commit_transaction_error = error;
1079    }
1080
1081    /// Error to return when [`abort_transaction`](Producer::abort_transaction)
1082    /// is called
1083    ///
1084    /// # Arguments
1085    ///
1086    /// - `error` - `None` clears it
1087    // Java's public `MockProducer.abortTransactionException` field: the error persists across
1088    // calls until set back to `None` (Java's `null`).
1089    #[doc(alias = "org.apache.kafka.clients.producer.MockProducer#abortTransactionException")]
1090    pub fn set_abort_transaction_error(&self, error: Option<Error>) {
1091        let mut inner = self.inner.lock().unwrap();
1092        inner.abort_transaction_error = error;
1093    }
1094}
1095
1096impl<K, V> Default for MockProducer<K, V> {
1097    // Create a new mock producer with an empty cluster and `auto_complete=false`.
1098    //
1099    // Corresponds to Java's no-arg `MockProducer()` constructor.
1100    fn default() -> Self {
1101        Self::with_cluster_auto_complete(Cluster::empty(), false)
1102    }
1103}
1104
1105impl<K: Send + Sync, V: Send + Sync> Producer<K, V> for MockProducer<K, V> {
1106    // Initialize this mock for transactions.
1107    //
1108    // Corresponds to Java's `MockProducer.initTransactions()`
1109    // (`MockProducer#initTransactions`). Stays `async` because the trait declares it so
1110    // (Java's `KafkaProducer.initTransactions` blocks); the mock never awaits.
1111    //
1112    // # Errors
1113    //
1114    // Returns `Err` if the producer is closed, is fenced, has already been
1115    // initialized, or an error was installed with
1116    // [`set_init_transaction_error`](MockProducer::set_init_transaction_error).
1117    async fn init_transactions(&self) -> Result<(), Error> {
1118        let mut inner = self.inner.lock().unwrap();
1119        inner.verify_not_closed()?;
1120        inner.verify_not_fenced()?;
1121        if inner.transaction_initialized {
1122            return Err(Error::local_illegal_state(
1123                "MockProducer has already been initialized for transactions.",
1124            ));
1125        }
1126        if let Some(err) = inner.init_transaction_error.as_ref() {
1127            return Err(err.clone());
1128        }
1129        inner.transaction_initialized = true;
1130        inner.transaction_in_flight = false;
1131        inner.transaction_committed = false;
1132        inner.transaction_aborted = false;
1133        inner.sent_offsets = false;
1134        Ok(())
1135    }
1136
1137    // Begin a transaction.
1138    //
1139    // Corresponds to Java's `MockProducer.beginTransaction()`
1140    // (`MockProducer#beginTransaction`).
1141    //
1142    // # Errors
1143    //
1144    // Returns `Err` if the producer is closed, is fenced, was not initialized for
1145    // transactions, a transaction is already in flight, or an error was installed
1146    // with [`set_begin_transaction_error`](MockProducer::set_begin_transaction_error).
1147    fn begin_transaction(&self) -> Result<(), Error> {
1148        let mut inner = self.inner.lock().unwrap();
1149        inner.verify_not_closed()?;
1150        inner.verify_not_fenced()?;
1151        inner.verify_transactions_initialized()?;
1152
1153        if let Some(err) = inner.begin_transaction_error.as_ref() {
1154            return Err(err.clone());
1155        }
1156
1157        if inner.transaction_in_flight {
1158            return Err(Error::local_illegal_state("Transaction already started"));
1159        }
1160
1161        inner.transaction_in_flight = true;
1162        inner.transaction_committed = false;
1163        inner.transaction_aborted = false;
1164        inner.sent_offsets = false;
1165        Ok(())
1166    }
1167
1168    // Stage consumer group offsets as part of the in-flight transaction.
1169    //
1170    // Corresponds to Java's `MockProducer.sendOffsetsToTransaction(Map,
1171    // ConsumerGroupMetadata)` (`MockProducer#sendOffsetsToTransaction`). Java's
1172    // `Objects.requireNonNull(groupMetadata)` (`MockProducer#sendOffsetsToTransaction`) has no counterpart: the
1173    // parameter is a reference and not an `Option`, so a missing metadata is not
1174    // expressible.
1175    //
1176    // An empty `offsets` map is ignored and leaves
1177    // [`sent_offsets()`](MockProducer::sent_offsets) `false` (Java `MockProducer#sendOffsetsToTransaction`).
1178    //
1179    // # Errors
1180    //
1181    // Returns `Err` if the producer is closed, is fenced, was not initialized for
1182    // transactions, has no open transaction, or an error was installed with
1183    // [`set_send_offsets_to_transaction_error`](MockProducer::set_send_offsets_to_transaction_error).
1184    async fn send_offsets_to_transaction(
1185        &self,
1186        offsets: HashMap<TopicPartition, OffsetAndMetadata>,
1187        group_metadata: &dyn ConsumerGroupMetadata,
1188    ) -> Result<(), Error> {
1189        let mut inner = self.inner.lock().unwrap();
1190        inner.verify_not_closed()?;
1191        inner.verify_not_fenced()?;
1192        inner.verify_transactions_initialized()?;
1193        inner.verify_transaction_in_flight()?;
1194
1195        if let Some(err) = inner.send_offsets_to_transaction_error.as_ref() {
1196            return Err(err.clone());
1197        }
1198
1199        if offsets.is_empty() {
1200            return Ok(());
1201        }
1202
1203        // `MockProducer#sendOffsetsToTransaction`: `computeIfAbsent` then `putAll`, so a second call for the
1204        // same group merges into the first, later offsets winning per partition.
1205        let uncommitted_offsets = inner
1206            .uncommitted_consumer_group_offsets
1207            .entry(group_metadata.group_id().to_string())
1208            .or_default();
1209        uncommitted_offsets.extend(offsets);
1210        inner.sent_offsets = true;
1211        Ok(())
1212    }
1213
1214    // Commit the in-flight transaction, publishing its records and offsets.
1215    //
1216    // Corresponds to Java's `MockProducer.commitTransaction()`
1217    // (`MockProducer#commitTransaction`).
1218    //
1219    // # Errors
1220    //
1221    // Returns `Err` if the producer is closed, is fenced, was not initialized for
1222    // transactions, has no open transaction, an error was installed with
1223    // [`set_commit_transaction_error`](MockProducer::set_commit_transaction_error),
1224    // or the `flush()` this performs fails.
1225    async fn commit_transaction(&self) -> Result<(), Error> {
1226        let inner = self.inner.lock().unwrap();
1227        inner.verify_not_closed()?;
1228        inner.verify_not_fenced()?;
1229        inner.verify_transactions_initialized()?;
1230        inner.verify_transaction_in_flight()?;
1231
1232        if let Some(err) = inner.commit_transaction_error.as_ref() {
1233            return Err(err.clone());
1234        }
1235
1236        // The completions `flush()` runs are made without the lock (`complete_one`).
1237        drop(inner);
1238        self.flush_pending()?;
1239        let mut inner = self.inner.lock().unwrap();
1240
1241        // `MockProducer#commitTransaction`: `sent.addAll(uncommittedSends)` then
1242        // `uncommittedSends.clear()`.
1243        let uncommitted_sends = std::mem::take(&mut inner.uncommitted_sends);
1244        inner.sent.extend(uncommitted_sends);
1245
1246        // `MockProducer#commitTransaction`: the map *object* is appended to the history and the
1247        // field is then reassigned to a fresh map — deliberately not `clear()`,
1248        // which would empty the very map just published. `mem::take` is that pair.
1249        let uncommitted_offsets = std::mem::take(&mut inner.uncommitted_consumer_group_offsets);
1250        if !uncommitted_offsets.is_empty() {
1251            inner.consumer_group_offsets.push(uncommitted_offsets);
1252        }
1253
1254        inner.transaction_committed = true;
1255        inner.transaction_aborted = false;
1256        inner.transaction_in_flight = false;
1257
1258        inner.commit_count += 1;
1259        Ok(())
1260    }
1261
1262    // Abort the in-flight transaction, discarding its records and offsets.
1263    //
1264    // Corresponds to Java's `MockProducer.abortTransaction()`
1265    // (`MockProducer#abortTransaction`).
1266    //
1267    // # Errors
1268    //
1269    // Returns `Err` if the producer is closed, is fenced, was not initialized for
1270    // transactions, has no open transaction, an error was installed with
1271    // [`set_abort_transaction_error`](MockProducer::set_abort_transaction_error),
1272    // or the `flush()` this performs fails.
1273    async fn abort_transaction(&self) -> Result<(), Error> {
1274        let inner = self.inner.lock().unwrap();
1275        inner.verify_not_closed()?;
1276        inner.verify_not_fenced()?;
1277        inner.verify_transactions_initialized()?;
1278        inner.verify_transaction_in_flight()?;
1279
1280        if let Some(err) = inner.abort_transaction_error.as_ref() {
1281            return Err(err.clone());
1282        }
1283
1284        // The completions `flush()` runs are made without the lock (`complete_one`).
1285        drop(inner);
1286        self.flush_pending()?;
1287        let mut inner = self.inner.lock().unwrap();
1288        // `MockProducer#abortTransaction`: `clear()` on both — unlike the commit path, neither
1289        // collection has been handed to the history, so there is nothing to detach.
1290        inner.uncommitted_sends.clear();
1291        inner.uncommitted_consumer_group_offsets.clear();
1292        inner.transaction_committed = false;
1293        inner.transaction_aborted = true;
1294        inner.transaction_in_flight = false;
1295        Ok(())
1296    }
1297
1298    async fn send(&self, record: ProducerRecord<K, V>) -> Result<KafkaFuture<RecordMetadata>, Error> {
1299        self.send_with_callback(record, None).await
1300    }
1301
1302    async fn send_with_callback(
1303        &self,
1304        record: ProducerRecord<K, V>,
1305        callback: Option<Box<dyn Callback>>,
1306    ) -> Result<KafkaFuture<RecordMetadata>, Error> {
1307        // Java's `send` is `synchronized` throughout, serializers included. Here
1308        // the serializers are awaited, which must not happen with the `inner`
1309        // guard held (CLAUDE.md §11.6), so the method locks twice: once for the
1310        // checks and the explicit partition, then again after serializing. The
1311        // checks run again under the second lock, since another task may have
1312        // closed or fenced the producer while the serializers ran.
1313        let (has_partitions, explicit_partition) = {
1314            let inner = self.inner.lock().unwrap();
1315            inner.verify_can_send()?;
1316            // `MockProducer#send`: with cluster metadata for the topic, delegate to
1317            // `partition()`; otherwise serialize key/value (with fresh
1318            // `RecordHeaders`) and leave the partition at 0.
1319            let has_partitions = !inner.cluster.partitions_for_topic(record.topic()).is_empty();
1320            let explicit_partition = if has_partitions {
1321                inner.explicit_partition(&record)?
1322            } else {
1323                None
1324            };
1325            (has_partitions, explicit_partition)
1326        };
1327
1328        // `partition()` serializes with the record's headers (`MockProducer#partition`); the
1329        // empty-cluster branch with fresh ones (`MockProducer#send`).
1330        //
1331        // DEVIATION (`definition-of-done.md` §7): in the empty-cluster branch Java
1332        // serializes purely for the `ClassCastException` side effect (the comment in
1333        // `MockProducer#send`) — a case static typing rules out in Rust — and then uses
1334        // partition 0. Rust still consults the serializers when present, so a
1335        // serializer error surfaces as Java's would, but honours an explicit
1336        // `record.partition()` before falling back to 0 so the C FFI
1337        // empty-cluster mock keeps working
1338        // (`c/tests/test_mock_producer.c:133,600,640,735,1637`).
1339        // The serializers may add to the headers they are given (Java's
1340        // `Serializer.serialize(String, Headers, T)`): the record's own ones are
1341        // kept with the record in `history()`, the fresh ones dropped. Java's
1342        // mock does not make them read-only afterwards, unlike `KafkaProducer`.
1343        // The record's headers are moved out while the serializers borrow its
1344        // key and value (a move of the `Vec`, no copy), and moved back once the
1345        // serialized bytes are no longer needed.
1346        let mut record = record;
1347        let mut headers = if has_partitions {
1348            std::mem::take(record.headers_mut())
1349        } else {
1350            RecordHeaders::new()
1351        };
1352        let mut key_bytes = None;
1353        let mut value_bytes = None;
1354        if explicit_partition.is_none() {
1355            if let Some(key_serializer) = self.key_serializer.as_deref() {
1356                key_bytes =
1357                    DynSerializer::serialize_with_headers(key_serializer, record.topic(), &mut headers, record.key())
1358                        .await?;
1359            }
1360            if let Some(value_serializer) = self.value_serializer.as_deref() {
1361                value_bytes = DynSerializer::serialize_with_headers(
1362                    value_serializer,
1363                    record.topic(),
1364                    &mut headers,
1365                    record.value(),
1366                )
1367                .await?;
1368            }
1369        }
1370
1371        let mut inner = self.inner.lock().unwrap();
1372        inner.verify_can_send()?;
1373        let partition = match explicit_partition {
1374            Some(partition) => partition,
1375            None if has_partitions => inner.partition(&record, key_bytes.as_deref(), value_bytes.as_deref()),
1376            None => record.partition().unwrap_or(0),
1377        };
1378        drop((key_bytes, value_bytes));
1379        if has_partitions {
1380            *record.headers_mut() = headers;
1381        }
1382        let tp = TopicPartition::new(record.topic().to_string(), partition);
1383
1384        let result = Arc::new(ProduceRequestResult::new(tp.clone()));
1385        let future = Arc::new(FutureRecordMetadata::new(
1386            Arc::clone(&result),
1387            0,
1388            RecordBatch::NO_TIMESTAMP,
1389            0,
1390            0,
1391            &SystemTime,
1392        ));
1393
1394        let offset = next_offset(&mut inner.offsets, &tp);
1395        let base_offset = 0i64.max(offset - i64::from(i32::MAX));
1396        let batch_index = (offset.min(i64::from(i32::MAX))) as i32;
1397
1398        let metadata = RecordMetadata::new(tp.clone(), base_offset, batch_index, RecordBatch::NO_TIMESTAMP, 0, 0);
1399
1400        // `MockProducer#send`: inside a transaction the record is staged rather than
1401        // published, and only `commitTransaction` moves it across.
1402        if inner.transaction_in_flight {
1403            inner.uncommitted_sends.push(record);
1404        } else {
1405            inner.sent.push(record);
1406        }
1407
1408        let completion = Completion { offset, metadata, result: Arc::clone(&result), callback, topic_partition: tp };
1409
1410        if inner.auto_complete {
1411            // Completed without the lock, as `complete_one` does.
1412            drop(inner);
1413            completion.complete(None);
1414        } else {
1415            inner.completions.push_back(completion);
1416        }
1417
1418        Ok(KafkaFuture::new(future))
1419    }
1420
1421    async fn flush(&self) -> Result<(), Error> {
1422        self.flush_pending()
1423    }
1424
1425    async fn partitions_for(&self, topic: &str) -> Result<Vec<PartitionInfo>, Error> {
1426        let inner = self.inner.lock().unwrap();
1427
1428        if let Some(err) = inner.partitions_for_error.as_ref() {
1429            return Err(err.clone());
1430        }
1431
1432        Ok(inner.cluster.partitions_for_topic(topic).to_vec())
1433    }
1434
1435    // Return the mock metrics. Corresponds to Java's `MockProducer.metrics()`
1436    // returning the `mockMetrics` map.
1437    fn metrics(&self) -> MetricsView {
1438        MetricsView::of(self.mock_metrics.clone())
1439    }
1440
1441    async fn close(&self) -> Result<(), Error> {
1442        let mut inner = self.inner.lock().unwrap();
1443
1444        if let Some(err) = inner.close_error.as_ref() {
1445            return Err(err.clone());
1446        }
1447
1448        inner.closed = true;
1449        Ok(())
1450    }
1451
1452    async fn close_with_timeout(&self, _timeout: Duration) -> Result<(), Error> {
1453        self.close().await
1454    }
1455}
1456
1457/// Get the next offset for this topic/partition.
1458///
1459/// First call for a topic-partition returns 0 and stores 1. Subsequent calls
1460/// increment and return the previous value.
1461///
1462/// Corresponds to Java's `MockProducer.nextOffset(TopicPartition)`.
1463#[doc(alias = "org.apache.kafka.clients.producer.MockProducer#nextOffset")]
1464fn next_offset(offsets: &mut HashMap<TopicPartition, i64>, tp: &TopicPartition) -> i64 {
1465    match offsets.get_mut(tp) {
1466        Some(offset) => {
1467            let current = *offset;
1468            *offset = current + 1;
1469            current
1470        },
1471        None => {
1472            offsets.insert(tp.clone(), 1);
1473            0
1474        },
1475    }
1476}
1477
1478#[cfg(test)]
1479mod tests {
1480    use super::*;
1481    use std::borrow::Cow;
1482
1483    use crate::common::header::Headers;
1484    use crate::common::serialization::Serializer;
1485    use crate::common::serialization::StringSerializer;
1486    use crate::consumer::ConsumerGroupMetadataImpl;
1487    use crate::producer::RoundRobinPartitioner;
1488
1489    // -----------------------------------------------------------------------
1490    // Fixtures, mirroring `MockProducerTest.java`'s fields
1491    // -----------------------------------------------------------------------
1492
1493    /// Java's `MockProducerTest.topic`.
1494    const TOPIC: &str = "topic";
1495
1496    /// The four `RecordMetadata` fields `testMetadataOnException` inspects
1497    /// (`MockProducerTest#testMetadataOnException`): offset, timestamp, serialized key size,
1498    /// serialized value size.
1499    type ObservedMetadata = (i64, i64, i32, i32);
1500
1501    /// Java's `MockProducerTest.groupId`.
1502    const GROUP_ID: &str = "group";
1503
1504    fn make_record(topic: &str, key: &str, value: &str) -> ProducerRecord<String, String> {
1505        ProducerRecord::with_key(topic.to_string(), Some(key.to_string()), Some(value.to_string()))
1506    }
1507
1508    /// Java's `MockProducerTest.record1`. A function
1509    /// rather than a field because the tests hand records to `send` by value.
1510    fn record1() -> ProducerRecord<String, String> {
1511        make_record(TOPIC, "key1", "value1")
1512    }
1513
1514    /// Java's `MockProducerTest.record2`.
1515    fn record2() -> ProducerRecord<String, String> {
1516        make_record(TOPIC, "key2", "value2")
1517    }
1518
1519    /// Java's `MockProducerTest.buildMockProducer(boolean)`
1520    /// (`MockProducerTest#buildMockProducer`), which passes `Cluster.empty()` and the two
1521    /// `MockSerializer`s the Rust mock has no counterpart for (it takes
1522    /// pre-serialized bytes — see [`MockProducer::with_options`]).
1523    fn build_mock_producer(auto_complete: bool) -> MockProducer<String, String> {
1524        MockProducer::with_auto_complete(auto_complete)
1525    }
1526
1527    /// Java's `new ConsumerGroupMetadata(groupId)`. Java has deprecated that
1528    /// constructor since 4.2 and the public Rust API has none, so the tests build
1529    /// the crate-internal implementation.
1530    fn group_metadata(group_id: &str) -> ConsumerGroupMetadataImpl {
1531        ConsumerGroupMetadataImpl::new(group_id)
1532    }
1533
1534    /// Assert `result` failed the way Java's `IllegalStateException` does, with
1535    /// Java's message text (`definition-of-done.md` §3 — the message is part of
1536    /// the contract, so `is_err()` alone is not enough).
1537    fn assert_illegal_state<T>(result: Result<T, Error>, message: &str) {
1538        let error = result.err().expect("expected an IllegalState error, got Ok");
1539        assert!(
1540            matches!(error, Error::LocalIllegalState(_)),
1541            "expected IllegalState, got {error}"
1542        );
1543        assert_eq!(message, error.message());
1544    }
1545
1546    /// Build the `(partition, offset)` map Java spells as an anonymous `HashMap`
1547    /// subclass, e.g. `MockProducerTest`.
1548    ///
1549    /// Java writes `new OffsetAndMetadata(42L, null)`; Rust's `metadata` is a
1550    /// non-nullable `String`, so `OffsetAndMetadata::new(offset)` — metadata `""`,
1551    /// Java's own one-arg default — is the closest analogue. Every expected and
1552    /// actual value below is built through this one helper, so the equality
1553    /// comparisons are the comparisons Java makes.
1554    fn offsets(entries: &[(i32, i64)]) -> HashMap<TopicPartition, OffsetAndMetadata> {
1555        entries
1556            .iter()
1557            .map(|&(partition, offset)| {
1558                (
1559                    TopicPartition::new(TOPIC.to_string(), partition),
1560                    OffsetAndMetadata::new(offset).expect("offset must be non-negative"),
1561                )
1562            })
1563            .collect()
1564    }
1565
1566    /// Assert `result` failed the way Java's `ProducerFencedException` does.
1567    ///
1568    /// `MockProducer` raises it with exactly one message, from `verifyNotFenced`
1569    /// (`MockProducer#verifyNotFenced`) and from the fenced `send`.
1570    fn assert_producer_fenced<T>(result: Result<T, Error>) {
1571        let error = result.err().expect("expected a ProducerFenced error, got Ok");
1572        assert_eq!(Errors::ProducerFenced, error.error(), "expected ProducerFenced, got {error}");
1573        assert_eq!("MockProducer is fenced.", error.message());
1574    }
1575
1576    // -----------------------------------------------------------------------
1577    // Tests translated from MockProducerTest.java
1578    // -----------------------------------------------------------------------
1579
1580    /// Translated from `MockProducerTest.testAutoCompleteMock`.
1581    #[tokio::test]
1582    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#testAutoCompleteMock")]
1583    async fn test_auto_complete_mock() {
1584        let producer = build_mock_producer(true);
1585        let record1 = record1();
1586
1587        let future = producer.send(record1.clone()).await.unwrap();
1588        assert!(future.is_done(), "Send should be immediately complete");
1589
1590        let metadata = future.get().await;
1591        assert!(metadata.is_ok(), "Send should be successful");
1592        let md = metadata.unwrap();
1593        assert_eq!(0, md.offset(), "Offset should be 0");
1594        assert_eq!("topic", md.topic());
1595
1596        assert_eq!(vec![record1], producer.history(), "We should have the record in our history");
1597
1598        producer.clear();
1599        assert_eq!(0, producer.history().len(), "Clear should erase our history");
1600    }
1601
1602    /// Two-partition cluster whose partitions have **no** leader, matching Java's
1603    /// `testPartitioner` fixture (`MockProducerTest#testPartitioner`, `new
1604    /// PartitionInfo(topic, n, null, null, null)`). The Rust
1605    /// `Cluster::with_invalid_topics_controller_topic_ids` takes eight arguments
1606    /// where Java's constructor takes five; the extra three (controller,
1607    /// topic-ids, unauthorized/invalid topics) default to empty.
1608    fn two_partition_cluster() -> Cluster {
1609        let pi0 = PartitionInfo::new(TOPIC.to_string(), 0, None, vec![], vec![]);
1610        let pi1 = PartitionInfo::new(TOPIC.to_string(), 1, None, vec![], vec![]);
1611        Cluster::with_invalid_topics_controller_topic_ids(
1612            None,
1613            vec![],
1614            vec![pi0, pi1],
1615            std::collections::HashSet::new(),
1616            std::collections::HashSet::new(),
1617            std::collections::HashSet::new(),
1618            None,
1619            HashMap::new(),
1620        )
1621    }
1622
1623    /// Translated from `MockProducerTest.testPartitioner`.
1624    ///
1625    /// Drives a `RoundRobinPartitioner` over a two-partition cluster. Because the
1626    /// partitions have no leader, `available_partitions_for_topic` is empty, so the
1627    /// round-robin falls back to the full partition count and the first send lands
1628    /// on partition 0 — the value Java asserts.
1629    #[tokio::test]
1630    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#testPartitioner")]
1631    async fn test_partitioner() {
1632        let producer: MockProducer<String, String> = MockProducer::with_options(
1633            MockProducerOptionsBuilder::new()
1634                .set_cluster(two_partition_cluster())
1635                .set_auto_complete(true)
1636                .set_partitioner(Some(Box::new(RoundRobinPartitioner::new())))
1637                .set_key_serializer(StringSerializer)
1638                .set_value_serializer(StringSerializer)
1639                .build()
1640                .expect("every mandatory parameter is set above"),
1641        );
1642
1643        let record = ProducerRecord::with_key(TOPIC.to_string(), Some("key".to_string()), Some("value".to_string()));
1644        let future = producer.send(record).await.unwrap();
1645        let md = future.get().await.unwrap();
1646        assert_eq!(0, md.partition(), "Partition should be correct");
1647
1648        producer.clear();
1649        assert_eq!(0, producer.history().len(), "Clear should erase our history");
1650        producer.close().await.unwrap();
1651    }
1652
1653    /// A `String` serializer that adds a header named `name`, with the data as
1654    /// its value.
1655    struct HeaderAddingSerializer {
1656        name: &'static str,
1657    }
1658
1659    impl Serializer<String> for HeaderAddingSerializer {
1660        async fn serialize<'a>(
1661            &'a self,
1662            _topic: &'a str,
1663            data: Option<&'a String>,
1664        ) -> Result<Option<Cow<'a, [u8]>>, Error> {
1665            Ok(data.map(|data| Cow::Borrowed(data.as_bytes())))
1666        }
1667
1668        async fn serialize_with_headers<'a, 'h>(
1669            &'a self,
1670            topic: &'a str,
1671            headers: &'h mut RecordHeaders,
1672            data: Option<&'a String>,
1673        ) -> Result<Option<Cow<'a, [u8]>>, Error>
1674        where
1675            'a: 'h,
1676        {
1677            headers
1678                .add_with_key_value(self.name, data.map(|data| data.as_bytes()))
1679                .map_err(Error::LocalIllegalState)?;
1680            Serializer::serialize(self, topic, data).await
1681        }
1682    }
1683
1684    fn header_adding_producer(cluster: Cluster) -> MockProducer<String, String> {
1685        MockProducer::with_options(
1686            MockProducerOptionsBuilder::new()
1687                .set_cluster(cluster)
1688                .set_auto_complete(true)
1689                .set_key_serializer(HeaderAddingSerializer { name: "key-header" })
1690                .set_value_serializer(HeaderAddingSerializer { name: "value-header" })
1691                .build()
1692                .expect("every mandatory parameter is set above"),
1693        )
1694    }
1695
1696    fn header_entries(record: &ProducerRecord<String, String>) -> Vec<(String, Option<Vec<u8>>)> {
1697        use crate::common::header::Header;
1698        record
1699            .headers()
1700            .iter()
1701            .map(|header| (header.key().to_string(), header.value().map(<[u8]>::to_vec)))
1702            .collect()
1703    }
1704
1705    /// Java's mock serializes through `partition()` with the record's own
1706    /// headers (`MockProducer#partition`), so a header a serializer adds is
1707    /// on the record `history()` returns; the mock does not make them
1708    /// read-only, unlike `KafkaProducer`.
1709    #[tokio::test]
1710    async fn test_serializer_added_headers_are_kept_with_the_record() {
1711        let producer = header_adding_producer(two_partition_cluster());
1712        let record = ProducerRecord::with_key(TOPIC.to_string(), Some("key".to_string()), Some("value".to_string()));
1713        producer.send(record).await.unwrap().get().await.unwrap();
1714
1715        let history = producer.history();
1716        assert_eq!(
1717            header_entries(&history[0]),
1718            [
1719                ("key-header".to_string(), Some(b"key".to_vec())),
1720                ("value-header".to_string(), Some(b"value".to_vec())),
1721            ]
1722        );
1723        assert!(!history[0].headers().is_read_only());
1724    }
1725
1726    /// With no partitions for the topic, Java's mock serializes with fresh
1727    /// headers (`MockProducer#send(ProducerRecord,Callback)`), so what a serializer adds there
1728    /// does not reach the record.
1729    #[tokio::test]
1730    async fn test_serializer_added_headers_are_dropped_on_an_empty_cluster() {
1731        let producer = header_adding_producer(Cluster::empty());
1732        let record = ProducerRecord::with_key(TOPIC.to_string(), Some("key".to_string()), Some("value".to_string()));
1733        producer.send(record).await.unwrap().get().await.unwrap();
1734
1735        assert!(header_entries(&producer.history()[0]).is_empty());
1736    }
1737
1738    /// Rust-only: an explicit record partition outside `[0, numPartitions)` is
1739    /// rejected with Java's exact `IllegalArgumentException` message
1740    /// (`MockProducer#partition`). `MockProducerTest.java` has no dedicated
1741    /// test for this branch, so it is local coverage of a translated behaviour.
1742    #[tokio::test]
1743    async fn test_partition_out_of_range_is_rejected() {
1744        let producer: MockProducer<String, String> =
1745            MockProducer::with_cluster_auto_complete(two_partition_cluster(), true);
1746        let record = ProducerRecord::with_partition_key(
1747            TOPIC.to_string(),
1748            Some(2),
1749            Some("key".to_string()),
1750            Some("value".to_string()),
1751        )
1752        .unwrap();
1753
1754        let error = producer
1755            .send(record)
1756            .await
1757            .expect_err("out-of-range partition should be rejected");
1758        assert!(
1759            matches!(error, Error::LocalIllegalArgument(_)),
1760            "expected LocalIllegalArgument, got {error}"
1761        );
1762        assert_eq!(
1763            "Invalid partition given with record: 2 is not in the range [0...2].",
1764            error.message()
1765        );
1766    }
1767
1768    /// Rust-only: an in-range explicit record partition is honoured as-is
1769    /// (`MockProducer#partition`), taking priority over the partitioner.
1770    #[tokio::test]
1771    async fn test_explicit_partition_is_honoured() {
1772        let producer: MockProducer<String, String> = MockProducer::with_options(
1773            MockProducerOptionsBuilder::new()
1774                .set_cluster(two_partition_cluster())
1775                .set_auto_complete(true)
1776                .set_partitioner(Some(Box::new(RoundRobinPartitioner::new())))
1777                .set_key_serializer(StringSerializer)
1778                .set_value_serializer(StringSerializer)
1779                .build()
1780                .expect("every mandatory parameter is set above"),
1781        );
1782        let record = ProducerRecord::with_partition_key(
1783            TOPIC.to_string(),
1784            Some(1),
1785            Some("key".to_string()),
1786            Some("value".to_string()),
1787        )
1788        .unwrap();
1789
1790        let md = producer.send(record).await.unwrap().get().await.unwrap();
1791        assert_eq!(1, md.partition(), "explicit partition wins over the partitioner");
1792    }
1793
1794    /// Rust-only: with no partitioner set, a keyed record on a populated cluster
1795    /// lands on the topic's first partition, translating Java's `partitioner ==
1796    /// null` branch (`MockProducer#partition`).
1797    #[tokio::test]
1798    async fn test_no_partitioner_uses_first_partition() {
1799        let producer: MockProducer<String, String> =
1800            MockProducer::with_cluster_auto_complete(two_partition_cluster(), true);
1801        let record = ProducerRecord::with_key(TOPIC.to_string(), Some("key".to_string()), Some("value".to_string()));
1802
1803        let md = producer.send(record).await.unwrap().get().await.unwrap();
1804        assert_eq!(0, md.partition(), "no-partitioner path uses the first partition");
1805    }
1806
1807    /// Rust-only: on an **empty** cluster the mock keeps honouring an explicit
1808    /// `record.partition()` and otherwise defaults to 0 — the documented deviation
1809    /// from Java's empty-cluster branch (Java forces 0 and serializes only for a
1810    /// `ClassCastException` side effect that static typing rules out). This is the
1811    /// path the C FFI empty-cluster mock relies on.
1812    #[tokio::test]
1813    async fn test_empty_cluster_honours_explicit_partition() {
1814        let producer: MockProducer<String, String> = MockProducer::with_cluster_auto_complete(Cluster::empty(), true);
1815
1816        let keyless = ProducerRecord::with_key(TOPIC.to_string(), None, Some("value".to_string()));
1817        let md = producer.send(keyless).await.unwrap().get().await.unwrap();
1818        assert_eq!(0, md.partition(), "empty cluster with no explicit partition defaults to 0");
1819
1820        let pinned = ProducerRecord::with_partition_key(
1821            TOPIC.to_string(),
1822            Some(7),
1823            Some("key".to_string()),
1824            Some("value".to_string()),
1825        )
1826        .unwrap();
1827        let md = producer.send(pinned).await.unwrap().get().await.unwrap();
1828        assert_eq!(7, md.partition(), "empty cluster still honours an explicit partition");
1829    }
1830
1831    /// Translated from `MockProducerTest.testManualCompletion`.
1832    #[tokio::test]
1833    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#testManualCompletion")]
1834    async fn test_manual_completion() {
1835        let producer = build_mock_producer(false);
1836        let record1 = record1();
1837        let record2 = record2();
1838
1839        let md1 = producer.send(record1.clone()).await.unwrap();
1840        assert!(!md1.is_done(), "Send shouldn't have completed");
1841
1842        let md2 = producer.send(record2.clone()).await.unwrap();
1843        assert!(!md2.is_done(), "Send shouldn't have completed");
1844
1845        assert!(producer.complete_next(), "Complete the first request");
1846        let result1 = md1.get().await;
1847        assert!(result1.is_ok(), "Request should be successful");
1848        assert!(!md2.is_done(), "Second request still incomplete");
1849
1850        assert!(
1851            producer.error_next(Error::local_illegal_argument("blah")),
1852            "Complete the second request with an error"
1853        );
1854        // Java asserts `assertEquals(e, err.getCause())`; `Error` has no cause
1855        // chain, so the message identifies the injected error.
1856        let error = md2.get().await.expect_err("Expected error to be thrown");
1857        assert_eq!("blah", error.message());
1858
1859        assert!(!producer.complete_next(), "No more requests to complete");
1860
1861        // Test flush completes remaining sends
1862        let md3 = producer.send(record1).await.unwrap();
1863        let md4 = producer.send(record2).await.unwrap();
1864        assert!(!md3.is_done() && !md4.is_done(), "Requests should not be completed.");
1865        producer.flush().await.unwrap();
1866        assert!(md3.is_done() && md4.is_done(), "Requests should be completed.");
1867    }
1868
1869    /// Translated from `MockProducerTest.shouldThrowOnSendIfProducerIsClosed`.
1870    #[tokio::test]
1871    async fn should_throw_on_send_if_producer_is_closed() {
1872        let producer = build_mock_producer(true);
1873        producer.close().await.unwrap();
1874        assert_illegal_state(producer.send(record1()).await, "MockProducer is already closed.");
1875    }
1876
1877    /// Translated from `MockProducerTest.shouldThrowOnFlushProducerIfProducerIsClosed`.
1878    #[tokio::test]
1879    async fn should_throw_on_flush_if_producer_is_closed() {
1880        let producer = build_mock_producer(true);
1881        producer.close().await.unwrap();
1882        assert_illegal_state(producer.flush().await, "MockProducer is already closed.");
1883    }
1884
1885    /// Translated from `MockProducerTest.shouldBeFlushedIfNoBufferedRecords`.
1886    #[test]
1887    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldBeFlushedIfNoBufferedRecords")]
1888    fn should_be_flushed_if_no_buffered_records() {
1889        let producer = build_mock_producer(true);
1890        assert!(producer.flushed());
1891    }
1892
1893    /// Translated from `MockProducerTest.shouldBeFlushedWithAutoCompleteIfBufferedRecords`.
1894    #[tokio::test]
1895    #[doc(
1896        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldBeFlushedWithAutoCompleteIfBufferedRecords"
1897    )]
1898    async fn should_be_flushed_with_auto_complete_if_buffered_records() {
1899        let producer = build_mock_producer(true);
1900        producer.send(make_record("topic", "key1", "value1")).await.unwrap();
1901        assert!(producer.flushed());
1902    }
1903
1904    /// Translated from `MockProducerTest.shouldNotBeFlushedWithNoAutoCompleteIfBufferedRecords`.
1905    #[tokio::test]
1906    #[doc(
1907        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldNotBeFlushedWithNoAutoCompleteIfBufferedRecords"
1908    )]
1909    async fn should_not_be_flushed_with_no_auto_complete_if_buffered_records() {
1910        let producer = build_mock_producer(false);
1911        producer.send(make_record("topic", "key1", "value1")).await.unwrap();
1912        assert!(!producer.flushed());
1913    }
1914
1915    /// Translated from `MockProducerTest.shouldNotBeFlushedAfterFlush`.
1916    ///
1917    /// Note: the Java name is misleading — the body asserts that after flush
1918    /// `flushed()` returns `true`, not `false`. The name is kept as Java spells it
1919    /// (the same call this phase makes for
1920    /// `shouldThrowOnAbortForNonAutoCompleteIfTransactionsAreEnabled`, whose name
1921    /// is misleading in the same way) and the assertion follows the body.
1922    #[tokio::test]
1923    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldNotBeFlushedAfterFlush")]
1924    async fn should_not_be_flushed_after_flush() {
1925        let producer = build_mock_producer(false);
1926        producer.send(record1()).await.unwrap();
1927        producer.flush().await.unwrap();
1928        assert!(producer.flushed());
1929    }
1930
1931    /// Translated from `MockProducerTest.testMetadataOnException`.
1932    ///
1933    /// Java asserts on the metadata handed to the send callback, inline in the
1934    /// callback body. The four values are captured here and asserted
1935    /// *after* `error_next` returns instead, because an assertion that only ever
1936    /// runs inside the callback cannot fail when the callback **never runs** — Java
1937    /// has that hole too, and its `assertNotNull(md)` in the callback does not close it. Capturing
1938    /// outside makes "never fired" a failure, so this form is strictly stronger.
1939    ///
1940    /// A panic *is* propagated on this path — `error_next` → `Completion::complete`
1941    /// → `cb(..)` is synchronous with no `catch_unwind`, and the closure's own
1942    /// `expect` below relies on that. (Java's producer does swallow callback
1943    /// exceptions, but in `ProducerBatch.completeFutureAndFireCallbacks`
1944    /// (`ProducerBatch#completeFutureAndFireCallbacks`), not in `MockProducer.Completion.complete`,
1945    /// which has no try/catch.)
1946    #[tokio::test]
1947    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#testMetadataOnException")]
1948    async fn test_metadata_on_error() {
1949        let producer = build_mock_producer(false);
1950
1951        let observed: Arc<Mutex<Option<ObservedMetadata>>> = Arc::new(Mutex::new(None));
1952        let sink = Arc::clone(&observed);
1953        let callback: Box<dyn Callback> = Box::new(move |metadata: Option<&RecordMetadata>, _error: Option<&Error>| {
1954            let metadata = metadata.expect("the callback must receive metadata on the error path");
1955            *sink.lock().unwrap() = Some((
1956                metadata.offset(),
1957                metadata.timestamp(),
1958                metadata.serialized_key_size(),
1959                metadata.serialized_value_size(),
1960            ));
1961        });
1962
1963        let future = producer.send_with_callback(record2(), Some(callback)).await.unwrap();
1964        let e = Error::local_illegal_argument("dummy error");
1965        assert!(producer.error_next(e), "Complete the second request with an error");
1966
1967        let (offset, timestamp, key_size, value_size) = observed.lock().unwrap().expect("the callback did not fire");
1968        assert_eq!(-1, offset, "Invalid offset");
1969        assert_eq!(RecordBatch::NO_TIMESTAMP, timestamp, "Invalid timestamp");
1970        assert_eq!(-1, key_size, "Invalid Serialized Key size");
1971        assert_eq!(-1, value_size, "Invalid Serialized value size");
1972
1973        // Java asserts the injected exception is the future's cause; `Error`
1974        // has no cause chain, so the message identifies it.
1975        let result = future.get().await;
1976        let error = result.expect_err("Something went wrong, expected an error");
1977        assert_eq!("dummy error", error.message());
1978    }
1979
1980    /// A callback and a `then_apply` function that call back into the mock,
1981    /// as Java's reentrant monitor allows, complete instead of deadlocking:
1982    /// from `complete_next`, `flush`, `commit_transaction` and an
1983    /// auto-completing `send`. Rust-only: Java's mock cannot deadlock there.
1984    #[tokio::test]
1985    async fn test_completion_calls_back_into_producer() {
1986        /// A callback that reads `history()` and sends a record of its own.
1987        fn reentrant_callback(
1988            producer: &Arc<MockProducer<String, String>>,
1989            seen: &Arc<Mutex<Vec<usize>>>,
1990        ) -> Box<dyn Callback> {
1991            let producer = Arc::clone(producer);
1992            let seen = Arc::clone(seen);
1993            Box::new(move |_metadata: Option<&RecordMetadata>, _error: Option<&Error>| {
1994                seen.lock().unwrap().push(producer.history().len());
1995                // Without serializers the mock's `send` never waits, so one poll completes it.
1996                let send = futures_util::FutureExt::now_or_never(producer.send(record2()));
1997                drop(
1998                    send.expect("the mock's send completes at once")
1999                        .expect("a send from the callback"),
2000                );
2001            })
2002        }
2003
2004        let seen = Arc::new(Mutex::new(Vec::new()));
2005        let producer = Arc::new(build_mock_producer(false));
2006        let future = producer
2007            .send_with_callback(record1(), Some(reentrant_callback(&producer, &seen)))
2008            .await
2009            .unwrap();
2010        let reader = Arc::clone(&producer);
2011        let derived = future.then_apply(move |_metadata: RecordMetadata| Ok(reader.history().len()));
2012        assert!(producer.complete_next());
2013        // The function runs after the callback, which sent a second record.
2014        assert_eq!(2, derived.get().await.unwrap());
2015        // The callback's own send is pending; `flush` completes it.
2016        producer.flush().await.unwrap();
2017        assert!(producer.flushed());
2018
2019        let transactional = Arc::new(build_mock_producer(false));
2020        transactional.init_transactions().await.unwrap();
2021        transactional.begin_transaction().unwrap();
2022        drop(
2023            transactional
2024                .send_with_callback(record1(), Some(reentrant_callback(&transactional, &seen)))
2025                .await
2026                .unwrap(),
2027        );
2028        transactional.commit_transaction().await.unwrap();
2029        // The callback ran inside the commit's flush, before the records were committed, as in
2030        // Java, and the record it sent was completed by the same flush.
2031        assert!(transactional.flushed());
2032        assert_eq!(2, transactional.history().len());
2033
2034        let auto = Arc::new(build_mock_producer(true));
2035        drop(
2036            auto.send_with_callback(record1(), Some(reentrant_callback(&auto, &seen)))
2037                .await
2038                .unwrap(),
2039        );
2040        assert_eq!(2, auto.history().len());
2041
2042        assert_eq!(vec![1, 0, 1], *seen.lock().unwrap());
2043    }
2044
2045    // -----------------------------------------------------------------------
2046    // Transaction lifecycle: init / begin / commit / abort state and counts
2047    // -----------------------------------------------------------------------
2048
2049    /// Translated from `MockProducerTest.shouldInitTransactions`.
2050    #[tokio::test]
2051    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldInitTransactions")]
2052    async fn should_init_transactions() {
2053        let producer = build_mock_producer(true);
2054        producer.init_transactions().await.unwrap();
2055        assert!(producer.transaction_initialized());
2056    }
2057
2058    /// Translated from
2059    /// `MockProducerTest.shouldThrowOnInitTransactionIfProducerAlreadyInitializedForTransactions`.
2060    #[tokio::test]
2061    async fn should_throw_on_init_transaction_if_producer_already_initialized_for_transactions() {
2062        let producer = build_mock_producer(true);
2063        producer.init_transactions().await.unwrap();
2064        assert_illegal_state(
2065            producer.init_transactions().await,
2066            "MockProducer has already been initialized for transactions.",
2067        );
2068    }
2069
2070    /// Translated from
2071    /// `MockProducerTest.shouldThrowOnBeginTransactionIfTransactionsNotInitialized`.
2072    #[test]
2073    fn should_throw_on_begin_transaction_if_transactions_not_initialized() {
2074        let producer = build_mock_producer(true);
2075        assert_illegal_state(
2076            producer.begin_transaction(),
2077            "MockProducer hasn't been initialized for transactions.",
2078        );
2079    }
2080
2081    /// Translated from `MockProducerTest.shouldBeginTransactions`.
2082    #[tokio::test]
2083    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldBeginTransactions")]
2084    async fn should_begin_transactions() {
2085        let producer = build_mock_producer(true);
2086        producer.init_transactions().await.unwrap();
2087        producer.begin_transaction().unwrap();
2088        assert!(producer.transaction_in_flight());
2089    }
2090
2091    /// Translated from
2092    /// `MockProducerTest.shouldThrowOnBeginTransactionsIfTransactionInflight`.
2093    #[tokio::test]
2094    async fn should_throw_on_begin_transactions_if_transaction_inflight() {
2095        let producer = build_mock_producer(true);
2096        producer.init_transactions().await.unwrap();
2097        producer.begin_transaction().unwrap();
2098        assert_illegal_state(producer.begin_transaction(), "Transaction already started");
2099    }
2100
2101    /// Translated from
2102    /// `MockProducerTest.shouldThrowOnSendOffsetsToTransactionIfTransactionsNotInitialized`.
2103    ///
2104    /// Java passes `null` for the offsets map, relying on the guard firing before
2105    /// `offsets.isEmpty()` is reached (`MockProducer#sendOffsetsToTransaction`). A `HashMap`
2106    /// parameter is not nullable, so the empty map stands in — and it tests the
2107    /// same ordering: were the guard to move below the emptiness check, Java would
2108    /// throw `NullPointerException` and this assertion would see `Ok`.
2109    #[tokio::test]
2110    async fn should_throw_on_send_offsets_to_transaction_if_transactions_not_initialized() {
2111        let producer = build_mock_producer(true);
2112        assert_illegal_state(
2113            producer
2114                .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
2115                .await,
2116            "MockProducer hasn't been initialized for transactions.",
2117        );
2118    }
2119
2120    /// Translated from
2121    /// `MockProducerTest.shouldThrowOnSendOffsetsToTransactionTransactionIfNoTransactionGotStarted`. Java's `null` offsets map becomes the empty map, as in
2122    /// `should_throw_on_send_offsets_to_transaction_if_transactions_not_initialized`.
2123    #[tokio::test]
2124    async fn should_throw_on_send_offsets_to_transaction_transaction_if_no_transaction_got_started() {
2125        let producer = build_mock_producer(true);
2126        producer.init_transactions().await.unwrap();
2127        assert_illegal_state(
2128            producer
2129                .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
2130                .await,
2131            "There is no open transaction.",
2132        );
2133    }
2134
2135    /// Translated from `MockProducerTest.shouldThrowOnCommitIfTransactionsNotInitialized`.
2136    #[tokio::test]
2137    async fn should_throw_on_commit_if_transactions_not_initialized() {
2138        let producer = build_mock_producer(true);
2139        assert_illegal_state(
2140            producer.commit_transaction().await,
2141            "MockProducer hasn't been initialized for transactions.",
2142        );
2143    }
2144
2145    /// Translated from
2146    /// `MockProducerTest.shouldThrowOnCommitTransactionIfNoTransactionGotStarted`.
2147    #[tokio::test]
2148    async fn should_throw_on_commit_transaction_if_no_transaction_got_started() {
2149        let producer = build_mock_producer(true);
2150        producer.init_transactions().await.unwrap();
2151        assert_illegal_state(producer.commit_transaction().await, "There is no open transaction.");
2152    }
2153
2154    /// Translated from `MockProducerTest.shouldCommitEmptyTransaction`.
2155    #[tokio::test]
2156    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldCommitEmptyTransaction")]
2157    async fn should_commit_empty_transaction() {
2158        let producer = build_mock_producer(true);
2159        producer.init_transactions().await.unwrap();
2160        producer.begin_transaction().unwrap();
2161        producer.commit_transaction().await.unwrap();
2162        assert!(!producer.transaction_in_flight());
2163        assert!(producer.transaction_committed());
2164        assert!(!producer.transaction_aborted());
2165    }
2166
2167    /// Translated from `MockProducerTest.shouldCountCommittedTransaction`.
2168    #[tokio::test]
2169    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldCountCommittedTransaction")]
2170    async fn should_count_committed_transaction() {
2171        let producer = build_mock_producer(true);
2172        producer.init_transactions().await.unwrap();
2173        producer.begin_transaction().unwrap();
2174
2175        assert_eq!(0, producer.commit_count());
2176        producer.commit_transaction().await.unwrap();
2177        assert_eq!(1, producer.commit_count());
2178    }
2179
2180    /// Translated from `MockProducerTest.shouldNotCountAbortedTransaction`.
2181    #[tokio::test]
2182    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldNotCountAbortedTransaction")]
2183    async fn should_not_count_aborted_transaction() {
2184        let producer = build_mock_producer(true);
2185        producer.init_transactions().await.unwrap();
2186
2187        producer.begin_transaction().unwrap();
2188        producer.abort_transaction().await.unwrap();
2189
2190        producer.begin_transaction().unwrap();
2191        producer.commit_transaction().await.unwrap();
2192        assert_eq!(1, producer.commit_count());
2193    }
2194
2195    /// Translated from `MockProducerTest.shouldThrowOnAbortIfTransactionsNotInitialized`.
2196    #[tokio::test]
2197    async fn should_throw_on_abort_if_transactions_not_initialized() {
2198        let producer = build_mock_producer(true);
2199        assert_illegal_state(
2200            producer.abort_transaction().await,
2201            "MockProducer hasn't been initialized for transactions.",
2202        );
2203    }
2204
2205    /// Translated from
2206    /// `MockProducerTest.shouldThrowOnAbortTransactionIfNoTransactionGotStarted`.
2207    #[tokio::test]
2208    async fn should_throw_on_abort_transaction_if_no_transaction_got_started() {
2209        let producer = build_mock_producer(true);
2210        producer.init_transactions().await.unwrap();
2211        assert_illegal_state(producer.abort_transaction().await, "There is no open transaction.");
2212    }
2213
2214    /// Translated from `MockProducerTest.shouldAbortEmptyTransaction`.
2215    #[tokio::test]
2216    #[doc(alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldAbortEmptyTransaction")]
2217    async fn should_abort_empty_transaction() {
2218        let producer = build_mock_producer(true);
2219        producer.init_transactions().await.unwrap();
2220        producer.begin_transaction().unwrap();
2221        producer.abort_transaction().await.unwrap();
2222        assert!(!producer.transaction_in_flight());
2223        assert!(producer.transaction_aborted());
2224        assert!(!producer.transaction_committed());
2225    }
2226
2227    // -----------------------------------------------------------------------
2228    // Fencing: every entry point after `fenceProducer()`
2229    // -----------------------------------------------------------------------
2230
2231    /// Translated from
2232    /// `MockProducerTest.shouldThrowFenceProducerIfTransactionsNotInitialized`.
2233    #[test]
2234    fn should_throw_fence_producer_if_transactions_not_initialized() {
2235        let producer = build_mock_producer(true);
2236        assert_illegal_state(
2237            producer.fence_producer(),
2238            "MockProducer hasn't been initialized for transactions.",
2239        );
2240    }
2241
2242    /// Translated from
2243    /// `MockProducerTest.shouldThrowOnBeginTransactionsIfProducerGotFenced`.
2244    #[tokio::test]
2245    async fn should_throw_on_begin_transactions_if_producer_got_fenced() {
2246        let producer = build_mock_producer(true);
2247        producer.init_transactions().await.unwrap();
2248        producer.fence_producer().unwrap();
2249        assert_producer_fenced(producer.begin_transaction());
2250    }
2251
2252    /// Translated from `MockProducerTest.shouldThrowOnSendIfProducerGotFenced`.
2253    ///
2254    /// Java calls `producer.send(null)`; the fenced check (`MockProducer#send`)
2255    /// precedes any use of the record, and `ProducerRecord` is taken by value here,
2256    /// so a real record makes the same point.
2257    ///
2258    /// Java throws a bare `KafkaException` *wrapping* a `ProducerFencedException`
2259    /// and asserts on the cause, so both halves are checked here: the outer error is
2260    /// a bare `KafkaError` (`is_api_error() == false`) carrying the wrapper message,
2261    /// and its `source()` is the `ProducerFenced` error.
2262    #[tokio::test]
2263    async fn should_throw_on_send_if_producer_got_fenced() {
2264        let producer = build_mock_producer(true);
2265        producer.init_transactions().await.unwrap();
2266        producer.fence_producer().unwrap();
2267        let error = producer.send(record1()).await.expect_err("expected a fenced error, got Ok");
2268
2269        // `assertThrows(KafkaException.class, ..)` — a BARE `KafkaException`, not the
2270        // `ProducerFencedException` that `verify_not_fenced` raises.
2271        assert!(
2272            matches!(error, Error::KafkaError(_)),
2273            "expected a bare KafkaError, got {error:?}"
2274        );
2275        assert!(error.is_kafka_error(), "Java throws KafkaException here");
2276        assert!(!error.is_api_error(), "a bare KafkaException is not an ApiException");
2277        assert_eq!("MockProducer is fenced.", error.message());
2278
2279        // `assertInstanceOf(ProducerFencedException.class, e.getCause())`.
2280        let cause = crate::common::error::ErrorSource::source(&error)
2281            .expect("Java chains a ProducerFencedException as the cause");
2282        assert_eq!(Errors::ProducerFenced, cause.error(), "expected ProducerFenced, got {cause}");
2283        assert_eq!("Fenced", cause.message());
2284    }
2285
2286    /// Translated from
2287    /// `MockProducerTest.shouldThrowOnSendOffsetsToTransactionByGroupIdIfProducerGotFenced`.
2288    ///
2289    /// This test and
2290    /// `shouldThrowOnSendOffsetsToTransactionByGroupMetadataIfProducerGotFenced` have identical bodies: the group-id overload of
2291    /// `sendOffsetsToTransaction` was removed, so both now call the
2292    /// `ConsumerGroupMetadata` one. Both are translated anyway —
2293    /// `definition-of-done.md` §3 does not allow skipping a Java test because a
2294    /// sibling duplicates it.
2295    #[tokio::test]
2296    async fn should_throw_on_send_offsets_to_transaction_by_group_id_if_producer_got_fenced() {
2297        let producer = build_mock_producer(true);
2298        producer.init_transactions().await.unwrap();
2299        producer.fence_producer().unwrap();
2300        assert_producer_fenced(
2301            producer
2302                .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
2303                .await,
2304        );
2305    }
2306
2307    /// Translated from
2308    /// `MockProducerTest.shouldThrowOnSendOffsetsToTransactionByGroupMetadataIfProducerGotFenced`
2309    /// — the identical twin of
2310    /// `shouldThrowOnSendOffsetsToTransactionByGroupIdIfProducerGotFenced`, see the note there.
2311    #[tokio::test]
2312    async fn should_throw_on_send_offsets_to_transaction_by_group_metadata_if_producer_got_fenced() {
2313        let producer = build_mock_producer(true);
2314        producer.init_transactions().await.unwrap();
2315        producer.fence_producer().unwrap();
2316        assert_producer_fenced(
2317            producer
2318                .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
2319                .await,
2320        );
2321    }
2322
2323    /// Translated from
2324    /// `MockProducerTest.shouldThrowOnCommitTransactionIfProducerGotFenced`.
2325    #[tokio::test]
2326    async fn should_throw_on_commit_transaction_if_producer_got_fenced() {
2327        let producer = build_mock_producer(true);
2328        producer.init_transactions().await.unwrap();
2329        producer.fence_producer().unwrap();
2330        assert_producer_fenced(producer.commit_transaction().await);
2331    }
2332
2333    /// Translated from
2334    /// `MockProducerTest.shouldThrowOnAbortTransactionIfProducerGotFenced`.
2335    #[tokio::test]
2336    async fn should_throw_on_abort_transaction_if_producer_got_fenced() {
2337        let producer = build_mock_producer(true);
2338        producer.init_transactions().await.unwrap();
2339        producer.fence_producer().unwrap();
2340        assert_producer_fenced(producer.abort_transaction().await);
2341    }
2342
2343    /// Translated from
2344    /// `MockProducerTest.shouldNotThrowOnFlushProducerIfProducerIsFenced`.
2345    ///
2346    /// The one entry point fencing does *not* close: Java's `flush()`
2347    /// (`MockProducer#flush`) omits `verifyNotFenced()`.
2348    #[tokio::test]
2349    async fn should_not_throw_on_flush_producer_if_producer_is_fenced() {
2350        let producer = build_mock_producer(true);
2351        producer.init_transactions().await.unwrap();
2352        producer.fence_producer().unwrap();
2353        producer.flush().await.expect("flush must not fail on a fenced producer");
2354    }
2355
2356    // -----------------------------------------------------------------------
2357    // Closed producer: every transactional entry point after `close()`
2358    // -----------------------------------------------------------------------
2359
2360    /// Translated from
2361    /// `MockProducerTest.shouldThrowOnInitTransactionIfProducerIsClosed`.
2362    #[tokio::test]
2363    async fn should_throw_on_init_transaction_if_producer_is_closed() {
2364        let producer = build_mock_producer(true);
2365        producer.close().await.unwrap();
2366        assert_illegal_state(producer.init_transactions().await, "MockProducer is already closed.");
2367    }
2368
2369    /// Translated from
2370    /// `MockProducerTest.shouldThrowOnBeginTransactionIfProducerIsClosed`.
2371    #[tokio::test]
2372    async fn should_throw_on_begin_transaction_if_producer_is_closed() {
2373        let producer = build_mock_producer(true);
2374        producer.close().await.unwrap();
2375        assert_illegal_state(producer.begin_transaction(), "MockProducer is already closed.");
2376    }
2377
2378    /// Translated from
2379    /// `MockProducerTest.shouldThrowSendOffsetsToTransactionByGroupIdIfProducerIsClosed`
2380    /// This test and
2381    /// `shouldThrowSendOffsetsToTransactionByGroupMetadataIfProducerIsClosed` have identical bodies — see the note on
2382    /// `should_throw_on_send_offsets_to_transaction_by_group_id_if_producer_got_fenced`.
2383    #[tokio::test]
2384    async fn should_throw_send_offsets_to_transaction_by_group_id_if_producer_is_closed() {
2385        let producer = build_mock_producer(true);
2386        producer.close().await.unwrap();
2387        assert_illegal_state(
2388            producer
2389                .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
2390                .await,
2391            "MockProducer is already closed.",
2392        );
2393    }
2394
2395    /// Translated from
2396    /// `MockProducerTest.shouldThrowSendOffsetsToTransactionByGroupMetadataIfProducerIsClosed`
2397    /// — the identical twin of
2398    /// `shouldThrowSendOffsetsToTransactionByGroupIdIfProducerIsClosed`.
2399    #[tokio::test]
2400    async fn should_throw_send_offsets_to_transaction_by_group_metadata_if_producer_is_closed() {
2401        let producer = build_mock_producer(true);
2402        producer.close().await.unwrap();
2403        assert_illegal_state(
2404            producer
2405                .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
2406                .await,
2407            "MockProducer is already closed.",
2408        );
2409    }
2410
2411    /// Translated from
2412    /// `MockProducerTest.shouldThrowOnCommitTransactionIfProducerIsClosed`.
2413    #[tokio::test]
2414    async fn should_throw_on_commit_transaction_if_producer_is_closed() {
2415        let producer = build_mock_producer(true);
2416        producer.close().await.unwrap();
2417        assert_illegal_state(producer.commit_transaction().await, "MockProducer is already closed.");
2418    }
2419
2420    /// Translated from
2421    /// `MockProducerTest.shouldThrowOnAbortTransactionIfProducerIsClosed`.
2422    #[tokio::test]
2423    async fn should_throw_on_abort_transaction_if_producer_is_closed() {
2424        let producer = build_mock_producer(true);
2425        producer.close().await.unwrap();
2426        assert_illegal_state(producer.abort_transaction().await, "MockProducer is already closed.");
2427    }
2428
2429    /// Translated from
2430    /// `MockProducerTest.shouldThrowOnFenceProducerIfProducerIsClosed`.
2431    #[tokio::test]
2432    async fn should_throw_on_fence_producer_if_producer_is_closed() {
2433        let producer = build_mock_producer(true);
2434        producer.close().await.unwrap();
2435        assert_illegal_state(producer.fence_producer(), "MockProducer is already closed.");
2436    }
2437
2438    // -----------------------------------------------------------------------
2439    // Record staging: published on commit, dropped on abort
2440    // -----------------------------------------------------------------------
2441
2442    /// Translated from
2443    /// `MockProducerTest.shouldPublishMessagesOnlyAfterCommitIfTransactionsAreEnabled`.
2444    #[tokio::test]
2445    #[doc(
2446        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldPublishMessagesOnlyAfterCommitIfTransactionsAreEnabled"
2447    )]
2448    async fn should_publish_messages_only_after_commit_if_transactions_are_enabled() {
2449        let producer = build_mock_producer(true);
2450        producer.init_transactions().await.unwrap();
2451        producer.begin_transaction().unwrap();
2452
2453        producer.send(record1()).await.unwrap();
2454        producer.send(record2()).await.unwrap();
2455
2456        assert!(producer.history().is_empty());
2457
2458        producer.commit_transaction().await.unwrap();
2459
2460        assert_eq!(vec![record1(), record2()], producer.history());
2461    }
2462
2463    /// Translated from
2464    /// `MockProducerTest.shouldFlushOnCommitForNonAutoCompleteIfTransactionsAreEnabled`.
2465    #[tokio::test]
2466    #[doc(
2467        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldFlushOnCommitForNonAutoCompleteIfTransactionsAreEnabled"
2468    )]
2469    async fn should_flush_on_commit_for_non_auto_complete_if_transactions_are_enabled() {
2470        let producer = build_mock_producer(false);
2471        producer.init_transactions().await.unwrap();
2472        producer.begin_transaction().unwrap();
2473
2474        let md1 = producer.send(record1()).await.unwrap();
2475        let md2 = producer.send(record2()).await.unwrap();
2476
2477        assert!(!md1.is_done());
2478        assert!(!md2.is_done());
2479
2480        producer.commit_transaction().await.unwrap();
2481
2482        assert!(md1.is_done());
2483        assert!(md2.is_done());
2484    }
2485
2486    /// Translated from
2487    /// `MockProducerTest.shouldDropMessagesOnAbortIfTransactionsAreEnabled`.
2488    #[tokio::test]
2489    #[doc(
2490        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldDropMessagesOnAbortIfTransactionsAreEnabled"
2491    )]
2492    async fn should_drop_messages_on_abort_if_transactions_are_enabled() {
2493        let producer = build_mock_producer(true);
2494        producer.init_transactions().await.unwrap();
2495
2496        producer.begin_transaction().unwrap();
2497        producer.send(record1()).await.unwrap();
2498        producer.send(record2()).await.unwrap();
2499        producer.abort_transaction().await.unwrap();
2500        assert!(producer.history().is_empty());
2501
2502        producer.begin_transaction().unwrap();
2503        producer.commit_transaction().await.unwrap();
2504        assert!(producer.history().is_empty());
2505    }
2506
2507    /// Translated from
2508    /// `MockProducerTest.shouldThrowOnAbortForNonAutoCompleteIfTransactionsAreEnabled`.
2509    ///
2510    /// The Java name says "shouldThrow"; the body asserts the opposite — the abort
2511    /// succeeds and flushes the pending send. The name is kept as Java spells it
2512    /// (as with `shouldNotBeFlushedAfterFlush`) and the assertions follow the body.
2513    #[tokio::test]
2514    async fn should_throw_on_abort_for_non_auto_complete_if_transactions_are_enabled() {
2515        let producer = build_mock_producer(false);
2516        producer.init_transactions().await.unwrap();
2517        producer.begin_transaction().unwrap();
2518
2519        let md1 = producer.send(record1()).await.unwrap();
2520        assert!(!md1.is_done());
2521
2522        producer.abort_transaction().await.unwrap();
2523        assert!(md1.is_done());
2524    }
2525
2526    /// Translated from
2527    /// `MockProducerTest.shouldPreserveCommittedMessagesOnAbortIfTransactionsAreEnabled`.
2528    #[tokio::test]
2529    #[doc(
2530        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldPreserveCommittedMessagesOnAbortIfTransactionsAreEnabled"
2531    )]
2532    async fn should_preserve_committed_messages_on_abort_if_transactions_are_enabled() {
2533        let producer = build_mock_producer(true);
2534        producer.init_transactions().await.unwrap();
2535
2536        producer.begin_transaction().unwrap();
2537        producer.send(record1()).await.unwrap();
2538        producer.send(record2()).await.unwrap();
2539        producer.commit_transaction().await.unwrap();
2540
2541        producer.begin_transaction().unwrap();
2542        producer.abort_transaction().await.unwrap();
2543
2544        assert_eq!(vec![record1(), record2()], producer.history());
2545    }
2546
2547    // -----------------------------------------------------------------------
2548    // Consumer group offset staging
2549    // -----------------------------------------------------------------------
2550
2551    /// Translated from
2552    /// `MockProducerTest.shouldPublishConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled`.
2553    #[tokio::test]
2554    #[doc(
2555        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldPublishConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled"
2556    )]
2557    async fn should_publish_consumer_group_offsets_only_after_commit_if_transactions_are_enabled() {
2558        let producer = build_mock_producer(true);
2559        producer.init_transactions().await.unwrap();
2560        producer.begin_transaction().unwrap();
2561
2562        let group1 = "g1";
2563        let group1_commit = offsets(&[(0, 42), (1, 73)]);
2564        let group2 = "g2";
2565        let group2_commit = offsets(&[(0, 101), (1, 21)]);
2566        producer
2567            .send_offsets_to_transaction(group1_commit.clone(), &group_metadata(group1))
2568            .await
2569            .unwrap();
2570        producer
2571            .send_offsets_to_transaction(group2_commit.clone(), &group_metadata(group2))
2572            .await
2573            .unwrap();
2574
2575        assert!(producer.consumer_group_offsets_history().is_empty());
2576
2577        let expected: ConsumerGroupOffsets =
2578            HashMap::from([(group1.to_string(), group1_commit), (group2.to_string(), group2_commit)]);
2579
2580        producer.commit_transaction().await.unwrap();
2581        assert_eq!(vec![expected], producer.consumer_group_offsets_history());
2582    }
2583
2584    /// Translated from
2585    /// `MockProducerTest.shouldIgnoreEmptyOffsetsWhenSendOffsetsToTransactionByGroupMetadata`.
2586    #[tokio::test]
2587    #[doc(
2588        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldIgnoreEmptyOffsetsWhenSendOffsetsToTransactionByGroupMetadata"
2589    )]
2590    async fn should_ignore_empty_offsets_when_send_offsets_to_transaction_by_group_metadata() {
2591        let producer = build_mock_producer(true);
2592        producer.init_transactions().await.unwrap();
2593        producer.begin_transaction().unwrap();
2594        producer
2595            .send_offsets_to_transaction(HashMap::new(), &group_metadata("groupId"))
2596            .await
2597            .unwrap();
2598        assert!(!producer.sent_offsets());
2599    }
2600
2601    /// Translated from
2602    /// `MockProducerTest.shouldAddOffsetsWhenSendOffsetsToTransactionByGroupMetadata`.
2603    #[tokio::test]
2604    #[doc(
2605        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldAddOffsetsWhenSendOffsetsToTransactionByGroupMetadata"
2606    )]
2607    async fn should_add_offsets_when_send_offsets_to_transaction_by_group_metadata() {
2608        let producer = build_mock_producer(true);
2609        producer.init_transactions().await.unwrap();
2610        producer.begin_transaction().unwrap();
2611
2612        assert!(!producer.sent_offsets());
2613
2614        let group_commit = offsets(&[(0, 42)]);
2615        producer
2616            .send_offsets_to_transaction(group_commit, &group_metadata("groupId"))
2617            .await
2618            .unwrap();
2619        assert!(producer.sent_offsets());
2620    }
2621
2622    /// Translated from
2623    /// `MockProducerTest.shouldResetSentOffsetsFlagOnlyWhenBeginningNewTransaction`.
2624    #[tokio::test]
2625    #[doc(
2626        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldResetSentOffsetsFlagOnlyWhenBeginningNewTransaction"
2627    )]
2628    async fn should_reset_sent_offsets_flag_only_when_beginning_new_transaction() {
2629        let producer = build_mock_producer(true);
2630        producer.init_transactions().await.unwrap();
2631        producer.begin_transaction().unwrap();
2632
2633        assert!(!producer.sent_offsets());
2634
2635        let group_commit = offsets(&[(0, 42)]);
2636        producer
2637            .send_offsets_to_transaction(group_commit.clone(), &group_metadata("groupId"))
2638            .await
2639            .unwrap();
2640        producer.commit_transaction().await.unwrap(); // commit should not reset "sentOffsets"
2641        assert!(producer.sent_offsets());
2642
2643        producer.begin_transaction().unwrap();
2644        assert!(!producer.sent_offsets());
2645
2646        producer
2647            .send_offsets_to_transaction(group_commit, &group_metadata("groupId"))
2648            .await
2649            .unwrap();
2650        producer.commit_transaction().await.unwrap(); // commit should not reset "sentOffsets"
2651        assert!(producer.sent_offsets());
2652
2653        producer.begin_transaction().unwrap();
2654        assert!(!producer.sent_offsets());
2655    }
2656
2657    /// Translated from
2658    /// `MockProducerTest.shouldPublishLatestAndCumulativeConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled`.
2659    ///
2660    /// The two calls for the same group merge, and partition 1's offset moves from
2661    /// 73 to 101 — Java's `putAll` semantics (`MockProducer#sendOffsetsToTransaction`), spelled as
2662    /// `HashMap::extend`.
2663    #[tokio::test]
2664    #[doc(
2665        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldPublishLatestAndCumulativeConsumerGroupOffsetsOnlyAfterCommitIfTransactionsAreEnabled"
2666    )]
2667    async fn should_publish_latest_and_cumulative_consumer_group_offsets_only_after_commit_if_transactions_are_enabled()
2668    {
2669        let producer = build_mock_producer(true);
2670        producer.init_transactions().await.unwrap();
2671        producer.begin_transaction().unwrap();
2672
2673        let group = "g";
2674        let group_commit1 = offsets(&[(0, 42), (1, 73)]);
2675        let group_commit2 = offsets(&[(1, 101), (2, 21)]);
2676        producer
2677            .send_offsets_to_transaction(group_commit1, &group_metadata(group))
2678            .await
2679            .unwrap();
2680        producer
2681            .send_offsets_to_transaction(group_commit2, &group_metadata(group))
2682            .await
2683            .unwrap();
2684
2685        assert!(producer.consumer_group_offsets_history().is_empty());
2686
2687        let expected: ConsumerGroupOffsets =
2688            HashMap::from([(group.to_string(), offsets(&[(0, 42), (1, 101), (2, 21)]))]);
2689
2690        producer.commit_transaction().await.unwrap();
2691        assert_eq!(vec![expected], producer.consumer_group_offsets_history());
2692    }
2693
2694    /// Translated from
2695    /// `MockProducerTest.shouldDropConsumerGroupOffsetsOnAbortIfTransactionsAreEnabled`.
2696    #[tokio::test]
2697    #[doc(
2698        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldDropConsumerGroupOffsetsOnAbortIfTransactionsAreEnabled"
2699    )]
2700    async fn should_drop_consumer_group_offsets_on_abort_if_transactions_are_enabled() {
2701        let producer = build_mock_producer(true);
2702        producer.init_transactions().await.unwrap();
2703        producer.begin_transaction().unwrap();
2704
2705        let group = "g";
2706        let group_commit = offsets(&[(0, 42), (1, 73)]);
2707        producer
2708            .send_offsets_to_transaction(group_commit.clone(), &group_metadata(group))
2709            .await
2710            .unwrap();
2711        producer.abort_transaction().await.unwrap();
2712
2713        producer.begin_transaction().unwrap();
2714        producer.commit_transaction().await.unwrap();
2715        assert!(producer.consumer_group_offsets_history().is_empty());
2716
2717        producer.begin_transaction().unwrap();
2718        producer
2719            .send_offsets_to_transaction(group_commit, &group_metadata(group))
2720            .await
2721            .unwrap();
2722        producer.abort_transaction().await.unwrap();
2723
2724        producer.begin_transaction().unwrap();
2725        producer.commit_transaction().await.unwrap();
2726        assert!(producer.consumer_group_offsets_history().is_empty());
2727    }
2728
2729    /// Translated from
2730    /// `MockProducerTest.shouldPreserveOffsetsFromCommitByGroupIdOnAbortIfTransactionsAreEnabled`.
2731    #[tokio::test]
2732    #[doc(
2733        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldPreserveOffsetsFromCommitByGroupIdOnAbortIfTransactionsAreEnabled"
2734    )]
2735    async fn should_preserve_offsets_from_commit_by_group_id_on_abort_if_transactions_are_enabled() {
2736        let producer = build_mock_producer(true);
2737        producer.init_transactions().await.unwrap();
2738        producer.begin_transaction().unwrap();
2739
2740        let group = "g";
2741        let group_commit = offsets(&[(0, 42), (1, 73)]);
2742        producer
2743            .send_offsets_to_transaction(group_commit.clone(), &group_metadata(group))
2744            .await
2745            .unwrap();
2746        producer.commit_transaction().await.unwrap();
2747
2748        producer.begin_transaction().unwrap();
2749        producer.abort_transaction().await.unwrap();
2750
2751        let expected: ConsumerGroupOffsets = HashMap::from([(group.to_string(), group_commit)]);
2752
2753        assert_eq!(vec![expected], producer.consumer_group_offsets_history());
2754    }
2755
2756    /// Translated from
2757    /// `MockProducerTest.shouldPreserveOffsetsFromCommitByGroupMetadataOnAbortIfTransactionsAreEnabled`.
2758    ///
2759    /// The load-bearing case for the `mem::take` in `commit_transaction`: the map
2760    /// published by the first commit must survive the second transaction's abort,
2761    /// which `clear()`s the (now separate) staging map.
2762    #[tokio::test]
2763    #[doc(
2764        alias = "org.apache.kafka.clients.producer.MockProducerTest#shouldPreserveOffsetsFromCommitByGroupMetadataOnAbortIfTransactionsAreEnabled"
2765    )]
2766    async fn should_preserve_offsets_from_commit_by_group_metadata_on_abort_if_transactions_are_enabled() {
2767        let producer = build_mock_producer(true);
2768        producer.init_transactions().await.unwrap();
2769        producer.begin_transaction().unwrap();
2770
2771        let group = "g";
2772        let group_commit = offsets(&[(0, 42), (1, 73)]);
2773        producer
2774            .send_offsets_to_transaction(group_commit.clone(), &group_metadata(group))
2775            .await
2776            .unwrap();
2777        producer.commit_transaction().await.unwrap();
2778
2779        producer.begin_transaction().unwrap();
2780
2781        let group2 = "g2";
2782        let group_commit2 = offsets(&[(2, 53), (3, 84)]);
2783        producer
2784            .send_offsets_to_transaction(group_commit2, &group_metadata(group2))
2785            .await
2786            .unwrap();
2787        producer.abort_transaction().await.unwrap();
2788
2789        let expected: ConsumerGroupOffsets = HashMap::from([(group.to_string(), group_commit)]);
2790
2791        assert_eq!(vec![expected], producer.consumer_group_offsets_history());
2792    }
2793
2794    // -----------------------------------------------------------------------
2795    // Additional unit tests
2796    // -----------------------------------------------------------------------
2797
2798    /// Tests that `Default` creates a producer with `auto_complete=false`.
2799    #[tokio::test]
2800    async fn test_default() {
2801        let producer: MockProducer<String, String> = MockProducer::default();
2802        assert!(!producer.closed());
2803        assert!(producer.flushed());
2804
2805        // auto_complete=false means sends don't complete immediately
2806        let future = producer.send(make_record("topic", "k", "v")).await.unwrap();
2807        assert!(!future.is_done());
2808    }
2809
2810    /// Tests that multiple sends to the same topic-partition get incrementing
2811    /// offsets.
2812    #[tokio::test]
2813    async fn test_incrementing_offsets() {
2814        let producer = build_mock_producer(true);
2815        let f0 = producer.send(make_record("t", "k", "v0")).await.unwrap();
2816        let f1 = producer.send(make_record("t", "k", "v1")).await.unwrap();
2817        let f2 = producer.send(make_record("t", "k", "v2")).await.unwrap();
2818
2819        assert_eq!(0, f0.get().await.unwrap().offset());
2820        assert_eq!(1, f1.get().await.unwrap().offset());
2821        assert_eq!(2, f2.get().await.unwrap().offset());
2822    }
2823
2824    /// Tests that sends to different topic-partitions have independent offsets.
2825    #[tokio::test]
2826    async fn test_independent_topic_partition_offsets() {
2827        let producer = build_mock_producer(true);
2828        let r1 = ProducerRecord::new("t1".to_string(), Some("k".to_string()));
2829        let r2 = ProducerRecord::new("t2".to_string(), Some("k".to_string()));
2830
2831        let f1 = producer.send(r1.clone()).await.unwrap();
2832        let f2 = producer.send(r2.clone()).await.unwrap();
2833        let f3 = producer.send(r1).await.unwrap();
2834
2835        assert_eq!(0, f1.get().await.unwrap().offset());
2836        assert_eq!(0, f2.get().await.unwrap().offset());
2837        assert_eq!(1, f3.get().await.unwrap().offset());
2838    }
2839
2840    /// Tests `set_send_error` injection persists until cleared.
2841    ///
2842    /// Matches Java behavior: `sendException` is a field that persists until
2843    /// manually set to `null`.
2844    #[tokio::test]
2845    async fn test_set_send_error() {
2846        let producer = build_mock_producer(true);
2847        producer.set_send_error(Some(Error::new(Errors::CorruptMessage)));
2848
2849        let result = producer.send(make_record("t", "k", "v")).await;
2850        assert!(result.is_err());
2851
2852        // Error persists — second send also fails
2853        let result = producer.send(make_record("t", "k", "v")).await;
2854        assert!(result.is_err(), "Error should persist until cleared");
2855
2856        // Clear the error — next send succeeds
2857        producer.set_send_error(None);
2858        let result = producer.send(make_record("t", "k", "v")).await;
2859        assert!(result.is_ok(), "Send should succeed after clearing error");
2860    }
2861
2862    /// Tests `set_flush_error` injection persists until cleared.
2863    ///
2864    /// Matches Java behavior: `flushException` is a field that persists until
2865    /// manually set to `null`.
2866    #[tokio::test]
2867    async fn test_set_flush_error() {
2868        let producer = build_mock_producer(true);
2869        producer.set_flush_error(Some(Error::new(Errors::CorruptMessage)));
2870
2871        let result = producer.flush().await;
2872        assert!(result.is_err());
2873
2874        // Error persists — second flush also fails
2875        let result = producer.flush().await;
2876        assert!(result.is_err(), "Error should persist until cleared");
2877
2878        // Clear the error — next flush succeeds
2879        producer.set_flush_error(None);
2880        let result = producer.flush().await;
2881        assert!(result.is_ok(), "Flush should succeed after clearing error");
2882    }
2883
2884    /// Tests `set_partitions_for_error` injection persists until cleared.
2885    ///
2886    /// Matches Java behavior: `partitionsForException` is a field that persists
2887    /// until manually set to `null`.
2888    #[tokio::test]
2889    async fn test_set_partitions_for_error() {
2890        let producer = build_mock_producer(true);
2891        producer.set_partitions_for_error(Some(Error::new(Errors::UnknownTopicOrPartition)));
2892
2893        let result = producer.partitions_for("t").await;
2894        assert!(result.is_err());
2895
2896        // Error persists — second call also fails
2897        let result = producer.partitions_for("t").await;
2898        assert!(result.is_err(), "Error should persist until cleared");
2899
2900        // Clear the error — next call succeeds
2901        producer.set_partitions_for_error(None);
2902        let result = producer.partitions_for("t").await;
2903        assert!(result.is_ok());
2904    }
2905
2906    /// Tests `set_close_error` injection persists until cleared.
2907    ///
2908    /// Matches Java behavior: `closeException` is a field that persists until
2909    /// manually set to `null`.
2910    #[tokio::test]
2911    async fn test_set_close_error() {
2912        let producer = build_mock_producer(true);
2913        producer.set_close_error(Some(Error::new(Errors::UnknownServerError)));
2914
2915        let result = producer.close().await;
2916        assert!(result.is_err());
2917
2918        // Error persists — second close also fails
2919        let result = producer.close().await;
2920        assert!(result.is_err(), "Error should persist until cleared");
2921        assert!(!producer.closed(), "Producer should not be closed when error persists");
2922
2923        // Clear the error — close now succeeds
2924        producer.set_close_error(None);
2925        let result = producer.close().await;
2926        assert!(result.is_ok(), "Close should succeed after clearing error");
2927        assert!(producer.closed());
2928    }
2929
2930    /// Tests `partitions_for` with cluster metadata.
2931    #[tokio::test]
2932    async fn test_partitions_for() {
2933        let node = crate::common::Node::new(0, "localhost".to_string(), 9092);
2934        let pi0 = PartitionInfo::new("topic".to_string(), 0, Some(node.clone()), vec![], vec![]);
2935        let pi1 = PartitionInfo::new("topic".to_string(), 1, Some(node), vec![], vec![]);
2936
2937        let cluster = Cluster::with_invalid_topics_controller_topic_ids(
2938            None,
2939            vec![],
2940            vec![pi0, pi1],
2941            std::collections::HashSet::new(),
2942            std::collections::HashSet::new(),
2943            std::collections::HashSet::new(),
2944            None,
2945            HashMap::new(),
2946        );
2947        let producer: MockProducer<String, String> = MockProducer::with_cluster_auto_complete(cluster, true);
2948
2949        let partitions = producer.partitions_for("topic").await.unwrap();
2950        assert_eq!(2, partitions.len());
2951        assert_eq!(0, partitions[0].partition());
2952        assert_eq!(1, partitions[1].partition());
2953
2954        // Unknown topic returns empty
2955        let partitions = producer.partitions_for("unknown").await.unwrap();
2956        assert!(partitions.is_empty());
2957    }
2958
2959    /// Tests `close_with_timeout` behaves like `close`.
2960    #[tokio::test]
2961    async fn test_close_timeout() {
2962        let producer = build_mock_producer(true);
2963        assert!(!producer.closed());
2964        producer.close_with_timeout(Duration::from_secs(5)).await.unwrap();
2965        assert!(producer.closed());
2966    }
2967
2968    /// Tests that `clear` preserves offset counters (matching Java behavior).
2969    ///
2970    /// Java's `MockProducer.clear()` does NOT reset the `offsets` map, so
2971    /// offset numbering continues after `clear()`.
2972    #[tokio::test]
2973    async fn test_clear_preserves_offsets() {
2974        let producer = build_mock_producer(true);
2975        producer.send(make_record("t", "k", "v")).await.unwrap();
2976        producer.send(make_record("t", "k", "v")).await.unwrap();
2977
2978        producer.clear();
2979
2980        let future = producer.send(make_record("t", "k", "v")).await.unwrap();
2981        assert_eq!(
2982            2,
2983            future.get().await.unwrap().offset(),
2984            "Offset should continue from 2 after clear (not restart from 0)"
2985        );
2986    }
2987
2988    /// Tests `next_offset` helper function.
2989    #[test]
2990    fn test_next_offset() {
2991        let mut offsets = HashMap::new();
2992        let tp = TopicPartition::new("t".to_string(), 0);
2993
2994        assert_eq!(0, next_offset(&mut offsets, &tp));
2995        assert_eq!(1, next_offset(&mut offsets, &tp));
2996        assert_eq!(2, next_offset(&mut offsets, &tp));
2997
2998        let tp2 = TopicPartition::new("t".to_string(), 1);
2999        assert_eq!(0, next_offset(&mut offsets, &tp2));
3000    }
3001
3002    /// Tests that `history` returns a clone (modifications don't affect internal state).
3003    #[tokio::test]
3004    async fn test_history_returns_clone() {
3005        let producer = build_mock_producer(true);
3006        let record = make_record("t", "k", "v");
3007        producer.send(record).await.unwrap();
3008
3009        let mut history = producer.history();
3010        assert_eq!(1, history.len());
3011
3012        // Modifying returned history does not affect internal state
3013        history.clear();
3014        assert_eq!(1, producer.history().len());
3015    }
3016
3017    /// Tests that `error_next` returns false when no completions are pending.
3018    #[test]
3019    fn test_error_next_no_pending() {
3020        let producer = build_mock_producer(false);
3021        assert!(!producer.error_next(Error::new(Errors::UnknownServerError)));
3022    }
3023
3024    /// `metrics()` returns the mock metrics seeded via `set_mock_metrics`,
3025    /// mirroring Java `MockProducer.setMockMetrics` + `metrics()`. Java
3026    /// `MockProducerTest` has no metrics test; this covers the Rust surface.
3027    #[test]
3028    fn test_set_and_get_mock_metrics() {
3029        use crate::common::metrics::Metrics;
3030        use crate::common::metrics::stats::CumulativeSum;
3031
3032        let producer: MockProducer<String, String> = MockProducer::default();
3033        assert!(producer.metrics().is_empty());
3034
3035        // Build a real KafkaMetric via a Metrics registry.
3036        let registry = Metrics::new();
3037        let sensor = registry.sensor("mock-sensor").unwrap();
3038        let name = registry.metric_name("mock-metric", "mock-group");
3039        sensor
3040            .add_with_metric_name(name.clone(), Box::new(CumulativeSum::new()))
3041            .unwrap();
3042        let metric = registry.metric(&name).unwrap();
3043
3044        let view = producer.metrics();
3045        producer.set_mock_metrics(name.clone(), Arc::clone(&metric));
3046
3047        // The view returned before `set_mock_metrics` sees the metric, as
3048        // Java's `metrics()` returns the `mockMetrics` field itself.
3049        assert_eq!(view.len(), 1);
3050        assert!(view.contains_key(&name));
3051        assert!(Arc::ptr_eq(&metric, &view.get(&name).unwrap()));
3052        assert_eq!(producer.metrics().len(), 1);
3053    }
3054
3055    /// Tests `uncommitted_records` and `uncommitted_offsets`, the two staging
3056    /// accessors `MockProducerTest` never calls — `uncommittedRecords`
3057    /// (`MockProducer#uncommittedRecords`) and `uncommittedOffsets` appear zero
3058    /// times in `MockProducerTest.java`; their Java callers are Kafka Streams
3059    /// tests, out of scope. Covered here so the accessors are not untested public
3060    /// API.
3061    #[tokio::test]
3062    async fn test_uncommitted_accessors() {
3063        let producer = build_mock_producer(true);
3064        producer.init_transactions().await.unwrap();
3065        producer.begin_transaction().unwrap();
3066
3067        assert!(producer.uncommitted_records().is_empty());
3068        assert!(producer.uncommitted_offsets().is_empty());
3069
3070        producer.send(record1()).await.unwrap();
3071        let group_commit = offsets(&[(0, 42)]);
3072        producer
3073            .send_offsets_to_transaction(group_commit.clone(), &group_metadata(GROUP_ID))
3074            .await
3075            .unwrap();
3076
3077        assert_eq!(vec![record1()], producer.uncommitted_records());
3078        assert_eq!(
3079            HashMap::from([(GROUP_ID.to_string(), group_commit)]) as ConsumerGroupOffsets,
3080            producer.uncommitted_offsets()
3081        );
3082
3083        // Both are drained by the commit that publishes them.
3084        producer.commit_transaction().await.unwrap();
3085        assert!(producer.uncommitted_records().is_empty());
3086        assert!(producer.uncommitted_offsets().is_empty());
3087    }
3088
3089    /// Tests that `clear` empties the four collections and the flag Java's
3090    /// `clear()` resets beyond `sent` (`MockProducer#clear`), and that it
3091    /// leaves the transaction flags alone — Java resets `sentOffsets` there but
3092    /// not `transactionInitialized` / `transactionInFlight`.
3093    #[tokio::test]
3094    async fn test_clear_resets_staging_but_not_transaction_flags() {
3095        let producer = build_mock_producer(false);
3096        producer.init_transactions().await.unwrap();
3097        producer.begin_transaction().unwrap();
3098        producer.send(record1()).await.unwrap();
3099        producer
3100            .send_offsets_to_transaction(offsets(&[(0, 42)]), &group_metadata(GROUP_ID))
3101            .await
3102            .unwrap();
3103        producer.commit_transaction().await.unwrap();
3104
3105        producer.begin_transaction().unwrap();
3106        producer.send(record2()).await.unwrap();
3107        producer
3108            .send_offsets_to_transaction(offsets(&[(1, 73)]), &group_metadata(GROUP_ID))
3109            .await
3110            .unwrap();
3111
3112        assert!(!producer.history().is_empty());
3113        assert!(!producer.uncommitted_records().is_empty());
3114        assert!(!producer.consumer_group_offsets_history().is_empty());
3115        assert!(!producer.uncommitted_offsets().is_empty());
3116        assert!(producer.sent_offsets());
3117        assert!(!producer.flushed());
3118
3119        producer.clear();
3120
3121        assert!(producer.history().is_empty());
3122        assert!(producer.uncommitted_records().is_empty());
3123        assert!(producer.consumer_group_offsets_history().is_empty());
3124        assert!(producer.uncommitted_offsets().is_empty());
3125        assert!(!producer.sent_offsets());
3126        assert!(producer.flushed());
3127
3128        // Untouched by `clear()`, so the open transaction can still be committed.
3129        assert!(producer.transaction_initialized());
3130        assert!(producer.transaction_in_flight());
3131        assert_eq!(1, producer.commit_count());
3132        producer.commit_transaction().await.unwrap();
3133    }
3134
3135    /// Tests the five transactional error knobs, Java's public
3136    /// `initTransactionException` … `abortTransactionException`
3137    /// (`MockProducer#initTransactionException`). Like `uncommittedRecords`, no
3138    /// `MockProducerTest` method touches them; the four non-transactional knobs
3139    /// above are covered the same way.
3140    ///
3141    /// Each is checked at Java's position in its method: after the `verify*`
3142    /// guards, so it does not mask them, and — for `beginTransaction` — before the
3143    /// in-flight check (`MockProducer#beginTransaction`).
3144    #[tokio::test]
3145    async fn test_set_transactional_errors() {
3146        let injected = || Error::new(Errors::CoordinatorNotAvailable);
3147
3148        // `auto_complete = false` so a pending send stays pending: that is what
3149        // shows the commit / abort errors firing *ahead* of Java's `flush()`
3150        // (`MockProducer#commitTransaction`, `MockProducer#abortTransaction`).
3151        let producer = build_mock_producer(false);
3152
3153        // init: installed before the first `init_transactions`, so it fires there.
3154        producer.set_init_transaction_error(Some(injected()));
3155        assert_eq!(
3156            Errors::CoordinatorNotAvailable,
3157            producer.init_transactions().await.unwrap_err().error()
3158        );
3159        // The failed init left the producer uninitialized.
3160        assert!(!producer.transaction_initialized());
3161        producer.set_init_transaction_error(None);
3162        producer.init_transactions().await.unwrap();
3163
3164        // begin: fires ahead of the "Transaction already started" check.
3165        producer.set_begin_transaction_error(Some(injected()));
3166        assert_eq!(
3167            Errors::CoordinatorNotAvailable,
3168            producer.begin_transaction().unwrap_err().error()
3169        );
3170        assert!(!producer.transaction_in_flight());
3171        producer.set_begin_transaction_error(None);
3172        producer.begin_transaction().unwrap();
3173
3174        // send_offsets: fires ahead of the empty-map short circuit, so even an
3175        // empty map surfaces it.
3176        producer.set_send_offsets_to_transaction_error(Some(injected()));
3177        let error = producer
3178            .send_offsets_to_transaction(HashMap::new(), &group_metadata(GROUP_ID))
3179            .await
3180            .unwrap_err();
3181        assert_eq!(Errors::CoordinatorNotAvailable, error.error());
3182        producer.set_send_offsets_to_transaction_error(None);
3183
3184        // commit: fires ahead of the flush, so the pending send stays pending.
3185        producer.send(record1()).await.unwrap();
3186        producer.set_commit_transaction_error(Some(injected()));
3187        assert_eq!(
3188            Errors::CoordinatorNotAvailable,
3189            producer.commit_transaction().await.unwrap_err().error()
3190        );
3191        assert!(!producer.flushed(), "a failed commit must not have flushed");
3192        assert_eq!(0, producer.commit_count());
3193        producer.set_commit_transaction_error(None);
3194
3195        // abort: same, and the transaction is still in flight afterwards.
3196        producer.set_abort_transaction_error(Some(injected()));
3197        assert_eq!(
3198            Errors::CoordinatorNotAvailable,
3199            producer.abort_transaction().await.unwrap_err().error()
3200        );
3201        assert!(!producer.flushed(), "a failed abort must not have flushed");
3202        assert!(producer.transaction_in_flight());
3203        producer.set_abort_transaction_error(None);
3204
3205        producer.commit_transaction().await.unwrap();
3206        assert_eq!(1, producer.commit_count());
3207        assert_eq!(vec![record1()], producer.history());
3208    }
3209
3210    // =====================================================================
3211    // PHASE-7 TEST ACCOUNTING (`definition-of-done.md` §3)
3212    //
3213    // `MockProducerTest.java` declares **55** `@Test` methods. Every one is
3214    // placed in exactly one group: **53** TRANSLATED, **2** NOT APPLICABLE. The
3215    // split is derived, not asserted, and the derivation is below with its real
3216    // output. Java citations are the **declaration** line, never the `@Test`
3217    // line (Critic 46 filed the inverse).
3218    //
3219    // Two extraction guards, each for a failure that has already happened
3220    // somewhere in this milestone:
3221    //
3222    //   1. The `@Test`-to-declaration walk skips *intervening annotations*.
3223    //      `shouldThrowClassCastException` carries a second one
3224    //      (`@SuppressWarnings("unchecked")`), and without the skip the
3225    //      walk yields `SuppressWarnings` for it — the count stays 55 while a
3226    //      name is silently wrong, which a count-only guard cannot see. Check it
3227    //      by deleting the `w && /^    @/{next}` clause: the only row that
3228    //      changes is 688 `SuppressWarnings`.
3229    //   2. TRANSLATED is detected on **three**-slash lines only, so the two NOT
3230    //      APPLICABLE entries below — `//` lines that necessarily spell a real
3231    //      prefixed name — cannot also score as translated. Relax the anchor to
3232    //      `^ *//` and those two rows score in both columns, which the
3233    //      "neither, or both" check then reports:
3234    //        while IFS=$'\t' read -r ln nm; do
3235    //          t3=$(grep -c "^ *///.*\`MockProducerTest\.$nm\`" $R || true)
3236    //          t2=$(grep -c "^ *//.*\`MockProducerTest\.$nm\`"  $R || true)
3237    //          [ "$t3" != "$t2" ] && echo "CHANGES $nm $t3 $t2"
3238    //        done < $W.java.tsv
3239    //      prints exactly those two names, and nothing else. This is the Phase-6
3240    //      lesson (a paragraph documenting an escape shape is an adversarial input
3241    //      for a checker in the same file) handled before the fact: the block's
3242    //      prose and its own derivation text are invisible to it, because the only
3243    //      prefixed names they contain are the unexpanded `$nm` literal and these
3244    //      two markers.
3245    //
3246    // The closing backtick in the pattern is **not** load-bearing today, and the
3247    // honest form of that claim is worth writing down rather than the tempting
3248    // one: it would matter only if some `@Test` name were a strict prefix of
3249    // another, and none is —
3250    //   python3 -c "
3251    //   names=[l.split(chr(9))[1].strip() for l in open('$W.java.tsv')]
3252    //   print([(a,b) for a in names for b in names if a!=b and b.startswith(a)])"
3253    // prints `[]`, and dropping the backtick changes no row's count. It is kept as
3254    // defence against a future name that does nest, since the `MockProducerTest.`
3255    // prefix alone would not stop it.
3256    //
3257    //   J=kafka/clients/src/test/java/org/apache/kafka/clients/producer/MockProducerTest.java
3258    //   R=src/producer/mock_producer.rs
3259    //   W=/tmp/mp
3260    //
3261    //   # (1) the 55, as (declaration line, name)
3262    //   awk '/^    @Test$/{w=1;next} w && /^    @/{next}
3263    //        w{match($0,/[a-zA-Z_][A-Za-z0-9_]*\(/)
3264    //          printf "%s\t%s\n", NR, substr($0,RSTART,RLENGTH-1); w=0}' $J > $W.java.tsv
3265    //   echo "rows=$(wc -l < $W.java.tsv | tr -d ' ') atTest=$(grep -c '^    @Test$' $J)"
3266    //
3267    //   # (2) status of each. `|| true` because `grep -c` exits 1 on a zero
3268    //   # count, which would abandon the loop at the first unplaced name — i.e.
3269    //   # exactly when the check matters most.
3270    //   while IFS=$'\t' read -r ln nm; do
3271    //     t=$(grep -c "^ *///.*\`MockProducerTest\.$nm\`" $R || true)
3272    //     n=$(grep -c "^    //   NOT APPLICABLE — \`MockProducerTest\.$nm\`" $R || true)
3273    //     printf "%s\t%s\t%s\t%s\n" "$ln" "$nm" "$t" "$n"
3274    //   done < $W.java.tsv > $W.status.tsv
3275    //   echo "TRANSLATED=$(awk -F'\t' '$3==1 && $4==0' $W.status.tsv | wc -l | tr -d ' ')"
3276    //   echo "NOT_APPLICABLE=$(awk -F'\t' '$3==0 && $4==1' $W.status.tsv | wc -l | tr -d ' ')"
3277    //   echo '--- neither, or both ---'
3278    //   awk -F'\t' '!(($3==1&&$4==0)||($3==0&&$4==1))' $W.status.tsv
3279    //
3280    //   # (3) the reverse direction: no rustdoc claims a name that is not a
3281    //   # `@Test` method. This is what catches a typo in a header, which would
3282    //   # otherwise surface as an unplaced Java method and get mis-explained.
3283    //   # The five excluded are Java's own fixture *fields* plus the file itself,
3284    //   # which the fixture rustdoc cites in the same prefixed form.
3285    //   grep -o '^ *///.*`MockProducerTest\.[A-Za-z0-9_]*`' $R \
3286    //   | grep -o 'MockProducerTest\.[A-Za-z0-9_]*' | sed 's/.*\.//' | sort -u \
3287    //   | grep -vxE 'topic|groupId|record1|record2|java' > $W.claimed.txt
3288    //   comm -23 $W.claimed.txt <(cut -f2 $W.java.tsv | sort -u)
3289    //
3290    // Real output, run from the repo root: `rows=55 atTest=55`, then
3291    // `TRANSLATED=53`, `NOT_APPLICABLE=2`, then (2)'s last command prints
3292    // nothing, and (3) prints nothing. 53 + 2 = 55, so every method is placed
3293    // exactly once and nothing is claimed that Java does not declare.
3294    //
3295    // NOT APPLICABLE (2). Neither is blocked on unwritten Rust: each tests a
3296    // Java-language property that has no Rust counterpart, so there is nothing to
3297    // implement and nothing to defer.
3298    //
3299    //   NOT APPLICABLE — `MockProducerTest.shouldThrowOnNullConsumerGroupMetadataWhenSendOffsetsToTransaction`
3300    //     Despite the name, the `NullPointerException` it asserts does
3301    //     not come from `sendOffsetsToTransaction`: it comes from evaluating
3302    //     `new ConsumerGroupMetadata(null)` in the lambda — the one-arg constructor
3303    //     at `ConsumerGroupMetadata#ConsumerGroupMetadata`, delegating to the four-arg one
3304    //     declared at `ConsumerGroupMetadata#ConsumerGroupMetadata`, whose first statement is
3305    //     `Objects.requireNonNull(groupId, "group.id can't be null")` at `ConsumerGroupMetadata#ConsumerGroupMetadata` —
3306    //     before the mock is entered at all. With `Collections.emptyMap()` for the offsets
3307    //     there is no other reachable throw — `sendOffsetsToTransaction` would
3308    //     return at `MockProducer`. Rust's
3309    //     `ConsumerGroupMetadata::new(impl Into<String>)` cannot receive null, and
3310    //     `send_offsets_to_transaction` takes the metadata by value rather than as
3311    //     an `Option`, so Java's own `Objects.requireNonNull(groupMetadata)`
3312    //     (`MockProducer`) is equally unrepresentable. The empty-offsets
3313    //     path it incidentally exercises is covered by
3314    //     `should_ignore_empty_offsets_when_send_offsets_to_transaction_by_group_metadata`.
3315    //   NOT APPLICABLE — `MockProducerTest.shouldThrowClassCastException`
3316    //     Asserts that Java's type erasure lets a raw
3317    //     `ProducerRecord` carry a `String` key into an `IntegerSerializer`,
3318    //     producing `ClassCastException` inside `send`. Rust has no type erasure —
3319    //     `MockProducer<i32, String>` will not accept a `ProducerRecord<String,
3320    //     String>` at compile time. The pluggable-partitioner phase gave the mock
3321    //     the `keySerializer`/`valueSerializer` fields that `MockProducer#partition`
3322    //     invokes, but they are `Serializer<K>`/`Serializer<V>` — a
3323    //     `MockProducer<i32, String>` can only hold an `i32`/`String` serializer, so
3324    //     there is still no erased path on which a `String` key could reach an
3325    //     `IntegerSerializer`. Re-checked for this phase and still accurate.
3326    //
3327    // What the derivation cannot see, and where each is pinned instead:
3328    //
3329    //   - No test remains weakened. `test_partitioner` was the last
3330    //     such case: Java drives a `RoundRobinPartitioner` and asserts it picks
3331    //     partition 0, and the translation now does the same through
3332    //     `MockProducer::with_options`, replacing the earlier stopgap that had
3333    //     no partitioner and asserted an explicit `record.partition()` instead.
3334    //
3335    //     **Five** tests were weakened at some point and are no longer. Naming all
3336    //     five matters more than the number: the two artifacts that first recorded
3337    //     this each said "three" and each named a *different* three, because each
3338    //     was written beside the commit that made its own subset visible. A
3339    //     count-level check finds nothing wrong with two lists that agree on `3`;
3340    //     only membership does. So the set is re-derived from the diff rather than
3341    //     from either list — normalise away the two mechanical swaps
3342    //     (`MockProducer::with_auto_complete(x)` → `build_mock_producer(x)`, and
3343    //     `make_record("topic", "keyN", "valueN")` → `recordN()`), then compare
3344    //     every pre-Phase-7 test body at `82aa2da` against its current form. Seven
3345    //     bodies differ before that normalisation and exactly these five after
3346    //     (the partitioner phase un-weakened `test_partitioner`, which uses neither
3347    //     mechanical swap, so it is the seventh differing body and the fifth to
3348    //     survive normalisation):
3349    //
3350    //       `testManualCompletion` — Java compares the cause; the
3351    //         translation asserted a bare `is_err()`. Now `assert_eq!("blah", ..)`.
3352    //       `testMetadataOnException` — asserted only that the future failed,
3353    //         dropping all four values Java checks in the callback. Now asserts them.
3354    //       `shouldThrowOnSendIfProducerIsClosed` and
3355    //         `shouldThrowOnFlushProducerIfProducerIsClosed` — matched their
3356    //         message with `contains`. Now `assert_illegal_state`, i.e. variant plus
3357    //         exact message.
3358    //       `testPartitioner` — had no partitioner and asserted an explicit
3359    //         `record.partition()`; now drives a `RoundRobinPartitioner` over a
3360    //         leaderless two-partition cluster and asserts partition 0, as Java does.
3361    //   - A test that passes with the bug reintroduced. Twelve mutations of the
3362    //     Phase-7 surface were each applied and each failed the suite; the commit
3363    //     that landed them lists them.
3364    //   - Whether the *production* surface is complete. That is the method
3365    //     accounting block's job (`definition-of-done.md` §2), above the struct.
3366    //
3367    // The derivation classifies *Java* methods, so no Rust-only test appears in it
3368    // at all. This module has 74 tests against the 53 translated, i.e. 21 with no
3369    // Java counterpart. Thirteen predate Phase 7; Phase 7 added three (the §2 ones
3370    // below); a later producer-metrics milestone added `test_set_and_get_mock_metrics`
3371    // (this block read 69/16 at Phase-7 close, which was correct then; the metrics
3372    // merge added that 17th Rust-only test without revisiting this Phase-7 count, so
3373    // the numbers had drifted by one before this phase); and the pluggable-partitioner
3374    // phase adds four (`test_partition_out_of_range_is_rejected`,
3375    // `test_explicit_partition_is_honoured`, `test_no_partitioner_uses_first_partition`
3376    // and `test_empty_cluster_honours_explicit_partition` — edge cases of the new
3377    // `partition` helper that `MockProducerTest.java` does not exercise). The three
3378    // Phase-7 ones are called out here because they are the only ones discharging a
3379    // `definition-of-done.md` §2 obligation rather than adding local coverage:
3380    // `test_uncommitted_accessors`,
3381    // `test_clear_resets_staging_but_not_transaction_flags` and
3382    // `test_set_transactional_errors` cover `uncommittedRecords`,
3383    // `uncommittedOffsets` and the five transactional `*Exception` fields — each of which appears **zero** times in `MockProducerTest.java`,
3384    // its Java callers being Kafka Streams tests, out of scope. They sit with the
3385    // other Rust-only additions under "Additional unit tests".
3386    //
3387    //   cargo test --lib producer::mock_producer   # 74 passed
3388    // =====================================================================
3389}