1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#![cfg(feature = "invocation")]
use std::{sync::Arc, thread::spawn};
use jni::{
errors::{Error, JniError},
Executor, JavaVM,
};
mod util;
use util::jvm;
/// Checks if nested attaches are working properly and threads detach themselves
/// on exit.
#[test]
fn nested_attach() {
let executor = Executor::new(jvm().clone());
assert_eq!(jvm().threads_attached(), 0);
let thread = spawn(|| {
assert_eq!(jvm().threads_attached(), 0);
check_nested_attach(jvm(), executor);
assert_eq!(jvm().threads_attached(), 1);
});
thread.join().unwrap();
assert_eq!(jvm().threads_attached(), 0);
}
/// Checks if nested `with_attached` calls does not detach the thread before the outer-most
/// call is finished.
fn check_nested_attach(vm: &Arc<JavaVM>, executor: Executor) {
check_detached(vm);
executor
.with_attached(|_| {
check_attached(vm);
executor.with_attached(|_| {
check_attached(vm);
Ok(())
})?;
check_attached(vm);
Ok(())
})
.unwrap();
}
fn check_attached(vm: &JavaVM) {
assert!(is_attached(vm));
}
fn check_detached(vm: &JavaVM) {
assert!(!is_attached(vm));
}
fn is_attached(vm: &JavaVM) -> bool {
vm.get_env()
.map(|_| true)
.or_else(|jni_err| match jni_err {
Error::JniCall(JniError::ThreadDetached) => Ok(false),
_ => Err(jni_err),
})
.expect("An unexpected JNI error occurred")
}
|