elfo-rs / elfo

An asynchronous distributed actor framework in Rust with robust observability
217 stars 12 forks source link

Test utilities can be improved #115

Closed sargarass closed 6 months ago

sargarass commented 10 months ago

For now, Elfo provides us with assert_msg_eq and assert_msg, which support assertions for specific message types and values inside. However, these assertions do not offer the capability to unpack the message and bind it to a value. Therefore, I suggest that we introduce the expect_x functions to handle these cases in the test environment.

#[track_caller]
pub fn expect_message<T: Message>(msg: Envelope) -> T {
    msg!(match msg {
        msg @ T => msg,
        msg => panic!(
            "unexpected message: expected {}, got: {:?}",
            elfo::dumping::extract_name_by_type::<T>(),
            msg
        ),
    })
}

#[track_caller]
pub fn expect_request<T: Request>(msg: Envelope) -> (T, ResponseToken<T>) {
    msg!(match msg {
        (msg @ T, token) => (msg, token),
        msg => panic!(
            "unexpected request: expected {}, got: {:?}",
            elfo::dumping::extract_name_by_type::<T>(),
            msg
        ),
    })
}

Note that all of these functions return the unpacked value to a user.

sargarass commented 6 months ago

we got it here PR121