Fix concurrency bug while testing

This commit is contained in:
2022-01-17 17:09:54 +01:00
parent 29b5f84d92
commit 00ead3f73d
9 changed files with 67 additions and 44 deletions

View File

@@ -1,12 +1,12 @@
pub mod test_helper;
use rstest::*;
use test_helper::app_address;
#[rstest]
#[tokio::test]
async fn health_check_works() {
let address = test_helper::spawn_app();
let client = reqwest::Client::new();
let response = client
.get(&format!("{}/ping", &address))
async fn health_check_works(app_address: &str) {
let response = reqwest::Client::new()
.get(&format!("{}/ping", &app_address))
.send()
.await
.expect("Failed to execute request.");

View File

@@ -3,15 +3,19 @@ pub mod test_helper;
use contextswitch_api::taskwarrior;
use contextswitch_types::Task;
use contextswitch_types::TaskDefinition;
use rstest::*;
use test_helper::app_address;
#[rstest]
#[tokio::test]
async fn list_tasks() {
let address = test_helper::spawn_app();
let task_id =
taskwarrior::add(vec!["test", "list_tasks", "contextswitch:'{\"test\": 1}'"]).unwrap();
async fn list_tasks(app_address: &str) {
let task_id = taskwarrior::add(vec!["test", "list_tasks", "contextswitch:'{\"test\": 1}'"])
.await
.unwrap();
println!("LIST TASKS ID: {}", task_id);
let tasks: Vec<Task> = reqwest::Client::new()
.get(&format!("{}/tasks?filter={}", &address, task_id))
.get(&format!("{}/tasks?filter={}", &app_address, task_id))
.send()
.await
.expect("Failed to execute request")
@@ -25,13 +29,11 @@ async fn list_tasks() {
assert_eq!(cs_metadata.test, 1);
}
#[rstest]
#[tokio::test]
async fn add_task() {
let address = test_helper::spawn_app();
println!("add_task address: {}", address);
async fn add_task(app_address: &str) {
let response: serde_json::Value = reqwest::Client::new()
.post(&format!("{}/tasks", &address))
.post(&format!("{}/tasks", &app_address))
.json(&TaskDefinition {
definition: "test add_task contextswitch:{\"test\":1}".to_string(),
})
@@ -41,8 +43,10 @@ async fn add_task() {
.json()
.await
.expect("Cannot parse JSON result");
println!("ADD RESPONSE: {:?}", response);
let new_task_id = response["id"].as_u64().unwrap();
let tasks = taskwarrior::export(vec![&new_task_id.to_string()]).unwrap();
println!("TASKS={:?}", tasks);
assert_eq!(tasks.len(), 1);
assert_eq!(tasks[0].id, new_task_id);

View File

@@ -1,42 +1,44 @@
use contextswitch_api::observability::{get_subscriber, init_subscriber};
use contextswitch_api::taskwarrior;
use mktemp::Temp;
use once_cell::sync::Lazy;
use rstest::*;
use std::fs;
use std::net::TcpListener;
use tracing::info;
static TRACING: Lazy<()> = Lazy::new(|| {
let subscriber = get_subscriber("info".to_string());
fn setup_tracing() {
info!("Setting up tracing");
let subscriber = get_subscriber("debug".to_string());
init_subscriber(subscriber);
});
}
static SERVER_ADDRESS: Lazy<String> = Lazy::new(|| {
fn setup_server() -> String {
info!("Setting up server");
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let server = contextswitch_api::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
});
}
static TASK_DATA_LOCATION: Lazy<String> = Lazy::new(|| {
fn setup_taskwarrior() -> String {
info!("Setting up TW");
let tmp_dir = Temp::new_dir().unwrap();
let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
tmp_dir.release();
task_data_location
});
pub fn spawn_app() -> String {
Lazy::force(&TRACING);
setup_tasks();
Lazy::force(&SERVER_ADDRESS).to_string()
}
pub fn setup_tasks() -> String {
Lazy::force(&TASK_DATA_LOCATION).to_string()
}
pub fn clear_tasks(task_data_location: String) {
fs::remove_dir_all(task_data_location).unwrap();
}
#[fixture]
#[once]
pub fn app_address() -> String {
setup_tracing();
setup_taskwarrior();
setup_server()
}