Skip to content

Commit

Permalink
upgrade embedded redis version to 7.4.2 (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewacker authored Feb 24, 2025
1 parent 3c40431 commit 84e57e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
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.

0 comments on commit 84e57e5

Please sign in to comment.