Skip to content

Commit

Permalink
added a test, fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
uberFoo committed Nov 25, 2023
1 parent d307b90 commit 5856725
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "puteketeke"
version = "0.0.1"
version = "0.0.3"
authors = ["Keith T. Star <[email protected]>"]
categories = ["asynchronous"]
categories = ["asynchronous", "concurrency"]
description = "An asynchronous runtime built on smol."
include = ["src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
keywords = ["executor", "async", "non-blocking", "futures"]
Expand Down
54 changes: 44 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,10 @@ impl<'a> UberExecutor<'a> {
// reference to the task which causes "data escapes the function" errors.
let worker_id = task.worker.id();
let guard = self.workers.lock();
let worker = guard.get(worker_id).unwrap();
if let Some(sender) = &self.sender {
let _ = sender.send(worker.clone());
if let Some(worker) = guard.get(worker_id) {
if let Some(sender) = &self.sender {
let _ = sender.send(worker.clone());
}
}
}

Expand Down Expand Up @@ -635,8 +636,11 @@ impl<'a> UberExecutor<'a> {
where
T: Send + std::fmt::Debug + 'a,
{
self.worker_at_index(key)
.map(|worker| AsyncTask::new(worker, future))
self.worker_at_index(key).map(|worker| {
let task = AsyncTask::new(worker, future);
tracing::debug!(target: "async", "create_task: {task:?}");
task
})
}

fn start(&self, thread_count: usize) {
Expand Down Expand Up @@ -907,20 +911,40 @@ mod tests {
}

#[test]
fn race() {
fn test_two_timer() {
color_backtrace::install();
let executor = Executor::new(1);

let inner_executor = executor.clone();
let task_0 = executor
.create_task(async move {
let now = Instant::now();
inner_executor.timer(Duration::from_millis(500)).await;
now.elapsed()
})
.unwrap();

let inner_executor = executor.clone();
let task_1 = executor
.create_task(async move {
for _ in 0..96 {
inner_executor.timer(Duration::from_millis(1)).await;
}
96
let now = Instant::now();
inner_executor.timer(Duration::from_millis(100)).await;
now.elapsed()
})
.unwrap();

executor.start_task(&task_0);
executor.start_task(&task_1);

let (a, b) = future::block_on(future::zip(task_0, task_1));
assert!(b < a);
}

#[test]
fn race() {
color_backtrace::install();
let executor = Executor::new(1);

let inner_executor = executor.clone();
let task_0 = executor
.create_task(async move {
Expand All @@ -931,6 +955,16 @@ mod tests {
})
.unwrap();

let inner_executor = executor.clone();
let task_1 = executor
.create_task(async move {
for _ in 0..96 {
inner_executor.timer(Duration::from_millis(1)).await;
}
96
})
.unwrap();

executor.start_task(&task_0);
executor.start_task(&task_1);

Expand Down

0 comments on commit 5856725

Please sign in to comment.