Skip to content

Commit

Permalink
Support of GlideString interface for echo command (#1668)
Browse files Browse the repository at this point in the history
* Add support of GlideString interface for echo command for both standalone and cluster clients.
In addition added handleGlideStringResponse to return a GlideString from a non nullable response.


---------

Co-authored-by: Yulazari <[email protected]>
  • Loading branch information
yulazariy and Yulazari authored Jun 27, 2024
1 parent 3e2d969 commit d5cb19d
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 0 deletions.
4 changes: 4 additions & 0 deletions java/client/src/main/java/glide/api/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ protected GlideString handleGlideStringOrNullResponse(Response response) throws
return handleRedisResponse(GlideString.class, EnumSet.of(ResponseFlags.IS_NULLABLE), response);
}

protected GlideString handleGlideStringResponse(Response response) throws RedisException {
return handleRedisResponse(GlideString.class, EnumSet.noneOf(ResponseFlags.class), response);
}

protected Boolean handleBooleanResponse(Response response) throws RedisException {
return handleRedisResponse(Boolean.class, EnumSet.noneOf(ResponseFlags.class), response);
}
Expand Down
6 changes: 6 additions & 0 deletions java/client/src/main/java/glide/api/RedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public CompletableFuture<String> echo(@NonNull String message) {
Echo, new String[] {message}, this::handleStringResponse);
}

@Override
public CompletableFuture<GlideString> echo(@NonNull GlideString message) {
return commandManager.submitNewCommand(
Echo, new GlideString[] {message}, this::handleGlideStringResponse);
}

@Override
public CompletableFuture<String[]> time() {
return commandManager.submitNewCommand(
Expand Down
19 changes: 19 additions & 0 deletions java/client/src/main/java/glide/api/RedisClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ public CompletableFuture<String> echo(@NonNull String message) {
Echo, new String[] {message}, this::handleStringResponse);
}

@Override
public CompletableFuture<GlideString> echo(@NonNull GlideString message) {
return commandManager.submitNewCommand(
Echo, new GlideString[] {message}, this::handleGlideStringResponse);
}

@Override
public CompletableFuture<ClusterValue<String>> echo(
@NonNull String message, @NonNull Route route) {
Expand All @@ -298,6 +304,19 @@ public CompletableFuture<ClusterValue<String>> echo(
: ClusterValue.ofMultiValue(handleMapResponse(response)));
}

@Override
public CompletableFuture<ClusterValue<GlideString>> echo(
@NonNull GlideString message, @NonNull Route route) {
return commandManager.submitNewCommand(
Echo,
new GlideString[] {message},
route,
response ->
route instanceof SingleNodeRoute
? ClusterValue.ofSingleValue(handleGlideStringResponse(response))
: ClusterValue.ofMultiValueBinary(handleBinaryStringMapResponse(response)));
}

@Override
public CompletableFuture<String[]> time() {
return commandManager.submitNewCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package glide.api.commands;

import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.configuration.RequestRoutingConfiguration.Route;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -157,6 +158,21 @@ public interface ConnectionManagementClusterCommands {
*/
CompletableFuture<String> echo(String message);

/**
* Echoes the provided <code>message</code> back.<br>
* The command will be routed a random node.
*
* @see <a href="https://redis.io/commands/echo/">redis.io</a> for details.
* @param message The message to be echoed back.
* @return The provided <code>message</code>.
* @example
* <pre>{@code
* GlideString payload = client.echo(gs("GLIDE")).get();
* assert payload.equals(gs("GLIDE"));
* }</pre>
*/
CompletableFuture<GlideString> echo(GlideString message);

/**
* Echoes the provided <code>message</code> back.
*
Expand All @@ -179,4 +195,27 @@ public interface ConnectionManagementClusterCommands {
* }</pre>
*/
CompletableFuture<ClusterValue<String>> echo(String message, Route route);

/**
* Echoes the provided <code>message</code> back.
*
* @see <a href="https://redis.io/commands/echo/">redis.io</a> for details.
* @param message The message to be echoed back.
* @param route Specifies the routing configuration for the command. The client will route the
* command to the nodes defined by <code>route</code>.
* @return The provided <code>message</code>.
* @example
* <pre>{@code
* // Command sent to a single random node via RANDOM route, expecting a SingleValue result.
* GlideString message = client.echo(gs("GLIDE"), RANDOM).get().getSingleValue();
* assert message.equals(gs("GLIDE"));
*
* // Command sent to all nodes via ALL_NODES route, expecting a MultiValue result.
* Map<String, GlideString> msgForAllNodes = client.echo(gs("GLIDE"), ALL_NODES).get().getMultiValue();
* for(var msgPerNode : msgForAllNodes.entrySet()) {
* assert msgPerNode.equals(gs("GLIDE"));
* }
* }</pre>
*/
CompletableFuture<ClusterValue<GlideString>> echo(GlideString message, Route route);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */
package glide.api.commands;

import glide.api.models.GlideString;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -77,4 +78,18 @@ public interface ConnectionManagementCommands {
* }</pre>
*/
CompletableFuture<String> echo(String message);

/**
* Echoes the provided <code>message</code> back.
*
* @see <a href="https://redis.io/commands/echo/>redis.io</a> for details.
* @param message The message to be echoed back.
* @return The provided <code>message</code>.
* @example
* <pre>{@code
* GlideString payload = client.echo(gs("GLIDE")).get();
* assert payload.equals(gs("GLIDE"));
* }</pre>
*/
CompletableFuture<GlideString> echo(GlideString message);
}
22 changes: 22 additions & 0 deletions java/client/src/test/java/glide/api/RedisClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,28 @@ public void echo_returns_success() {
assertEquals(message, echo);
}

@SneakyThrows
@Test
public void echo_binary_returns_success() {
// setup
GlideString message = gs("GLIDE FOR REDIS");
GlideString[] arguments = new GlideString[] {message};
CompletableFuture<GlideString> testResponse = new CompletableFuture<>();
testResponse.complete(message);

// match on protobuf request
when(commandManager.<GlideString>submitNewCommand(eq(Echo), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<GlideString> response = service.echo(message);
GlideString echo = response.get();

// verify
assertEquals(testResponse, response);
assertEquals(message, echo);
}

@SneakyThrows
@Test
public void ping_returns_success() {
Expand Down
48 changes: 48 additions & 0 deletions java/client/src/test/java/glide/api/RedisClusterClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import static glide.api.models.GlideString.gs;
import static glide.api.models.commands.FlushMode.ASYNC;
import static glide.api.models.commands.FlushMode.SYNC;
import static glide.api.models.commands.SortBaseOptions.ALPHA_COMMAND_STRING;
import static glide.api.models.commands.SortBaseOptions.LIMIT_COMMAND_STRING;
import static glide.api.models.commands.SortBaseOptions.OrderBy.DESC;
import static glide.api.models.commands.SortBaseOptions.STORE_COMMAND_STRING;
import static glide.api.models.commands.SortOptions.ALPHA_COMMAND_STRING;
import static glide.api.models.commands.SortOptions.LIMIT_COMMAND_STRING;
import static glide.api.models.commands.SortOptions.STORE_COMMAND_STRING;
Expand Down Expand Up @@ -353,6 +356,28 @@ public void echo_returns_success() {
assertEquals(message, echo);
}

@SneakyThrows
@Test
public void echo_binary_returns_success() {
// setup
GlideString message = gs("GLIDE FOR REDIS");
GlideString[] arguments = new GlideString[] {message};
CompletableFuture<GlideString> testResponse = new CompletableFuture<>();
testResponse.complete(message);

// match on protobuf request
when(commandManager.<GlideString>submitNewCommand(eq(Echo), eq(arguments), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<GlideString> response = service.echo(message);
GlideString echo = response.get();

// verify
assertEquals(testResponse, response);
assertEquals(message, echo);
}

@SneakyThrows
@Test
public void echo_with_route_returns_success() {
Expand All @@ -376,6 +401,29 @@ public void echo_with_route_returns_success() {
assertEquals(message, echo);
}

@SneakyThrows
@Test
public void echo_binary_with_route_returns_success() {
// setup
GlideString message = gs("GLIDE FOR REDIS");
GlideString[] arguments = new GlideString[] {message};
CompletableFuture<ClusterValue<GlideString>> testResponse = new CompletableFuture<>();
testResponse.complete(ClusterValue.ofSingleValue(message));

// match on protobuf request
when(commandManager.<ClusterValue<GlideString>>submitNewCommand(
eq(Echo), eq(arguments), eq(RANDOM), any()))
.thenReturn(testResponse);

// exercise
CompletableFuture<ClusterValue<GlideString>> response = service.echo(message, RANDOM);
GlideString echo = response.get().getSingleValue();

// verify
assertEquals(testResponse, response);
assertEquals(message, echo);
}

@SneakyThrows
@Test
public void info_returns_string() {
Expand Down
20 changes: 20 additions & 0 deletions java/integTest/src/test/java/glide/cluster/CommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,26 @@ public void echo_with_route() {
multiPayload.forEach((key, value) -> assertEquals(message, value));
}

@SneakyThrows
@Test
public void echo_gs() {
byte[] message = {(byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02};
GlideString response = clusterClient.echo(gs(message)).get();
assertEquals(gs(message), response);
}

@SneakyThrows
@Test
public void echo_gs_with_route() {
byte[] message = {(byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02};
GlideString singlePayload = clusterClient.echo(gs(message), RANDOM).get().getSingleValue();
assertEquals(gs(message), singlePayload);

Map<String, GlideString> multiPayload =
clusterClient.echo(gs(message), ALL_NODES).get().getMultiValue();
multiPayload.forEach((key, value) -> assertEquals(gs(message), value));
}

@Test
@SneakyThrows
public void time() {
Expand Down
12 changes: 12 additions & 0 deletions java/integTest/src/test/java/glide/standalone/CommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static glide.TestUtilities.getValueFromInfo;
import static glide.TestUtilities.parseInfoResponseToMap;
import static glide.api.BaseClient.OK;
import static glide.api.models.GlideString.gs;
import static glide.api.models.commands.FlushMode.ASYNC;
import static glide.api.models.commands.FlushMode.SYNC;
import static glide.api.models.commands.InfoOptions.Section.CLUSTER;
Expand Down Expand Up @@ -335,6 +336,17 @@ public void echo() {
String message = "GLIDE";
String response = regularClient.echo(message).get();
assertEquals(message, response);
message = "";
response = regularClient.echo(message).get();
assertEquals(message, response);
}

@SneakyThrows
@Test
public void echo_gs() {
byte[] message = {(byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02};
GlideString response = regularClient.echo(gs(message)).get();
assertEquals(gs(message), response);
}

@Test
Expand Down

0 comments on commit d5cb19d

Please sign in to comment.