client/authority-discovery: Rework error handling (#5631)

* client/authority-discovery: Rework error handling

Instead of `handle_dht_events` returning a `Result<(), Error>`, return
a `Poll<Error>` where `Poll::Pending` signals that there are no more
events to handle and `Poll::Ready(Error)` signals that a fatal error
occured. Non fatal errors are handled within `handle_dht_events`
directly, thus looping in `poll` is not necessary anymore.

* client/authority-discovery: Return () instead of error on termiantion

* Update client/authority-discovery/src/lib.rs

Co-Authored-By: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Max Inden
2020-04-16 17:09:13 +02:00
committed by GitHub
parent 239d0998ea
commit 6bd93825ca
3 changed files with 73 additions and 64 deletions
@@ -286,6 +286,7 @@ fn request_addresses_of_others_triggers_dht_get_query() {
#[test]
fn handle_dht_events_with_value_found_should_call_set_priority_group() {
let _ = ::env_logger::try_init();
// Create authority discovery.
let (mut dht_event_tx, dht_event_rx) = channel(1000);
@@ -331,7 +332,9 @@ fn handle_dht_events_with_value_found_should_call_set_priority_group() {
// Make authority discovery handle the event.
let f = |cx: &mut Context<'_>| -> Poll<()> {
authority_discovery.handle_dht_events(cx).unwrap();
if let Poll::Ready(e) = authority_discovery.handle_dht_events(cx) {
panic!("Unexpected error: {:?}", e);
}
// Expect authority discovery to set the priority set.
assert_eq!(network.set_priority_group_call.lock().unwrap().len(), 1);
@@ -439,15 +442,16 @@ fn dont_stop_polling_when_error_is_returned() {
// Now we call `await` and give the control to the authority discovery future.
assert_eq!(Some(Event::Processed), discovery_update_rx.next().await);
// Drop the event rx to stop the authority discovery. If it was polled correctly, it should
// end properly.
// Drop the event rx to stop the authority discovery. If it was polled correctly, it
// should end properly.
drop(dht_event_tx);
assert!(
discovery_update_rx.collect::<Vec<Event>>()
.await
.into_iter()
.any(|evt| evt == Event::End), "The authority should have ended",
.any(|evt| evt == Event::End),
"The authority discovery should have ended",
);
}
);