soil-client 0.2.0

Soil client libraries
Documentation
// This file is part of Soil.

// Copyright (C) Soil contributors.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0

use super::*;

#[test]
fn positive_rx_receives_relevant_messages_and_terminates_upon_hub_drop() {
	block_on(async {
		let hub = TestHub::new(TK);
		assert_eq!(hub.subs_count(), 0);

		// No subscribers yet. That message is not supposed to get to anyone.
		hub.send(0);

		let mut rx_01 = hub.subscribe(SubsKey::new(), 100_000);
		assert_eq!(hub.subs_count(), 1);

		// That message is sent after subscription. Should be delivered into rx_01.
		hub.send(1);
		assert_eq!(Some(1), rx_01.next().await);

		// Hub is disposed. The rx_01 should be over after that.
		std::mem::drop(hub);

		assert!(rx_01.is_terminated());
		assert_eq!(None, rx_01.next().await);
	});
}

#[test]
fn positive_subs_count_is_correct_upon_drop_of_rxs() {
	block_on(async {
		let hub = TestHub::new(TK);
		assert_eq!(hub.subs_count(), 0);

		let rx_01 = hub.subscribe(SubsKey::new(), 100_000);
		assert_eq!(hub.subs_count(), 1);
		let rx_02 = hub.subscribe(SubsKey::new(), 100_000);
		assert_eq!(hub.subs_count(), 2);

		std::mem::drop(rx_01);
		assert_eq!(hub.subs_count(), 1);
		std::mem::drop(rx_02);
		assert_eq!(hub.subs_count(), 0);
	});
}

#[test]
fn positive_subs_count_is_correct_upon_drop_of_rxs_on_cloned_hubs() {
	block_on(async {
		let hub_01 = TestHub::new(TK);
		let hub_02 = hub_01.clone();
		assert_eq!(hub_01.subs_count(), 0);
		assert_eq!(hub_02.subs_count(), 0);

		let rx_01 = hub_02.subscribe(SubsKey::new(), 100_000);
		assert_eq!(hub_01.subs_count(), 1);
		assert_eq!(hub_02.subs_count(), 1);

		let rx_02 = hub_02.subscribe(SubsKey::new(), 100_000);
		assert_eq!(hub_01.subs_count(), 2);
		assert_eq!(hub_02.subs_count(), 2);

		std::mem::drop(rx_01);
		assert_eq!(hub_01.subs_count(), 1);
		assert_eq!(hub_02.subs_count(), 1);

		std::mem::drop(rx_02);
		assert_eq!(hub_01.subs_count(), 0);
		assert_eq!(hub_02.subs_count(), 0);
	});
}