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

Fetch server version using info command #2258

Merged
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a function to TestUtilities which runs info and parses the response. The function has one argument - client.
Then static block will call that function. Add @SneakyThrows to the function - we don't care about exceptions.

This could be used in some specific tests where we could have servers of different versions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.
Moved function to TestUtilities.

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;
}
}
Loading