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

Add support for RESP3 types (Part 1) #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 31 additions & 7 deletions java/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,34 @@ tasks.register('cleanProtobuf') {
}
}

tasks.register('cleanRust') {
doFirst {
project.delete(Paths.get(project.projectDir.path, '../target').toString())
}
}

tasks.register('buildRustRelease', Exec) {
commandLine 'cargo', 'build', '--release'
workingDir project.rootDir
environment 'CARGO_TERM_COLOR', 'always'
environment CARGO_TERM_COLOR: 'always'
}

tasks.register('buildRustReleaseStrip', Exec) {
commandLine 'cargo', 'build', '--release', '--strip'
workingDir project.rootDir
environment 'CARGO_TERM_COLOR', 'always'
environment CARGO_TERM_COLOR: 'always'
}

tasks.register('buildRust', Exec) {
commandLine 'cargo', 'build'
workingDir project.rootDir
environment 'CARGO_TERM_COLOR', 'always'
environment CARGO_TERM_COLOR: 'always'
}

tasks.register('buildRustFfi', Exec) {
commandLine 'cargo', 'build'
workingDir project.rootDir
environment CARGO_TERM_COLOR: 'always', CARGO_BUILD_RUSTFLAGS: '--cfg ffi_test'
}

tasks.register('buildWithRust') {
Expand All @@ -87,19 +99,31 @@ tasks.register('buildWithProto') {
finalizedBy 'build'
}

tasks.register('testFfi', Test) {
dependsOn 'buildRustFfi'
include "glide/ffi/FfiTest.class"
}

tasks.register('buildAll') {
dependsOn 'protobuf', 'buildRust'
dependsOn 'protobuf', 'buildRust', 'testFfi'
finalizedBy 'build'
}

compileJava.dependsOn('protobuf')
clean.dependsOn('cleanProtobuf')
compileJava.dependsOn('protobuf', 'buildRustRelease')
clean.dependsOn('cleanProtobuf', 'cleanRust')
test.dependsOn('buildRust')
testFfi.dependsOn('buildRust')

test {
exclude "glide/ffi/FfiTest.class"
}

tasks.withType(Test) {
testLogging {
exceptionFormat "full"
events "started", "skipped", "passed", "failed"
showStandardStreams true
}
jvmArgs "-Djava.library.path=${projectDir}/../target/release:${projectDir}/../target/debug"
jvmArgs "-Djava.library.path=${projectDir}/../target/debug"
}

169 changes: 169 additions & 0 deletions java/client/src/test/java/glide/ffi/FfiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package glide.ffi;

import static java.lang.Math.toIntExact;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import glide.ffi.resolvers.RedisValueResolver;
import java.util.HashMap;
import java.util.HashSet;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class FfiTest {

static {
System.loadLibrary("glide_rs");
}

public static native long createLeakedNil();

public static native long createLeakedSimpleString(String value);

public static native long createLeakedOkay();

public static native long createLeakedInt(long value);

public static native long createLeakedBulkString(byte[] value);

public static native long createLeakedLongArray(long[] value);

public static native long createLeakedMap();

public static native long createLeakedDouble(double value);

public static native long createLeakedBoolean(boolean value);

public static native long createLeakedVerbatimString(String value);

public static native long createLeakedLongSet(long[] value);

@Test
public void redisValueToJavaValue_Nil() {
long ptr = FfiTest.createLeakedNil();
Object nilValue = RedisValueResolver.valueFromPointer(ptr);
assertNull(nilValue);
}

@ParameterizedTest
@ValueSource(strings = {"hello", "cat", "dog"})
public void redisValueToJavaValue_SimpleString(String input) {
long ptr = FfiTest.createLeakedSimpleString(input);
Object simpleStringValue = RedisValueResolver.valueFromPointer(ptr);
assertEquals(input, simpleStringValue);
}

@Test
public void redisValueToJavaValue_Okay() {
long ptr = FfiTest.createLeakedOkay();
Object okayValue = RedisValueResolver.valueFromPointer(ptr);
assertEquals("OK", okayValue);
}

@ParameterizedTest
@ValueSource(longs = {0L, 100L, 774L})
public void redisValueToJavaValue_Int(Long input) {
long ptr = FfiTest.createLeakedInt(input);
Object longValue = RedisValueResolver.valueFromPointer(ptr);
assertEquals(input, longValue);
}

@Test
public void redisValueToJavaValue_CastToIntegerMax() {
long ptr = FfiTest.createLeakedInt(Integer.MAX_VALUE);
Object longValue = RedisValueResolver.valueFromPointer(ptr);
toIntExact((Long) longValue);
}

@Test
public void redisValueToJavaValue_CastToIntegerTooLarge() {
long ptr = FfiTest.createLeakedInt(((long) Integer.MAX_VALUE) + 1);
Object longValue = RedisValueResolver.valueFromPointer(ptr);
assertThrows(ArithmeticException.class, () -> toIntExact((Long) longValue));
}

@Test
public void redisValueToJavaValue_CastToIntegerMin() {
long ptr = FfiTest.createLeakedInt(Integer.MIN_VALUE);
Object longValue = RedisValueResolver.valueFromPointer(ptr);
toIntExact((Long) longValue);
}

@Test
public void redisValueToJavaValue_CastToIntegerTooSmall() {
long ptr = FfiTest.createLeakedInt(((long) Integer.MIN_VALUE) - 1);
Object longValue = RedisValueResolver.valueFromPointer(ptr);
assertThrows(ArithmeticException.class, () -> toIntExact((Long) longValue));
}

@ParameterizedTest
@ValueSource(strings = {"hello", "cat", "dog"})
public void redisValueToJavaValue_BulkString(String input) {
byte[] bulkString = input.getBytes();
long ptr = FfiTest.createLeakedBulkString(bulkString);
Object bulkStringValue = RedisValueResolver.valueFromPointer(ptr);
assertEquals(input, bulkStringValue);
}

@Test
public void redisValueToJavaValue_Array() {
long[] array = {1L, 2L, 3L};
long ptr = FfiTest.createLeakedLongArray(array);
Object longArrayValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(longArrayValue instanceof Object[]);
Object[] result = (Object[]) longArrayValue;
assertAll(
() -> assertEquals(1L, result[0]),
() -> assertEquals(2L, result[1]),
() -> assertEquals(3L, result[2]));
}

@Test
public void redisValueToJavaValue_Map() {
long ptr = FfiTest.createLeakedMap();
Object mapValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(mapValue instanceof HashMap);
HashMap<Object, Object> result = (HashMap<Object, Object>) mapValue;
assertAll(() -> assertEquals(2L, result.get(1L)), () -> assertEquals("hi", result.get(3L)));
}

@ParameterizedTest
@ValueSource(doubles = {1.0d, 25.2d, 103.5d})
public void redisValueToJavaValue_Double(Double input) {
long ptr = FfiTest.createLeakedDouble(input);
Object doubleValue = RedisValueResolver.valueFromPointer(ptr);
assertEquals(input, doubleValue);
}

@Test
public void redisValueToJavaValue_Boolean() {
long ptr = FfiTest.createLeakedBoolean(true);
Object booleanValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue((Boolean) booleanValue);
}

@ParameterizedTest
@ValueSource(strings = {"hello", "cat", "dog"})
public void redisValueToJavaValue_VerbatimString(String input) {
long ptr = FfiTest.createLeakedVerbatimString(input);
Object verbatimStringValue = RedisValueResolver.valueFromPointer(ptr);
assertEquals(input, verbatimStringValue);
}

@Test
public void redisValueToJavaValue_Set() {
long[] array = {1L, 2L, 2L};
long ptr = FfiTest.createLeakedLongSet(array);
Object longSetValue = RedisValueResolver.valueFromPointer(ptr);
assertTrue(longSetValue instanceof HashSet);
HashSet<Long> result = (HashSet<Long>) longSetValue;
assertAll(
() -> assertTrue(result.contains(1L)),
() -> assertTrue(result.contains(2L)),
() -> assertEquals(result.size(), 2));
}
}
138 changes: 138 additions & 0 deletions java/src/ffi_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use jni::{objects::JClass, sys::jlong, JNIEnv};
use redis::Value;

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedNil<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
) -> jlong {
let redis_value = Value::Nil;
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedSimpleString<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::objects::JString<'local>,
) -> jlong {
let value: String = env.get_string(&value).unwrap().into();
let redis_value = Value::SimpleString(value);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedOkay<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
) -> jlong {
let redis_value = Value::Okay;
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedInt<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
value: jlong,
) -> jlong {
let redis_value = Value::Int(value);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedBulkString<'local>(
env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::objects::JByteArray<'local>,
) -> jlong {
let value = env.convert_byte_array(&value).unwrap();
let value = value.into_iter().collect::<Vec<u8>>();
let redis_value = Value::BulkString(value);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedLongArray<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::objects::JLongArray<'local>,
) -> jlong {
use jni::objects::ReleaseMode;
let value = unsafe {
env.get_array_elements(&value, ReleaseMode::NoCopyBack)
.unwrap()
};
let array = value
.iter()
.map(|val| Value::Int(*val))
.collect::<Vec<Value>>();
let redis_value = Value::Array(array);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedMap<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
) -> jlong {
let mut map: Vec<(Value, Value)> = Vec::new();
map.push((Value::Int(1i64), Value::Int(2i64)));
map.push((Value::Int(3i64), Value::SimpleString("hi".to_string())));
let redis_value = Value::Map(map);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedDouble<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::sys::jdouble,
) -> jlong {
let redis_value = Value::Double(value.into());
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedBoolean<'local>(
_env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::sys::jboolean,
) -> jlong {
let redis_value = Value::Boolean(value != 0);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedVerbatimString<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::objects::JString<'local>,
) -> jlong {
use redis::VerbatimFormat;
let value: String = env.get_string(&value).unwrap().into();
let redis_value = Value::VerbatimString {
format: VerbatimFormat::Text,
text: value,
};
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}

#[no_mangle]
pub extern "system" fn Java_glide_ffi_FfiTest_createLeakedLongSet<'local>(
mut env: JNIEnv<'local>,
_class: JClass<'local>,
value: jni::objects::JLongArray<'local>,
) -> jlong {
use jni::objects::ReleaseMode;
let value = unsafe {
env.get_array_elements(&value, ReleaseMode::NoCopyBack)
.unwrap()
};
let set = value
.iter()
.map(|val| Value::Int(*val))
.collect::<Vec<Value>>();
let redis_value = Value::Set(set);
Box::leak(Box::new(redis_value)) as *mut Value as jlong
}
Loading