Skip to content

Commit

Permalink
Run tests serial, not parallel; begin test on ordering feed; better d…
Browse files Browse the repository at this point in the history
…uration handling
  • Loading branch information
hwood-fg committed Apr 14, 2023
1 parent fe7cc19 commit 0989a0b
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 69 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dnas/clutter/coordinator_zomes/mews/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ hdk = {version = "0.1", features = ["encoding"]}
holochain = { version = "=0.1.3", default-features = false, features = [ "test_utils" ] }
tokio = { version = "1.3", features = ["full"] }
futures = { version = "0.3.1", default-features = false }
serial_test = "*"
17 changes: 6 additions & 11 deletions dnas/clutter/coordinator_zomes/mews/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,12 @@ pub fn recommended(input: RecommendedInput) -> ExternResult<Vec<FeedMew>> {
.into_iter()
.filter_map(|feed_mew| {
let allowed_age = core::time::Duration::new(oldest_mew_seconds, 0);
let oldest_allowed = input.now - allowed_age;

match oldest_allowed {
Ok(oldest_allowed) => {
if feed_mew.action.timestamp() >= oldest_allowed {
Some(feed_mew)
} else {
None
}
}
Err(_) => None,
let oldest_allowed = input.now.saturating_sub(&allowed_age);

if feed_mew.action.timestamp() >= oldest_allowed {
Some(feed_mew)
} else {
None
}
})
.collect();
Expand Down
230 changes: 172 additions & 58 deletions dnas/clutter/coordinator_zomes/mews/tests/mews_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(warnings)]

use serial_test::serial;

use hdk::prelude::*;
use holochain::conductor::config::ConductorConfig;
use holochain::sweettest::{
Expand All @@ -13,6 +15,8 @@ const DNA_FILEPATH: &str = "../../workdir/clutter.dna";
const ZOME_NAME: &str = "mews";

#[tokio::test(flavor = "multi_thread")]
#[serial]

async fn trusted_feed_is_based_on_follow_topics() {
let mut agent_group = setup().await;
let agents = agent_group.create_agents().await;
Expand Down Expand Up @@ -75,65 +79,175 @@ async fn trusted_feed_is_based_on_follow_topics() {
);
}

// #[tokio::test(flavor = "multi_thread")]
// async fn trusted_feed_is_filtered_by_recency() {
// let mut agent_group = setup().await;
// let agents = agent_group.create_agents().await;
// let ann = &agents[0];
// let bob = &agents[1];

// ann.follow(FollowInput {
// agent: bob.pubkey.clone(),
// follow_topics: vec![FollowTopicInput {
// topic: String::from("holochain"),
// weight: String::from("0.9"),
// }],
// follow_other: false,
// })
// .await;

// bob.create_mew(CreateMewInput {
// mew_type: MewType::Original,
// text: Some(String::from("OLD #holochain mew")),
// links: None,
// })
// .await;

// let oldest_mew_seconds = 2;
// // Idiomatic sleep:
// // std::thread::sleep(std::time::Duration::from_secs(oldest_mew_seconds));

// // CI friendly sleep??
// let sleep_duration = std::time::Duration::from_secs(oldest_mew_seconds);
// let start_time = std::time::Instant::now();
// while std::time::Instant::now().duration_since(start_time) < sleep_duration {
// // Do nothing, just busy waiting
// }

// bob.create_mew(CreateMewInput {
// mew_type: MewType::Original,
// text: Some(String::from("NEW #holochain mew")),
// links: None,
// })
// .await;

// consistency_10s([&(ann.cell.clone()), &(bob.cell.clone())]).await;

// let recommended_feed = ann
// .recommended(RecommendedInput {
// now: Timestamp::now(),
// oldest_mew_seconds: Some(oldest_mew_seconds),
// })
// .await;

// assert_eq!(recommended_feed.len(), 1);
// assert_eq!(
// recommended_feed[0].mew.content.as_ref().unwrap().text,
// String::from("NEW #holochain mew")
// );
// }
#[tokio::test(flavor = "multi_thread")]
#[serial]

async fn trusted_feed_is_filtered_by_recency() {
let mut agent_group = setup().await;
let agents = agent_group.create_agents().await;
let ann = &agents[0];
let bob = &agents[1];

ann.follow(FollowInput {
agent: bob.pubkey.clone(),
follow_topics: vec![FollowTopicInput {
topic: String::from("holochain"),
weight: String::from("0.9"),
}],
follow_other: false,
})
.await;

bob.create_mew(CreateMewInput {
mew_type: MewType::Original,
text: Some(String::from("OLD #holochain mew")),
links: None,
})
.await;

let oldest_mew_seconds = 2;
// Idiomatic sleep:
std::thread::sleep(std::time::Duration::from_secs(oldest_mew_seconds));

bob.create_mew(CreateMewInput {
mew_type: MewType::Original,
text: Some(String::from("NEW #holochain mew")),
links: None,
})
.await;

consistency_10s([&(ann.cell.clone()), &(bob.cell.clone())]).await;

let recommended_feed = ann
.recommended(RecommendedInput {
now: Timestamp::now(),
oldest_mew_seconds: Some(oldest_mew_seconds),
})
.await;

assert_eq!(recommended_feed.len(), 1);
assert_eq!(
recommended_feed[0].mew.content.as_ref().unwrap().text,
String::from("NEW #holochain mew")
);
}

#[tokio::test(flavor = "multi_thread")]
#[serial]

async fn trusted_feed_is_ordered_by_topic_weights() {
let mut agent_group = setup().await;
let agents = agent_group.create_agents().await;
let ann = &agents[0];
let bob = &agents[1];
let cat = &agents[2];

ann.follow(FollowInput {
agent: bob.pubkey.clone(),
follow_topics: vec![FollowTopicInput {
topic: String::from("holochain"),
weight: String::from("1.0"),
}],
follow_other: false,
})
.await;

ann.follow(FollowInput {
agent: cat.pubkey.clone(),
follow_topics: vec![FollowTopicInput {
topic: String::from("holochain"),
weight: String::from("0.5"),
}],
follow_other: false,
})
.await;

ann.follow(FollowInput {
agent: bob.pubkey.clone(),
follow_topics: vec![FollowTopicInput {
topic: String::from("blockchain"),
weight: String::from("0.25"),
}],
follow_other: false,
})
.await;

ann.follow(FollowInput {
agent: cat.pubkey.clone(),
follow_topics: vec![FollowTopicInput {
topic: String::from("blockchain"),
weight: String::from("0"),
}],
follow_other: false,
})
.await;

bob.create_mew(CreateMewInput {
mew_type: MewType::Original,
text: Some(String::from("#holochain from bob, weight 1.0")),
links: None,
})
.await;

bob.create_mew(CreateMewInput {
mew_type: MewType::Original,
text: Some(String::from("#blockchain from bob, weight 0.25")),
links: None,
})
.await;

cat.create_mew(CreateMewInput {
mew_type: MewType::Original,
text: Some(String::from("#blockchain from cat, weight 0.0")),
links: None,
})
.await;

cat.create_mew(CreateMewInput {
mew_type: MewType::Original,
text: Some(String::from("#holochain from cat, weight 0.5")),
links: None,
})
.await;

consistency_10s([
&(ann.cell.clone()),
&(bob.cell.clone()),
&(cat.cell.clone()),
])
.await;

let recommended_feed = ann
.recommended(RecommendedInput {
now: Timestamp::now(),
oldest_mew_seconds: Some(60 * 60), // last hour
})
.await;

assert_eq!(recommended_feed.len(), 4);
// assert_eq!(
// recommended_feed[0].mew.content.as_ref().unwrap().text,
// String::from("#holochain from bob, weight 1.0")
// );
// assert_eq!(
// recommended_feed[1].mew.content.as_ref().unwrap().text,
// String::from("#holochain from cat, weight 0.5")
// );
// assert_eq!(
// recommended_feed[2].mew.content.as_ref().unwrap().text,
// String::from("#blockchain from bob, weight 0.25")
// );
// assert_eq!(
// recommended_feed[3].mew.content.as_ref().unwrap().text,
// String::from("#blockchain from cat, weight 0.0")
// );
}

// TEST HELPERS:
//
// ^^^ TESTS: ^^^
//
// vvv TEST HELPERS: vvv
//

pub struct Agent<'a> {
pub cell: SweetCell,
Expand Down

0 comments on commit 0989a0b

Please sign in to comment.