[][src]Struct exonum_time::MockTimeProvider

pub struct MockTimeProvider { /* fields omitted */ }

Mock time provider for service testing.

In terms of use, the mock time provider is similar to Arc; that is, clones of the provider control the same time record as the original instance. Therefore, to use the mock provider, one may clone its instance and use the clone to construct a TimeService, while keeping the original instance to adjust the time reported to the validators along various test scenarios.

Examples

use chrono::{Utc, Duration, TimeZone};
use exonum::{helpers::Height, runtime::SnapshotExt};
use exonum_testkit::TestKit;
use exonum_time::{MockTimeProvider, TimeSchema, TimeServiceFactory};

let service_name = "time";
let service_id = 12;

let mock_provider = MockTimeProvider::default();
let mut testkit = TestKit::for_rust_service(
    TimeServiceFactory::with_provider(mock_provider.clone()),
    service_name,
    service_id,
    ()
);
mock_provider.add_time(Duration::seconds(15));
testkit.create_blocks_until(Height(2));

// The time reported by the mock time provider is reflected by the service.
let snapshot = testkit.snapshot();
let schema: TimeSchema<_> = snapshot.service_schema(service_name).unwrap();
assert_eq!(
    Utc.timestamp(15, 0),
    schema.time.get().unwrap()
);

Methods

impl MockTimeProvider[src]

pub fn new(time: DateTime<Utc>) -> Self[src]

Creates a new MockTimeProvider with time value equal to time.

pub fn time(&self) -> DateTime<Utc>[src]

Gets the time value currently reported by the provider.

pub fn set_time(&self, new_time: DateTime<Utc>)[src]

Sets the time value to new_time.

pub fn add_time(&self, duration: Duration)[src]

Adds duration to the value of time.

Trait Implementations

impl Clone for MockTimeProvider[src]

impl Debug for MockTimeProvider[src]

impl Default for MockTimeProvider[src]

fn default() -> Self[src]

Initializes the provider with the time set to the Unix epoch start.

impl From<MockTimeProvider> for Arc<dyn TimeProvider>[src]

impl TimeProvider for MockTimeProvider[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Clear for T where
    T: InitializableFromZeroed + ?Sized

impl<T> From<T> for T[src]

impl<T> InitializableFromZeroed for T where
    T: Default

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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