client/authority-discovery: Terminate when network does (#5562)

* client/authority-discovery: Add test ensure termination on network termi

* client/authority-discovery: Terminate when network does

When the dht event stream returns Poll::Ready(None) it is likely due to
the network terminating. When the network terminates due to the node
itself shutting down or due to a fatal error, there is no purpose in
continuing to run the authority discovery module.

* client/authority-discovery/src/lib: Apply suggestions

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

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Max Inden
2020-04-09 10:34:51 +02:00
committed by GitHub
parent 8576937f44
commit 5913969f8d
3 changed files with 59 additions and 14 deletions
@@ -19,6 +19,7 @@ use std::{iter::FromIterator, sync::{Arc, Mutex}};
use futures::channel::mpsc::channel;
use futures::executor::block_on;
use futures::future::poll_fn;
use futures::poll;
use libp2p::{kad, PeerId};
use sp_api::{ProvideRuntimeApi, ApiRef};
@@ -346,3 +347,36 @@ fn handle_dht_events_with_value_found_should_call_set_priority_group() {
let _ = block_on(poll_fn(f));
}
#[test]
fn terminate_when_event_stream_terminates() {
let (dht_event_tx, dht_event_rx) = channel(1000);
let network: Arc<TestNetwork> = Arc::new(Default::default());
let key_store = KeyStore::new();
let test_api = Arc::new(TestApi {
authorities: vec![],
});
let mut authority_discovery = AuthorityDiscovery::new(
test_api,
network.clone(),
vec![],
key_store,
dht_event_rx.boxed(),
None,
);
block_on(async {
assert_eq!(Poll::Pending, poll!(&mut authority_discovery));
// Simulate termination of the network through dropping the sender side of the dht event
// channel.
drop(dht_event_tx);
assert_eq!(
Poll::Ready(()), poll!(&mut authority_discovery),
"Expect the authority discovery module to terminate once the sending side of the dht \
event channel is terminated.",
);
});
}