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, Python: Rename `stop` to `end` in sorted set queries ([#2214](https://github.com/valkey-io/valkey-glide/pull/2214))
* Node: Added binary variant to sorted set commands - part 1 ([#2190](https://github.com/valkey-io/valkey-glide/pull/2190))
* Node: Added binary variant to HSCAN command ([#2240](https://github.com/valkey-io/valkey-glide/pull/2240))
Expand Down
48 changes: 44 additions & 4 deletions java/integTest/src/test/java/glide/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/
package glide;

import com.vdurmont.semver4j.Semver;
import glide.api.GlideClient;
import glide.api.models.commands.InfoOptions;

import java.util.Arrays;

import static glide.TestUtilities.commonClientConfig;

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"));
private static final GlideClient standaloneClient;
public static final Semver SERVER_VERSION;

static {
try {
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.

standaloneClient = GlideClient.createClient(commonClientConfig().build()).get();
String infoResponse = standaloneClient.info(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
standaloneClient = GlideClient.createClient(commonClientConfig().build()).get();
var standaloneClient = GlideClient.createClient(commonClientConfig().build()).get();

No need static lifetime for the client

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.

InfoOptions.builder()
.section(InfoOptions.Section.SERVER)
.build()
).get();
String serverVersion = null;
String[] serverSectionArray = infoResponse.split("\n");

for (int i = 0; i < serverSectionArray.length; i++) {
if (serverSectionArray[i].contains("redis_version")) {
String[] versionKeyValue = serverSectionArray[i].split(":");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check for valkey version first

127.0.0.1:7000> info server
# Server
redis_version:7.2.4
server_name:valkey
valkey_version:7.9.240

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.

if (versionKeyValue.length > 1) {
serverVersion = versionKeyValue[1];
}
break;
}
}
if (serverVersion != null) {
SERVER_VERSION = new Semver(serverVersion);
} else {
throw new Exception("Error in getting server version");
}

} catch (Exception e) {
throw new RuntimeException(e);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a message, e.g. "failed to get server version"

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.


}

private static int[] getPortsFromProperty(String propName) {
return Arrays.stream(System.getProperty(propName).split(","))
.mapToInt(Integer::parseInt)
.toArray();
.mapToInt(Integer::parseInt)
.toArray();
}
}
Loading