@@ -2,25 +2,20 @@ use crate::redis_helpers::*;
2
2
use crate :: test_helpers:: * ;
3
3
use futures:: Future ;
4
4
use ilp_node:: InterledgerNode ;
5
- use reqwest:: r#async :: Client ;
5
+ use reqwest:: Client ;
6
6
use secrecy:: SecretString ;
7
7
use serde_json:: { self , json, Value } ;
8
8
use std:: env;
9
+ use std:: time:: Duration ;
9
10
use tokio:: runtime:: Builder as RuntimeBuilder ;
10
11
use tokio_retry:: { strategy:: FibonacciBackoff , Retry } ;
11
12
use tracing:: error;
12
13
use tracing_subscriber;
13
14
14
- #[ test]
15
- fn coincap ( ) {
16
- install_tracing_subscriber ( ) ;
15
+ #[ tokio:: test]
16
+ async fn coincap ( ) {
17
17
let context = TestContext :: new ( ) ;
18
18
19
- let mut runtime = RuntimeBuilder :: new ( )
20
- . panic_handler ( |err| std:: panic:: resume_unwind ( err) )
21
- . build ( )
22
- . unwrap ( ) ;
23
-
24
19
let http_port = get_open_port ( None ) ;
25
20
26
21
let node: InterledgerNode = serde_json:: from_value ( json ! ( {
@@ -33,56 +28,38 @@ fn coincap() {
33
28
"secret_seed" : random_secret( ) ,
34
29
"route_broadcast_interval" : 200 ,
35
30
"exchange_rate" : {
36
- "poll_interval" : 60000 ,
31
+ "poll_interval" : 100 ,
37
32
"provider" : "coincap" ,
38
33
} ,
39
34
} ) )
40
35
. unwrap ( ) ;
41
- runtime . spawn ( node. serve ( ) ) ;
36
+ node. serve ( ) . await . unwrap ( ) ;
42
37
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 ;
67
40
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
80
45
. 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( ) ) ;
81
58
}
82
59
83
60
// TODO can we disable this with conditional compilation?
84
- #[ test]
85
- fn cryptocompare ( ) {
61
+ #[ tokio :: test]
62
+ async fn cryptocompare ( ) {
86
63
tracing_subscriber:: fmt:: try_init ( ) . unwrap_or ( ( ) ) ;
87
64
let context = TestContext :: new ( ) ;
88
65
@@ -93,11 +70,6 @@ fn cryptocompare() {
93
70
}
94
71
let api_key = SecretString :: new ( api_key. unwrap ( ) ) ;
95
72
96
- let mut runtime = RuntimeBuilder :: new ( )
97
- . panic_handler ( |err| std:: panic:: resume_unwind ( err) )
98
- . build ( )
99
- . unwrap ( ) ;
100
-
101
73
let http_port = get_open_port ( Some ( 3011 ) ) ;
102
74
103
75
let node: InterledgerNode = serde_json:: from_value ( json ! ( {
@@ -118,42 +90,21 @@ fn cryptocompare() {
118
90
} ,
119
91
} ) )
120
92
. 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 ( ) ;
145
94
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
158
99
. 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( ) ) ;
159
110
}
0 commit comments