Skip to content

Commit

Permalink
Refactored shared commands in to a shared tests file.
Browse files Browse the repository at this point in the history
  • Loading branch information
SanHalacogluImproving committed Mar 12, 2024
1 parent cb3389c commit 0793cfb
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 152 deletions.
113 changes: 113 additions & 0 deletions java/integTest/src/test/java/glide/SharedClientTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide;

import static glide.TestUtilities.commonClientConfig;
import static glide.TestUtilities.commonClusterClientConfig;
import static glide.TestUtilities.getRandomString;
import static glide.api.BaseClient.OK;
import static org.junit.jupiter.api.Assertions.assertEquals;

import glide.api.BaseClient;
import glide.api.RedisClient;
import glide.api.RedisClusterClient;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.SneakyThrows;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

@Timeout(25)
public class SharedClientTests {

private static RedisClient standaloneClient = null;
private static RedisClusterClient clusterClient = null;

@Getter private static List<Arguments> clients;

@BeforeAll
@SneakyThrows
public static void init() {
standaloneClient = RedisClient.CreateClient(commonClientConfig().build()).get();
clusterClient =
RedisClusterClient.CreateClient(commonClusterClientConfig().requestTimeout(5000).build())
.get();

clients = List.of(Arguments.of(standaloneClient), Arguments.of(clusterClient));
}

@AfterAll
@SneakyThrows
public static void teardown() {
standaloneClient.close();
clusterClient.close();
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void send_and_receive_large_values(BaseClient client) {
int length = 1 << 16;
String key = getRandomString(length);
String value = getRandomString(length);

assertEquals(length, key.length());
assertEquals(length, value.length());
assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());
}

@SneakyThrows
@ParameterizedTest
@MethodSource("getClients")
public void send_and_receive_non_ascii_unicode(BaseClient client) {
String key = "foo";
String value = "שלום hello 汉字";

assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());
}

private static Stream<Arguments> clientAndDataSize() {
return Stream.of(
Arguments.of(standaloneClient, 100),
Arguments.of(standaloneClient, 1 << 16),
Arguments.of(clusterClient, 100),
Arguments.of(clusterClient, 1 << 16));
}

@ParameterizedTest
@MethodSource("clientAndDataSize")
public void client_can_handle_concurrent_workload(BaseClient client, int valueSize) {
ExecutorService executorService = Executors.newCachedThreadPool();
CompletableFuture[] futures = new CompletableFuture[100];

for (int i = 0; i < 100; i++) {
futures[i] =
CompletableFuture.runAsync(
() -> {
String key = getRandomString(valueSize);
String value = getRandomString(valueSize);
try {
assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
},
executorService);
}

CompletableFuture.allOf(futures).join();

executorService.shutdown();
}
}
17 changes: 17 additions & 0 deletions java/integTest/src/test/java/glide/TestUtilities.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide;

import static glide.TestConfiguration.CLUSTER_PORTS;
import static glide.TestConfiguration.STANDALONE_PORTS;
import static org.junit.jupiter.api.Assertions.fail;

import glide.api.models.ClusterValue;
import glide.api.models.configuration.NodeAddress;
import glide.api.models.configuration.RedisClientConfiguration;
import glide.api.models.configuration.RedisClusterClientConfiguration;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -59,4 +64,16 @@ public static Map<String, String> parseInfoResponseToMap(String serverInfo) {
(existingValue, newValue) -> newValue,
HashMap::new));
}

public static RedisClientConfiguration.RedisClientConfigurationBuilder<?, ?>
commonClientConfig() {
return RedisClientConfiguration.builder()
.address(NodeAddress.builder().port(STANDALONE_PORTS[0]).build());
}

public static RedisClusterClientConfiguration.RedisClusterClientConfigurationBuilder<?, ?>
commonClusterClientConfig() {
return RedisClusterClientConfiguration.builder()
.address(NodeAddress.builder().port(CLUSTER_PORTS[0]).build());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.cluster;

import static glide.TestConfiguration.CLUSTER_PORTS;
import static glide.TestUtilities.commonClusterClientConfig;
import static glide.TestUtilities.getRandomString;
import static glide.TestUtilities.parseInfoResponseToMap;
import static glide.api.BaseClient.OK;
Expand All @@ -15,31 +15,18 @@
import glide.api.RedisClusterClient;
import glide.api.models.ClusterValue;
import glide.api.models.commands.InfoOptions;
import glide.api.models.configuration.NodeAddress;
import glide.api.models.configuration.RedisClusterClientConfiguration;
import glide.api.models.configuration.RedisCredentials;
import glide.api.models.exceptions.ClosingException;
import glide.api.models.exceptions.RequestException;
import java.lang.module.ModuleDescriptor.Version;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Timeout(10)
public class ClusterClientTests {

private RedisClusterClientConfiguration.RedisClusterClientConfigurationBuilder<?, ?>
commonClusterClientConfig() {
return RedisClusterClientConfiguration.builder()
.address(NodeAddress.builder().port(CLUSTER_PORTS[0]).build());
}

@SneakyThrows
private Boolean check_if_server_version_gte(RedisClusterClient client, String minVersion) {
ClusterValue<String> infoClusterValue =
Expand Down Expand Up @@ -74,70 +61,6 @@ public void register_client_name_and_version() {
client.close();
}

@SneakyThrows
@Test
public void send_and_receive_large_values() {
RedisClusterClient client =
RedisClusterClient.CreateClient(commonClusterClientConfig().build()).get();

int length = 65536;
String key = getRandomString(length);
String value = getRandomString(length);

assertEquals(length, key.length());
assertEquals(length, value.length());
assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());

client.close();
}

@SneakyThrows
@Test
public void send_and_receive_non_ascii_unicode() {
RedisClusterClient client =
RedisClusterClient.CreateClient(commonClusterClientConfig().build()).get();

String key = "foo";
String value = "שלום hello 汉字";

assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());

client.close();
}

@SneakyThrows
@ParameterizedTest
@ValueSource(ints = {100, 65536})
public void client_can_handle_concurrent_workload(int valueSize) {
ExecutorService executorService = Executors.newCachedThreadPool();
RedisClusterClient client =
RedisClusterClient.CreateClient(commonClusterClientConfig().build()).get();
CompletableFuture[] futures = new CompletableFuture[100];

for (int i = 0; i < 100; i++) {
futures[i] =
CompletableFuture.runAsync(
() -> {
String key = getRandomString(valueSize);
String value = getRandomString(valueSize);
try {
assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
},
executorService);
}

CompletableFuture.allOf(futures).join();

client.close();
executorService.shutdown();
}

@SneakyThrows
@Test
public void can_connect_with_auth_requirepass() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.standalone;

import static glide.TestConfiguration.STANDALONE_PORTS;
import static glide.TestUtilities.commonClientConfig;
import static glide.TestUtilities.getRandomString;
import static glide.TestUtilities.parseInfoResponseToMap;
import static glide.api.BaseClient.OK;
Expand All @@ -14,30 +14,18 @@

import glide.api.RedisClient;
import glide.api.models.commands.InfoOptions;
import glide.api.models.configuration.NodeAddress;
import glide.api.models.configuration.RedisClientConfiguration;
import glide.api.models.configuration.RedisCredentials;
import glide.api.models.exceptions.ClosingException;
import glide.api.models.exceptions.RequestException;
import java.lang.module.ModuleDescriptor.Version;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Timeout(10)
public class StandaloneClientTests {

private RedisClientConfiguration.RedisClientConfigurationBuilder<?, ?> commonClientConfig() {
return RedisClientConfiguration.builder()
.address(NodeAddress.builder().port(STANDALONE_PORTS[0]).build());
}

@SneakyThrows
private Boolean check_if_server_version_gte(RedisClient client, String minVersion) {
String infoStr = client.info(InfoOptions.builder().section(SERVER).build()).get();
Expand All @@ -61,67 +49,6 @@ public void register_client_name_and_version() {
client.close();
}

@SneakyThrows
@Test
public void send_and_receive_large_values() {
RedisClient client = RedisClient.CreateClient(commonClientConfig().build()).get();

int length = 65536;
String key = getRandomString(length);
String value = getRandomString(length);

assertEquals(length, key.length());
assertEquals(length, value.length());
assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());

client.close();
}

@SneakyThrows
@Test
public void send_and_receive_non_ascii_unicode() {
RedisClient client = RedisClient.CreateClient(commonClientConfig().build()).get();

String key = "foo";
String value = "שלום hello 汉字";

assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());

client.close();
}

@SneakyThrows
@ParameterizedTest
@ValueSource(ints = {100, 65536})
public void client_can_handle_concurrent_workload(int valueSize) {
ExecutorService executorService = Executors.newCachedThreadPool();
RedisClient client = RedisClient.CreateClient(commonClientConfig().build()).get();
CompletableFuture[] futures = new CompletableFuture[100];

for (int i = 0; i < 100; i++) {
futures[i] =
CompletableFuture.runAsync(
() -> {
String key = getRandomString(valueSize);
String value = getRandomString(valueSize);
try {
assertEquals(OK, client.set(key, value).get());
assertEquals(value, client.get(key).get());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
},
executorService);
}

CompletableFuture.allOf(futures).join();

client.close();
executorService.shutdown();
}

@SneakyThrows
@Test
public void can_connect_with_auth_require_pass() {
Expand Down

0 comments on commit 0793cfb

Please sign in to comment.