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 ;
26
+ import java .util .Date ;
24
27
import java .util .HashMap ;
28
+ import java .util .LinkedList ;
25
29
import java .util .List ;
26
30
import java .util .Map ;
31
+ import java .util .Queue ;
32
+ import java .util .concurrent .ConcurrentHashMap ;
33
+ import java .util .concurrent .ConcurrentLinkedDeque ;
34
+ import java .util .concurrent .atomic .AtomicInteger ;
27
35
import javax .sql .DataSource ;
28
36
import org .jooq .DSLContext ;
29
37
import org .jooq .SQLDialect ;
@@ -51,12 +59,29 @@ abstract public class TestDatabase<C extends JdbcDatabaseContainer<?>, T extends
51
59
final private ArrayList <String > cleanupSQL = new ArrayList <>();
52
60
final private Map <String , String > connectionProperties = new HashMap <>();
53
61
54
- private DataSource dataSource ;
55
- private DSLContext dslContext ;
62
+ private volatile DataSource dataSource ;
63
+ private volatile DSLContext dslContext ;
56
64
65
+ protected final int databaseId ;
66
+ private static final AtomicInteger nextDatabaseId = new AtomicInteger (0 );
67
+
68
+ protected final int containerId ;
69
+ private static final AtomicInteger nextContainerId = new AtomicInteger (0 );
70
+ private static final Map <String , Integer > containerUidToId = new ConcurrentHashMap <>();
71
+
72
+ @ SuppressWarnings ("this-escape" )
57
73
protected TestDatabase (C container ) {
58
74
this .container = container ;
59
75
this .suffix = Strings .addRandomSuffix ("" , "_" , 10 );
76
+ this .databaseId = nextDatabaseId .getAndIncrement ();
77
+ this .containerId = containerUidToId .computeIfAbsent (container .getContainerId (), k ->nextContainerId .getAndIncrement ());
78
+ LOGGER .info (formatLogLine ("creating database " + getDatabaseName ()));
79
+ }
80
+
81
+ private final DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS" );
82
+ protected String formatLogLine (String logLine ) {
83
+ String retVal = "SGX TestDatabase databaseId=" + databaseId + ", containerId=" + containerId + " - " + logLine ;
84
+ return retVal ;
60
85
}
61
86
62
87
@ SuppressWarnings ("unchecked" )
@@ -170,7 +195,14 @@ public Database getDatabase() {
170
195
protected void execSQL (final List <String > sqls ) {
171
196
try {
172
197
for (String sql : sqls ) {
173
- getDslContext ().execute (sql );
198
+ LOGGER .info (formatLogLine ("executing SQL: " + sql ));
199
+ try {
200
+ getDslContext ().execute (sql );
201
+ LOGGER .info (formatLogLine ("completed SQL: " + sql ));
202
+ } catch (Throwable t ) {
203
+ LOGGER .error (formatLogLine ("error when executing SQL: " + sql + "e=\n " + t ));
204
+ throw t ;
205
+ }
174
206
}
175
207
} catch (DataAccessException e ) {
176
208
throw new RuntimeException (e );
@@ -182,12 +214,12 @@ protected void execInContainer(List<String> cmd) {
182
214
return ;
183
215
}
184
216
try {
185
- LOGGER .debug ( "executing {} " , Strings .join (cmd , " " ));
217
+ LOGGER .info ( formatLogLine ( String . format ( "executing command %s " , Strings .join (cmd , " " )) ));
186
218
final var exec = getContainer ().execInContainer (cmd .toArray (new String [0 ]));
187
219
if (exec .getExitCode () == 0 ) {
188
- LOGGER .debug ( "execution success\n stdout:\n {} \n stderr:\n {} " , exec .getStdout (), exec .getStderr ());
220
+ LOGGER .info ( formatLogLine ( String . format ( "execution success\n stdout:\n %s \n stderr:\n %s " , exec .getStdout (), exec .getStderr ()) ));
189
221
} else {
190
- LOGGER .error ("execution failure, code {} \n stdout:\n {} \n stderr:\n {} " , exec .getExitCode (), exec .getStdout (), exec .getStderr ());
222
+ LOGGER .error (formatLogLine ( String . format ( "execution failure, code %s \n stdout:\n %s \n stderr:\n %s " , exec .getExitCode (), exec .getStdout (), exec .getStderr ()) ));
191
223
}
192
224
} catch (IOException e ) {
193
225
throw new UncheckedIOException (e );
@@ -229,6 +261,7 @@ public B integrationTestConfigBuilder() {
229
261
public void close () {
230
262
execSQL (this .cleanupSQL );
231
263
execInContainer (inContainerUndoBootstrapCmd ());
264
+ LOGGER .info ("closing database databaseId=" + databaseId );
232
265
}
233
266
234
267
static public class ConfigBuilder <T extends TestDatabase <?, ?, ?>, B extends ConfigBuilder <T , B >> {
0 commit comments