pub struct MockProducer<K, V> { /* private fields */ }Expand description
A mock of the producer interface for testing code that uses Kafka.
By default this mock will synchronously complete each send call successfully. However it can be configured to allow the user to control the completion of the call and supply an optional error for the producer to return.
Implementations§
Source§impl<K, V> MockProducer<K, V>
impl<K, V> MockProducer<K, V>
Sourcepub fn with_cluster_auto_complete(cluster: Cluster, auto_complete: bool) -> Self
pub fn with_cluster_auto_complete(cluster: Cluster, auto_complete: bool) -> Self
Create a mock producer
§Arguments
cluster- The cluster holding metadata for this producerauto_complete- If true automatically complete all requests successfully and execute the callback. Otherwise the user must callcomplete_nextorerror_nextaftersendto complete the call and resolve the returned future.
§Translation notes
Java’s MockProducer(Cluster, boolean, Partitioner, Serializer, Serializer) with a null partitioner and null serializers.
Sourcepub fn with_options(options: MockProducerOptions<K, V>) -> Self
pub fn with_options(options: MockProducerOptions<K, V>) -> Self
Create a mock producer
§Arguments
options- The cluster holding metadata for this producer (unset: an empty cluster), whether to automatically complete all requests successfully and execute the callback (otherwise the user must callcomplete_nextorerror_nextaftersend), the partition strategy, and the serializers for key and value.
§Translation notes
Java’s MockProducer(Cluster, boolean, Partitioner, Serializer, Serializer) and, with cluster unset, MockProducer(boolean, Partitioner, Serializer, Serializer), which passes Cluster.empty().
Sourcepub fn with_auto_complete(auto_complete: bool) -> Self
pub fn with_auto_complete(auto_complete: bool) -> Self
Create a new mock producer with invented metadata and the given
auto_complete setting.
§Arguments
auto_complete- If true automatically complete all requests successfully and execute the callback. Otherwise the user must callcomplete_nextorerror_nextaftersendto complete the call and resolve the returned future.
Sourcepub fn history(&self) -> Vec<ProducerRecord<K, V>>
pub fn history(&self) -> Vec<ProducerRecord<K, V>>
Returns the list of sent records since the last call to
clear.
Sourcepub fn uncommitted_records(&self) -> Vec<ProducerRecord<K, V>>
pub fn uncommitted_records(&self) -> Vec<ProducerRecord<K, V>>
Gets the list of records sent in the current transaction that have not yet been committed.
§Returns
A list of uncommitted producer records
Sourcepub fn consumer_group_offsets_history(
&self,
) -> Vec<HashMap<String, HashMap<TopicPartition, OffsetAndMetadata>>>
pub fn consumer_group_offsets_history( &self, ) -> Vec<HashMap<String, HashMap<TopicPartition, OffsetAndMetadata>>>
Returns the list of committed consumer group offsets since the last
call to clear.
Sourcepub fn uncommitted_offsets(
&self,
) -> HashMap<String, HashMap<TopicPartition, OffsetAndMetadata>>
pub fn uncommitted_offsets( &self, ) -> HashMap<String, HashMap<TopicPartition, OffsetAndMetadata>>
Gets the consumer group offsets sent in the current transaction that have not yet been committed.
§Returns
A map of consumer group IDs to their uncommitted offsets
Sourcepub fn complete_next(&self) -> bool
pub fn complete_next(&self) -> bool
Complete the earliest uncompleted call successfully.
§Returns
true if there was an uncompleted call to complete
Sourcepub fn error_next(&self, error: Error) -> bool
pub fn error_next(&self, error: Error) -> bool
Sourcepub fn fence_producer(&self) -> Result<(), Error>
pub fn fence_producer(&self) -> Result<(), Error>
Fences this mock producer, causing it to return a producer-fenced error on subsequent transactional operations.
§Errors
Error::is_local_illegal_state_errorif closed or not initialized for transactions.Error::is_producer_fenced_errorif already fenced.
Sourcepub fn transaction_initialized(&self) -> bool
pub fn transaction_initialized(&self) -> bool
Returns true if init_transactions() has
completed successfully.
Sourcepub fn transaction_in_flight(&self) -> bool
pub fn transaction_in_flight(&self) -> bool
Returns true if a transaction has been begun and neither committed nor
aborted.
Sourcepub fn transaction_committed(&self) -> bool
pub fn transaction_committed(&self) -> bool
Returns true if the most recent transaction was committed.
Sourcepub fn transaction_aborted(&self) -> bool
pub fn transaction_aborted(&self) -> bool
Returns true if the most recent transaction was aborted.
Sourcepub fn sent_offsets(&self) -> bool
pub fn sent_offsets(&self) -> bool
Checks whether offsets have been sent to the current transaction.
§Returns
true if offsets were sent in the current transaction, false
otherwise
Sourcepub fn commit_count(&self) -> i64
pub fn commit_count(&self) -> i64
Sourcepub fn set_send_error(&self, error: Option<Error>)
pub fn set_send_error(&self, error: Option<Error>)
Sourcepub fn set_flush_error(&self, error: Option<Error>)
pub fn set_flush_error(&self, error: Option<Error>)
Sourcepub fn set_partitions_for_error(&self, error: Option<Error>)
pub fn set_partitions_for_error(&self, error: Option<Error>)
Sourcepub fn set_close_error(&self, error: Option<Error>)
pub fn set_close_error(&self, error: Option<Error>)
Sourcepub fn set_mock_metrics(&self, name: MetricName, metric: Arc<KafkaMetric>)
pub fn set_mock_metrics(&self, name: MetricName, metric: Arc<KafkaMetric>)
Sourcepub fn set_init_transaction_error(&self, error: Option<Error>)
pub fn set_init_transaction_error(&self, error: Option<Error>)
Sourcepub fn set_begin_transaction_error(&self, error: Option<Error>)
pub fn set_begin_transaction_error(&self, error: Option<Error>)
Sourcepub fn set_send_offsets_to_transaction_error(&self, error: Option<Error>)
pub fn set_send_offsets_to_transaction_error(&self, error: Option<Error>)
Sourcepub fn set_commit_transaction_error(&self, error: Option<Error>)
pub fn set_commit_transaction_error(&self, error: Option<Error>)
Sourcepub fn set_abort_transaction_error(&self, error: Option<Error>)
pub fn set_abort_transaction_error(&self, error: Option<Error>)
Trait Implementations§
Source§impl<K, V> Debug for MockProducer<K, V>
impl<K, V> Debug for MockProducer<K, V>
Source§impl<K, V> Default for MockProducer<K, V>
impl<K, V> Default for MockProducer<K, V>
Source§impl<K: Send + Sync, V: Send + Sync> Producer<K, V> for MockProducer<K, V>
impl<K: Send + Sync, V: Send + Sync> Producer<K, V> for MockProducer<K, V>
Source§async fn init_transactions(&self) -> Result<(), Error>
async fn init_transactions(&self) -> Result<(), Error>
transactional.id
is set in the configuration. Read moreSource§fn begin_transaction(&self) -> Result<(), Error>
fn begin_transaction(&self) -> Result<(), Error>
init_transactions exactly one time. Read moreSource§async fn send_offsets_to_transaction(
&self,
offsets: HashMap<TopicPartition, OffsetAndMetadata>,
group_metadata: &dyn ConsumerGroupMetadata,
) -> Result<(), Error>
async fn send_offsets_to_transaction( &self, offsets: HashMap<TopicPartition, OffsetAndMetadata>, group_metadata: &dyn ConsumerGroupMetadata, ) -> Result<(), Error>
Source§async fn commit_transaction(&self) -> Result<(), Error>
async fn commit_transaction(&self) -> Result<(), Error>
Source§async fn abort_transaction(&self) -> Result<(), Error>
async fn abort_transaction(&self) -> Result<(), Error>
Source§async fn send(
&self,
record: ProducerRecord<K, V>,
) -> Result<KafkaFuture<RecordMetadata>, Error>
async fn send( &self, record: ProducerRecord<K, V>, ) -> Result<KafkaFuture<RecordMetadata>, Error>
send_with_callback(record, None). See
send_with_callback for details. Read moreSource§async fn send_with_callback(
&self,
record: ProducerRecord<K, V>,
callback: Option<Box<dyn Callback>>,
) -> Result<KafkaFuture<RecordMetadata>, Error>
async fn send_with_callback( &self, record: ProducerRecord<K, V>, callback: Option<Box<dyn Callback>>, ) -> Result<KafkaFuture<RecordMetadata>, Error>
Source§async fn flush(&self) -> Result<(), Error>
async fn flush(&self) -> Result<(), Error>
linger.ms is greater than 0) and blocks on the completion
of the requests associated with these records. The post-condition of
flush() is that any previously sent record will have completed (e.g.
its future is done and callbacks passed to Self::send_with_callback
have been called). A request is considered completed when it is
successfully acknowledged according to the acks configuration you have
specified or else it results in an error. Read moreSource§async fn partitions_for(&self, topic: &str) -> Result<Vec<PartitionInfo>, Error>
async fn partitions_for(&self, topic: &str) -> Result<Vec<PartitionInfo>, Error>
Source§fn metrics(&self) -> MetricsView
fn metrics(&self) -> MetricsView
Auto Trait Implementations§
impl<K, V> !Freeze for MockProducer<K, V>
impl<K, V> !RefUnwindSafe for MockProducer<K, V>
impl<K, V> Send for MockProducer<K, V>
impl<K, V> Sync for MockProducer<K, V>
impl<K, V> Unpin for MockProducer<K, V>
impl<K, V> UnsafeUnpin for MockProducer<K, V>
impl<K, V> !UnwindSafe for MockProducer<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<K, V, P> DynProducer<K, V> for Pwhere
P: Producer<K, V>,
impl<K, V, P> DynProducer<K, V> for Pwhere
P: Producer<K, V>,
Source§fn init_transactions<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn init_transactions<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Source§fn send_offsets_to_transaction<'a>(
&'a self,
offsets: HashMap<TopicPartition, OffsetAndMetadata>,
group_metadata: &'a (dyn ConsumerGroupMetadata + 'static),
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn send_offsets_to_transaction<'a>(
&'a self,
offsets: HashMap<TopicPartition, OffsetAndMetadata>,
group_metadata: &'a (dyn ConsumerGroupMetadata + 'static),
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Source§fn commit_transaction<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn commit_transaction<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Source§fn abort_transaction<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn abort_transaction<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Source§fn send<'a>(
&'a self,
record: ProducerRecord<K, V>,
) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn send<'a>(
&'a self,
record: ProducerRecord<K, V>,
) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Producer::send. Read moreSource§fn send_with_callback<'a>(
&'a self,
record: ProducerRecord<K, V>,
callback: Option<Box<dyn Callback>>,
) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn send_with_callback<'a>(
&'a self,
record: ProducerRecord<K, V>,
callback: Option<Box<dyn Callback>>,
) -> Pin<Box<dyn Future<Output = Result<KafkaFuture<RecordMetadata>, Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Source§fn flush<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
fn flush<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>where
K: 'a,
V: 'a,
Producer::flush. Read more