use deloxide::{Mutex, thread};
use std::sync::Arc;
use std::time::Duration;
mod common;
use common::{DEADLOCK_TIMEOUT, expect_deadlock, start_detector};
#[test]
fn test_simple_two_thread_deadlock() {
let harness = start_detector();
let mutex_a = Arc::new(Mutex::new("Resource A"));
let mutex_b = Arc::new(Mutex::new("Resource B"));
let mutex_a_clone = Arc::clone(&mutex_a);
let mutex_b_clone = Arc::clone(&mutex_b);
let _thread1 = thread::spawn(move || {
let _guard_a = mutex_a.lock();
thread::sleep(Duration::from_millis(100));
let _guard_b = mutex_b.lock();
false
});
let _thread2 = thread::spawn(move || {
let _guard_b = mutex_b_clone.lock();
thread::sleep(Duration::from_millis(100));
let _guard_a = mutex_a_clone.lock();
false
});
let info = expect_deadlock(&harness, DEADLOCK_TIMEOUT);
assert_eq!(info.thread_cycle.len(), 2);
assert_eq!(info.thread_waiting_for_locks.len(), 2);
}