Skip to content

Commit b7de9a2

Browse files
loserwang1024ChaomingZhangCN
authored andcommitted
[test][postgres] Close postgres containers after tests and fix container reuse bug (apache#2394)
1 parent 479801b commit b7de9a2

3 files changed

Lines changed: 73 additions & 19 deletions

File tree

flink-connector-postgres-cdc/src/test/java/com/ververica/cdc/connectors/postgres/PostgreSQLSourceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.ververica.cdc.debezium.DebeziumDeserializationSchema;
3838
import com.ververica.cdc.debezium.DebeziumSourceFunction;
3939
import org.apache.kafka.connect.source.SourceRecord;
40+
import org.junit.AfterClass;
4041
import org.junit.Before;
4142
import org.junit.BeforeClass;
4243
import org.junit.Test;
@@ -97,6 +98,13 @@ public static void startAll() {
9798
LOG.info("Containers are started.");
9899
}
99100

101+
@AfterClass
102+
public static void stopAll() {
103+
LOG.info("Stopping containers...");
104+
POSTGRES_CONTAINER_OLD.stop();
105+
LOG.info("Containers are stopped.");
106+
}
107+
100108
@Before
101109
public void before() {
102110
initializePostgresTable(POSTGRES_CONTAINER_OLD, "inventory");

flink-connector-postgres-cdc/src/test/java/com/ververica/cdc/connectors/postgres/PostgresTestBase.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import org.apache.flink.table.planner.factories.TestValuesTableFactory;
2020
import org.apache.flink.test.util.AbstractTestBase;
2121

22+
import com.ververica.cdc.connectors.postgres.source.PostgresConnectionPoolFactory;
2223
import io.debezium.config.Configuration;
2324
import io.debezium.connector.postgresql.connection.PostgresConnection;
2425
import io.debezium.jdbc.JdbcConfiguration;
26+
import org.junit.AfterClass;
2527
import org.junit.BeforeClass;
2628
import org.slf4j.Logger;
2729
import org.slf4j.LoggerFactory;
@@ -83,17 +85,28 @@ public static void startContainers() {
8385
LOG.info("Containers are started.");
8486
}
8587

88+
@AfterClass
89+
public static void stopContainers() {
90+
LOG.info("Stopping containers...");
91+
POSTGRES_CONTAINER.stop();
92+
LOG.info("Containers are stopped.");
93+
}
94+
8695
protected Connection getJdbcConnection(PostgreSQLContainer container) throws SQLException {
8796
return DriverManager.getConnection(
8897
container.getJdbcUrl(), container.getUsername(), container.getPassword());
8998
}
9099

91100
public static Connection getJdbcConnection(PostgreSQLContainer container, String databaseName)
92101
throws SQLException {
102+
String jdbcUrl =
103+
String.format(
104+
PostgresConnectionPoolFactory.JDBC_URL_PATTERN,
105+
container.getHost(),
106+
container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT),
107+
databaseName);
93108
return DriverManager.getConnection(
94-
container.withDatabaseName(databaseName).getJdbcUrl(),
95-
container.getUsername(),
96-
container.getPassword());
109+
jdbcUrl, container.getUsername(), container.getPassword());
97110
}
98111

99112
public static String getSlotName() {

flink-connector-postgres-cdc/src/test/java/com/ververica/cdc/connectors/postgres/table/PostgreSQLConnectorITCase.java

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -535,25 +535,58 @@ public void testMetadataColumns() throws Throwable {
535535

536536
// waiting for change events finished.
537537
waitForSinkSize("sink", 16);
538+
String databaseName = POSTGRES_CONTAINER.getDatabaseName();
538539

539540
List<String> expected =
540541
Arrays.asList(
541-
"+I(postgres,inventory,products,101,scooter,Small 2-wheel scooter,3.140)",
542-
"+I(postgres,inventory,products,102,car battery,12V car battery,8.100)",
543-
"+I(postgres,inventory,products,103,12-pack drill bits,12-pack of drill bits with sizes ranging from #40 to #3,0.800)",
544-
"+I(postgres,inventory,products,104,hammer,12oz carpenter's hammer,0.750)",
545-
"+I(postgres,inventory,products,105,hammer,14oz carpenter's hammer,0.875)",
546-
"+I(postgres,inventory,products,106,hammer,16oz carpenter's hammer,1.000)",
547-
"+I(postgres,inventory,products,107,rocks,box of assorted rocks,5.300)",
548-
"+I(postgres,inventory,products,108,jacket,water resistent black wind breaker,0.100)",
549-
"+I(postgres,inventory,products,109,spare tire,24 inch spare tire,22.200)",
550-
"+I(postgres,inventory,products,110,jacket,water resistent white wind breaker,0.200)",
551-
"+I(postgres,inventory,products,111,scooter,Big 2-wheel scooter ,5.180)",
552-
"+U(postgres,inventory,products,106,hammer,18oz carpenter hammer,1.000)",
553-
"+U(postgres,inventory,products,107,rocks,box of assorted rocks,5.100)",
554-
"+U(postgres,inventory,products,110,jacket,new water resistent white wind breaker,0.500)",
555-
"+U(postgres,inventory,products,111,scooter,Big 2-wheel scooter ,5.170)",
556-
"-D(postgres,inventory,products,111,scooter,Big 2-wheel scooter ,5.170)");
542+
"+I("
543+
+ databaseName
544+
+ ",inventory,products,101,scooter,Small 2-wheel scooter,3.140)",
545+
"+I("
546+
+ databaseName
547+
+ ",inventory,products,102,car battery,12V car battery,8.100)",
548+
"+I("
549+
+ databaseName
550+
+ ",inventory,products,103,12-pack drill bits,12-pack of drill bits with sizes ranging from #40 to #3,0.800)",
551+
"+I("
552+
+ databaseName
553+
+ ",inventory,products,104,hammer,12oz carpenter's hammer,0.750)",
554+
"+I("
555+
+ databaseName
556+
+ ",inventory,products,105,hammer,14oz carpenter's hammer,0.875)",
557+
"+I("
558+
+ databaseName
559+
+ ",inventory,products,106,hammer,16oz carpenter's hammer,1.000)",
560+
"+I("
561+
+ databaseName
562+
+ ",inventory,products,107,rocks,box of assorted rocks,5.300)",
563+
"+I("
564+
+ databaseName
565+
+ ",inventory,products,108,jacket,water resistent black wind breaker,0.100)",
566+
"+I("
567+
+ databaseName
568+
+ ",inventory,products,109,spare tire,24 inch spare tire,22.200)",
569+
"+I("
570+
+ databaseName
571+
+ ",inventory,products,110,jacket,water resistent white wind breaker,0.200)",
572+
"+I("
573+
+ databaseName
574+
+ ",inventory,products,111,scooter,Big 2-wheel scooter ,5.180)",
575+
"+U("
576+
+ databaseName
577+
+ ",inventory,products,106,hammer,18oz carpenter hammer,1.000)",
578+
"+U("
579+
+ databaseName
580+
+ ",inventory,products,107,rocks,box of assorted rocks,5.100)",
581+
"+U("
582+
+ databaseName
583+
+ ",inventory,products,110,jacket,new water resistent white wind breaker,0.500)",
584+
"+U("
585+
+ databaseName
586+
+ ",inventory,products,111,scooter,Big 2-wheel scooter ,5.170)",
587+
"-D("
588+
+ databaseName
589+
+ ",inventory,products,111,scooter,Big 2-wheel scooter ,5.170)");
557590
List<String> actual = TestValuesTableFactory.getRawResults("sink");
558591
Collections.sort(actual);
559592
Collections.sort(expected);

0 commit comments

Comments
 (0)