mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 11:28:02 +00:00
bc53b9a03a
* Change copyright year to 2023 from 2022 * Fix incorrect update of copyright year * Remove years from copy right header * Fix remaining files * Fix typo in a header and remove update-copyright.sh
124 lines
3.1 KiB
Rust
124 lines
3.1 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
use futures::StreamExt;
|
|
use tokio_test::block_on;
|
|
|
|
use super::*;
|
|
|
|
mod normal_operation;
|
|
mod panicking_registry;
|
|
|
|
const TK: &str = "a_tracing_key";
|
|
|
|
type Message = u64;
|
|
type TestHub = Hub<Message, Registry>;
|
|
type TestReceiver = Receiver<Message, Registry>;
|
|
|
|
#[derive(Default)]
|
|
struct Registry {
|
|
subscribers: HashMap<SeqID, SubsKey>,
|
|
}
|
|
|
|
struct SubsKey {
|
|
_receiver: Option<TestReceiver>,
|
|
panic: SubsKeyPanic,
|
|
}
|
|
|
|
impl SubsKey {
|
|
fn new() -> Self {
|
|
Self { _receiver: None, panic: SubsKeyPanic::None }
|
|
}
|
|
fn with_receiver(self, receiver: TestReceiver) -> Self {
|
|
Self { _receiver: Some(receiver), ..self }
|
|
}
|
|
fn with_panic(self, panic: SubsKeyPanic) -> Self {
|
|
Self { panic, ..self }
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum SubsKeyPanic {
|
|
None,
|
|
|
|
OnSubscribePanicBefore,
|
|
OnSubscribePanicAfter,
|
|
|
|
OnUnsubscribePanicBefore,
|
|
OnUnsubscribePanicAfter,
|
|
|
|
OnDispatchPanicBefore,
|
|
OnDispatchPanicAfter,
|
|
}
|
|
|
|
impl<M> Hub<M, Registry> {
|
|
fn subs_count(&self) -> usize {
|
|
self.map_registry_for_tests(|r| r.subscribers.len())
|
|
}
|
|
fn sink_count(&self) -> usize {
|
|
self.shared.lock().borrow().sinks.len()
|
|
}
|
|
}
|
|
|
|
impl Subscribe<SubsKey> for Registry {
|
|
fn subscribe(&mut self, subs_key: SubsKey, subs_id: SeqID) {
|
|
let sk_panic = subs_key.panic;
|
|
|
|
if sk_panic == SubsKeyPanic::OnSubscribePanicBefore {
|
|
panic!("on-subscribe-panic-before")
|
|
}
|
|
self.subscribers.insert(subs_id, subs_key);
|
|
if sk_panic == SubsKeyPanic::OnSubscribePanicAfter {
|
|
panic!("on-subscribe-panic-after")
|
|
}
|
|
}
|
|
}
|
|
impl Unsubscribe for Registry {
|
|
fn unsubscribe(&mut self, subs_id: SeqID) {
|
|
let sk_panic =
|
|
self.subscribers.get(&subs_id).map(|sk| sk.panic).unwrap_or(SubsKeyPanic::None);
|
|
|
|
if sk_panic == SubsKeyPanic::OnUnsubscribePanicBefore {
|
|
panic!("on-unsubscribe-panic-before")
|
|
}
|
|
self.subscribers.remove(&subs_id);
|
|
if sk_panic == SubsKeyPanic::OnUnsubscribePanicAfter {
|
|
panic!("on-unsubscribe-panic-after")
|
|
}
|
|
}
|
|
}
|
|
impl Dispatch<Message> for Registry {
|
|
type Item = Message;
|
|
type Ret = ();
|
|
|
|
fn dispatch<F>(&mut self, message: Message, mut dispatch: F) -> Self::Ret
|
|
where
|
|
F: FnMut(&SeqID, Self::Item),
|
|
{
|
|
self.subscribers.iter().for_each(|(id, subs_key)| {
|
|
if subs_key.panic == SubsKeyPanic::OnDispatchPanicBefore {
|
|
panic!("on-dispatch-panic-before")
|
|
}
|
|
dispatch(id, message);
|
|
if subs_key.panic == SubsKeyPanic::OnDispatchPanicAfter {
|
|
panic!("on-dispatch-panic-after")
|
|
}
|
|
});
|
|
}
|
|
}
|