Skip to content

Commit

Permalink
feat: Return values
Browse files Browse the repository at this point in the history
Allowing a return value for the event closures. 
This allows for proper error propagation.

Signed-off-by: Jonathan Woollett-Light <[email protected]>
  • Loading branch information
Jonathan Woollett-Light committed Aug 13, 2023
1 parent f199fa8 commit de9e1f4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 50 deletions.
33 changes: 19 additions & 14 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn run_basic_subscriber(c: &mut Criterion) {
OwnedFd::from_raw_fd(raw_fd)
};

event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager, event_set: EventSet| {
event_manager.add(event_fd.as_fd(), EventSet::IN | EventSet::ERROR | EventSet::HANG_UP, Box::new(move |_:&mut EventManager<()>, event_set: EventSet| {
match event_set {
EventSet::IN => (),
EventSet::ERROR => {
Expand All @@ -47,9 +47,10 @@ fn run_basic_subscriber(c: &mut Criterion) {
event_fd
}).collect::<Vec<_>>();

let expected = vec![(); usize::try_from(no_of_subscribers).unwrap()];
c.bench_function("process_basic", |b| {
b.iter(|| {
assert_eq!(event_manager.wait(Some(0)), Ok(no_of_subscribers));
assert_eq!(event_manager.wait(Some(0)), Ok(expected.as_slice()));
})
});

Expand All @@ -75,7 +76,7 @@ fn run_arc_mutex_subscriber(c: &mut Criterion) {
let counter = Arc::new(Mutex::new(0u64));
let counter_clone = counter.clone();

event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager, event_set: EventSet| {
event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager<()>, event_set: EventSet| {
match event_set {
EventSet::IN => {
*counter_clone.lock().unwrap() += 1;
Expand All @@ -95,9 +96,10 @@ fn run_arc_mutex_subscriber(c: &mut Criterion) {
(event_fd,counter)
}).collect::<Vec<_>>();

let expected = vec![(); usize::try_from(no_of_subscribers).unwrap()];
c.bench_function("process_with_arc_mutex", |b| {
b.iter(|| {
assert_eq!(event_manager.wait(Some(0)), Ok(no_of_subscribers));
assert_eq!(event_manager.wait(Some(0)), Ok(expected.as_slice()));
})
});

Expand All @@ -124,7 +126,7 @@ fn run_subscriber_with_inner_mut(c: &mut Criterion) {
let counter = Arc::new(AtomicU64::new(0));
let counter_clone = counter.clone();

event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager, event_set: EventSet| {
event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager<()>, event_set: EventSet| {
match event_set {
EventSet::IN => {
counter_clone.fetch_add(1, Ordering::SeqCst);
Expand All @@ -144,9 +146,10 @@ fn run_subscriber_with_inner_mut(c: &mut Criterion) {
(event_fd,counter)
}).collect::<Vec<_>>();

let expected = vec![(); usize::try_from(no_of_subscribers).unwrap()];
c.bench_function("process_with_inner_mut", |b| {
b.iter(|| {
assert_eq!(event_manager.wait(Some(0)), Ok(no_of_subscribers));
assert_eq!(event_manager.wait(Some(0)), Ok(expected.as_slice()));
})
});

Expand Down Expand Up @@ -177,7 +180,7 @@ fn run_multiple_subscriber_types(c: &mut Criterion) {
let counter = Arc::new(AtomicU64::new(0));
let counter_clone = counter.clone();

event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager, event_set: EventSet| {
event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager<()>, event_set: EventSet| {
match event_set {
EventSet::IN => {
counter_clone.fetch_add(1, Ordering::SeqCst);
Expand Down Expand Up @@ -222,8 +225,8 @@ fn run_multiple_subscriber_types(c: &mut Criterion) {
.add(
inner_subscribers[i].as_fd(),
EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,
Box::new(
move |_: &mut EventManager, event_set: EventSet| match event_set {
Box::new(move |_: &mut EventManager<()>, event_set: EventSet| {
match event_set {
EventSet::IN => {
data_clone[i].fetch_add(1, Ordering::SeqCst);
}
Expand All @@ -234,8 +237,8 @@ fn run_multiple_subscriber_types(c: &mut Criterion) {
panic!("Cannot continue execution. Associated fd was closed.");
}
_ => {}
},
),
}
}),
)
.unwrap();
}
Expand All @@ -244,9 +247,10 @@ fn run_multiple_subscriber_types(c: &mut Criterion) {
})
.collect::<Vec<_>>();

let expected = vec![(); usize::try_from(total).unwrap()];
c.bench_function("process_dynamic_dispatch", |b| {
b.iter(|| {
assert_eq!(event_manager.wait(Some(0)), Ok(total));
assert_eq!(event_manager.wait(Some(0)), Ok(expected.as_slice()));
})
});

Expand All @@ -272,7 +276,7 @@ fn run_with_few_active_events(c: &mut Criterion) {
OwnedFd::from_raw_fd(raw_fd)
};

event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager, event_set: EventSet| {
event_manager.add(event_fd.as_fd(),EventSet::IN | EventSet::ERROR | EventSet::HANG_UP,Box::new(move |_:&mut EventManager<()>, event_set: EventSet| {
match event_set {
EventSet::IN => (),
EventSet::ERROR => {
Expand All @@ -290,9 +294,10 @@ fn run_with_few_active_events(c: &mut Criterion) {
event_fd
}).collect::<Vec<_>>();

let expected = vec![(); usize::try_from(active).unwrap()];
c.bench_function("process_dispatch_few_events", |b| {
b.iter(|| {
assert_eq!(event_manager.wait(Some(0)), Ok(active));
assert_eq!(event_manager.wait(Some(0)), Ok(expected.as_slice()));
})
});

Expand Down
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 81.4,
"coverage_score": 86.9,
"exclude_path": "benches/",
"crate_features": ""
}
Loading

0 comments on commit de9e1f4

Please sign in to comment.