Skip to content

Commit

Permalink
Small cleanup to tulsa tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleragreen committed Sep 14, 2023
1 parent 60df00a commit 1bee6f5
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions tulsa/tests/scheduler_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod tests {

use tulsa::{AsyncTask, Scheduler, SyncTask};

fn confirm_wc(file_path: &str, expected: i32) {
fn wc(file_path: &str) -> i32 {
let output = Command::new("wc")
.arg(file_path)
.output()
Expand All @@ -23,20 +23,21 @@ mod tests {
let output_trimmed = output_str.trim();
let output_vec: Vec<&str> = output_trimmed.split(" ").collect();
let output_num: i32 = output_vec[0].parse().unwrap();
assert_eq!(output_num, expected);
output_num
} else {
assert!(false);
panic!("failed to execute wc successful");
}
}

fn touch(file_name: &str) {
let mut file = OpenOptions::new()
OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_name)
.unwrap()
.write_all(b"")
.unwrap();
file.write_all(b"").unwrap();
}

fn create_async_task(id: usize, file_name: &'static str, millis: u64) -> AsyncTask {
Expand All @@ -54,6 +55,21 @@ mod tests {
})
}

fn create_sync_task(id: usize, file_name: &'static str, millis: u64) -> SyncTask {
let file = OpenOptions::new()
.write(true)
.append(true)
.open(file_name)
.unwrap();
let file_mutex = Mutex::new(file);

SyncTask::new(id, Duration::from_millis(millis), move || {
let mut file = file_mutex.lock().unwrap();
file.write_all("i".to_string().as_bytes()).unwrap();
file.write_all(b"\n").unwrap();
})
}

#[test]
fn async_scheduler_create() {
let (sender, receiver): (Sender<AsyncTask>, Receiver<AsyncTask>) = mpsc::channel();
Expand All @@ -67,7 +83,7 @@ mod tests {
let task = create_async_task(1, FILE_NAME, 100);

// File should still be empty before the send the task to the scheduler
confirm_wc(FILE_NAME, 0);
assert_eq!(wc(FILE_NAME), 0);
match sender.send(task) {
Ok(_) => assert!(true),
Err(e) => {
Expand All @@ -78,7 +94,7 @@ mod tests {

// Wait for the task to finish and then confirm the file has the correct contents
thread::sleep(Duration::from_millis(550));
confirm_wc(FILE_NAME, 6);
assert_eq!(wc(FILE_NAME), 6);
}

#[test]
Expand All @@ -95,7 +111,7 @@ mod tests {
let task = create_async_task(task_id, FILE_NAME, 100);

// File should still be empty before the send the task to the scheduler
confirm_wc(FILE_NAME, 0);
assert_eq!(wc(FILE_NAME), 0);
match sender.send(task) {
Ok(_) => assert!(true),
Err(e) => {
Expand All @@ -107,7 +123,7 @@ mod tests {
// Wait for the task to finish and then confirm the file has the correct contents
thread::sleep(Duration::from_millis(550));

confirm_wc(FILE_NAME, 6);
assert_eq!(wc(FILE_NAME), 6);
let task = AsyncTask::stop(task_id);
match sender.send(task) {
Ok(_) => assert!(true),
Expand All @@ -118,22 +134,7 @@ mod tests {
}

thread::sleep(Duration::from_millis(550));
confirm_wc(FILE_NAME, 6);
}

fn create_sync_task(id: usize, file_name: &'static str, millis: u64) -> SyncTask {
let file = OpenOptions::new()
.write(true)
.append(true)
.open(file_name)
.unwrap();
let file_mutex = Mutex::new(file);

SyncTask::new(id, Duration::from_millis(millis), move || {
let mut file = file_mutex.lock().unwrap();
file.write_all("i".to_string().as_bytes()).unwrap();
file.write_all(b"\n").unwrap();
})
assert_eq!(wc(FILE_NAME), 6);
}

#[test]
Expand All @@ -149,7 +150,7 @@ mod tests {
let task = create_sync_task(1, FILE_NAME, 100);

// File should still be empty before the send the task to the scheduler
confirm_wc(FILE_NAME, 0);
assert_eq!(wc(FILE_NAME), 0);
match sender.send(task) {
Ok(_) => assert!(true),
Err(e) => {
Expand All @@ -160,7 +161,7 @@ mod tests {

// Wait for the task to finish and then confirm the file has the correct contents
thread::sleep(Duration::from_millis(550));
confirm_wc(FILE_NAME, 6);
assert_eq!(wc(FILE_NAME), 6);
}

#[test]
Expand All @@ -177,7 +178,7 @@ mod tests {
let task = create_sync_task(task_id, FILE_NAME, 100);

// File should still be empty before the send the task to the scheduler
confirm_wc(FILE_NAME, 0);
assert_eq!(wc(FILE_NAME), 0);
match sender.send(task) {
Ok(_) => assert!(true),
Err(e) => {
Expand All @@ -189,7 +190,7 @@ mod tests {
// Wait for the task to finish and then confirm the file has the correct contents
thread::sleep(Duration::from_millis(550));

confirm_wc(FILE_NAME, 6);
assert_eq!(wc(FILE_NAME), 6);
let task = SyncTask::stop(task_id);
match sender.send(task) {
Ok(_) => assert!(true),
Expand All @@ -200,6 +201,6 @@ mod tests {
}

thread::sleep(Duration::from_millis(550));
confirm_wc(FILE_NAME, 6);
assert_eq!(wc(FILE_NAME), 6);
}
}

0 comments on commit 1bee6f5

Please sign in to comment.