[][src]Struct exonum_testkit::StoppedTestKit

pub struct StoppedTestKit { /* fields omitted */ }

Persistent state of an Exonum node allowing to emulate node restart.

The persistent state holds the database (including uncommitted transactions) and the network configuration, but does not retain the internal state of the services.

This method is useful to test scenarios that may play a different way depending on node restarts, such as services with dynamic internal state modified in response to blockchain events (e.g., in Service::after_commit).

Examples

// Service with internal state modified by a custom `after_commit` hook.
#[derive(Clone)]
struct AfterCommitService {
    counter: Arc<AtomicUsize>,
}

impl AfterCommitService {
    pub fn new() -> Self {
        AfterCommitService { counter: Arc::new(AtomicUsize::default()) }
    }

    pub fn counter(&self) -> usize {
        self.counter.load(Ordering::SeqCst)
    }
}

impl Service for AfterCommitService {
    // Other methods...

    fn after_commit(&self, context: &ServiceContext) {
        self.counter.fetch_add(1, Ordering::SeqCst);
    }
}

let service = AfterCommitService::new();
let mut testkit = TestKit::for_service(service.clone());
testkit.create_blocks_until(Height(5));
assert_eq!(service.counter(), 5);

// Stop the testkit.
let stopped = testkit.stop();
assert_eq!(stopped.height(), Height(5));

// Resume with the same single service with a fresh state.
let service = AfterCommitService::new();
let mut testkit = stopped.resume(vec![service.clone().into()]);
testkit.create_blocks_until(Height(8));
assert_eq!(service.counter(), 3); // We've only created 3 new blocks.

Methods

impl StoppedTestKit[src]

pub fn snapshot(&self) -> Box<dyn Snapshot>[src]

Returns a snapshot of the database state.

pub fn height(&self) -> Height[src]

Returns the height of latest committed block.

pub fn network(&self) -> &TestNetwork[src]

Returns the reference to test network.

pub fn resume(self, services: Vec<Box<dyn Service>>) -> TestKit[src]

Resumes the operation of the testkit.

Note that services may differ from the vector of services initially passed to the TestKit (which is also what may happen with real Exonum apps).

Trait Implementations

impl Debug for StoppedTestKit[src]

Auto Trait Implementations

Blanket Implementations

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

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

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<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

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

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

impl<T> Erased for T

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

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