@@ -311,30 +311,34 @@ void testSchemaChangeUpdatesSnapshotState() throws Exception {
311311 "INSERT INTO customer.\" Customers\" VALUES (3002, 'after_ddl', 'Shanghai', '222', 'after@test.com')" );
312312 }
313313
314- // Wait for the schema change event to be processed
315- Thread .sleep (1000L );
316-
317- // Poll records so the emitter processes all events
314+ // Poll until the reader captures both stream records in order.
318315 final SimpleReaderOutput output = new SimpleReaderOutput ();
319- for (int i = 0 ; i < 10 ; i ++) {
320- reader .pollNext (output );
321- }
322-
323- // Verify the emitted records contain data before and after DDL in correct order
324- List <SourceRecord > results = output .getResults ();
325316 int beforeDdlPos = -1 ;
326317 int afterDdlPos = -1 ;
327- for (int i = 0 ; i < results .size (); i ++) {
328- SourceRecord record = results .get (i );
329- if (record .value () != null ) {
330- String value = record .value ().toString ();
331- if (value .contains ("before_ddl" )) {
332- beforeDdlPos = i ;
333- } else if (value .contains ("after_ddl" )) {
334- afterDdlPos = i ;
318+ long recordDeadline = System .currentTimeMillis () + 10_000L ;
319+ while (System .currentTimeMillis () < recordDeadline ) {
320+ reader .pollNext (output );
321+
322+ List <SourceRecord > results = output .getResults ();
323+ beforeDdlPos = -1 ;
324+ afterDdlPos = -1 ;
325+ for (int i = 0 ; i < results .size (); i ++) {
326+ SourceRecord record = results .get (i );
327+ if (record .value () != null ) {
328+ String value = record .value ().toString ();
329+ if (value .contains ("before_ddl" )) {
330+ beforeDdlPos = i ;
331+ } else if (value .contains ("after_ddl" )) {
332+ afterDdlPos = i ;
333+ }
335334 }
336335 }
336+ if (beforeDdlPos >= 0 && afterDdlPos >= 0 && beforeDdlPos < afterDdlPos ) {
337+ break ;
338+ }
339+ Thread .sleep (100L );
337340 }
341+
338342 assertThat (beforeDdlPos )
339343 .as ("Should capture the INSERT before DDL" )
340344 .isGreaterThanOrEqualTo (0 );
@@ -343,22 +347,31 @@ void testSchemaChangeUpdatesSnapshotState() throws Exception {
343347 .as ("INSERT before DDL should appear before INSERT after DDL" )
344348 .isLessThan (afterDdlPos );
345349
346- // Verify that snapshotState returns splits with updated table schema
347- List <SourceSplitBase > splits = reader .snapshotState (1L );
348- assertThat (splits ).isNotEmpty ();
349-
350+ // Verify that snapshotState returns splits with updated table schema.
351+ List <SourceSplitBase > splits = Collections .emptyList ();
350352 boolean foundUpdatedSchema = false ;
351- for (SourceSplitBase split : splits ) {
352- if (split .isStreamSplit ()) {
353- Map <TableId , TableChanges .TableChange > schemas =
354- split .asStreamSplit ().getTableSchemas ();
355- if (schemas .containsKey (tableId )
356- && schemas .get (tableId ).getTable ().columnWithName ("email" ) != null ) {
357- foundUpdatedSchema = true ;
358- break ;
353+ long schemaDeadline = System .currentTimeMillis () + 10_000L ;
354+ while (System .currentTimeMillis () < schemaDeadline ) {
355+ splits = reader .snapshotState (1L );
356+ foundUpdatedSchema = false ;
357+ for (SourceSplitBase split : splits ) {
358+ if (split .isStreamSplit ()) {
359+ Map <TableId , TableChanges .TableChange > schemas =
360+ split .asStreamSplit ().getTableSchemas ();
361+ if (schemas .containsKey (tableId )
362+ && schemas .get (tableId ).getTable ().columnWithName ("email" ) != null ) {
363+ foundUpdatedSchema = true ;
364+ break ;
365+ }
359366 }
360367 }
368+ if (foundUpdatedSchema ) {
369+ break ;
370+ }
371+ Thread .sleep (100L );
361372 }
373+
374+ assertThat (splits ).isNotEmpty ();
362375 assertThat (foundUpdatedSchema )
363376 .as ("The snapshotState should contain the updated table schema with 'email' column" )
364377 .isTrue ();
0 commit comments