Skip to content

Commit 9346d65

Browse files
committed
make lib and async
1 parent 5b6d85b commit 9346d65

8 files changed

Lines changed: 56 additions & 139 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dst/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "spacetimedb-dst"
2+
name = "spacetimedb-dst-lib"
33
version.workspace = true
44
edition.workspace = true
55
rust-version.workspace = true

crates/dst/README.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

crates/dst/src/engine.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,14 @@ impl EngineTarget {
274274
impl TargetDriver<Interaction> for EngineTarget {
275275
type Observation = Observation;
276276

277-
fn execute(&mut self, interaction: &Interaction) -> Result<Self::Observation, anyhow::Error> {
278-
EngineTarget::execute(self, interaction)
277+
fn execute<'a>(
278+
&'a mut self,
279+
interaction: &'a Interaction,
280+
) -> impl std::future::Future<Output = Result<Self::Observation, anyhow::Error>> + 'a {
281+
async move { EngineTarget::execute(self, interaction) }
279282
}
280283
}
284+
281285
pub struct EngineTest;
282286

283287
impl TestSuite for EngineTest {
@@ -289,15 +293,21 @@ impl TestSuite for EngineTest {
289293

290294
type Properties = EngineProperties;
291295

292-
fn build(&self, rng: Rng) -> Result<(Self::Interactions, Self::Target, Self::Properties), anyhow::Error> {
293-
let schema = default_schema(rng.clone());
294-
let runtime_seed = rng.next_u64();
295-
let target = EngineTarget::init(schema.clone(), runtime_seed)?;
296-
let properties = EngineProperties::new(schema.clone());
297-
298-
let model = Model::new(schema);
299-
let interactions = WorkloadGen::new(rng, model);
300-
301-
Ok((interactions, target, properties))
296+
fn build(
297+
&self,
298+
rng: Rng,
299+
) -> impl std::future::Future<Output = Result<(Self::Interactions, Self::Target, Self::Properties), anyhow::Error>> + '_
300+
{
301+
async move {
302+
let schema = default_schema(rng.clone());
303+
let runtime_seed = rng.next_u64();
304+
let target = EngineTarget::init(schema.clone(), runtime_seed)?;
305+
let properties = EngineProperties::new(schema.clone());
306+
307+
let model = Model::new(schema);
308+
let interactions = WorkloadGen::new(rng, model);
309+
310+
Ok((interactions, target, properties))
311+
}
302312
}
303313
}

crates/dst/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod engine;
2+
pub mod schema;
3+
pub mod sim;
4+
pub mod traits;
5+
6+
pub use traits::{Properties, TargetDriver, TestSuite, TestSuiteParts};

crates/dst/src/main.rs

Lines changed: 0 additions & 91 deletions
This file was deleted.

crates/dst/src/sim/commitlog.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ pub struct InMemoryCommitlog {
2222
options: Options,
2323
}
2424

25+
impl Default for InMemoryCommitlog {
26+
fn default() -> Self {
27+
Self::new()
28+
}
29+
}
30+
2531
impl InMemoryCommitlog {
2632
pub fn new() -> Self {
2733
Self {

crates/dst/src/traits.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use spacetimedb_runtime::sim::Rng;
55
pub trait TargetDriver<I> {
66
type Observation;
77

8-
fn execute(&mut self, interaction: &I) -> Result<Self::Observation, Error>;
8+
fn execute<'a>(
9+
&'a mut self,
10+
interaction: &'a I,
11+
) -> impl std::future::Future<Output = Result<Self::Observation, Error>> + 'a;
912
}
1013

1114
/// Ensures if Output of `TargetDrive` is expected for the input
@@ -20,32 +23,35 @@ pub type TestSuiteParts<S> = (
2023
);
2124

2225
pub trait TestSuite {
23-
type Interaction;
26+
type Interaction: std::fmt::Debug;
2427
type Interactions: Iterator<Item = Self::Interaction> + std::fmt::Debug;
2528
type Target: TargetDriver<Self::Interaction>;
2629
type Properties: Properties<Self::Interaction, <Self::Target as TargetDriver<Self::Interaction>>::Observation>;
2730

28-
fn build(&self, rng: Rng) -> Result<TestSuiteParts<Self>, Error>
31+
fn build(&self, rng: Rng) -> impl std::future::Future<Output = Result<TestSuiteParts<Self>, Error>> + '_
2932
where
3033
Self: Sized;
3134

32-
fn run(&self, rng: Rng, max_interactions: Option<usize>) -> Result<(), Error>
35+
fn run(&self, rng: Rng, max_interactions: usize) -> impl std::future::Future<Output = Result<(), Error>> + '_
3336
where
3437
Self: Sized,
3538
{
36-
let (mut interactions, mut target, mut properties) = self.build(rng)?;
39+
async move {
40+
let (mut interactions, mut target, mut properties) = self.build(rng).await?;
3741

38-
let result = (|| {
39-
for interaction in interactions.by_ref().take(max_interactions.unwrap_or(usize::MAX)) {
40-
let observation = target.execute(&interaction)?;
41-
properties.observe(&interaction, &observation)?;
42-
}
42+
let result = async {
43+
for interaction in interactions.by_ref().take(max_interactions) {
44+
let observation = target.execute(&interaction).await?;
45+
properties.observe(&interaction, &observation)?;
46+
}
4347

44-
Ok(())
45-
})();
48+
Ok(())
49+
}
50+
.await;
4651

47-
tracing::info!(interaction_counts = ?interactions, "final interaction counts");
52+
tracing::info!(interaction_counts = ?interactions, "final interaction counts");
4853

49-
result
54+
result
55+
}
5056
}
5157
}

0 commit comments

Comments
 (0)