|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.io.UncheckedIOException;
|
21 | 21 | import java.sql.SQLException;
|
| 22 | +import java.text.DateFormat; |
| 23 | +import java.text.SimpleDateFormat; |
22 | 24 | import java.time.Duration;
|
23 | 25 | import java.util.ArrayList;
|
24 | 26 | import java.util.HashMap;
|
25 | 27 | import java.util.List;
|
26 | 28 | import java.util.Map;
|
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
27 | 31 | import java.util.stream.Stream;
|
28 | 32 | import javax.sql.DataSource;
|
29 | 33 | import org.jooq.DSLContext;
|
@@ -51,12 +55,30 @@ abstract public class TestDatabase<C extends JdbcDatabaseContainer<?>, T extends
|
51 | 55 | final private ArrayList<String> cleanupSQL = new ArrayList<>();
|
52 | 56 | final private Map<String, String> connectionProperties = new HashMap<>();
|
53 | 57 |
|
54 |
| - private DataSource dataSource; |
55 |
| - private DSLContext dslContext; |
| 58 | + private volatile DataSource dataSource; |
| 59 | + private volatile DSLContext dslContext; |
56 | 60 |
|
| 61 | + protected final int databaseId; |
| 62 | + private static final AtomicInteger nextDatabaseId = new AtomicInteger(0); |
| 63 | + |
| 64 | + protected final int containerId; |
| 65 | + private static final AtomicInteger nextContainerId = new AtomicInteger(0); |
| 66 | + private static final Map<String, Integer> containerUidToId = new ConcurrentHashMap<>(); |
| 67 | + |
| 68 | + @SuppressWarnings("this-escape") |
57 | 69 | protected TestDatabase(C container) {
|
58 | 70 | this.container = container;
|
59 | 71 | this.suffix = Strings.addRandomSuffix("", "_", 10);
|
| 72 | + this.databaseId = nextDatabaseId.getAndIncrement(); |
| 73 | + this.containerId = containerUidToId.computeIfAbsent(container.getContainerId(), k -> nextContainerId.getAndIncrement()); |
| 74 | + LOGGER.info(formatLogLine("creating database " + getDatabaseName())); |
| 75 | + } |
| 76 | + |
| 77 | + private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); |
| 78 | + |
| 79 | + protected String formatLogLine(String logLine) { |
| 80 | + String retVal = "TestDatabase databaseId=" + databaseId + ", containerId=" + containerId + " - " + logLine; |
| 81 | + return retVal; |
60 | 82 | }
|
61 | 83 |
|
62 | 84 | @SuppressWarnings("unchecked")
|
@@ -187,12 +209,13 @@ protected void execInContainer(Stream<String> cmds) {
|
187 | 209 | return;
|
188 | 210 | }
|
189 | 211 | try {
|
190 |
| - LOGGER.debug("executing {}", Strings.join(cmd, " ")); |
| 212 | + LOGGER.info(formatLogLine(String.format("executing command %s", Strings.join(cmd, " ")))); |
191 | 213 | final var exec = getContainer().execInContainer(cmd.toArray(new String[0]));
|
192 | 214 | if (exec.getExitCode() == 0) {
|
193 |
| - LOGGER.debug("execution success\nstdout:\n{}\nstderr:\n{}", exec.getStdout(), exec.getStderr()); |
| 215 | + LOGGER.info(formatLogLine(String.format("execution success\nstdout:\n%s\nstderr:\n%s", exec.getStdout(), exec.getStderr()))); |
194 | 216 | } else {
|
195 |
| - LOGGER.error("execution failure, code {}\nstdout:\n{}\nstderr:\n{}", exec.getExitCode(), exec.getStdout(), exec.getStderr()); |
| 217 | + LOGGER.error(formatLogLine( |
| 218 | + String.format("execution failure, code %s\nstdout:\n%s\nstderr:\n%s", exec.getExitCode(), exec.getStdout(), exec.getStderr()))); |
196 | 219 | }
|
197 | 220 | } catch (IOException e) {
|
198 | 221 | throw new UncheckedIOException(e);
|
@@ -234,6 +257,7 @@ public B integrationTestConfigBuilder() {
|
234 | 257 | public void close() {
|
235 | 258 | execSQL(this.cleanupSQL.stream());
|
236 | 259 | execInContainer(inContainerUndoBootstrapCmd());
|
| 260 | + LOGGER.info("closing database databaseId=" + databaseId); |
237 | 261 | }
|
238 | 262 |
|
239 | 263 | static public class ConfigBuilder<T extends TestDatabase<?, ?, ?>, B extends ConfigBuilder<T, B>> {
|
|
0 commit comments