Skip to content

Commit

Permalink
Fetch server version using info command (valkey-io#2258)
Browse files Browse the repository at this point in the history
* Fetch server version using info command

Signed-off-by: Prateek Kumar <prateek.kumar@improving.com>
  • Loading branch information
prateek-kumar-improving authored Sep 10, 2024
1 parent 71d8558 commit b081eec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Java: Fetch server version using info command ([#2258](https://github.com/valkey-io/valkey-glide/pull/2258))
* Node: Added binary variant for commands which have `Record` as input or output ([#2207](https://github.com/valkey-io/valkey-glide/pull/2207))
* Node: Renamed `ReturnType` to `GlideReturnType` ([#2241](https://github.com/valkey-io/valkey-glide/pull/2241))
* Node, Python: Rename `stop` to `end` in sorted set queries ([#2214](https://github.com/valkey-io/valkey-glide/pull/2214))
Expand Down
21 changes: 20 additions & 1 deletion java/integTest/src/test/java/glide/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */
package glide;

import static glide.TestUtilities.commonClientConfig;

import com.vdurmont.semver4j.Semver;
import glide.api.GlideClient;
import java.util.Arrays;

public final class TestConfiguration {
// All servers are hosted on localhost
public static final int[] STANDALONE_PORTS = getPortsFromProperty("test.server.standalone.ports");
public static final int[] CLUSTER_PORTS = getPortsFromProperty("test.server.cluster.ports");
public static final Semver SERVER_VERSION = new Semver(System.getProperty("test.server.version"));
public static final Semver SERVER_VERSION;

static {
try {
String serverVersion =
TestUtilities.getServerVersion(
GlideClient.createClient(commonClientConfig().build()).get());
if (serverVersion != null) {
SERVER_VERSION = new Semver(serverVersion);
} else {
throw new Exception("Failed to get server version");
}

} catch (Exception e) {
throw new RuntimeException("Failed to get server version", e);
}
}

private static int[] getPortsFromProperty(String propName) {
return Arrays.stream(System.getProperty(propName).split(","))
Expand Down
27 changes: 27 additions & 0 deletions java/integTest/src/test/java/glide/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import glide.api.GlideClusterClient;
import glide.api.models.ClusterValue;
import glide.api.models.GlideString;
import glide.api.models.commands.InfoOptions;
import glide.api.models.configuration.GlideClientConfiguration;
import glide.api.models.configuration.GlideClusterClientConfiguration;
import glide.api.models.configuration.NodeAddress;
Expand All @@ -25,10 +26,17 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

@UtilityClass
public class TestUtilities {
/** Key names for versions returned in info command. */
private static final String VALKEY_VERSION_KEY = "valkey_version";

private static final String REDIS_VERSION_KEY = "redis_version";

/** Extract integer parameter value from INFO command output */
public static long getValueFromInfo(String data, String value) {
for (var line : data.split("\r\n")) {
Expand Down Expand Up @@ -371,4 +379,23 @@ public static void waitForNotBusy(BaseClient client) {
}
} while (isBusy);
}

/**
* This method returns the server version using a glide client.
*
* @param glideClient Glide client to be used for running the info command.
* @return String The server version number.
*/
@SneakyThrows
public static String getServerVersion(@NonNull final GlideClient glideClient) {
String infoResponse =
glideClient.info(InfoOptions.builder().section(InfoOptions.Section.SERVER).build()).get();
Map<String, String> infoResponseMap = parseInfoResponseToMap(infoResponse);
if (infoResponseMap.containsKey(VALKEY_VERSION_KEY)) {
return infoResponseMap.get(VALKEY_VERSION_KEY);
} else if (infoResponseMap.containsKey(REDIS_VERSION_KEY)) {
return infoResponseMap.get(REDIS_VERSION_KEY);
}
return null;
}
}

0 comments on commit b081eec

Please sign in to comment.