Skip to content

Commit

Permalink
testing-concord-server: allow modules to re-use the same config object
Browse files Browse the repository at this point in the history
  • Loading branch information
ibodrov authored and brig committed Nov 11, 2023
1 parent ed6c6dd commit f46c11f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* =====
*/

import com.google.inject.Module;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigParseOptions;
Expand All @@ -30,25 +31,44 @@

import java.security.SecureRandom;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

public class TestingConcordServer implements AutoCloseable {

private PostgreSQLContainer<?> db;
private ConcordServer server;
private Map<String, String> extraConfiguration;
private List<Function<Config, Module>> extraModules;

public TestingConcordServer(Map<String, String> extraConfiguration, List<Function<Config, Module>> extraModules) {
this.extraConfiguration = requireNonNull(extraConfiguration);
this.extraModules = requireNonNull(extraModules);
}

public TestingConcordServer() {
this(Map.of(), List.of());
}

public TestingConcordServer start() throws Exception {
public synchronized TestingConcordServer start() throws Exception {
db = new PostgreSQLContainer<>("postgres:15-alpine");
db.start();

server = ConcordServer.withModules(new ConcordServerModule(prepareConfig(db)))
var config = prepareConfig(db);
var system = new ConcordServerModule(config);
var allModules = Stream.concat(extraModules.stream().map(f -> f.apply(config)), Stream.of(system)).toList();
server = ConcordServer.withModules(allModules)
.start();

return this;
}

@Override
public void close() throws Exception {
public synchronized void close() throws Exception {
this.stop();
}

Expand All @@ -64,8 +84,14 @@ public void stop() throws Exception {
}
}

private static Config prepareConfig(PostgreSQLContainer<?> db) {
Config testConfig = ConfigFactory.parseMap(Map.of(
public PostgreSQLContainer<?> getDb() {
return db;
}

private Config prepareConfig(PostgreSQLContainer<?> db) {
var extraConfig = ConfigFactory.parseMap(this.extraConfiguration);

var testConfig = ConfigFactory.parseMap(Map.of(
"db.url", db.getJdbcUrl(),
"db.appUsername", db.getUsername(),
"db.appPassword", db.getPassword(),
Expand All @@ -77,10 +103,10 @@ private static Config prepareConfig(PostgreSQLContainer<?> db) {
"secretStore.projectSecretSalt", randomString()
));

Config defaultConfig = ConfigFactory.load("concord-server.conf", ConfigParseOptions.defaults(), ConfigResolveOptions.defaults().setAllowUnresolved(true))
var defaultConfig = ConfigFactory.load("concord-server.conf", ConfigParseOptions.defaults(), ConfigResolveOptions.defaults().setAllowUnresolved(true))
.getConfig("concord-server");

return testConfig.withFallback(defaultConfig).resolve();
return extraConfig.withFallback(testConfig.withFallback(defaultConfig)).resolve();
}

private static String randomString() {
Expand All @@ -90,8 +116,9 @@ private static String randomString() {
}

public static void main(String[] args) throws Exception {
TestingConcordServer server = new TestingConcordServer().start();
Thread.sleep(100000);
server.stop();
try (TestingConcordServer server = new TestingConcordServer(Map.of("process.watchdogPeriod", "10 seconds"), List.of())) {
server.start();
Thread.sleep(100000);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.Collection;
import java.util.List;

public final class ConcordServer {

Expand All @@ -59,10 +61,14 @@ public static ConcordServer withAutoWiring() throws Exception {
new WireModule(new SpaceModule(new URLClassSpace(cl), BeanScanning.GLOBAL_INDEX)));
}

public static ConcordServer withModules(Module... modules) throws Exception {
return withModules(List.of(modules));
}

/**
* Start ConcordServer using the provided modules.
*/
public static ConcordServer withModules(Module... modules) throws Exception {
public static ConcordServer withModules(Collection<Module> modules) throws Exception {
Injector injector = Guice.createInjector(modules);

ConcordServer instance = new ConcordServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public ConfigurationModule(Config config) {

@Override
public void configure(Binder binder) {
binder.bind(Config.class).toInstance(config);

binder.install(new com.walmartlabs.ollie.config.OllieConfigurationModule("com.walmartlabs.concord.server", config));

binder.bind(AgentConfiguration.class).in(SINGLETON);
Expand Down

0 comments on commit f46c11f

Please sign in to comment.