@@ -6,6 +6,7 @@ use std::sync::{Arc, Mutex};
6
6
use std:: task:: Poll ;
7
7
use tokio:: macros:: support:: poll_fn;
8
8
9
+ use tokio:: runtime:: CounterPair ;
9
10
use tokio:: runtime:: Runtime ;
10
11
use tokio:: task:: consume_budget;
11
12
use tokio:: time:: { self , Duration } ;
@@ -104,36 +105,32 @@ fn active_tasks_count() {
104
105
fn active_tasks_count_pairs ( ) {
105
106
let rt = current_thread ( ) ;
106
107
let metrics = rt. metrics ( ) ;
107
- assert_eq ! ( 0 , metrics. start_tasks_count( ) ) ;
108
- assert_eq ! ( 0 , metrics. stop_tasks_count( ) ) ;
108
+ assert_eq ! ( CounterPair { inc: 0 , dec: 0 } , metrics. task_counts( ) ) ;
109
109
110
110
rt. block_on ( rt. spawn ( async move {
111
- assert_eq ! ( 1 , metrics. start_tasks_count( ) ) ;
112
- assert_eq ! ( 0 , metrics. stop_tasks_count( ) ) ;
111
+ assert_eq ! ( CounterPair { inc: 1 , dec: 0 } , metrics. task_counts( ) ) ;
113
112
} ) )
114
113
. unwrap ( ) ;
115
114
116
- assert_eq ! ( 1 , rt. metrics( ) . start_tasks_count( ) ) ;
117
- assert_eq ! ( 1 , rt. metrics( ) . stop_tasks_count( ) ) ;
115
+ assert_eq ! ( CounterPair { inc: 1 , dec: 1 } , rt. metrics( ) . task_counts( ) ) ;
118
116
119
117
let rt = threaded ( ) ;
120
118
let metrics = rt. metrics ( ) ;
121
- assert_eq ! ( 0 , metrics. start_tasks_count( ) ) ;
122
- assert_eq ! ( 0 , metrics. stop_tasks_count( ) ) ;
119
+ assert_eq ! ( CounterPair { inc: 0 , dec: 0 } , metrics. task_counts( ) ) ;
123
120
124
121
rt. block_on ( rt. spawn ( async move {
125
- assert_eq ! ( 1 , metrics. start_tasks_count( ) ) ;
126
- assert_eq ! ( 0 , metrics. stop_tasks_count( ) ) ;
122
+ assert_eq ! ( CounterPair { inc: 1 , dec: 0 } , metrics. task_counts( ) ) ;
127
123
} ) )
128
124
. unwrap ( ) ;
129
125
130
- // for some reason, sometimes the stop count doesn't get a chance to incremenet before we get here.
131
- // Only observed on single-cpu systems. Most likely the worker thread doesn't a chance to clean up
132
- // the spawned task yet. We yield to give it an opportunity.
133
- std:: thread:: yield_now ( ) ;
134
-
135
- assert_eq ! ( 1 , rt. metrics( ) . start_tasks_count( ) ) ;
136
- assert_eq ! ( 1 , rt. metrics( ) . stop_tasks_count( ) ) ;
126
+ for _ in 0 ..100 {
127
+ if rt. metrics ( ) . task_counts ( ) == ( CounterPair { inc : 1 , dec : 1 } ) {
128
+ return ;
129
+ }
130
+ // on single threaded machines (like in CI), we need to force the OS to run the runtime threads
131
+ std:: thread:: yield_now ( ) ;
132
+ }
133
+ panic ! ( "runtime didn't decrement active task gauge" )
137
134
}
138
135
139
136
#[ test]
0 commit comments