Skip to content

Commit

Permalink
fix: Added SidecarDriver method to clear all resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Jul 6, 2022
1 parent a0a0de2 commit 2ca185f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class SidecarDriver implements Driver {

private static final String JDBC_URL_REGEX = "jdbc\\:(rediss?(\\-(socket|sentinel))?\\:\\/\\/.*)";
private static final Pattern JDBC_URL_PATTERN = Pattern.compile(JDBC_URL_REGEX);

private static final String PROPERTY_PREFIX = "sidecar";
private static final String PROPERTY_DRIVER_PREFIX = PROPERTY_PREFIX + ".driver";

Expand Down Expand Up @@ -172,4 +171,10 @@ public Logger getParentLogger() {
return log;
}

public void clear() {
configManager.close();
meterRegistryManager.clear();
redisManager.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,10 @@ private void read(StatefulRedisModulesConnection<String, String> connection, Str
reader.readValue(json);
}

public void stop() {
close();
}

@Override
public void close() {
futures.forEach((k, v) -> v.cancel(false));
futures.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public Duration step() {
}, Clock.SYSTEM, redisClient);
}

public void clear() {
registries.forEach((k, v) -> v.close());
registries.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ private GenericObjectPoolConfig<StatefulConnection<String, ResultSet>> poolConfi
return config;
}

public void clear() {
pools.forEach((k, v) -> v.close());
pools.clear();
clients.forEach((k, v) -> {
v.shutdown();
v.getResources().shutdown();
});
clients.clear();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import java.util.Properties;

import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.springframework.util.unit.DataSize;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.JdbcDatabaseContainer;

import com.redis.enterprise.Database;
Expand All @@ -37,14 +38,26 @@ public abstract class AbstractSidecarTests extends AbstractTestcontainersRedisTe
RedisModulesContainer.DEFAULT_IMAGE_NAME.withTag(RedisModulesContainer.DEFAULT_TAG));
private final RedisEnterpriseContainer redisEnterprise = new RedisEnterpriseContainer(
RedisEnterpriseContainer.DEFAULT_IMAGE_NAME.withTag("latest"))
.withDatabase(Database.name("SidecarTests").memory(DataSize.ofMegabytes(900)).ossCluster(true)
.withDatabase(Database.name("SidecarTests").ossCluster(true)
.modules(RedisModule.JSON, RedisModule.TIMESERIES).build());

private SidecarDriver sidecarDriver;

@Override
protected Collection<RedisServer> redisServers() {
return Arrays.asList(redis, redisEnterprise);
}

@BeforeAll
private void setupSidecarDriver() {
sidecarDriver = new SidecarDriver();
}

@AfterAll
private void teardownSidecarDriver() {
sidecarDriver.clear();
}

protected void runScript(Connection backendConnection, String script) throws SQLException, IOException {
ScriptRunner scriptRunner = new ScriptRunner(backendConnection);
scriptRunner.setAutoCommit(false);
Expand All @@ -64,7 +77,6 @@ protected Connection connection(JdbcDatabaseContainer<?> container) throws SQLEx
}

protected Connection connection(JdbcDatabaseContainer<?> database, RedisTestContext redis) throws SQLException {
SidecarDriver driver = new SidecarDriver();
Properties info = new Properties();
info.put("sidecar.buffer-size", String.valueOf(BUFFER_SIZE));
info.put("sidecar.metrics.publish-interval", "1");
Expand All @@ -73,7 +85,7 @@ protected Connection connection(JdbcDatabaseContainer<?> database, RedisTestCont
info.put("sidecar.driver.url", database.getJdbcUrl());
info.put("user", database.getUsername());
info.put("password", database.getPassword());
return driver.connect("jdbc:" + redis.getRedisURI(), info);
return sidecarDriver.connect("jdbc:" + redis.getRedisURI(), info);
}

private static interface StatementExecutor {
Expand Down

0 comments on commit 2ca185f

Please sign in to comment.