~thatonelutenist/actm

0309947e217fa458f37c40e78efecba8805129bc — Nathan McCarty 1 year, 10 months ago 78905ef
test: Factor out MathEvent

Refactor out MathEvent to decrease code duplication in tests
4 files changed, 55 insertions(+), 94 deletions(-)

M src/lib.rs
A src/testing_util.rs
M src/util/async_actor.rs
M src/util/sync_actor.rs
M src/lib.rs => src/lib.rs +2 -0
@@ 14,6 14,8 @@
    clippy::implicit_hasher
)]
pub mod executor;
#[cfg(test)]
pub(crate) mod testing_util;
pub mod traits;
pub mod types;
pub mod util;

A src/testing_util.rs => src/testing_util.rs +44 -0
@@ 0,0 1,44 @@
use enum_dispatch::enum_dispatch;
use proptest_derive::Arbitrary;

use crate::util::WrappedEvent;

/// Dummy Event implementation for use in unit testing
#[enum_dispatch]
pub(crate) trait Math {
    // Operate on a value
    fn operate(&self, value: i64) -> i64;
}

#[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)]
pub(crate) struct Add(pub(crate) i64);
impl Math for Add {
    fn operate(&self, value: i64) -> i64 {
        value.overflowing_add(self.0).0
    }
}

#[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)]
pub(crate) struct Subtract(pub(crate) i64);
impl Math for Subtract {
    fn operate(&self, value: i64) -> i64 {
        value.overflowing_sub(self.0).0
    }
}

#[enum_dispatch(Math)]
#[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
pub(crate) enum MathEventType {
    Add,
    Subtract,
}

#[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
pub(crate) struct Output {
    pub(crate) before: i64,
    pub(crate) after: i64,
    pub(crate) input: MathEventType,
}

pub(crate) type MathEvent = WrappedEvent<MathEventType>;
pub(crate) type OutputEvent = WrappedEvent<Output>;

M src/util/async_actor.rs => src/util/async_actor.rs +2 -45
@@ 287,55 287,12 @@ impl<X: Executor, I: Event, O: Event> Actor<I, O, X> for AsyncActor<I, O, X> {

#[cfg(test)]
mod tests {
    use enum_dispatch::enum_dispatch;
    use proptest_derive::Arbitrary;

    use super::*;
    use crate::{
        executor::{AsyncStd, Threads},
        util::{Collector, WrappedEvent},
        testing_util::{Add, Math, MathEvent, MathEventType, Output, OutputEvent},
        util::Collector,
    };

    // Dummy Event impls for testing `BasicActor`
    #[enum_dispatch]
    trait Math {
        // Operate on a value
        fn operate(&self, value: i64) -> i64;
    }

    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)]
    pub struct Add(i64);
    impl Math for Add {
        fn operate(&self, value: i64) -> i64 {
            value.overflowing_add(self.0).0
        }
    }

    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)]
    pub struct Subtract(i64);
    impl Math for Subtract {
        fn operate(&self, value: i64) -> i64 {
            value.overflowing_sub(self.0).0
        }
    }

    #[enum_dispatch(Math)]
    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
    enum MathEventType {
        Add,
        Subtract,
    }

    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
    struct Output {
        before: i64,
        after: i64,
        input: MathEventType,
    }

    type MathEvent = WrappedEvent<MathEventType>;
    type OutputEvent = WrappedEvent<Output>;

    // Basic smoke test, increment the counter by one a few times
    async fn smoke<X: Executor>() {
        // Create our actor

M src/util/sync_actor.rs => src/util/sync_actor.rs +7 -49
@@ 261,60 261,18 @@ impl<X: Executor, I: Event, O: Event> Actor<I, O, X> for SyncActor<I, O, X> {

#[cfg(test)]
mod tests {
    use enum_dispatch::enum_dispatch;
    use proptest_derive::Arbitrary;

    use super::*;
    use crate::{
        executor::{AsyncStd, Threads},
        util::{Collector, WrappedEvent},
        testing_util::{Add, Math, MathEvent, MathEventType, Output, OutputEvent},
        util::Collector,
    };

    // Dummy Event impls for testing `BasicActor`
    #[enum_dispatch]
    trait Math2 {
        // Operate on a value
        fn operate(&self, value: i64) -> i64;
    }

    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)]
    pub struct Add(i64);
    impl Math2 for Add {
        fn operate(&self, value: i64) -> i64 {
            value.overflowing_add(self.0).0
        }
    }

    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Default)]
    pub struct Subtract(i64);
    impl Math2 for Subtract {
        fn operate(&self, value: i64) -> i64 {
            value.overflowing_sub(self.0).0
        }
    }

    #[enum_dispatch(Math2)]
    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
    enum Math2EventType {
        Add,
        Subtract,
    }

    #[derive(Arbitrary, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
    struct Output {
        before: i64,
        after: i64,
        input: Math2EventType,
    }

    type Math2Event = WrappedEvent<Math2EventType>;
    type OutputEvent = WrappedEvent<Output>;

    // Basic smoke test, increment the counter by one a few times
    fn smoke<X: Executor>() {
        // Create our actor
        let actor: SyncActor<Math2Event, OutputEvent, X> = SyncActor::spawn(
            |value: &mut i64, mut math: Math2Event| {
        let actor: SyncActor<MathEvent, OutputEvent, X> = SyncActor::spawn(
            |value: &mut i64, mut math: MathEvent| {
                // Pull out the completion token, if there is any
                let token = math.token();
                // Perform the operation


@@ 349,8 307,8 @@ mod tests {
        let mut events = vec![];
        for _ in 0..100 {
            // Create 100 individual tasks
            let event_type = Math2EventType::Add(Add(1));
            let mut event = Math2Event::from(event_type);
            let event_type = MathEventType::Add(Add(1));
            let mut event = MathEvent::from(event_type);
            let token = event.tokenize().unwrap();
            let tx = tx.clone();
            // Register the callback


@@ 393,7 351,7 @@ mod tests {
                OutputEvent::from(Output {
                    before: x,
                    after: x + 1,
                    input: Math2EventType::Add(Add(1)),
                    input: MathEventType::Add(Add(1)),
                })
            })
            .collect();