Skip to content

Commit

Permalink
Add function to get current task and write unit test for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixANNERAUD committed Jan 26, 2024
1 parent 736b951 commit 9d55f20
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Task/Manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Task_internal_type {
}

/// A manager for tasks.
#[derive(Clone)]
pub struct Manager_type {
/// A map of all tasks managed by the manager.
Tasks: Arc<RwLock<HashMap<Task_identifier_type, Task_internal_type>>>,
Expand Down Expand Up @@ -146,6 +147,18 @@ impl Manager_type {

R
}

pub fn Get_current_task_identifier(&self) -> Result<Task_identifier_type, ()> {
let Tasks = self.Tasks.read().unwrap(); // Acquire lock

for (Task_identifier, Task) in Tasks.iter() {
if Task.Thread.Get_name().unwrap() == std::thread::current().name().unwrap() {
return Ok(*Task_identifier);
}
}

Err(())
}
}

#[cfg(test)]
Expand All @@ -154,7 +167,7 @@ mod tests {
use std::{thread, time::Duration};

#[test]
fn New_tasks() {
fn Test() {
let Manager = Manager_type::New();

Manager.New_root_task(None, || {
Expand Down
35 changes: 35 additions & 0 deletions src/Task/Task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,39 @@ impl<'a> Task_type<'a> {
pub fn Get_name(&self) -> Result<String, ()> {
self.Manager.Get_task_name(self.Identifier)
}

pub fn Get_identifier(&self) -> Task_identifier_type {
self.Identifier
}

pub fn Get_manager(&self) -> &'a Manager_type {
self.Manager
}

pub fn Get_current_task(Manager: &'a Manager_type) -> Result<Self, ()> {
let Current_task_identifier = Manager.Get_current_task_identifier()?;
Ok(Self::New(Current_task_identifier, Manager))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn Test() {
let Manager = Manager_type::New();

let Manager_copy = Manager.clone();

let Root_task = Manager.New_root_task(None, move || {
let Task = Task_type::Get_current_task(&Manager_copy).unwrap();

let Child_task = Task
.New_child_task("Child task", None, || {
std::thread::sleep(std::time::Duration::from_secs(1));
})
.unwrap();
});
}
}

0 comments on commit 9d55f20

Please sign in to comment.