tests.rs 3.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.

use super::*;

use polkadot_node_subsystem_test_helpers::*;

use polkadot_node_subsystem::{
	messages::{AllMessages, AvailabilityStoreMessage},
23
24
	overseer::{dummy::DummySubsystem, gen::TimeoutExt, Subsystem},
	SubsystemError,
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
};

#[derive(Clone, Debug)]
struct BlackHoleInterceptor;

impl<Sender> MessageInterceptor<Sender> for BlackHoleInterceptor
where
	Sender: overseer::SubsystemSender<AllMessages>
		+ overseer::SubsystemSender<AvailabilityStoreMessage>
		+ Clone
		+ 'static,
{
	type Message = AvailabilityStoreMessage;
	fn intercept_incoming(
		&self,
		_sender: &mut Sender,
		msg: FromOverseer<Self::Message>,
	) -> Option<FromOverseer<Self::Message>> {
		match msg {
			FromOverseer::Communication { msg: _msg } => None,
			// to conclude the test cleanly
			sig => Some(sig),
		}
	}
}

51
52
53
54
55
56
57
58
59
60
61
62
63
#[derive(Clone, Debug)]
struct PassInterceptor;

impl<Sender> MessageInterceptor<Sender> for PassInterceptor
where
	Sender: overseer::SubsystemSender<AllMessages>
		+ overseer::SubsystemSender<AvailabilityStoreMessage>
		+ Clone
		+ 'static,
{
	type Message = AvailabilityStoreMessage;
}

64
65
66
67
async fn overseer_send<T: Into<AllMessages>>(overseer: &mut TestSubsystemContextHandle<T>, msg: T) {
	overseer.send(FromOverseer::Communication { msg }).await;
}

68
69
70
71
72
73
74
75
fn launch_harness<F, M, Sub, G>(test_gen: G)
where
	F: Future<Output = TestSubsystemContextHandle<M>> + Send,
	M: Into<AllMessages> + std::fmt::Debug + Send + 'static,
	AllMessages: From<M>,
	Sub: Subsystem<TestSubsystemContext<M, sp_core::testing::TaskExecutor>, SubsystemError>,
	G: Fn(TestSubsystemContextHandle<M>) -> (F, Sub),
{
76
	let pool = sp_core::testing::TaskExecutor::new();
77
	let (context, overseer) = make_subsystem_context(pool);
78

79
	let (test_fut, subsystem) = test_gen(overseer);
80
	let subsystem = async move {
81
		subsystem.start(context).future.await.unwrap();
82
83
84
85
86
87
88
89
90
91
92
93
94
	};
	futures::pin_mut!(test_fut);
	futures::pin_mut!(subsystem);

	futures::executor::block_on(futures::future::join(
		async move {
			let mut overseer = test_fut.await;
			overseer.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
		},
		subsystem,
	))
	.1;
}
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

#[test]
fn integrity_test_intercept() {
	launch_harness(|mut overseer| {
		let sub = DummySubsystem;

		let sub_intercepted = InterceptedSubsystem::new(sub, BlackHoleInterceptor);

		(
			async move {
				let (tx, rx) = futures::channel::oneshot::channel();
				overseer_send(
					&mut overseer,
					AvailabilityStoreMessage::QueryChunk(Default::default(), 0.into(), tx),
				)
				.await;
				let _ = rx.timeout(std::time::Duration::from_millis(100)).await.unwrap();
				overseer
			},
			sub_intercepted,
		)
	})
}

#[test]
fn integrity_test_pass() {
	launch_harness(|mut overseer| {
		let sub = DummySubsystem;

		let sub_intercepted = InterceptedSubsystem::new(sub, PassInterceptor);

		(
			async move {
				let (tx, rx) = futures::channel::oneshot::channel();
				overseer_send(
					&mut overseer,
					AvailabilityStoreMessage::QueryChunk(Default::default(), 0.into(), tx),
				)
				.await;
				let _ = rx.timeout(std::time::Duration::from_millis(100)).await.unwrap();
				overseer
			},
			sub_intercepted,
		)
	})
}