Debian Codegraph @

third_party/rust/tokio/tests/rt_unstable_metrics.rs

raw
1 #![allow(unknown_lints, unexpected_cfgs)]
2 #![warn(rust_2018_idioms)]
3 #![cfg(all(
4 feature = "full",
5 tokio_unstable,
6 not(target_os = "wasi"),
7 target_has_atomic = "64"
8 ))]
9
10 use std::future::Future;
11 use std::sync::{Arc, Barrier, Mutex};
12 use std::task::Poll;
13 use std::thread;
14 use tokio::macros::support::poll_fn;
15
16 use tokio::runtime::Runtime;
17 use tokio::task::consume_budget;
18 use tokio::time::{self, Duration};
19
20 #[test]
21 fn num_workers() {
22 let rt = current_thread();
23 assert_eq!(1, rt.metrics().num_workers());
24
25 let rt = threaded();
26 assert_eq!(2, rt.metrics().num_workers());
27 }
28
29 #[test]
30 fn num_blocking_threads() {
31 let rt = current_thread();
32 assert_eq!(0, rt.metrics().num_blocking_threads());
33 let _ = rt.block_on(rt.spawn_blocking(move || {}));
34 assert_eq!(1, rt.metrics().num_blocking_threads());
35
36 let rt = threaded();
37 assert_eq!(0, rt.metrics().num_blocking_threads());
38 let _ = rt.block_on(rt.spawn_blocking(move || {}));
39 assert_eq!(1, rt.metrics().num_blocking_threads());
40 }
41
42 #[test]
43 fn num_idle_blocking_threads() {
44 let rt = current_thread();
45 assert_eq!(0, rt.metrics().num_idle_blocking_threads());
46 let _ = rt.block_on(rt.spawn_blocking(move || {}));
47 rt.block_on(async {
48 time::sleep(Duration::from_millis(5)).await;
49 });
50
51 // We need to wait until the blocking thread has become idle. Usually 5ms is
52 // enough for this to happen, but not always. When it isn't enough, sleep
53 // for another second. We don't always wait for a whole second since we want
54 // the test suite to finish quickly.
55 //
56 // Note that the timeout for idle threads to be killed is 10 seconds.
57 if 0 == rt.metrics().num_idle_blocking_threads() {
58 rt.block_on(async {
59 time::sleep(Duration::from_secs(1)).await;
60 });
61 }
62
63 assert_eq!(1, rt.metrics().num_idle_blocking_threads());
64 }
65
66 #[test]
67 fn blocking_queue_depth() {
68 let rt = tokio::runtime::Builder::new_current_thread()
69 .enable_all()
70 .max_blocking_threads(1)
71 .build()
72 .unwrap();
73
74 assert_eq!(0, rt.metrics().blocking_queue_depth());
75
76 let ready = Arc::new(Mutex::new(()));
77 let guard = ready.lock().unwrap();
78
79 let ready_cloned = ready.clone();
80 let wait_until_ready = move || {
81 let _unused = ready_cloned.lock().unwrap();
82 };
83
84 let h1 = rt.spawn_blocking(wait_until_ready.clone());
85 let h2 = rt.spawn_blocking(wait_until_ready);
86 assert!(rt.metrics().blocking_queue_depth() > 0);
87
88 drop(guard);
89
90 let _ = rt.block_on(h1);
91 let _ = rt.block_on(h2);
92
93 assert_eq!(0, rt.metrics().blocking_queue_depth());
94 }
95
96 #[test]
97 fn spawned_tasks_count() {
98 let rt = current_thread();
99 let metrics = rt.metrics();
100 assert_eq!(0, metrics.spawned_tasks_count());
101
102 rt.block_on(rt.spawn(async move {
103 assert_eq!(1, metrics.spawned_tasks_count());
104 }))
105 .unwrap();
106
107 assert_eq!(1, rt.metrics().spawned_tasks_count());
108
109 let rt = threaded();
110 let metrics = rt.metrics();
111 assert_eq!(0, metrics.spawned_tasks_count());
112
113 rt.block_on(rt.spawn(async move {
114 assert_eq!(1, metrics.spawned_tasks_count());
115 }))
116 .unwrap();
117
118 assert_eq!(1, rt.metrics().spawned_tasks_count());
119 }
120
121 #[test]
122 fn remote_schedule_count() {
123 use std::thread;
124
125 let rt = current_thread();
126 let handle = rt.handle().clone();
127 let task = thread::spawn(move || {
128 handle.spawn(async {
129 // DO nothing
130 })
131 })
132 .join()
133 .unwrap();
134
135 rt.block_on(task).unwrap();
136
137 assert_eq!(1, rt.metrics().remote_schedule_count());
138
139 let rt = threaded();
140 let handle = rt.handle().clone();
141 let task = thread::spawn(move || {
142 handle.spawn(async {
143 // DO nothing
144 })
145 })
146 .join()
147 .unwrap();
148
149 rt.block_on(task).unwrap();
150
151 assert_eq!(1, rt.metrics().remote_schedule_count());
152 }
153
154 #[test]
155 fn worker_thread_id_current_thread() {
156 let rt = current_thread();
157 let metrics = rt.metrics();
158
159 // Check that runtime is on this thread.
160 rt.block_on(async {});
161 assert_eq!(Some(thread::current().id()), metrics.worker_thread_id(0));
162
163 // Move runtime to another thread.
164 let thread_id = std::thread::scope(|scope| {
165 let join_handle = scope.spawn(|| {
166 rt.block_on(async {});
167 });
168 join_handle.thread().id()
169 });
170 assert_eq!(Some(thread_id), metrics.worker_thread_id(0));
171
172 // Move runtime back to this thread.
173 rt.block_on(async {});
174 assert_eq!(Some(thread::current().id()), metrics.worker_thread_id(0));
175 }
176
177 #[test]
178 fn worker_thread_id_threaded() {
179 let rt = threaded();
180 let metrics = rt.metrics();
181
182 rt.block_on(rt.spawn(async move {
183 // Check that we are running on a worker thread and determine
184 // the index of our worker.
185 let thread_id = std::thread::current().id();
186 let this_worker = (0..2)
187 .position(|w| metrics.worker_thread_id(w) == Some(thread_id))
188 .expect("task not running on any worker thread");
189
190 // Force worker to another thread.
191 let moved_thread_id = tokio::task::block_in_place(|| {
192 assert_eq!(thread_id, std::thread::current().id());
193
194 // Wait for worker to move to another thread.
195 for _ in 0..100 {
196 let new_id = metrics.worker_thread_id(this_worker).unwrap();
197 if thread_id != new_id {
198 return new_id;
199 }
200 std::thread::sleep(Duration::from_millis(100));
201 }
202
203 panic!("worker did not move to new thread");
204 });
205
206 // After blocking task worker either stays on new thread or
207 // is moved back to current thread.
208 assert!(
209 metrics.worker_thread_id(this_worker) == Some(moved_thread_id)
210 || metrics.worker_thread_id(this_worker) == Some(thread_id)
211 );
212 }))
213 .unwrap()
214 }
215
216 #[test]
217 fn worker_park_count() {
218 let rt = current_thread();
219 let metrics = rt.metrics();
220 rt.block_on(async {
221 time::sleep(Duration::from_millis(1)).await;
222 });
223 drop(rt);
224 assert!(1 <= metrics.worker_park_count(0));
225
226 let rt = threaded();
227 let metrics = rt.metrics();
228 rt.block_on(async {
229 time::sleep(Duration::from_millis(1)).await;
230 });
231 drop(rt);
232 assert!(1 <= metrics.worker_park_count(0));
233 assert!(1 <= metrics.worker_park_count(1));
234 }
235
236 #[test]
237 fn worker_park_unpark_count() {
238 let rt = current_thread();
239 let metrics = rt.metrics();
240 rt.block_on(rt.spawn(async {})).unwrap();
241 drop(rt);
242 assert!(2 <= metrics.worker_park_unpark_count(0));
243
244 let rt = threaded();
245 let metrics = rt.metrics();
246
247 // Wait for workers to be parked after runtime startup.
248 for _ in 0..100 {
249 if 1 <= metrics.worker_park_unpark_count(0) && 1 <= metrics.worker_park_unpark_count(1) {
250 break;
251 }
252 std::thread::sleep(std::time::Duration::from_millis(100));
253 }
254 assert_eq!(1, metrics.worker_park_unpark_count(0));
255 assert_eq!(1, metrics.worker_park_unpark_count(1));
256
257 // Spawn a task to unpark and then park a worker.
258 rt.block_on(rt.spawn(async {})).unwrap();
259 for _ in 0..100 {
260 if 3 <= metrics.worker_park_unpark_count(0) || 3 <= metrics.worker_park_unpark_count(1) {
261 break;
262 }
263 std::thread::sleep(std::time::Duration::from_millis(100));
264 }
265 assert!(3 <= metrics.worker_park_unpark_count(0) || 3 <= metrics.worker_park_unpark_count(1));
266
267 // Both threads unpark for runtime shutdown.
268 drop(rt);
269 assert_eq!(0, metrics.worker_park_unpark_count(0) % 2);
270 assert_eq!(0, metrics.worker_park_unpark_count(1) % 2);
271 assert!(4 <= metrics.worker_park_unpark_count(0) || 4 <= metrics.worker_park_unpark_count(1));
272 }
273
274 #[test]
275 fn worker_noop_count() {
276 // There isn't really a great way to generate no-op parks as they happen as
277 // false-positive events under concurrency.
278
279 let rt = current_thread();
280 let metrics = rt.metrics();
281 rt.block_on(async {
282 time::sleep(Duration::from_millis(1)).await;
283 });
284 drop(rt);
285 assert!(0 < metrics.worker_noop_count(0));
286
287 let rt = threaded();
288 let metrics = rt.metrics();
289 rt.block_on(async {
290 time::sleep(Duration::from_millis(1)).await;
291 });
292 drop(rt);
293 assert!(0 < metrics.worker_noop_count(0));
294 assert!(0 < metrics.worker_noop_count(1));
295 }
296
297 #[test]
298 #[ignore] // this test is flaky, see https://github.com/tokio-rs/tokio/issues/6470
299 fn worker_steal_count() {
300 // This metric only applies to the multi-threaded runtime.
301 //
302 // We use a blocking channel to backup one worker thread.
303 use std::sync::mpsc::channel;
304
305 let rt = threaded_no_lifo();
306 let metrics = rt.metrics();
307
308 rt.block_on(async {
309 let (tx, rx) = channel();
310
311 // Move to the runtime.
312 tokio::spawn(async move {
313 // Spawn the task that sends to the channel
314 //
315 // Since the lifo slot is disabled, this task is stealable.
316 tokio::spawn(async move {
317 tx.send(()).unwrap();
318 });
319
320 // Blocking receive on the channel.
321 rx.recv().unwrap();
322 })
323 .await
324 .unwrap();
325 });
326
327 drop(rt);
328
329 let n: u64 = (0..metrics.num_workers())
330 .map(|i| metrics.worker_steal_count(i))
331 .sum();
332
333 assert_eq!(1, n);
334 }
335
336 #[test]
337 fn worker_poll_count_and_time() {
338 const N: u64 = 5;
339
340 async fn task() {
341 // Sync sleep
342 std::thread::sleep(std::time::Duration::from_micros(10));
343 }
344
345 let rt = current_thread();
346 let metrics = rt.metrics();
347 rt.block_on(async {
348 for _ in 0..N {
349 tokio::spawn(task()).await.unwrap();
350 }
351 });
352 drop(rt);
353 assert_eq!(N, metrics.worker_poll_count(0));
354 // Not currently supported for current-thread runtime
355 assert_eq!(Duration::default(), metrics.worker_mean_poll_time(0));
356
357 // Does not populate the histogram
358 assert!(!metrics.poll_count_histogram_enabled());
359 for i in 0..10 {
360 assert_eq!(0, metrics.poll_count_histogram_bucket_count(0, i));
361 }
362
363 let rt = threaded();
364 let metrics = rt.metrics();
365 rt.block_on(async {
366 for _ in 0..N {
367 tokio::spawn(task()).await.unwrap();
368 }
369 });
370 drop(rt);
371 // Account for the `block_on` task
372 let n = (0..metrics.num_workers())
373 .map(|i| metrics.worker_poll_count(i))
374 .sum();
375
376 assert_eq!(N, n);
377
378 let n: Duration = (0..metrics.num_workers())
379 .map(|i| metrics.worker_mean_poll_time(i))
380 .sum();
381
382 assert!(n > Duration::default());
383
384 // Does not populate the histogram
385 assert!(!metrics.poll_count_histogram_enabled());
386 for n in 0..metrics.num_workers() {
387 for i in 0..10 {
388 assert_eq!(0, metrics.poll_count_histogram_bucket_count(n, i));
389 }
390 }
391 }
392
393 #[test]
394 fn worker_poll_count_histogram() {
395 const N: u64 = 5;
396
397 let rts = [
398 tokio::runtime::Builder::new_current_thread()
399 .enable_all()
400 .enable_metrics_poll_count_histogram()
401 .metrics_poll_count_histogram_scale(tokio::runtime::HistogramScale::Linear)
402 .metrics_poll_count_histogram_buckets(3)
403 .metrics_poll_count_histogram_resolution(Duration::from_millis(50))
404 .build()
405 .unwrap(),
406 tokio::runtime::Builder::new_multi_thread()
407 .worker_threads(2)
408 .enable_all()
409 .enable_metrics_poll_count_histogram()
410 .metrics_poll_count_histogram_scale(tokio::runtime::HistogramScale::Linear)
411 .metrics_poll_count_histogram_buckets(3)
412 .metrics_poll_count_histogram_resolution(Duration::from_millis(50))
413 .build()
414 .unwrap(),
415 ];
416
417 for rt in rts {
418 let metrics = rt.metrics();
419 rt.block_on(async {
420 for _ in 0..N {
421 tokio::spawn(async {}).await.unwrap();
422 }
423 });
424 drop(rt);
425
426 let num_workers = metrics.num_workers();
427 let num_buckets = metrics.poll_count_histogram_num_buckets();
428
429 assert!(metrics.poll_count_histogram_enabled());
430 assert_eq!(num_buckets, 3);
431
432 let n = (0..num_workers)
433 .flat_map(|i| (0..num_buckets).map(move |j| (i, j)))
434 .map(|(worker, bucket)| metrics.poll_count_histogram_bucket_count(worker, bucket))
435 .sum();
436 assert_eq!(N, n);
437 }
438 }
439
440 #[test]
441 fn worker_poll_count_histogram_range() {
442 let max = Duration::from_nanos(u64::MAX);
443
444 let rt = tokio::runtime::Builder::new_current_thread()
445 .enable_all()
446 .enable_metrics_poll_count_histogram()
447 .metrics_poll_count_histogram_scale(tokio::runtime::HistogramScale::Linear)
448 .metrics_poll_count_histogram_buckets(3)
449 .metrics_poll_count_histogram_resolution(us(50))
450 .build()
451 .unwrap();
452 let metrics = rt.metrics();
453
454 assert_eq!(metrics.poll_count_histogram_bucket_range(0), us(0)..us(50));
455 assert_eq!(
456 metrics.poll_count_histogram_bucket_range(1),
457 us(50)..us(100)
458 );
459 assert_eq!(metrics.poll_count_histogram_bucket_range(2), us(100)..max);
460
461 let rt = tokio::runtime::Builder::new_current_thread()
462 .enable_all()
463 .enable_metrics_poll_count_histogram()
464 .metrics_poll_count_histogram_scale(tokio::runtime::HistogramScale::Log)
465 .metrics_poll_count_histogram_buckets(3)
466 .metrics_poll_count_histogram_resolution(us(50))
467 .build()
468 .unwrap();
469 let metrics = rt.metrics();
470
471 let a = Duration::from_nanos(50000_u64.next_power_of_two());
472 let b = a * 2;
473
474 assert_eq!(metrics.poll_count_histogram_bucket_range(0), us(0)..a);
475 assert_eq!(metrics.poll_count_histogram_bucket_range(1), a..b);
476 assert_eq!(metrics.poll_count_histogram_bucket_range(2), b..max);
477 }
478
479 #[test]
480 fn worker_poll_count_histogram_disabled_without_explicit_enable() {
481 let rts = [
482 tokio::runtime::Builder::new_current_thread()
483 .enable_all()
484 .metrics_poll_count_histogram_scale(tokio::runtime::HistogramScale::Linear)
485 .metrics_poll_count_histogram_buckets(3)
486 .metrics_poll_count_histogram_resolution(Duration::from_millis(50))
487 .build()
488 .unwrap(),
489 tokio::runtime::Builder::new_multi_thread()
490 .worker_threads(2)
491 .enable_all()
492 .metrics_poll_count_histogram_scale(tokio::runtime::HistogramScale::Linear)
493 .metrics_poll_count_histogram_buckets(3)
494 .metrics_poll_count_histogram_resolution(Duration::from_millis(50))
495 .build()
496 .unwrap(),
497 ];
498
499 for rt in rts {
500 let metrics = rt.metrics();
501 assert!(!metrics.poll_count_histogram_enabled());
502 }
503 }
504
505 #[test]
506 fn worker_total_busy_duration() {
507 const N: usize = 5;
508
509 let zero = Duration::from_millis(0);
510
511 let rt = current_thread();
512 let metrics = rt.metrics();
513
514 rt.block_on(async {
515 for _ in 0..N {
516 tokio::spawn(async {
517 tokio::task::yield_now().await;
518 })
519 .await
520 .unwrap();
521 }
522 });
523
524 drop(rt);
525
526 assert!(zero < metrics.worker_total_busy_duration(0));
527
528 let rt = threaded();
529 let metrics = rt.metrics();
530
531 rt.block_on(async {
532 for _ in 0..N {
533 tokio::spawn(async {
534 tokio::task::yield_now().await;
535 })
536 .await
537 .unwrap();
538 }
539 });
540
541 drop(rt);
542
543 for i in 0..metrics.num_workers() {
544 assert!(zero < metrics.worker_total_busy_duration(i));
545 }
546 }
547
548 #[test]
549 fn worker_local_schedule_count() {
550 let rt = current_thread();
551 let metrics = rt.metrics();
552 rt.block_on(async {
553 tokio::spawn(async {}).await.unwrap();
554 });
555 drop(rt);
556
557 assert_eq!(1, metrics.worker_local_schedule_count(0));
558 assert_eq!(0, metrics.remote_schedule_count());
559
560 let rt = threaded();
561 let metrics = rt.metrics();
562 rt.block_on(async {
563 // Move to the runtime
564 tokio::spawn(async {
565 tokio::spawn(async {}).await.unwrap();
566 })
567 .await
568 .unwrap();
569 });
570 drop(rt);
571
572 let n: u64 = (0..metrics.num_workers())
573 .map(|i| metrics.worker_local_schedule_count(i))
574 .sum();
575
576 assert_eq!(2, n);
577 assert_eq!(1, metrics.remote_schedule_count());
578 }
579
580 #[test]
581 fn worker_overflow_count() {
582 // Only applies to the threaded worker
583 let rt = threaded();
584 let metrics = rt.metrics();
585 rt.block_on(async {
586 // Move to the runtime
587 tokio::spawn(async {
588 let (tx1, rx1) = std::sync::mpsc::channel();
589 let (tx2, rx2) = std::sync::mpsc::channel();
590
591 // First, we need to block the other worker until all tasks have
592 // been spawned.
593 //
594 // We spawn from outside the runtime to ensure that the other worker
595 // will pick it up:
596 // <https://github.com/tokio-rs/tokio/issues/4730>
597 tokio::task::spawn_blocking(|| {
598 tokio::spawn(async move {
599 tx1.send(()).unwrap();
600 rx2.recv().unwrap();
601 });
602 });
603
604 rx1.recv().unwrap();
605
606 // Spawn many tasks
607 for _ in 0..300 {
608 tokio::spawn(async {});
609 }
610
611 tx2.send(()).unwrap();
612 })
613 .await
614 .unwrap();
615 });
616 drop(rt);
617
618 let n: u64 = (0..metrics.num_workers())
619 .map(|i| metrics.worker_overflow_count(i))
620 .sum();
621
622 assert_eq!(1, n);
623 }
624
625 #[test]
626 fn injection_queue_depth_current_thread() {
627 use std::thread;
628
629 let rt = current_thread();
630 let handle = rt.handle().clone();
631 let metrics = rt.metrics();
632
633 thread::spawn(move || {
634 handle.spawn(async {});
635 })
636 .join()
637 .unwrap();
638
639 assert_eq!(1, metrics.injection_queue_depth());
640 }
641
642 #[test]
643 fn injection_queue_depth_multi_thread() {
644 let rt = threaded();
645 let metrics = rt.metrics();
646
647 let barrier1 = Arc::new(Barrier::new(3));
648 let barrier2 = Arc::new(Barrier::new(3));
649
650 // Spawn a task per runtime worker to block it.
651 for _ in 0..2 {
652 let barrier1 = barrier1.clone();
653 let barrier2 = barrier2.clone();
654 rt.spawn(async move {
655 barrier1.wait();
656 barrier2.wait();
657 });
658 }
659
660 barrier1.wait();
661
662 for i in 0..10 {
663 assert_eq!(i, metrics.injection_queue_depth());
664 rt.spawn(async {});
665 }
666
667 barrier2.wait();
668 }
669
670 #[test]
671 fn worker_local_queue_depth() {
672 const N: usize = 100;
673
674 let rt = current_thread();
675 let metrics = rt.metrics();
676 rt.block_on(async {
677 for _ in 0..N {
678 tokio::spawn(async {});
679 }
680
681 assert_eq!(N, metrics.worker_local_queue_depth(0));
682 });
683
684 let rt = threaded();
685 let metrics = rt.metrics();
686 rt.block_on(async move {
687 // Move to the runtime
688 tokio::spawn(async move {
689 let (tx1, rx1) = std::sync::mpsc::channel();
690 let (tx2, rx2) = std::sync::mpsc::channel();
691
692 // First, we need to block the other worker until all tasks have
693 // been spawned.
694 tokio::spawn(async move {
695 tx1.send(()).unwrap();
696 rx2.recv().unwrap();
697 });
698
699 // Bump the next-run spawn
700 tokio::spawn(async {});
701
702 rx1.recv().unwrap();
703
704 // Spawn some tasks
705 for _ in 0..100 {
706 tokio::spawn(async {});
707 }
708
709 let n: usize = (0..metrics.num_workers())
710 .map(|i| metrics.worker_local_queue_depth(i))
711 .sum();
712
713 assert_eq!(n, N);
714
715 tx2.send(()).unwrap();
716 })
717 .await
718 .unwrap();
719 });
720 }
721
722 #[test]
723 fn budget_exhaustion_yield() {
724 let rt = current_thread();
725 let metrics = rt.metrics();
726
727 assert_eq!(0, metrics.budget_forced_yield_count());
728
729 let mut did_yield = false;
730
731 // block on a task which consumes budget until it yields
732 rt.block_on(poll_fn(|cx| loop {
733 if did_yield {
734 return Poll::Ready(());
735 }
736
737 let fut = consume_budget();
738 tokio::pin!(fut);
739
740 if fut.poll(cx).is_pending() {
741 did_yield = true;
742 return Poll::Pending;
743 }
744 }));
745
746 assert_eq!(1, rt.metrics().budget_forced_yield_count());
747 }
748
749 #[test]
750 fn budget_exhaustion_yield_with_joins() {
751 let rt = current_thread();
752 let metrics = rt.metrics();
753
754 assert_eq!(0, metrics.budget_forced_yield_count());
755
756 let mut did_yield_1 = false;
757 let mut did_yield_2 = false;
758
759 // block on a task which consumes budget until it yields
760 rt.block_on(async {
761 tokio::join!(
762 poll_fn(|cx| loop {
763 if did_yield_1 {
764 return Poll::Ready(());
765 }
766
767 let fut = consume_budget();
768 tokio::pin!(fut);
769
770 if fut.poll(cx).is_pending() {
771 did_yield_1 = true;
772 return Poll::Pending;
773 }
774 }),
775 poll_fn(|cx| loop {
776 if did_yield_2 {
777 return Poll::Ready(());
778 }
779
780 let fut = consume_budget();
781 tokio::pin!(fut);
782
783 if fut.poll(cx).is_pending() {
784 did_yield_2 = true;
785 return Poll::Pending;
786 }
787 })
788 )
789 });
790
791 assert_eq!(1, rt.metrics().budget_forced_yield_count());
792 }
793
794 #[cfg(any(target_os = "linux", target_os = "macos"))]
795 #[test]
796 fn io_driver_fd_count() {
797 let rt = current_thread();
798 let metrics = rt.metrics();
799
800 assert_eq!(metrics.io_driver_fd_registered_count(), 0);
801
802 let stream = tokio::net::TcpStream::connect("google.com:80");
803 let stream = rt.block_on(async move { stream.await.unwrap() });
804
805 assert_eq!(metrics.io_driver_fd_registered_count(), 1);
806 assert_eq!(metrics.io_driver_fd_deregistered_count(), 0);
807
808 drop(stream);
809
810 assert_eq!(metrics.io_driver_fd_deregistered_count(), 1);
811 assert_eq!(metrics.io_driver_fd_registered_count(), 1);
812 }
813
814 #[cfg(any(target_os = "linux", target_os = "macos"))]
815 #[test]
816 fn io_driver_ready_count() {
817 let rt = current_thread();
818 let metrics = rt.metrics();
819
820 let stream = tokio::net::TcpStream::connect("google.com:80");
821 let _stream = rt.block_on(async move { stream.await.unwrap() });
822
823 assert_eq!(metrics.io_driver_ready_count(), 1);
824 }
825
826 fn current_thread() -> Runtime {
827 tokio::runtime::Builder::new_current_thread()
828 .enable_all()
829 .build()
830 .unwrap()
831 }
832
833 fn threaded() -> Runtime {
834 tokio::runtime::Builder::new_multi_thread()
835 .worker_threads(2)
836 .enable_all()
837 .build()
838 .unwrap()
839 }
840
841 fn threaded_no_lifo() -> Runtime {
842 tokio::runtime::Builder::new_multi_thread()
843 .worker_threads(2)
844 .disable_lifo_slot()
845 .enable_all()
846 .build()
847 .unwrap()
848 }
849
850 fn us(n: u64) -> Duration {
851 Duration::from_micros(n)
852 }