Substrate test client crate & chain subscription test (#139)

* Test client used in RPC tests.

* Use test-client for network tests.

* Expose BlockOrigin and clean up the API.
This commit is contained in:
Tomasz Drwięga
2018-05-01 16:39:55 +02:00
committed by Robert Habermeier
parent 101549238e
commit f116f67382
16 changed files with 290 additions and 223 deletions
+38 -16
View File
@@ -14,42 +14,64 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use substrate_executor as executor;
use client;
use runtime_support::Hashable;
use super::*;
use jsonrpc_macros::pubsub;
use client::BlockOrigin;
use test_client::{self, TestClient};
#[test]
fn should_return_header() {
let test_genesis_block = block::Header {
parent_hash: 0.into(),
number: 0,
state_root: 0.into(),
extrinsics_root: Default::default(),
digest: Default::default(),
};
let core = ::tokio_core::reactor::Core::new().unwrap();
let remote = core.remote();
let client = Chain {
client: Arc::new(client::new_in_mem(executor::WasmExecutor, || (test_genesis_block.clone(), vec![])).unwrap()),
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
assert_matches!(
ChainApi::header(&client, test_genesis_block.blake2_256().into()),
client.header(client.client.genesis_hash()),
Ok(Some(ref x)) if x == &block::Header {
parent_hash: 0.into(),
number: 0,
state_root: 0.into(),
extrinsics_root: Default::default(),
state_root: "6da331d07a82d99f4debaafb0110a2e36244ed34162f9a7f6312a23fd52989ed".into(),
extrinsics_root: "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".into(),
digest: Default::default(),
}
);
assert_matches!(
ChainApi::header(&client, 5.into()),
client.header(5.into()),
Ok(None)
);
}
#[test]
fn should_notify_about_latest_block() {
let mut core = ::tokio_core::reactor::Core::new().unwrap();
let remote = core.remote();
let (subscriber, id, transport) = pubsub::Subscriber::new_test("test");
{
let api = Chain {
client: Arc::new(test_client::new()),
subscriptions: Subscriptions::new(remote),
};
api.subscribe_new_head(Default::default(), subscriber);
// assert id assigned
assert_eq!(core.run(id), Ok(Ok(SubscriptionId::Number(0))));
let builder = api.client.new_block().unwrap();
api.client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
}
// assert notification send to transport
let (notification, next) = core.run(transport.into_future()).unwrap();
assert_eq!(notification, Some(
r#"{"jsonrpc":"2.0","method":"test","params":{"result":{"digest":{"logs":[]},"extrinsicsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","number":1,"parentHash":"0x4c4ab196ed07bbd5b8c901ae5092d9d3990cbb4d44421af8e988af7d3c2a4226","stateRoot":"0x75b634da2a0d272e8a5145ab704406d3b50676c7739f977f2ccb2d0e5a0cdbd0"},"subscription":0}}"#.to_owned()
));
// no more notifications on this channel
assert_eq!(core.run(next.into_future()).unwrap().0, None);
}