Skip to content

Commit 0aa6d8e

Browse files
Merge pull request #242 from Tcode-Motion/test-scriptmutex-unlock-7968661651229805973
🧪 Add tests for ScriptMutex::unlock
2 parents 4d5b4eb + 0f6b115 commit 0aa6d8e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎stdlib/src/sync.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,39 @@ mod tests {
125125
let guard = mutex.locked.lock().unwrap();
126126
assert_eq!(*guard, false);
127127
}
128+
129+
#[test]
130+
fn test_script_mutex_lock_unlock() {
131+
use std::sync::Arc;
132+
use std::thread;
133+
use std::time::Duration;
134+
135+
let mutex = Arc::new(ScriptMutex::new());
136+
137+
// Lock it in the main thread
138+
mutex.lock();
139+
assert_eq!(*mutex.locked.lock().unwrap(), true);
140+
141+
let mutex_clone = Arc::clone(&mutex);
142+
143+
// Spawn a thread that will block on lock()
144+
let handle = thread::spawn(move || {
145+
// This will block until the main thread calls unlock()
146+
mutex_clone.lock();
147+
assert_eq!(*mutex_clone.locked.lock().unwrap(), true);
148+
mutex_clone.unlock();
149+
});
150+
151+
// Give the spawned thread some time to block
152+
thread::sleep(Duration::from_millis(50));
153+
154+
// Unlock it in the main thread, this should wake up the spawned thread
155+
mutex.unlock();
156+
157+
// Wait for the spawned thread to finish
158+
handle.join().unwrap();
159+
160+
// Finally, it should be unlocked
161+
assert_eq!(*mutex.locked.lock().unwrap(), false);
162+
}
128163
}

0 commit comments

Comments
 (0)