Skip to content

Commit 17707a7

Browse files
committed
Tests: Make the code of downloading maven artifacts thread safe. This does not impact parallelization or performance, as it will actually block the first time maven downloads artifacts.
1 parent 93608e4 commit 17707a7

File tree

9 files changed

+18
-48
lines changed

9 files changed

+18
-48
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "j4rs"
3-
version = "0.20.0"
3+
version = "0.21.0"
44
authors = ["aston <[email protected]>"]
55
description = "j4rs stands for 'Java for Rust' and allows effortless calls to Java code, from Rust"
66
keywords = ["java", "jni"]
-679 Bytes
Binary file not shown.

rust/src/api/instance.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,7 @@ impl<'a> ChainableInstance<'a> {
250250
#[cfg(test)]
251251
mod instance_unit_tests {
252252
use crate::*;
253-
254-
fn create_tests_jvm() -> errors::Result<Jvm> {
255-
let jvm: Jvm = JvmBuilder::new().build()?;
256-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
257-
Ok(jvm)
258-
}
253+
use crate::lib_unit_tests::create_tests_jvm;
259254

260255
#[test]
261256
fn is_null() -> errors::Result<()> {

rust/src/api/invocation_arg.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,9 @@ impl TryFrom<Result<InvocationArg, errors::J4RsError>> for InvocationArg {
699699
mod inv_arg_unit_tests {
700700
use serde::Deserialize;
701701

702-
use crate::{api, errors, JvmBuilder, MavenArtifact};
703-
704702
use super::*;
705-
706-
fn create_tests_jvm() -> errors::Result<Jvm> {
707-
let jvm: Jvm = JvmBuilder::new().build()?;
708-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
709-
Ok(jvm)
710-
}
703+
use crate::lib_unit_tests::create_tests_jvm;
704+
use crate::errors;
711705

712706
#[test]
713707
fn new_invocation_arg() -> errors::Result<()> {

rust/src/api/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,14 +2086,9 @@ impl<'a> ToString for JavaOpt<'a> {
20862086

20872087
#[cfg(test)]
20882088
mod api_unit_tests {
2089+
use crate::lib_unit_tests::create_tests_jvm;
20892090
use super::*;
20902091

2091-
fn create_tests_jvm() -> errors::Result<Jvm> {
2092-
let jvm: Jvm = JvmBuilder::new().build()?;
2093-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", j4rs_version()).as_str()))?;
2094-
Ok(jvm)
2095-
}
2096-
20972092
#[test]
20982093
fn jvm_builder() -> errors::Result<()> {
20992094
let res = create_tests_jvm();

rust/src/async_api/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,10 @@ impl Jvm {
147147
#[cfg(test)]
148148
mod api_unit_tests {
149149
use super::*;
150-
use crate::{api, JvmBuilder, MavenArtifact};
150+
use crate::lib_unit_tests::create_tests_jvm;
151151
use futures::Future;
152152
use tokio;
153153

154-
fn create_tests_jvm() -> errors::Result<Jvm> {
155-
let jvm: Jvm = JvmBuilder::new().build()?;
156-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
157-
Ok(jvm)
158-
}
159-
160154
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
161155
async fn invoke_async_success_w_tokio() -> errors::Result<()> {
162156
let s_test = "j4rs_rust";

rust/src/jfx.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,8 @@ fn fx_event_type_to_event_class_and_field(event_type: FxEventType) -> (String, S
571571

572572
#[cfg(test)]
573573
mod api_unit_tests {
574-
use crate::JvmBuilder;
575-
576574
use super::*;
577-
578-
fn create_tests_jvm() -> errors::Result<Jvm> {
579-
let jvm: Jvm = JvmBuilder::new().build()?;
580-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
581-
Ok(jvm)
582-
}
575+
use crate::lib_unit_tests::create_tests_jvm;
583576

584577
#[test]
585578
#[should_panic]

rust/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,23 @@ mod lib_unit_tests {
166166
use std::ptr::null_mut;
167167
use std::thread::JoinHandle;
168168
use std::{thread, time};
169-
169+
use std::sync::Mutex;
170170
use crate::api::{self, JavaClass};
171171
use crate::provisioning::JavaArtifact;
172172
use crate::{LocalJarArtifact, MavenArtifactRepo, MavenSettings, Null};
173-
174173
use super::utils::jassets_path;
175174
use super::{errors, InvocationArg, Jvm, JvmBuilder, MavenArtifact};
176175

177-
fn create_tests_jvm() -> errors::Result<Jvm> {
176+
lazy_static! {
177+
static ref SYNC_GUARD: Mutex<()> = Mutex::new(());
178+
}
179+
180+
pub(crate) fn create_tests_jvm() -> errors::Result<Jvm> {
178181
let jvm: Jvm = JvmBuilder::new().build()?;
179-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
182+
{
183+
let _guard = SYNC_GUARD.lock().unwrap();
184+
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
185+
}
180186
Ok(jvm)
181187
}
182188

rust/src/utils.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,8 @@ pub(crate) fn get_class_name(inv_arg: &InvocationArg) -> &str {
181181
mod utils_unit_tests {
182182
use std::convert::TryFrom;
183183

184-
use crate::{api, Jvm, JvmBuilder, MavenArtifact};
185-
186184
use super::*;
187-
188-
fn create_tests_jvm() -> errors::Result<Jvm> {
189-
let jvm: Jvm = JvmBuilder::new().build()?;
190-
jvm.deploy_artifact(&MavenArtifact::from(format!("io.github.astonbitecode:j4rs-testing:{}", api::j4rs_version()).as_str()))?;
191-
Ok(jvm)
192-
}
185+
use crate::lib_unit_tests::create_tests_jvm;
193186

194187
#[test]
195188
fn get_class_name_test() -> errors::Result<()> {

0 commit comments

Comments
 (0)