@@ -22,6 +22,8 @@ impl Default for TickedAsyncExecutor {
22
22
}
23
23
}
24
24
25
+ // TODO, Observer: Task spawn/wake/drop events
26
+ // TODO, Task Identifier String
25
27
impl TickedAsyncExecutor {
26
28
pub fn new ( ) -> Self {
27
29
Self {
@@ -53,6 +55,10 @@ impl TickedAsyncExecutor {
53
55
task
54
56
}
55
57
58
+ pub fn num_tasks ( & self ) -> usize {
59
+ self . num_spawned_tasks . load ( Ordering :: Relaxed )
60
+ }
61
+
56
62
/// Run the woken tasks once
57
63
///
58
64
/// NOTE: Will not run tasks that are woken/scheduled immediately after `Runnable::run`
@@ -113,22 +119,47 @@ mod tests {
113
119
} )
114
120
. detach ( ) ;
115
121
116
- executor
117
- . spawn_local ( async move {
118
- println ! ( "C: Start" ) ;
119
- tokio:: task:: yield_now ( ) . await ;
120
- println ! ( "C: End" ) ;
121
- } )
122
- . detach ( ) ;
123
-
124
122
// A, B, C: Start
125
123
executor. tick ( ) ;
124
+ assert_eq ! ( executor. num_tasks( ) , 2 ) ;
126
125
127
126
// A, B, C: End
128
127
executor. tick ( ) ;
128
+ assert_eq ! ( executor. num_tasks( ) , 0 ) ;
129
129
}
130
130
131
- // TODO, Test Task cancellation
132
- // TODO, Test FallibleTasks
133
- // TODO, Test Edge cases
131
+ #[ test]
132
+ fn test_task_cancellation ( ) {
133
+ let executor = TickedAsyncExecutor :: new ( ) ;
134
+ let task1 = executor. spawn_local ( async move {
135
+ loop {
136
+ println ! ( "A: Start" ) ;
137
+ tokio:: task:: yield_now ( ) . await ;
138
+ println ! ( "A: End" ) ;
139
+ }
140
+ } ) ;
141
+
142
+ let task2 = executor. spawn_local ( async move {
143
+ loop {
144
+ println ! ( "B: Start" ) ;
145
+ tokio:: task:: yield_now ( ) . await ;
146
+ println ! ( "B: End" ) ;
147
+ }
148
+ } ) ;
149
+ assert_eq ! ( executor. num_tasks( ) , 2 ) ;
150
+ executor. tick ( ) ;
151
+
152
+ executor
153
+ . spawn_local ( async move {
154
+ task1. cancel ( ) . await ;
155
+ task2. cancel ( ) . await ;
156
+ } )
157
+ . detach ( ) ;
158
+ assert_eq ! ( executor. num_tasks( ) , 3 ) ;
159
+
160
+ // Since we have cancelled the tasks above, the loops should eventually end
161
+ while executor. num_tasks ( ) != 0 {
162
+ executor. tick ( ) ;
163
+ }
164
+ }
134
165
}
0 commit comments