@@ -28,7 +28,9 @@ use sp_core::{hash::H256, storage::ChildInfo, ChangesTrieConfiguration};
2828use sp_io:: hashing:: blake2_256;
2929use sp_runtime:: generic:: BlockId ;
3030use std:: sync:: Arc ;
31+ use serde_json:: value:: to_raw_value;
3132use substrate_test_runtime_client:: { prelude:: * , runtime} ;
33+ use crate :: testing:: timeout_secs;
3234
3335const STORAGE_KEY : & [ u8 ] = b"child" ;
3436
@@ -91,8 +93,8 @@ fn should_return_storage() {
9193 ) ;
9294}
9395
94- #[ test]
95- fn should_return_child_storage ( ) {
96+ #[ tokio :: test]
97+ async fn should_return_child_storage ( ) {
9698 let child_info = ChildInfo :: new_default ( STORAGE_KEY ) ;
9799 let client = Arc :: new (
98100 substrate_test_runtime_client:: TestClientBuilder :: new ( )
@@ -106,30 +108,30 @@ fn should_return_child_storage() {
106108 let key = StorageKey ( b"key" . to_vec ( ) ) ;
107109
108110 assert_matches ! (
109- executor :: block_on ( child. storage(
111+ child. storage(
110112 child_key. clone( ) ,
111113 key. clone( ) ,
112114 Some ( genesis_hash) . into( ) ,
113- ) ) ,
115+ ) . await ,
114116 Ok ( Some ( StorageData ( ref d) ) ) if d[ 0 ] == 42 && d. len( ) == 1
115117 ) ;
116118 assert_matches ! (
117- executor :: block_on ( child. storage_hash(
119+ child. storage_hash(
118120 child_key. clone( ) ,
119121 key. clone( ) ,
120122 Some ( genesis_hash) . into( ) ,
121- ) )
123+ ) . await
122124 . map( |x| x. is_some( ) ) ,
123125 Ok ( true )
124126 ) ;
125127 assert_matches ! (
126- executor :: block_on ( child. storage_size( child_key. clone( ) , key. clone( ) , None ) ) ,
128+ child. storage_size( child_key. clone( ) , key. clone( ) , None ) . await ,
127129 Ok ( Some ( 1 ) )
128130 ) ;
129131}
130132
131- #[ test]
132- fn should_call_contract ( ) {
133+ #[ tokio :: test]
134+ async fn should_call_contract ( ) {
133135 let client = Arc :: new ( substrate_test_runtime_client:: new ( ) ) ;
134136 let genesis_hash = client. genesis_hash ( ) ;
135137 let ( client, _child) =
@@ -138,93 +140,88 @@ fn should_call_contract() {
138140 use jsonrpsee:: types:: { Error , CallError } ;
139141
140142 assert_matches ! (
141- executor :: block_on ( client. call(
143+ client. call(
142144 "balanceOf" . into( ) ,
143145 Bytes ( vec![ 1 , 2 , 3 ] ) ,
144146 Some ( genesis_hash) . into( )
145- ) ) ,
147+ ) . await ,
146148 Err ( Error :: Call ( CallError :: Failed ( _) ) )
147149 )
148150}
149151
150- // #[test]
151- // fn should_notify_about_storage_changes() {
152- // let (subscriber, id, mut transport) = Subscriber::new_test("test");
153-
154- // {
155- // let mut client = Arc::new(substrate_test_runtime_client::new());
156- // let (api, _child) = new_full(
157- // client.clone(),
158- // SubscriptionTaskExecutor::new(TaskExecutor),
159- // DenyUnsafe::No,
160- // None,
161- // );
162-
163- // api.subscribe_storage(Default::default(), subscriber, None.into());
164-
165- // // assert id assigned
166- // assert!(matches!(executor::block_on(id), Ok(Ok(SubscriptionId::String(_)))));
167-
168- // let mut builder = client.new_block(Default::default()).unwrap();
169- // builder
170- // .push_transfer(runtime::Transfer {
171- // from: AccountKeyring::Alice.into(),
172- // to: AccountKeyring::Ferdie.into(),
173- // amount: 42,
174- // nonce: 0,
175- // })
176- // .unwrap();
177- // let block = builder.build().unwrap().block;
178- // executor::block_on(client.import(BlockOrigin::Own, block)).unwrap();
179- // }
180-
181- // // Check notification sent to transport
182- // executor::block_on((&mut transport).take(2).collect::<Vec<_>>());
183- // assert!(executor::block_on(transport.next()).is_none());
184- // }
185-
186- // #[test]
187- // fn should_send_initial_storage_changes_and_notifications() {
188- // let (subscriber, id, mut transport) = Subscriber::new_test("test");
189-
190- // {
191- // let mut client = Arc::new(substrate_test_runtime_client::new());
192- // let (api, _child) = new_full(
193- // client.clone(),
194- // SubscriptionTaskExecutor::new(TaskExecutor),
195- // DenyUnsafe::No,
196- // None,
197- // );
198-
199- // let alice_balance_key =
200- // blake2_256(&runtime::system::balance_of_key(AccountKeyring::Alice.into()));
201-
202- // api.subscribe_storage(
203- // Default::default(),
204- // subscriber,
205- // Some(vec![StorageKey(alice_balance_key.to_vec())]).into(),
206- // );
207-
208- // // assert id assigned
209- // assert!(matches!(executor::block_on(id), Ok(Ok(SubscriptionId::String(_)))));
210-
211- // let mut builder = client.new_block(Default::default()).unwrap();
212- // builder
213- // .push_transfer(runtime::Transfer {
214- // from: AccountKeyring::Alice.into(),
215- // to: AccountKeyring::Ferdie.into(),
216- // amount: 42,
217- // nonce: 0,
218- // })
219- // .unwrap();
220- // let block = builder.build().unwrap().block;
221- // executor::block_on(client.import(BlockOrigin::Own, block)).unwrap();
222- // }
223-
224- // // Check for the correct number of notifications
225- // executor::block_on((&mut transport).take(2).collect::<Vec<_>>());
226- // assert!(executor::block_on(transport.next()).is_none());
227- // }
152+ #[ tokio:: test]
153+ async fn should_notify_about_storage_changes ( ) {
154+ let mut client = Arc :: new ( substrate_test_runtime_client:: new ( ) ) ;
155+ let ( api, _child) = new_full (
156+ client. clone ( ) ,
157+ SubscriptionTaskExecutor :: new ( TaskExecutor ) ,
158+ DenyUnsafe :: No ,
159+ None ,
160+ ) ;
161+
162+ let api_rpc = api. into_rpc ( ) ;
163+ let ( _sub_id, mut sub_rx) = api_rpc. test_subscription ( "state_subscribeStorage" , None ) . await ;
164+
165+ // Cause a change:
166+ let mut builder = client. new_block ( Default :: default ( ) ) . unwrap ( ) ;
167+ builder
168+ . push_transfer ( runtime:: Transfer {
169+ from : AccountKeyring :: Alice . into ( ) ,
170+ to : AccountKeyring :: Ferdie . into ( ) ,
171+ amount : 42 ,
172+ nonce : 0 ,
173+ } )
174+ . unwrap ( ) ;
175+ let block = builder. build ( ) . unwrap ( ) . block ;
176+ client. import ( BlockOrigin :: Own , block) . await . unwrap ( ) ;
177+
178+ // We should get a message back on our subscription about the storage change:
179+ let msg = timeout_secs ( 5 , sub_rx. next ( ) ) . await ;
180+ assert_matches ! ( msg, Ok ( Some ( _) ) ) ;
181+
182+ // TODO (jsdw): The channel remains open here, so waiting for another message will time out.
183+ // Previously the channel returned None.
184+ assert_matches ! ( timeout_secs( 1 , sub_rx. next( ) ) . await , Err ( _) ) ;
185+ }
186+
187+ #[ tokio:: test]
188+ async fn should_send_initial_storage_changes_and_notifications ( ) {
189+ let mut client = Arc :: new ( substrate_test_runtime_client:: new ( ) ) ;
190+ let ( api, _child) = new_full (
191+ client. clone ( ) ,
192+ SubscriptionTaskExecutor :: new ( TaskExecutor ) ,
193+ DenyUnsafe :: No ,
194+ None ,
195+ ) ;
196+
197+ let alice_balance_key =
198+ blake2_256 ( & runtime:: system:: balance_of_key ( AccountKeyring :: Alice . into ( ) ) ) ;
199+
200+ let api_rpc = api. into_rpc ( ) ;
201+ let ( _sub_id, mut sub_rx) = api_rpc. test_subscription (
202+ "state_subscribeStorage" ,
203+ Some ( to_raw_value ( & [ StorageKey ( alice_balance_key. to_vec ( ) ) ] ) . unwrap ( ) ) ,
204+ ) . await ;
205+
206+ let mut builder = client. new_block ( Default :: default ( ) ) . unwrap ( ) ;
207+ builder
208+ . push_transfer ( runtime:: Transfer {
209+ from : AccountKeyring :: Alice . into ( ) ,
210+ to : AccountKeyring :: Ferdie . into ( ) ,
211+ amount : 42 ,
212+ nonce : 0 ,
213+ } )
214+ . unwrap ( ) ;
215+ let block = builder. build ( ) . unwrap ( ) . block ;
216+ client. import ( BlockOrigin :: Own , block) . await . unwrap ( ) ;
217+
218+ // Check for the correct number of notifications
219+ let msgs = timeout_secs ( 5 , ( & mut sub_rx) . take ( 2 ) . collect :: < Vec < _ > > ( ) ) . await ;
220+ assert_matches ! ( msgs, Ok ( _) ) ;
221+
222+ // No more messages to follow
223+ assert_matches ! ( timeout_secs( 1 , sub_rx. next( ) ) . await , Ok ( None ) ) ;
224+ }
228225
229226#[ test]
230227fn should_query_storage ( ) {
@@ -458,29 +455,26 @@ fn should_return_runtime_version() {
458455 assert_eq ! ( deserialized, runtime_version) ;
459456}
460457
461- // #[test]
462- // fn should_notify_on_runtime_version_initially() {
463- // let (subscriber, id, mut transport) = Subscriber::new_test("test");
464-
465- // {
466- // let client = Arc::new(substrate_test_runtime_client::new());
467- // let (api, _child) = new_full(
468- // client.clone(),
469- // SubscriptionTaskExecutor::new(TaskExecutor),
470- // DenyUnsafe::No,
471- // None,
472- // );
473-
474- // api.subscribe_runtime_version(Default::default(), subscriber);
475-
476- // // assert id assigned
477- // assert!(matches!(executor::block_on(id), Ok(Ok(SubscriptionId::String(_)))));
478- // }
479-
480- // // assert initial version sent.
481- // executor::block_on((&mut transport).take(1).collect::<Vec<_>>());
482- // assert!(executor::block_on(transport.next()).is_none());
483- // }
458+ #[ tokio:: test]
459+ async fn should_notify_on_runtime_version_initially ( ) {
460+ let client = Arc :: new ( substrate_test_runtime_client:: new ( ) ) ;
461+ let ( api, _child) = new_full (
462+ client. clone ( ) ,
463+ SubscriptionTaskExecutor :: new ( TaskExecutor ) ,
464+ DenyUnsafe :: No ,
465+ None ,
466+ ) ;
467+
468+ let api_rpc = api. into_rpc ( ) ;
469+ let ( _sub_id, mut sub_rx) = api_rpc. test_subscription ( "state_subscribeRuntimeVersion" , None ) . await ;
470+
471+ // assert initial version sent.
472+ assert_matches ! ( timeout_secs( 1 , sub_rx. next( ) ) . await , Ok ( Some ( _) ) ) ;
473+
474+ // TODO (jsdw): The channel remains open here, so waiting for another message will time out.
475+ // Previously the channel returned None.
476+ assert_matches ! ( timeout_secs( 1 , sub_rx. next( ) ) . await , Err ( _) ) ;
477+ }
484478
485479#[ test]
486480fn should_deserialize_storage_key ( ) {
0 commit comments