Skip to main content

MockProducer

Struct MockProducer 

Source
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>

Source

pub fn with_cluster_auto_complete(cluster: Cluster, auto_complete: bool) -> Self

Create a mock producer

§Arguments
  • cluster - The cluster holding metadata for this producer
  • auto_complete - If true automatically complete all requests successfully and execute the callback. Otherwise the user must call complete_next or error_next after send to 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.

Source

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 call complete_next or error_next after send), 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().

Source

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 call complete_next or error_next after send to complete the call and resolve the returned future.
Source

pub fn history(&self) -> Vec<ProducerRecord<K, V>>
where K: Clone, V: Clone,

Returns the list of sent records since the last call to clear.

Source

pub fn uncommitted_records(&self) -> Vec<ProducerRecord<K, V>>
where K: Clone, V: Clone,

Gets the list of records sent in the current transaction that have not yet been committed.

§Returns

A list of uncommitted producer records

Source

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.

Source

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

Source

pub fn clear(&self)

Clear the stored history of sent records, consumer group offsets

Source

pub fn complete_next(&self) -> bool

Complete the earliest uncompleted call successfully.

§Returns

true if there was an uncompleted call to complete

Source

pub fn error_next(&self, error: Error) -> bool

Complete the earliest uncompleted call with the given error.

§Arguments
  • error - the error
§Returns

true if there was an uncompleted call to complete

Source

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_error if closed or not initialized for transactions.
  • Error::is_producer_fenced_error if already fenced.
Source

pub fn transaction_initialized(&self) -> bool

Returns true if init_transactions() has completed successfully.

Source

pub fn transaction_in_flight(&self) -> bool

Returns true if a transaction has been begun and neither committed nor aborted.

Source

pub fn transaction_committed(&self) -> bool

Returns true if the most recent transaction was committed.

Source

pub fn transaction_aborted(&self) -> bool

Returns true if the most recent transaction was aborted.

Source

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

Source

pub fn commit_count(&self) -> i64

Gets the total number of transactions committed by this mock producer.

§Returns

The commit count

Source

pub fn closed(&self) -> bool

Returns true if the producer is closed.

Source

pub fn flushed(&self) -> bool

Returns true if there are no pending completions.

Source

pub fn set_send_error(&self, error: Option<Error>)

Error to return when send or send_with_callback is called

§Arguments
  • error - None clears it
Source

pub fn set_flush_error(&self, error: Option<Error>)

Error to return when flush is called

§Arguments
  • error - None clears it
Source

pub fn set_partitions_for_error(&self, error: Option<Error>)

Error to return when partitions_for is called

§Arguments
  • error - None clears it
Source

pub fn set_close_error(&self, error: Option<Error>)

Error to return when close or close_with_timeout is called

§Arguments
  • error - None clears it
Source

pub fn set_mock_metrics(&self, name: MetricName, metric: Arc<KafkaMetric>)

Set a mock metric for testing

§Arguments
  • name - key
  • metric - value
Source

pub fn set_init_transaction_error(&self, error: Option<Error>)

Error to return when init_transactions is called

§Arguments
  • error - None clears it
Source

pub fn set_begin_transaction_error(&self, error: Option<Error>)

Error to return when begin_transaction is called

§Arguments
  • error - None clears it
Source

pub fn set_send_offsets_to_transaction_error(&self, error: Option<Error>)

Error to return when send_offsets_to_transaction is called

§Arguments
  • error - None clears it
Source

pub fn set_commit_transaction_error(&self, error: Option<Error>)

Error to return when commit_transaction is called

§Arguments
  • error - None clears it
Source

pub fn set_abort_transaction_error(&self, error: Option<Error>)

Error to return when abort_transaction is called

§Arguments
  • error - None clears it

Trait Implementations§

Source§

impl<K, V> Debug for MockProducer<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for MockProducer<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K: Send + Sync, V: Send + Sync> Producer<K, V> for MockProducer<K, V>

Source§

async fn init_transactions(&self) -> Result<(), Error>

Needs to be called before any other methods when the transactional.id is set in the configuration. Read more
Source§

fn begin_transaction(&self) -> Result<(), Error>

Should be called before the start of each new transaction. Note that prior to the first invocation of this method, you must invoke init_transactions exactly one time. Read more
Source§

async fn send_offsets_to_transaction( &self, offsets: HashMap<TopicPartition, OffsetAndMetadata>, group_metadata: &dyn ConsumerGroupMetadata, ) -> Result<(), Error>

Sends a list of specified offsets to the consumer group coordinator, and also marks those offsets as part of the current transaction. These offsets will be considered committed only if the transaction is committed successfully. Read more
Source§

async fn commit_transaction(&self) -> Result<(), Error>

Commits the ongoing transaction. This method will flush any unsent records before actually committing the transaction. Read more
Source§

async fn abort_transaction(&self) -> Result<(), Error>

Aborts the ongoing transaction. Any unflushed produce messages will be aborted when this call is made. Read more
Source§

async fn send( &self, record: ProducerRecord<K, V>, ) -> Result<KafkaFuture<RecordMetadata>, Error>

Asynchronously send a record to a topic. Equivalent to send_with_callback(record, None). See send_with_callback for details. Read more
Source§

async fn send_with_callback( &self, record: ProducerRecord<K, V>, callback: Option<Box<dyn Callback>>, ) -> Result<KafkaFuture<RecordMetadata>, Error>

Asynchronously send a record to a topic and invoke the provided callback when the send has been acknowledged. Read more
Source§

async fn flush(&self) -> Result<(), Error>

Invoking this method makes all buffered records immediately available to send (even if 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 more
Source§

async fn partitions_for(&self, topic: &str) -> Result<Vec<PartitionInfo>, Error>

Get the partition metadata for the given topic. This can be used for custom partitioning. Read more
Source§

fn metrics(&self) -> MetricsView

Get the full set of internal metrics maintained by the producer. Read more
Source§

async fn close(&self) -> Result<(), Error>

Close this producer. This method blocks until all previously sent requests complete. This method is equivalent to close_with_timeout(Duration::MAX). Read more
Source§

async fn close_with_timeout(&self, _timeout: Duration) -> Result<(), Error>

This method waits up to timeout for the producer to complete the sending of all incomplete requests. Read more

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>
where K: Send, V: Send,

§

impl<K, V> Sync for MockProducer<K, V>
where K: Send, V: Send,

§

impl<K, V> Unpin for MockProducer<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnsafeUnpin for MockProducer<K, V>

§

impl<K, V> !UnwindSafe for MockProducer<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<K, V, P> DynProducer<K, V> for P
where 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,

Source§

fn begin_transaction(&self) -> Result<(), Error>

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,

Source§

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,

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,

Source§

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,

Source§

fn partitions_for<'a>( &'a self, topic: &'a str, ) -> Pin<Box<dyn Future<Output = Result<Vec<PartitionInfo>, Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source§

fn metrics(&self) -> MetricsView

Source§

fn close<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source§

fn close_with_timeout<'a>( &'a self, timeout: Duration, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
where K: 'a, V: 'a,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V