@@ -2,25 +2,20 @@ use crate::redis_helpers::*;
22use crate :: test_helpers:: * ;
33use futures:: Future ;
44use ilp_node:: InterledgerNode ;
5- use reqwest:: r#async :: Client ;
5+ use reqwest:: Client ;
66use secrecy:: SecretString ;
77use serde_json:: { self , json, Value } ;
88use std:: env;
9+ use std:: time:: Duration ;
910use tokio:: runtime:: Builder as RuntimeBuilder ;
1011use tokio_retry:: { strategy:: FibonacciBackoff , Retry } ;
1112use tracing:: error;
1213use tracing_subscriber;
1314
14- #[ test]
15- fn coincap ( ) {
16- install_tracing_subscriber ( ) ;
15+ #[ tokio:: test]
16+ async fn coincap ( ) {
1717 let context = TestContext :: new ( ) ;
1818
19- let mut runtime = RuntimeBuilder :: new ( )
20- . panic_handler ( |err| std:: panic:: resume_unwind ( err) )
21- . build ( )
22- . unwrap ( ) ;
23-
2419 let http_port = get_open_port ( None ) ;
2520
2621 let node: InterledgerNode = serde_json:: from_value ( json ! ( {
@@ -33,56 +28,38 @@ fn coincap() {
3328 "secret_seed" : random_secret( ) ,
3429 "route_broadcast_interval" : 200 ,
3530 "exchange_rate" : {
36- "poll_interval" : 60000 ,
31+ "poll_interval" : 100 ,
3732 "provider" : "coincap" ,
3833 } ,
3934 } ) )
4035 . unwrap ( ) ;
41- runtime . spawn ( node. serve ( ) ) ;
36+ node. serve ( ) . await . unwrap ( ) ;
4237
43- let get_rates = move || {
44- Client :: new ( )
45- . get ( & format ! ( "http://localhost:{}/rates" , http_port) )
46- . send ( )
47- . map_err ( |_| panic ! ( "Error getting rates" ) )
48- . and_then ( |mut res| res. json ( ) . map_err ( |_| panic ! ( "Error getting body" ) ) )
49- . and_then ( |body : Value | {
50- if let Value :: Object ( obj) = body {
51- if obj. is_empty ( ) {
52- error ! ( "Rates are empty" ) ;
53- return Err ( ( ) ) ;
54- }
55- assert_eq ! (
56- format!( "{}" , obj. get( "USD" ) . expect( "Should have USD rate" ) ) . as_str( ) ,
57- "1.0"
58- ) ;
59- assert ! ( obj. contains_key( "EUR" ) ) ;
60- assert ! ( obj. contains_key( "JPY" ) ) ;
61- assert ! ( obj. contains_key( "BTC" ) ) ;
62- assert ! ( obj. contains_key( "ETH" ) ) ;
63- assert ! ( obj. contains_key( "XRP" ) ) ;
64- } else {
65- panic ! ( "Not an object" ) ;
66- }
38+ // Wait a few seconds so our node can poll the API
39+ tokio:: time:: delay_for ( Duration :: from_millis ( 1000 ) ) . await ;
6740
68- Ok ( ( ) )
69- } )
70- } ;
71-
72- runtime
73- . block_on (
74- delay ( 1000 )
75- . map_err ( |_| panic ! ( "Something strange happened" ) )
76- . and_then ( move |_| {
77- Retry :: spawn ( FibonacciBackoff :: from_millis ( 1000 ) . take ( 5 ) , get_rates)
78- } ) ,
79- )
41+ let ret = Client :: new ( )
42+ . get ( & format ! ( "http://localhost:{}/rates" , http_port) )
43+ . send ( )
44+ . await
8045 . unwrap ( ) ;
46+ let txt = ret. text ( ) . await . unwrap ( ) ;
47+ let obj: Value = serde_json:: from_str ( & txt) . unwrap ( ) ;
48+
49+ assert_eq ! (
50+ format!( "{}" , obj. get( "USD" ) . expect( "Should have USD rate" ) ) . as_str( ) ,
51+ "1.0"
52+ ) ;
53+ assert ! ( obj. get( "EUR" ) . is_some( ) ) ;
54+ assert ! ( obj. get( "JPY" ) . is_some( ) ) ;
55+ assert ! ( obj. get( "BTC" ) . is_some( ) ) ;
56+ assert ! ( obj. get( "ETH" ) . is_some( ) ) ;
57+ assert ! ( obj. get( "XRP" ) . is_some( ) ) ;
8158}
8259
8360// TODO can we disable this with conditional compilation?
84- #[ test]
85- fn cryptocompare ( ) {
61+ #[ tokio :: test]
62+ async fn cryptocompare ( ) {
8663 tracing_subscriber:: fmt:: try_init ( ) . unwrap_or ( ( ) ) ;
8764 let context = TestContext :: new ( ) ;
8865
@@ -93,11 +70,6 @@ fn cryptocompare() {
9370 }
9471 let api_key = SecretString :: new ( api_key. unwrap ( ) ) ;
9572
96- let mut runtime = RuntimeBuilder :: new ( )
97- . panic_handler ( |err| std:: panic:: resume_unwind ( err) )
98- . build ( )
99- . unwrap ( ) ;
100-
10173 let http_port = get_open_port ( Some ( 3011 ) ) ;
10274
10375 let node: InterledgerNode = serde_json:: from_value ( json ! ( {
@@ -118,42 +90,21 @@ fn cryptocompare() {
11890 } ,
11991 } ) )
12092 . unwrap ( ) ;
121- runtime. spawn ( node. serve ( ) ) ;
122-
123- let get_rates = move || {
124- Client :: new ( )
125- . get ( & format ! ( "http://localhost:{}/rates" , http_port) )
126- . send ( )
127- . map_err ( |_| panic ! ( "Error getting rates" ) )
128- . and_then ( |mut res| res. json ( ) . map_err ( |_| panic ! ( "Error getting body" ) ) )
129- . and_then ( |body : Value | {
130- if let Value :: Object ( obj) = body {
131- if obj. is_empty ( ) {
132- error ! ( "Rates are empty" ) ;
133- return Err ( ( ) ) ;
134- }
135- assert_eq ! (
136- format!( "{}" , obj. get( "USD" ) . expect( "Should have USD rate" ) ) . as_str( ) ,
137- "1.0"
138- ) ;
139- assert ! ( obj. contains_key( "BTC" ) ) ;
140- assert ! ( obj. contains_key( "ETH" ) ) ;
141- assert ! ( obj. contains_key( "XRP" ) ) ;
142- } else {
143- panic ! ( "Not an object" ) ;
144- }
93+ node. serve ( ) . await . unwrap ( ) ;
14594
146- Ok ( ( ) )
147- } )
148- } ;
149-
150- runtime
151- . block_on (
152- delay ( 1000 )
153- . map_err ( |_| panic ! ( "Something strange happened" ) )
154- . and_then ( move |_| {
155- Retry :: spawn ( FibonacciBackoff :: from_millis ( 1000 ) . take ( 5 ) , get_rates)
156- } ) ,
157- )
95+ let ret = Client :: new ( )
96+ . get ( & format ! ( "http://localhost:{}/rates" , http_port) )
97+ . send ( )
98+ . await
15899 . unwrap ( ) ;
100+ let txt = ret. text ( ) . await . unwrap ( ) ;
101+ let obj: Value = serde_json:: from_str ( & txt) . unwrap ( ) ;
102+
103+ assert_eq ! (
104+ format!( "{}" , obj. get( "USD" ) . expect( "Should have USD rate" ) ) . as_str( ) ,
105+ "1.0"
106+ ) ;
107+ assert ! ( obj. get( "BTC" ) . is_some( ) ) ;
108+ assert ! ( obj. get( "ETH" ) . is_some( ) ) ;
109+ assert ! ( obj. get( "XRP" ) . is_some( ) ) ;
159110}
0 commit comments