[][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.
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 {
    fn after_commit(&self, _: AfterCommitContext) {
        self.counter.fetch_add(1, Ordering::SeqCst);
    }
}

let service = AfterCommitService::new();
let mut testkit = TestKit::for_rust_service(
    service.clone(),
    "after_commit",
    SERVICE_ID,
    (),
);
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 rust_runtime = RustRuntime::builder().with_factory(service.clone());
let mut testkit = stopped.resume(rust_runtime);
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, rust_runtime: RustRuntimeBuilder) -> TestKit[src]

Resumes the operation of the testkit with the Rust runtime.

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

pub fn resume_with_runtimes(
    self,
    rust_runtime: RustRuntimeBuilder,
    external_runtimes: Vec<RuntimeInstance>
) -> TestKit
[src]

Resumes the operation fo the testkit with the specified runtimes.

Trait Implementations

impl Debug for StoppedTestKit[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> 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<V, T> VZip<V> for T where
    V: MultiLane<T>,