Skip to content

Commit e363189

Browse files
committed
Updated unit tests
1 parent 1614bfc commit e363189

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/ticked_async_executor.rs

+42-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ impl Default for TickedAsyncExecutor {
2222
}
2323
}
2424

25+
// TODO, Observer: Task spawn/wake/drop events
26+
// TODO, Task Identifier String
2527
impl TickedAsyncExecutor {
2628
pub fn new() -> Self {
2729
Self {
@@ -53,6 +55,10 @@ impl TickedAsyncExecutor {
5355
task
5456
}
5557

58+
pub fn num_tasks(&self) -> usize {
59+
self.num_spawned_tasks.load(Ordering::Relaxed)
60+
}
61+
5662
/// Run the woken tasks once
5763
///
5864
/// NOTE: Will not run tasks that are woken/scheduled immediately after `Runnable::run`
@@ -113,22 +119,47 @@ mod tests {
113119
})
114120
.detach();
115121

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-
124122
// A, B, C: Start
125123
executor.tick();
124+
assert_eq!(executor.num_tasks(), 2);
126125

127126
// A, B, C: End
128127
executor.tick();
128+
assert_eq!(executor.num_tasks(), 0);
129129
}
130130

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+
}
134165
}

0 commit comments

Comments
 (0)