1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::{
CanisterSnapshotTarget, ControllerSnapshotError, ControllerSnapshots, PocketIcInstance,
PocketIcSnapshotExt, SnapshotRestoreFunding,
};
use candid::Principal;
use pocket_ic::PocketIc;
/// One owned PocketIC instance/server record with snapshots and caller metadata.
///
/// The value contains no synchronization. A [`super::PocketIcBaselineRecipe`]
/// can own it as its fixture in a [`super::CachedPocketIcBaselinePool`].
pub struct CachedPocketIcBaseline<T> {
instance: PocketIcInstance<PocketIc>,
snapshots: ControllerSnapshots,
metadata: T,
}
impl<T> CachedPocketIcBaseline<T> {
/// Capture a cached baseline with an explicit sender for every canister.
///
/// Capture is ordered and transactional as documented by
/// [`PocketIcSnapshotExt::capture_snapshots`]. No sender fallback is performed.
pub fn capture<I>(
instance: PocketIcInstance<PocketIc>,
targets: I,
metadata: T,
) -> Result<Self, ControllerSnapshotError>
where
I: IntoIterator<Item = CanisterSnapshotTarget>,
{
let snapshots = instance.instance.capture_snapshots(targets)?;
Ok(Self {
instance,
snapshots,
metadata,
})
}
/// Restore with exactly the captured senders and selected cycle funding.
pub fn restore(&self, funding: SnapshotRestoreFunding) -> Result<(), ControllerSnapshotError> {
self.instance
.instance
.restore_snapshots(&self.snapshots, funding)
}
/// Borrow the owned PocketIC instance behind this cached baseline.
#[must_use]
pub const fn pocket_ic(&self) -> &PocketIc {
&self.instance.instance
}
/// Return the number of canisters captured by this baseline.
#[must_use]
pub fn snapshot_count(&self) -> usize {
self.snapshots.len()
}
/// Iterate over captured canister ids in deterministic principal order.
pub fn snapshot_canister_ids(&self) -> impl Iterator<Item = Principal> + '_ {
self.snapshots.canister_ids()
}
/// Borrow the captured metadata associated with this cached baseline.
#[must_use]
pub const fn metadata(&self) -> &T {
&self.metadata
}
/// Mutably borrow the captured metadata associated with this cached baseline.
#[must_use]
pub const fn metadata_mut(&mut self) -> &mut T {
&mut self.metadata
}
}