Files
spring-data-demo/jooq/scripts/container-trap.sh
T

25 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Reproduces the Testcontainers + Spring context-cache trap: a per-class @Container is stopped after
# the first test class, but Spring reuses the cached ApplicationContext (and its connection pool) for the next one.
set -uo pipefail
HERE="$(cd "$(dirname "$0")/.." && pwd)"
OUT="$HERE/output/09-container-lifecycle-trap.txt"
WORK="$(mktemp -d)"
mkdir -p "$WORK/m" && cp -r "$HERE/pom.xml" "$HERE/src" "$WORK/m/"
cd "$WORK/m"
python3 - <<'PY'
p='src/test/java/com/ankurm/jooq/PostgresTestBase.java'
s=open(p).read()
s=s.replace('@SpringBootTest\nabstract','@org.testcontainers.junit.jupiter.Testcontainers\n@SpringBootTest\nabstract')
s=s.replace(' @ServiceConnection\n static final PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:17-alpine");\n\n static {\n postgres.start();\n }\n',
' @org.testcontainers.junit.jupiter.Container\n @ServiceConnection\n static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:17-alpine");\n')
open(p,'w').write(s)
PY
{
echo "per-class @Container, two test classes sharing one cached Spring context (hikari connection-timeout shortened to 2 s)"
mvn -B -q test -DargLine="-Dspring.datasource.hikari.connection-timeout=2000" -Dtest='TransactionTests,QueryTests' 2>&1 \
| grep -E 'Tests run:|TransientDataAccessResourceException|Connection is not available|Failed to validate connection' | sed 's/^[0-9:.]* //' | sed -E 's/PgConnection@[0-9a-f]+/PgConnection@HASH/; s/^[0-9T:.+-]+ +//' | sort | uniq -c | sed 's/^ *//' | cut -c1-260 | head -12
} > "$OUT"
cd "$HERE"; rm -rf "$WORK"
cat "$OUT"