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

Proxy: methods to test failed send() #72

Open
loyd opened this issue Jun 3, 2022 · 1 comment
Open

Proxy: methods to test failed send() #72

loyd opened this issue Jun 3, 2022 · 1 comment
Labels
A-test Area: The elfo-test crate. C-feature-request Category: A feature request.

Comments

@loyd
Copy link
Collaborator

loyd commented Jun 3, 2022

Now send() panics if there is an error. Sometimes it's useful to check it.

@loyd loyd added A-test Area: The elfo-test crate. C-feature-request Category: A feature request. labels Jun 3, 2022
@loyd loyd changed the title Proxy: methods to test failures send() Proxy: methods to test failed send() Jun 14, 2022
@sargarass
Copy link
Contributor

so does request().

It would be nice to have it without a built-in panic, so that in the following code, it would be possible to check for request errors.

#[tokio::test]
async fn test() {
    #[message(ret=())]
    struct Ping {
        target: u32,
    }

    #[message]
    struct Spawn {
        target: u32,
    }

    #[message]
    struct Stop {
        target: u32,
    }

    use elfo::{
        routers::{MapRouter, Outcome},
        ActorGroup, Context,
    };
    fn new() -> Blueprint {
        ActorGroup::new()
            .restart_policy(RestartPolicy::never())
            .router(MapRouter::new(|envelope| {
                msg!(match envelope {
                    Spawn { target, .. } => Outcome::Unicast(*target),
                    Ping { target } => Outcome::GentleUnicast(*target),
                    Stop { target, .. } => Outcome::GentleUnicast(*target),
                    _ => Outcome::Default,
                })
            }))
            .exec(move |ctx| async move {
                async fn run(mut ctx: Context<(), u32>) -> eyre::Result<()> {
                    info!("i'm awake", id = ctx.key());

                    while let Some(m) = ctx.recv().await {
                        msg!(match m {
                            Spawn => {}
                            Stop => break,
                            (Ping, token) => {},
                        })
                    }
                    info!("stopped", id = ctx.key());
                    Ok(())
                }

                run(ctx).await
            })
    }

    let mut test = elfo::test::proxy(new(), AnyConfig::default()).await;

    test.send(Spawn { target: 1 }).await;
    test.send(Spawn { target: 2 }).await;
    test.send(Spawn { target: 3 }).await;

    test.send(Stop { target: 2 }).await;

    tokio::time::sleep(Duration::from_secs(1)).await;

    // ignored
    test.request(Ping { target: 1}).await; //panics, would be nice to check error here
    // failed
    let test = test.request(Ping { target: 2}).await;  //panics, would be nice to check error here
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-test Area: The elfo-test crate. C-feature-request Category: A feature request.
Projects
None yet
Development

No branches or pull requests

2 participants