Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TestEnv #477

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ regex = "1.8"
tracing = "0.1"

[dev-dependencies]
tokio = { version = "1.12", features = ["rt", "macros"] }
tokio = { version = "1.12", features = ["rt", "macros", "time"] }
tokio-current-thread = "=0.2.0-alpha.1"
serde_test = "1.0"
assert_matches = "1.5"

once_cell = "1"
2 changes: 1 addition & 1 deletion src/models/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl fmt::Display for LinkError {
}
}

#[derive(Derivative, Serialize, Clone, Debug)]
#[derive(Derivative, Serialize, Debug)]
#[derivative(Default(bound = ""))]
#[serde(rename_all = "camelCase")]
pub struct Link<T> {
Expand Down
27 changes: 16 additions & 11 deletions src/runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::{Arc, LockResult, RwLock, RwLockReadGuard};
#[derive(Serialize, Debug, PartialEq)]
#[serde(tag = "name", content = "args")]
pub enum RuntimeEvent<E: Env, M: Model<E>> {
NewState(Vec<M::Field>, #[cfg(test)] M),
NewState(Vec<M::Field>),
CoreEvent(Event),
}

Expand All @@ -33,8 +33,8 @@ pub struct Runtime<E: Env, M: Model<E>> {

impl<E, M> Runtime<E, M>
where
E: Env + Send + 'static,
M: Model<E> + Send + Sync + 'static,
E: Env + core::fmt::Debug + Send + 'static,
M: Model<E> + core::fmt::Debug + Send + Sync + 'static,
{
pub fn new(
model: M,
Expand Down Expand Up @@ -67,24 +67,29 @@ where
};
self.handle_effects(effects, fields);
}

#[cfg(test)]
pub async fn close(&mut self) -> Result<(), anyhow::Error> {
self.tx.flush().await?;
self.tx.close_channel();
Ok(())
}

#[cfg(test)]
pub async fn flush(&mut self) -> Result<(), anyhow::Error> {
self.tx.flush().await?;
Ok(())
}
fn emit(&self, event: RuntimeEvent<E, M>) {
self.tx.clone().try_send(event).expect("emit event failed");
println!("emit event: {event:#?}");

let result = self.tx.clone().try_send(event);
println!("Result is ok? {}", result.is_ok());
result.expect("emit event failed")
}
fn handle_effects(&self, effects: Vec<Effect>, fields: Vec<M::Field>) {
if !fields.is_empty() {
#[cfg(test)]
let model = self.model.read().expect("model read failed");
self.emit(RuntimeEvent::<E, M>::NewState(
fields,
#[cfg(test)]
model.to_owned(),
));
self.emit(RuntimeEvent::<E, M>::NewState(fields));
};
effects
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::runtime::{Effect, Effects, Env};
use core::fmt::Debug;
use serde::{Deserialize, Serialize};

pub trait Model<E: Env>: Clone {
pub trait Model<E: Env> {
#[cfg(not(debug_assertions))]
type Field: Send + Sync + Serialize + for<'de> Deserialize<'de>;
#[cfg(debug_assertions)]
Expand Down
8 changes: 4 additions & 4 deletions src/unit_tests/catalog_with_filters/load_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ fn default_catalog() {
events[0]
.downcast_ref::<RuntimeEvent<TestEnv, TestModel>>()
.unwrap(),
RuntimeEvent::NewState(fields, _) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
RuntimeEvent::NewState(fields) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
);
assert_matches!(
events[1]
.downcast_ref::<RuntimeEvent<TestEnv, TestModel>>()
.unwrap(),
RuntimeEvent::NewState(fields, _) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
RuntimeEvent::NewState(fields) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
);
let states = STATES.read().unwrap();
let states = states
Expand Down Expand Up @@ -173,13 +173,13 @@ fn search_catalog() {
events[0]
.downcast_ref::<RuntimeEvent<TestEnv, TestModel>>()
.unwrap(),
RuntimeEvent::NewState(fields, _) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
RuntimeEvent::NewState(fields) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
);
assert_matches!(
events[1]
.downcast_ref::<RuntimeEvent<TestEnv, TestModel>>()
.unwrap(),
RuntimeEvent::NewState(fields, _) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
RuntimeEvent::NewState(fields) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::Discover
);
let states = STATES.read().unwrap();
let states = states
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/add_to_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use url::Url;

#[test]
fn actionctx_addtolibrary() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -159,7 +159,7 @@ fn actionctx_addtolibrary() {

#[test]
fn actionctx_addtolibrary_already_added() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
6 changes: 3 additions & 3 deletions src/unit_tests/ctx/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use stremio_derive::Model;

#[test]
fn actionctx_authenticate_login() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -218,7 +218,7 @@ fn actionctx_authenticate_login() {

#[test]
fn actionctx_authenticate_login_with_token() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -416,7 +416,7 @@ fn actionctx_authenticate_login_with_token() {

#[test]
fn actionctx_authenticate_register() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
8 changes: 4 additions & 4 deletions src/unit_tests/ctx/install_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use url::Url;

#[test]
fn actionctx_installaddon_install() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -84,7 +84,7 @@ fn actionctx_installaddon_install() {

#[test]
fn actionctx_installaddon_install_with_user() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -198,7 +198,7 @@ fn actionctx_installaddon_install_with_user() {

#[test]
fn actionctx_installaddon_update() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -306,7 +306,7 @@ fn actionctx_installaddon_update() {

#[test]
fn actionctx_installaddon_already_installed() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
2 changes: 1 addition & 1 deletion src/unit_tests/ctx/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use stremio_derive::Model;

#[test]
fn actionctx_logout() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/pull_addons_from_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use url::Url;

#[test]
fn actionctx_pulladdonsfromapi() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -73,7 +73,7 @@ fn actionctx_pulladdonsfromapi() {

#[test]
fn actionctx_pulladdonsfromapi_with_user() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/push_addons_to_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use url::Url;

#[test]
fn actionctx_pushaddonstoapi() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -65,7 +65,7 @@ fn actionctx_pushaddonstoapi() {

#[test]
fn actionctx_pushaddonstoapi_with_user() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/remove_from_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use stremio_derive::Model;

#[test]
fn actionctx_removefromlibrary() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -148,7 +148,7 @@ fn actionctx_removefromlibrary() {

#[test]
fn actionctx_removefromlibrary_not_added() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/rewind_library_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use stremio_derive::Model;

#[test]
fn actionctx_rewindlibraryitem() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -154,7 +154,7 @@ fn actionctx_rewindlibraryitem() {

#[test]
fn actionctx_rewindlibraryitem_not_added() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
6 changes: 3 additions & 3 deletions src/unit_tests/ctx/sync_library_with_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use stremio_derive::Model;

#[test]
fn actionctx_synclibrarywithapi() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand All @@ -40,7 +40,7 @@ fn actionctx_synclibrarywithapi() {

#[test]
fn actionctx_synclibrarywithapi_with_user() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -366,7 +366,7 @@ fn actionctx_synclibrarywithapi_with_user() {

#[test]
fn actionctx_synclibrarywithapi_with_user_empty_library() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
8 changes: 4 additions & 4 deletions src/unit_tests/ctx/uninstall_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use url::Url;

#[test]
fn actionctx_uninstalladdon() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -91,7 +91,7 @@ fn actionctx_uninstalladdon() {

#[test]
fn actionctx_uninstalladdon_with_user() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -212,7 +212,7 @@ fn actionctx_uninstalladdon_with_user() {

#[test]
fn actionctx_uninstalladdon_protected() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -287,7 +287,7 @@ fn actionctx_uninstalladdon_protected() {

#[test]
fn actionctx_uninstalladdon_not_installed() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/update_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use stremio_derive::Model;

#[test]
fn actionctx_updatesettings() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -49,7 +49,7 @@ fn actionctx_updatesettings() {

#[test]
fn actionctx_updatesettings_not_changed() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/ctx/upgrade_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use url::Url;

#[test]
fn actionctx_addon_upgrade() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down Expand Up @@ -118,7 +118,7 @@ fn actionctx_addon_upgrade() {

#[test]
fn actionctx_addon_upgrade_fail_due_to_different_url() {
#[derive(Model, Clone, Default)]
#[derive(Model, Debug, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/data_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ fn data_export_with_user() {
events[0]
.downcast_ref::<RuntimeEvent<TestEnv, TestModel>>()
.unwrap(),
RuntimeEvent::NewState(fields, _) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::DataExport
RuntimeEvent::NewState(fields) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::DataExport
);
assert_matches!(
events[1]
.downcast_ref::<RuntimeEvent<TestEnv, TestModel>>()
.unwrap(),
RuntimeEvent::NewState(fields, _) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::DataExport
RuntimeEvent::NewState(fields) if fields.len() == 1 && *fields.first().unwrap() == TestModelField::DataExport
);
let states = STATES.read().unwrap();
let states = states
Expand Down
Loading