Skip to content

Commit 677d309

Browse files
committed
tests: Cleanup: streamline structure of _server() helpers
Change the way parameter unwrapping / conditional execution is handled, so the active code in the `*_server()` functions is more in line with the corresponding `*_fork()` variants. This should faciliate keeping the fork/spawn variants in sync.
1 parent 3aaaf9e commit 677d309

File tree

2 files changed

+49
-60
lines changed

2 files changed

+49
-60
lines changed

src/platform/test.rs

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,12 @@ fn cross_process_server()
662662
{
663663
let data: &[u8] = b"1234567";
664664
let channel_name = get_channel_name_arg("server");
665-
if channel_name.is_none() {
666-
return;
667-
}
665+
if let Some(channel_name) = channel_name {
666+
let tx = OsIpcSender::connect(channel_name).unwrap();
667+
tx.send(data, vec![], vec![]).unwrap();
668668

669-
let tx = OsIpcSender::connect(channel_name.unwrap()).unwrap();
670-
tx.send(data, vec![], vec![]).unwrap();
671-
unsafe { libc::exit(0); }
669+
unsafe { libc::exit(0); }
670+
}
672671
}
673672

674673
#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
@@ -721,18 +720,17 @@ fn cross_process_fork() {
721720
fn cross_process_sender_transfer_server()
722721
{
723722
let channel_name = get_channel_name_arg("server");
724-
if channel_name.is_none() {
725-
return;
726-
}
723+
if let Some(channel_name) = channel_name {
724+
let super_tx = OsIpcSender::connect(channel_name).unwrap();
725+
let (sub_tx, sub_rx) = platform::channel().unwrap();
726+
let data: &[u8] = b"foo";
727+
super_tx.send(data, vec![OsIpcChannel::Sender(sub_tx)], vec![]).unwrap();
728+
sub_rx.recv().unwrap();
729+
let data: &[u8] = b"bar";
730+
super_tx.send(data, vec![], vec![]).unwrap();
727731

728-
let super_tx = OsIpcSender::connect(channel_name.unwrap()).unwrap();
729-
let (sub_tx, sub_rx) = platform::channel().unwrap();
730-
let data: &[u8] = b"foo";
731-
super_tx.send(data, vec![OsIpcChannel::Sender(sub_tx)], vec![]).unwrap();
732-
sub_rx.recv().unwrap();
733-
let data: &[u8] = b"bar";
734-
super_tx.send(data, vec![], vec![]).unwrap();
735-
unsafe { libc::exit(0); }
732+
unsafe { libc::exit(0); }
733+
}
736734
}
737735

738736
#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]
@@ -968,38 +966,36 @@ fn cross_process_two_step_transfer_server()
968966
{
969967
let cookie: &[u8] = b"cookie";
970968
let channel_name = get_channel_name_arg("server");
971-
if channel_name.is_none() {
972-
return;
973-
}
974-
975-
// connect by name to our other process
976-
let super_tx = OsIpcSender::connect(channel_name.unwrap()).unwrap();
969+
if let Some(channel_name) = channel_name {
970+
// connect by name to our other process
971+
let super_tx = OsIpcSender::connect(channel_name).unwrap();
977972

978-
// create a channel for real communication between the two processes
979-
let (sub_tx, sub_rx) = platform::channel().unwrap();
973+
// create a channel for real communication between the two processes
974+
let (sub_tx, sub_rx) = platform::channel().unwrap();
980975

981-
// send the other process the tx side, so it can send us the channels
982-
super_tx.send(&[], vec![OsIpcChannel::Sender(sub_tx)], vec![]).unwrap();
976+
// send the other process the tx side, so it can send us the channels
977+
super_tx.send(&[], vec![OsIpcChannel::Sender(sub_tx)], vec![]).unwrap();
983978

984-
// get two_rx from the other process
985-
let (_, mut received_channels, _) = sub_rx.recv().unwrap();
986-
assert_eq!(received_channels.len(), 1);
987-
let two_rx = received_channels[0].to_receiver();
979+
// get two_rx from the other process
980+
let (_, mut received_channels, _) = sub_rx.recv().unwrap();
981+
assert_eq!(received_channels.len(), 1);
982+
let two_rx = received_channels[0].to_receiver();
988983

989-
// get one_rx from two_rx's buffer
990-
let (_, mut received_channels, _) = two_rx.recv().unwrap();
991-
assert_eq!(received_channels.len(), 1);
992-
let one_rx = received_channels[0].to_receiver();
984+
// get one_rx from two_rx's buffer
985+
let (_, mut received_channels, _) = two_rx.recv().unwrap();
986+
assert_eq!(received_channels.len(), 1);
987+
let one_rx = received_channels[0].to_receiver();
993988

994-
// get a cookie from one_rx
995-
let (data, _, _) = one_rx.recv().unwrap();
996-
assert_eq!(&data[..], cookie);
989+
// get a cookie from one_rx
990+
let (data, _, _) = one_rx.recv().unwrap();
991+
assert_eq!(&data[..], cookie);
997992

998-
// finally, send a cookie back
999-
super_tx.send(&data, vec![], vec![]).unwrap();
993+
// finally, send a cookie back
994+
super_tx.send(&data, vec![], vec![]).unwrap();
1000995

1001-
// terminate
1002-
unsafe { libc::exit(0); }
996+
// terminate
997+
unsafe { libc::exit(0); }
998+
}
1003999
}
10041000

10051001
// TODO -- this fails on OSX with a MACH_SEND_INVALID_RIGHT!

src/test.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,25 +163,18 @@ fn select() {
163163
fn cross_process_embedded_senders_server() {
164164
let person = ("Patrick Walton".to_owned(), 29);
165165

166-
let server0_name = if let Some(name) = get_channel_name_arg("server0") {
167-
name
168-
} else {
169-
return
170-
};
171-
let server2_name = if let Some(name) = get_channel_name_arg("server2") {
172-
name
173-
} else {
174-
return
175-
};
176-
177-
let (tx1, rx1): (IpcSender<Person>, IpcReceiver<Person>) = ipc::channel().unwrap();
178-
let tx0 = IpcSender::connect(server0_name).unwrap();
179-
tx0.send(tx1).unwrap();
180-
rx1.recv().unwrap();
181-
let tx2: IpcSender<Person> = IpcSender::connect(server2_name).unwrap();
182-
tx2.send(person.clone()).unwrap();
166+
let server0_name = get_channel_name_arg("server0");
167+
let server2_name = get_channel_name_arg("server2");
168+
if let (Some(server0_name), Some(server2_name)) = (server0_name, server2_name) {
169+
let (tx1, rx1): (IpcSender<Person>, IpcReceiver<Person>) = ipc::channel().unwrap();
170+
let tx0 = IpcSender::connect(server0_name).unwrap();
171+
tx0.send(tx1).unwrap();
172+
rx1.recv().unwrap();
173+
let tx2: IpcSender<Person> = IpcSender::connect(server2_name).unwrap();
174+
tx2.send(person.clone()).unwrap();
183175

184-
unsafe { libc::exit(0); }
176+
unsafe { libc::exit(0); }
177+
}
185178
}
186179

187180
#[cfg(not(any(feature = "force-inprocess", target_os = "windows", target_os = "android")))]

0 commit comments

Comments
 (0)