From c40e0a138de2a1eb7496a7a3fc8a3168db1f6d42 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Tue, 28 Jul 2020 13:40:02 +0800 Subject: [PATCH] kernel: convert scalar message test to use processes Signed-off-by: Sean Cross --- kernel/src/arch/hosted.rs | 4 ++ kernel/src/test.rs | 97 ++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index 9b7acd67d..6eb4cee22 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -258,6 +258,10 @@ fn listen_thread( } } }); + + // Spawn a thread to listen for the `exit` command, and relay that + // to the main thread. This prevents us from needing to poll, since + // all messages are coalesced into a single channel. spawn(move || match exit_channel.recv() { Ok(ExitMessage::Exit) => { exit_sender.send(ClientMessage::Exit).unwrap() diff --git a/kernel/src/test.rs b/kernel/src/test.rs index 0d340e89e..08ca7ad49 100644 --- a/kernel/src/test.rs +++ b/kernel/src/test.rs @@ -105,59 +105,62 @@ fn shutdown() { main_thread.join().expect("couldn't join main thread"); } -// #[test] -// fn send_scalar_message() { -// // Start the server in another thread -// let main_thread = start_kernel(SERVER_SPEC); +#[test] +fn send_scalar_message() { + // Start the server in another thread + let main_thread = start_kernel(SERVER_SPEC); -// let (server_addr_send, server_addr_recv) = channel(); + let (server_addr_send, server_addr_recv) = channel(); -// // Spawn the server "process" (which just lives in a separate thread) -// // and receive the message. Note that we need to communicate to the -// // "Client" what our server ID is. Normally this would be done via -// // an external nameserver. -// let xous_server = as_process(move || { -// let sid = xous::create_server(b"send_scalar_mesg").expect("couldn't create test server"); -// server_addr_send.send(sid).unwrap(); -// let envelope = xous::receive_message(sid).expect("couldn't receive messages"); -// assert_eq!( -// envelope.message, -// xous::Message::Scalar(xous::ScalarMessage { -// id: 1, -// arg1: 2, -// arg2: 3, -// arg3: 4, -// arg4: 5 -// }) -// ); -// }); + // Spawn the server "process" (which just lives in a separate thread) + // and receive the message. Note that we need to communicate to the + // "Client" what our server ID is. Normally this would be done via + // an external nameserver. + let xous_server = + xous::create_process(xous::ProcessArgs::new(move || { + let sid = xous::create_server(b"send_scalar_mesg").expect("couldn't create test server"); + server_addr_send.send(sid).unwrap(); + let envelope = xous::receive_message(sid).expect("couldn't receive messages"); + assert_eq!( + envelope.message, + xous::Message::Scalar(xous::ScalarMessage { + id: 1, + arg1: 2, + arg2: 3, + arg3: 4, + arg4: 5 + }) + ); + })).expect("couldn't spawn server process"); -// // Spawn the client "process" and wait for the server address. -// let xous_client = as_process(move || { -// let sid = server_addr_recv.recv().unwrap(); -// let conn = xous::connect(sid).expect("couldn't connect to server"); -// xous::send_message( -// conn, -// xous::Message::Scalar(xous::ScalarMessage { -// id: 1, -// arg1: 2, -// arg2: 3, -// arg3: 4, -// arg4: 5, -// }), -// ) -// .expect("couldn't send message"); -// }); + // Spawn the client "process" and wait for the server address. + let xous_client = xous::create_process(xous::ProcessArgs::new(move || { + let sid = server_addr_recv.recv().unwrap(); + let conn = xous::connect(sid).expect("couldn't connect to server"); + xous::send_message( + conn, + xous::Message::Scalar(xous::ScalarMessage { + id: 1, + arg1: 2, + arg2: 3, + arg3: 4, + arg4: 5, + }), + ) + .expect("couldn't send message"); + })).expect("couldn't spawn client process"); -// // Wait for both processes to finish -// xous_server.join().expect("couldn't join server process"); -// xous_client.join().expect("couldn't join client process"); + // Wait for both processes to finish + crate::wait_process(xous_server).expect("couldn't join server process"); + crate::wait_process(xous_client).expect("couldn't join client process"); -// // Any process ought to be able to shut down the system currently. -// rsyscall(SysCall::Shutdown).expect("unable to shutdown server"); + // Any process ought to be able to shut down the system currently. + xous::create_process(xous::ProcessArgs::new(|| { + rsyscall(SysCall::Shutdown).expect("unable to shutdown server"); + })).expect("couldn't shut down the kernel"); -// main_thread.join().expect("couldn't join kernel process"); -// } + main_thread.join().expect("couldn't join kernel process"); +} // #[test] // fn send_move_message() {