Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 947750c

Browse files
committed
asyncify a little more
1 parent 3116aa1 commit 947750c

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

client/rpc/src/state/tests.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ fn prefixed_storage_key() -> PrefixedStorageKey {
3939
child_info.prefixed_storage_key()
4040
}
4141

42-
#[test]
43-
fn should_return_storage() {
42+
#[tokio::test]
43+
async fn should_return_storage() {
4444
const KEY: &[u8] = b":mock";
4545
const VALUE: &[u8] = b"hello world";
4646
const CHILD_VALUE: &[u8] = b"hello world !";
@@ -63,29 +63,36 @@ fn should_return_storage() {
6363
let key = StorageKey(KEY.to_vec());
6464

6565
assert_eq!(
66-
executor::block_on(client.storage(key.clone(), Some(genesis_hash).into()))
66+
client.storage(key.clone(), Some(genesis_hash).into())
67+
.await
6768
.map(|x| x.map(|x| x.0.len()))
6869
.unwrap()
6970
.unwrap() as usize,
7071
VALUE.len(),
7172
);
7273
assert_matches!(
73-
executor::block_on(client.storage_hash(key.clone(), Some(genesis_hash).into()))
74+
client.storage_hash(key.clone(), Some(genesis_hash).into())
75+
.await
7476
.map(|x| x.is_some()),
7577
Ok(true)
7678
);
7779
assert_eq!(
78-
executor::block_on(client.storage_size(key.clone(), None)).unwrap().unwrap() as usize,
80+
client.storage_size(key.clone(), None)
81+
.await
82+
.unwrap()
83+
.unwrap() as usize,
7984
VALUE.len(),
8085
);
8186
assert_eq!(
82-
executor::block_on(client.storage_size(StorageKey(b":map".to_vec()), None))
87+
client.storage_size(StorageKey(b":map".to_vec()), None)
88+
.await
8389
.unwrap()
8490
.unwrap() as usize,
8591
2 + 3,
8692
);
8793
assert_eq!(
88-
executor::block_on(child.storage(prefixed_storage_key(), key, Some(genesis_hash).into()))
94+
child.storage(prefixed_storage_key(), key, Some(genesis_hash).into())
95+
.await
8996
.map(|x| x.map(|x| x.0.len()))
9097
.unwrap()
9198
.unwrap() as usize,
@@ -223,9 +230,9 @@ async fn should_send_initial_storage_changes_and_notifications() {
223230
assert_matches!(timeout_secs(1, sub_rx.next()).await, Ok(None));
224231
}
225232

226-
#[test]
227-
fn should_query_storage() {
228-
fn run_tests(mut client: Arc<TestClient>, has_changes_trie_config: bool) {
233+
#[tokio::test]
234+
async fn should_query_storage() {
235+
async fn run_tests(mut client: Arc<TestClient>, has_changes_trie_config: bool) {
229236
let (api, _child) = new_full(
230237
client.clone(),
231238
SubscriptionTaskExecutor::new(TaskExecutor),
@@ -290,7 +297,7 @@ fn should_query_storage() {
290297
let keys = (1..6).map(|k| StorageKey(vec![k])).collect::<Vec<_>>();
291298
let result = api.query_storage(keys.clone(), genesis_hash, Some(block1_hash).into());
292299

293-
assert_eq!(executor::block_on(result).unwrap(), expected);
300+
assert_eq!(result.await.unwrap(), expected);
294301

295302
// Query all changes
296303
let result = api.query_storage(keys.clone(), genesis_hash, None.into());
@@ -303,20 +310,20 @@ fn should_query_storage() {
303310
(StorageKey(vec![5]), Some(StorageData(vec![1]))),
304311
],
305312
});
306-
assert_eq!(executor::block_on(result).unwrap(), expected);
313+
assert_eq!(result.await.unwrap(), expected);
307314

308315
// Query changes up to block2.
309316
let result = api.query_storage(keys.clone(), genesis_hash, Some(block2_hash));
310317

311-
assert_eq!(executor::block_on(result).unwrap(), expected);
318+
assert_eq!(result.await.unwrap(), expected);
312319

313320
// Inverted range.
314321
let result = api.query_storage(keys.clone(), block1_hash, Some(genesis_hash));
315322

316323
use jsonrpsee::types::{ Error as RpcError, CallError as RpcCallError };
317324

318325
assert_eq!(
319-
executor::block_on(result).map_err(|e| e.to_string()),
326+
result.await.map_err(|e| e.to_string()),
320327
Err(RpcError::Call(RpcCallError::Failed(Error::InvalidBlockRange {
321328
from: format!("1 ({:?})", block1_hash),
322329
to: format!("0 ({:?})", genesis_hash),
@@ -332,7 +339,7 @@ fn should_query_storage() {
332339
let result = api.query_storage(keys.clone(), genesis_hash, Some(random_hash1));
333340

334341
assert_eq!(
335-
executor::block_on(result).map_err(|e| e.to_string()),
342+
result.await.map_err(|e| e.to_string()),
336343
Err(RpcError::Call(RpcCallError::Failed(Error::InvalidBlockRange {
337344
from: format!("{:?}", genesis_hash),
338345
to: format!("{:?}", Some(random_hash1)),
@@ -348,7 +355,7 @@ fn should_query_storage() {
348355
let result = api.query_storage(keys.clone(), random_hash1, Some(genesis_hash));
349356

350357
assert_eq!(
351-
executor::block_on(result).map_err(|e| e.to_string()),
358+
result.await.map_err(|e| e.to_string()),
352359
Err(RpcError::Call(RpcCallError::Failed(Error::InvalidBlockRange {
353360
from: format!("{:?}", random_hash1),
354361
to: format!("{:?}", Some(genesis_hash)),
@@ -364,7 +371,7 @@ fn should_query_storage() {
364371
let result = api.query_storage(keys.clone(), random_hash1, None);
365372

366373
assert_eq!(
367-
executor::block_on(result).map_err(|e| e.to_string()),
374+
result.await.map_err(|e| e.to_string()),
368375
Err(RpcError::Call(RpcCallError::Failed(Error::InvalidBlockRange {
369376
from: format!("{:?}", random_hash1),
370377
to: format!("{:?}", Some(block2_hash)), // Best block hash.
@@ -380,7 +387,7 @@ fn should_query_storage() {
380387
let result = api.query_storage(keys.clone(), random_hash1, Some(random_hash2));
381388

382389
assert_eq!(
383-
executor::block_on(result).map_err(|e| e.to_string()),
390+
result.await.map_err(|e| e.to_string()),
384391
Err(RpcError::Call(RpcCallError::Failed(Error::InvalidBlockRange {
385392
from: format!("{:?}", random_hash1), // First hash not found.
386393
to: format!("{:?}", Some(random_hash2)),
@@ -396,7 +403,7 @@ fn should_query_storage() {
396403
let result = api.query_storage_at(keys.clone(), Some(block1_hash));
397404

398405
assert_eq!(
399-
executor::block_on(result).unwrap(),
406+
result.await.unwrap(),
400407
vec![StorageChangeSet {
401408
block: block1_hash,
402409
changes: vec![
@@ -410,15 +417,15 @@ fn should_query_storage() {
410417
);
411418
}
412419

413-
run_tests(Arc::new(substrate_test_runtime_client::new()), false);
420+
run_tests(Arc::new(substrate_test_runtime_client::new()), false).await;
414421
run_tests(
415422
Arc::new(
416423
TestClientBuilder::new()
417424
.changes_trie_config(Some(ChangesTrieConfiguration::new(4, 2)))
418425
.build(),
419426
),
420427
true,
421-
);
428+
).await;
422429
}
423430

424431
#[test]
@@ -430,8 +437,8 @@ fn should_split_ranges() {
430437
assert_eq!(split_range(100, Some(99)), (0..99, Some(99..100)));
431438
}
432439

433-
#[test]
434-
fn should_return_runtime_version() {
440+
#[tokio::test]
441+
async fn should_return_runtime_version() {
435442
let client = Arc::new(substrate_test_runtime_client::new());
436443
let (api, _child) = new_full(
437444
client.clone(),
@@ -447,7 +454,7 @@ fn should_return_runtime_version() {
447454
[\"0xf78b278be53f454c\",2],[\"0xab3c0572291feb8b\",1],[\"0xbc9d89904f5b923f\",1]],\
448455
\"transactionVersion\":1}";
449456

450-
let runtime_version = executor::block_on(api.runtime_version(None.into())).unwrap();
457+
let runtime_version = api.runtime_version(None.into()).await.unwrap();
451458
let serialized = serde_json::to_string(&runtime_version).unwrap();
452459
assert_eq!(serialized, result);
453460

0 commit comments

Comments
 (0)