Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(binding/java): add behavior test framework #3129

Merged
merged 26 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
43cbec8
test(binding/java): add behavior test
G-XD Sep 18, 2023
c51bbab
test(binding/java): override append test for s3
G-XD Sep 18, 2023
a878d0d
ci(services/redis): add behavior test for binding java
G-XD Sep 18, 2023
e72e742
ci(services/s3): add behavior test for binding java
G-XD Sep 18, 2023
2a165e4
test(binding/java): del redis test
G-XD Sep 18, 2023
396c019
test(binding/java): add behavior test for memory & fs
G-XD Sep 18, 2023
418d5e6
style(binding/java): fix code style
G-XD Sep 18, 2023
d5d8dd8
test(binding/java): del cucumber test
G-XD Sep 18, 2023
60e2b4d
test(binding/java): refactor append test
G-XD Sep 18, 2023
bd95181
test(binding/java): refactor blocking test
G-XD Sep 19, 2023
f14775a
test(binding/java): skip tests if the env is not set
G-XD Sep 19, 2023
9afb744
ci(services/redis): setup redis server
G-XD Sep 19, 2023
8e621a6
chore(binding/java): reuse the .env.example
G-XD Sep 19, 2023
708d325
feat(binding/java): support info ops
G-XD Sep 21, 2023
5b022c0
test(binding/java): add operator info test
G-XD Sep 21, 2023
1fe237c
test(binding/java): refactor behavior test
G-XD Sep 21, 2023
c7d354f
Merge branch 'main' into behavior_test_java
G-XD Sep 21, 2023
accfdc8
test(binding/java): fix code style
G-XD Sep 21, 2023
30d4df8
test(binding/java): reuse the .env file
G-XD Sep 21, 2023
cefcc73
ci(binding/java): fix test command
G-XD Sep 21, 2023
55a495a
Merge branch 'main' into behavior_test_java
G-XD Sep 21, 2023
5d9bfd9
test(binding/java): init all services test that OPENDAL_<SERVICE>_ON …
G-XD Sep 21, 2023
f3019b9
ci(binding/java): del test
G-XD Sep 21, 2023
f6d8395
test(binding/java): skip test when no services turn on
G-XD Sep 21, 2023
658a675
doc(binding/java): update doc
G-XD Sep 21, 2023
aee4b5c
test(binding/java): del schema enum
G-XD Sep 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/service_test_redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,26 @@ jobs:
OPENDAL_REDIS_ENDPOINT: tcp://127.0.0.1:6379
OPENDAL_REDIS_ROOT: /
OPENDAL_REDIS_DB: 0

binding_java_redis:
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Rust toolchain
uses: ./.github/actions/setup

- name: Setup Redis Server
shell: bash
working-directory: fixtures/redis
run: docker-compose -f docker-compose-redis.yml up -d

- name: Test
shell: bash
working-directory: bindings/java
run: ./mvnw test -Dtest=org.apache.opendal.OperatorTest -DOPENDAL_TEST_SCHEMA=redis -Dcargo-build.features=services-redis
G-XD marked this conversation as resolved.
Show resolved Hide resolved
env:
OPENDAL_REDIS_TEST: on
OPENDAL_REDIS_ENDPOINT: tcp://127.0.0.1:6379
OPENDAL_REDIS_ROOT: /
OPENDAL_REDIS_DB: 0
21 changes: 21 additions & 0 deletions .github/workflows/service_test_s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,24 @@ jobs:
# This is the R2's limitation
# Refer to https://opendal.apache.org/docs/services/s3#compatible-services for more information
OPENDAL_S3_ENABLE_EXACT_BUF_WRITE: true

binding_java_s3:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Rust toolchain
uses: ./.github/actions/setup

- name: Test
shell: bash
working-directory: bindings/java
run: ./mvnw test -Dtest=org.apache.opendal.OperatorTest -DOPENDAL_TEST_SCHEMA=s3
env:
OPENDAL_S3_TEST: ${{ secrets.OPENDAL_S3_TEST }}
OPENDAL_S3_ROOT: ${{ secrets.OPENDAL_S3_ROOT }}
OPENDAL_S3_BUCKET: ${{ secrets.OPENDAL_S3_BUCKET }}
OPENDAL_S3_ENDPOINT: ${{ secrets.OPENDAL_S3_ENDPOINT }}
OPENDAL_S3_ACCESS_KEY_ID: ${{ secrets.OPENDAL_S3_ACCESS_KEY_ID }}
OPENDAL_S3_SECRET_ACCESS_KEY: ${{ secrets.OPENDAL_S3_SECRET_ACCESS_KEY }}
OPENDAL_S3_REGION: ap-northeast-1
40 changes: 37 additions & 3 deletions bindings/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,48 @@ You can use Maven to build both Rust dynamic lib and JAR files with one command
./mvnw clean package -DskipTests=true
```

## Setup Tests
G-XD marked this conversation as resolved.
Show resolved Hide resolved

Please copy `{project.rootdir}/.env.example` to `src/test/resources/.env` and change the values on need.
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved

Take `fs` for example, we need to enable bench on `fs` on `/tmp`.

```dotenv
OPENDAL_FS_TEST=false
OPENDAL_FS_ROOT=/path/to/dir
```

into

```dotenv
OPENDAL_FS_TEST=on
OPENDAL_FS_ROOT=/tmp
```

Notice: The default will skip all benches if the env is not set.

## Run tests

Currently, all tests are written in Java. It contains the Cucumber feature tests and other unit tests.
Currently, all tests are written in Java.

You must set the `OPENDAL_TEST_SCHEMA` parameter to specify the backends to be tested.

You can run all available backends tests with the following command:

```shell
./mvnw clean verify -DOPENDAL_TEST_SCHEMA=redis -Dcargo-build.features=services-redis
```

Test specific backend(such as `fs`).

```shell
./mvnw test -Dtest=org.apache.opendal.OperatorTest -DOPENDAL_TEST_SCHEMA=fs
```

You can run tests with the following command:
Test specific backend(such as `redis`).

```shell
./mvnw clean verify -Dcargo-build.features=services-redis
./mvnw test -Dtest=org.apache.opendal.OperatorTest -DOPENDAL_TEST_SCHEMA=redis -Dcargo-build.features=services-redis
```

> **Note:**
Expand Down
25 changes: 25 additions & 0 deletions bindings/java/src/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use opendal::Scheme;

use crate::get_global_runtime;
use crate::jmap_to_hashmap;
use crate::make_operator_info;
use crate::Result;

#[no_mangle]
Expand Down Expand Up @@ -159,3 +160,27 @@ fn intern_delete(env: &mut JNIEnv, op: &mut BlockingOperator, path: JString) ->
let path = env.get_string(&path)?;
Ok(op.delete(path.to_str()?)?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_info<'local>(
G-XD marked this conversation as resolved.
Show resolved Hide resolved
mut env: JNIEnv<'local>,
_: JClass,
op: *mut BlockingOperator,
) -> JObject<'local> {
intern_info(&mut env, &mut *op).unwrap_or_else(|e| {
e.throw(&mut env);
JObject::null()
})
}

fn intern_info<'local>(
env: &mut JNIEnv<'local>,
op: &mut BlockingOperator,
) -> Result<JObject<'local>> {
let info = op.info();

make_operator_info(env, info)
}
98 changes: 94 additions & 4 deletions bindings/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::ffi::c_void;

use jni::objects::JMap;
use crate::error::Error;
use jni::objects::JObject;
use jni::objects::JString;
use jni::objects::JValue;
use jni::objects::{JMap, JValue};
use jni::sys::jboolean;
use jni::sys::jint;
use jni::sys::jlong;
use jni::sys::JNI_VERSION_1_8;
use jni::JNIEnv;
use jni::JavaVM;
use once_cell::sync::OnceCell;
use opendal::raw::PresignedRequest;
use opendal::Capability;
use opendal::OperatorInfo;
use tokio::runtime::Builder;
use tokio::runtime::Runtime;

use crate::error::Error;

mod blocking_operator;
mod error;
mod metadata;
Expand Down Expand Up @@ -144,3 +146,91 @@ fn make_presigned_request<'a>(env: &mut JNIEnv<'a>, req: PresignedRequest) -> Re
)?;
Ok(result)
}

fn make_operator_info<'a>(env: &mut JNIEnv<'a>, info: OperatorInfo) -> Result<JObject<'a>> {
let operator_info_class = env.find_class("org/apache/opendal/OperatorInfo")?;

let schema = env.new_string(info.scheme().to_string())?;
let root = env.new_string(info.root().to_string())?;
let name = env.new_string(info.name().to_string())?;
let full_capability_obj = make_capability(env, info.full_capability())?;
let native_capability_obj = make_capability(env, info.native_capability())?;

let operator_info_obj = env
.new_object(
operator_info_class,
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/apache/opendal/Capability;Lorg/apache/opendal/Capability;)V",
&[
JValue::Object(&schema),
JValue::Object(&root),
JValue::Object(&name),
JValue::Object(&full_capability_obj),
JValue::Object(&native_capability_obj),
],
)?;

Ok(operator_info_obj)
}

fn make_capability<'a>(env: &mut JNIEnv<'a>, cap: Capability) -> Result<JObject<'a>> {
let capability_class = env.find_class("org/apache/opendal/Capability")?;

let write_multi_max_size = make_long(env, cap.write_multi_max_size)?;
let write_multi_min_size = make_long(env, cap.write_multi_min_size)?;
let write_multi_align_size = make_long(env, cap.write_multi_align_size)?;
let batch_max_operations = make_long(env, cap.batch_max_operations)?;

let capability = env.new_object(capability_class, "(ZZZZZZZZZZZZZZZZZZLjava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;ZZZZZZZZZZZZZZZLjava/lang/Long;Z)V",
&[
JValue::Bool(cap.stat as jboolean),
JValue::Bool(cap.stat_with_if_match as jboolean),
JValue::Bool(cap.stat_with_if_none_match as jboolean),
JValue::Bool(cap.read as jboolean),
JValue::Bool(cap.read_can_seek as jboolean),
JValue::Bool(cap.read_can_next as jboolean),
JValue::Bool(cap.read_with_range as jboolean),
JValue::Bool(cap.read_with_if_match as jboolean),
JValue::Bool(cap.read_with_if_none_match as jboolean),
JValue::Bool(cap.read_with_override_cache_control as jboolean),
JValue::Bool(cap.read_with_override_content_disposition as jboolean),
JValue::Bool(cap.read_with_override_content_type as jboolean),
JValue::Bool(cap.write as jboolean),
JValue::Bool(cap.write_can_multi as jboolean),
JValue::Bool(cap.write_can_append as jboolean),
JValue::Bool(cap.write_with_content_type as jboolean),
JValue::Bool(cap.write_with_content_disposition as jboolean),
JValue::Bool(cap.write_with_cache_control as jboolean),
JValue::Object(&write_multi_max_size),
JValue::Object(&write_multi_min_size),
JValue::Object(&write_multi_align_size),
JValue::Bool(cap.create_dir as jboolean),
JValue::Bool(cap.delete as jboolean),
JValue::Bool(cap.copy as jboolean),
JValue::Bool(cap.rename as jboolean),
JValue::Bool(cap.list as jboolean),
JValue::Bool(cap.list_with_limit as jboolean),
JValue::Bool(cap.list_with_start_after as jboolean),
JValue::Bool(cap.list_with_delimiter_slash as jboolean),
JValue::Bool(cap.list_without_delimiter as jboolean),
JValue::Bool(cap.presign as jboolean),
JValue::Bool(cap.presign_read as jboolean),
JValue::Bool(cap.presign_stat as jboolean),
JValue::Bool(cap.presign_write as jboolean),
JValue::Bool(cap.batch as jboolean),
JValue::Bool(cap.batch_delete as jboolean),
JValue::Object(&batch_max_operations),
JValue::Bool(cap.blocking as jboolean),
])?;

Ok(capability)
}

fn make_long<'a>(env: &mut JNIEnv<'a>, value: Option<usize>) -> Result<JObject<'a>> {
let long_class = env.find_class("java/lang/Long")?;

let result = match value {
Some(val) => env.new_object(long_class, "(J)V", &[JValue::Long(val as jlong)])?,
None => JObject::null(),
};
Ok(result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public Metadata stat(String path) {
return new Metadata(stat(nativeHandle, path));
}

public OperatorInfo info() {
return info(nativeHandle);
}

@Override
protected native void disposeInternal(long handle);

Expand All @@ -73,4 +77,6 @@ public Metadata stat(String path) {
private static native void delete(long nativeHandle, String path);

private static native long stat(long nativeHandle, String path);

private static native OperatorInfo info(long nativeHandle);
}
Loading