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

upgrade embedded redis version to 7.4.2 #383

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions testing/src/main/java/org/example/age/testing/RedisExtension.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.example.age.testing;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -13,6 +19,7 @@ public final class RedisExtension implements BeforeAllCallback, AfterAllCallback

private int port;

private Path redisDir;
private RedisServer server;
private JedisPooled client;

Expand Down Expand Up @@ -47,18 +54,25 @@ public JedisPooled client() {
@Override
public void beforeAll(ExtensionContext context) throws IOException {
port = (port != 0) ? port : findAvailablePort();
server = new RedisServer(port);
redisDir = Files.createTempDirectory("redis");
File redisBinary = copyRedisBinaryFromResources(redisDir);
server = new RedisServer(port, redisBinary);
server.start();
client = new JedisPooled("localhost", port);
}

@Override
public void afterAll(ExtensionContext context) throws IOException {
if (client != null) {
client.close();
}
if (server != null) {
server.stop();
}
if (client != null) {
client.close();
if (redisDir != null) {
try (Stream<Path> paths = Files.walk(redisDir)) {
paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
}
}

Expand All @@ -69,6 +83,18 @@ private static int findAvailablePort() throws IOException {
}
}

/** Copies the Redis binary from resources to a file, returning the file. */
private static File copyRedisBinaryFromResources(Path redisDir) throws IOException {
Path redisBinaryPath = redisDir.resolve("redis-server");
ClassLoader classLoader = RedisExtension.class.getClassLoader();
try (InputStream resourceStream = classLoader.getResourceAsStream("redis-server")) {
Files.copy(resourceStream, redisBinaryPath);
}
File redisBinary = redisBinaryPath.toFile();
redisBinary.setExecutable(true);
return redisBinary;
}

/** Checks that Redis is initialized. */
private void checkIsInitialized() {
if (client == null) {
Expand Down
Binary file added testing/src/main/resources/redis-server
Binary file not shown.
Loading