Made the jobs do something.

Also, stamped the advanced async file with the license.
This commit is contained in:
2025-06-28 18:00:11 -04:00
parent 4b16a23712
commit d16f8e333e

View File

@ -1,3 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// Sealed with Magistamp.
//! An example job execution framework.
//!
//! It includes:
@ -8,6 +11,7 @@
//! Jobs are run concurrently using Tokio, and results are gathered
//! with proper error handling.
use std::io::Write;
use std::pin::Pin;
use tokio::time::{sleep, Duration};
@ -159,7 +163,16 @@ impl Job for FileJob
where Self: Sync + 'a
{
Box::pin(async move {
Ok(String::from("Reading file"))
let mut write_target = std::env::temp_dir();
write_target.push("file_job");
write_target.set_extension("txt");
let mut file = std::fs::File::create(write_target)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
file.write_all(b"Jason is an awesome programmer!!")
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
Ok(String::from("File written"))
})
}
}
@ -229,6 +242,7 @@ impl Job for MathJob
where Self: Sync + 'a
{
Box::pin(async move {
let _ans = 10 * 4 + 2;
Ok(String::from("Math stuff"))
})
}
@ -251,7 +265,7 @@ mod tests
let result = job.run().await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Reading file");
assert_eq!(result.unwrap(), "File written");
}
#[tokio::test]
@ -299,7 +313,7 @@ mod tests
assert_eq!(results.len(), 3);
assert_eq!(results[0].0, "File Job");
assert_eq!(results[0].1.as_ref().unwrap(), "Reading file");
assert_eq!(results[0].1.as_ref().unwrap(), "File written");
assert_eq!(results[1].0, "Math Job");
assert_eq!(results[1].1.as_ref().unwrap(), "Math stuff");