Debian Codegraph @

third_party/rust/tokio-util/tests/spawn_pinned.rs

raw
1 #![warn(rust_2018_idioms)]
2
3 use std::rc::Rc;
4 use std::sync::Arc;
5 use tokio_util::task;
6
7 /// Simple test of running a !Send future via spawn_pinned
8 #[tokio::test]
9 async fn can_spawn_not_send_future() {
10 let pool = task::LocalPoolHandle::new(1);
11
12 let output = pool
13 .spawn_pinned(|| {
14 // Rc is !Send + !Sync
15 let local_data = Rc::new("test");
16
17 // This future holds an Rc, so it is !Send
18 async move { local_data.to_string() }
19 })
20 .await
21 .unwrap();
22
23 assert_eq!(output, "test");
24 }
25
26 /// Dropping the join handle still lets the task execute
27 #[test]
28 fn can_drop_future_and_still_get_output() {
29 let pool = task::LocalPoolHandle::new(1);
30 let (sender, receiver) = std::sync::mpsc::channel();
31
32 let _ = pool.spawn_pinned(move || {
33 // Rc is !Send + !Sync
34 let local_data = Rc::new("test");
35
36 // This future holds an Rc, so it is !Send
37 async move {
38 let _ = sender.send(local_data.to_string());
39 }
40 });
41
42 assert_eq!(receiver.recv(), Ok("test".to_string()));
43 }
44
45 #[test]
46 #[should_panic(expected = "assertion failed: pool_size > 0")]
47 fn cannot_create_zero_sized_pool() {
48 let _pool = task::LocalPoolHandle::new(0);
49 }
50
51 /// We should be able to spawn multiple futures onto the pool at the same time.
52 #[tokio::test]
53 async fn can_spawn_multiple_futures() {
54 let pool = task::LocalPoolHandle::new(2);
55
56 let join_handle1 = pool.spawn_pinned(|| {
57 let local_data = Rc::new("test1");
58 async move { local_data.to_string() }
59 });
60 let join_handle2 = pool.spawn_pinned(|| {
61 let local_data = Rc::new("test2");
62 async move { local_data.to_string() }
63 });
64
65 assert_eq!(join_handle1.await.unwrap(), "test1");
66 assert_eq!(join_handle2.await.unwrap(), "test2");
67 }
68
69 /// A panic in the spawned task causes the join handle to return an error.
70 /// But, you can continue to spawn tasks.
71 #[tokio::test]
72 async fn task_panic_propagates() {
73 let pool = task::LocalPoolHandle::new(1);
74
75 let join_handle = pool.spawn_pinned(|| async {
76 panic!("Test panic");
77 });
78
79 let result = join_handle.await;
80 assert!(result.is_err());
81 let error = result.unwrap_err();
82 assert!(error.is_panic());
83 let panic_str: &str = *error.into_panic().downcast().unwrap();
84 assert_eq!(panic_str, "Test panic");
85
86 // Trying again with a "safe" task still works
87 let join_handle = pool.spawn_pinned(|| async { "test" });
88 let result = join_handle.await;
89 assert!(result.is_ok());
90 assert_eq!(result.unwrap(), "test");
91 }
92
93 /// A panic during task creation causes the join handle to return an error.
94 /// But, you can continue to spawn tasks.
95 #[tokio::test]
96 async fn callback_panic_does_not_kill_worker() {
97 let pool = task::LocalPoolHandle::new(1);
98
99 let join_handle = pool.spawn_pinned(|| {
100 panic!("Test panic");
101 #[allow(unreachable_code)]
102 async {}
103 });
104
105 let result = join_handle.await;
106 assert!(result.is_err());
107 let error = result.unwrap_err();
108 assert!(error.is_panic());
109 let panic_str: &str = *error.into_panic().downcast().unwrap();
110 assert_eq!(panic_str, "Test panic");
111
112 // Trying again with a "safe" callback works
113 let join_handle = pool.spawn_pinned(|| async { "test" });
114 let result = join_handle.await;
115 assert!(result.is_ok());
116 assert_eq!(result.unwrap(), "test");
117 }
118
119 /// Canceling the task via the returned join handle cancels the spawned task
120 /// (which has a different, internal join handle).
121 #[tokio::test]
122 async fn task_cancellation_propagates() {
123 let pool = task::LocalPoolHandle::new(1);
124 let notify_dropped = Arc::new(());
125 let weak_notify_dropped = Arc::downgrade(&notify_dropped);
126
127 let (start_sender, start_receiver) = tokio::sync::oneshot::channel();
128 let (drop_sender, drop_receiver) = tokio::sync::oneshot::channel::<()>();
129 let join_handle = pool.spawn_pinned(|| async move {
130 let _drop_sender = drop_sender;
131 // Move the Arc into the task
132 let _notify_dropped = notify_dropped;
133 let _ = start_sender.send(());
134
135 // Keep the task running until it gets aborted
136 futures::future::pending::<()>().await;
137 });
138
139 // Wait for the task to start
140 let _ = start_receiver.await;
141
142 join_handle.abort();
143
144 // Wait for the inner task to abort, dropping the sender.
145 // The top level join handle aborts quicker than the inner task (the abort
146 // needs to propagate and get processed on the worker thread), so we can't
147 // just await the top level join handle.
148 let _ = drop_receiver.await;
149
150 // Check that the Arc has been dropped. This verifies that the inner task
151 // was canceled as well.
152 assert!(weak_notify_dropped.upgrade().is_none());
153 }
154
155 /// Tasks should be given to the least burdened worker. When spawning two tasks
156 /// on a pool with two empty workers the tasks should be spawned on separate
157 /// workers.
158 #[tokio::test]
159 async fn tasks_are_balanced() {
160 let pool = task::LocalPoolHandle::new(2);
161
162 // Spawn a task so one thread has a task count of 1
163 let (start_sender1, start_receiver1) = tokio::sync::oneshot::channel();
164 let (end_sender1, end_receiver1) = tokio::sync::oneshot::channel();
165 let join_handle1 = pool.spawn_pinned(|| async move {
166 let _ = start_sender1.send(());
167 let _ = end_receiver1.await;
168 std::thread::current().id()
169 });
170
171 // Wait for the first task to start up
172 let _ = start_receiver1.await;
173
174 // This task should be spawned on the other thread
175 let (start_sender2, start_receiver2) = tokio::sync::oneshot::channel();
176 let join_handle2 = pool.spawn_pinned(|| async move {
177 let _ = start_sender2.send(());
178 std::thread::current().id()
179 });
180
181 // Wait for the second task to start up
182 let _ = start_receiver2.await;
183
184 // Allow the first task to end
185 let _ = end_sender1.send(());
186
187 let thread_id1 = join_handle1.await.unwrap();
188 let thread_id2 = join_handle2.await.unwrap();
189
190 // Since the first task was active when the second task spawned, they should
191 // be on separate workers/threads.
192 assert_ne!(thread_id1, thread_id2);
193 }