From 32e3d18c94999f913177cfe44ad08d5b4b5dccd1 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 11 Feb 2025 11:59:54 -0500 Subject: [PATCH] Fix race condition in the `PostgresChannelMessageTableSubscriberTests` Adding two messages into a group may end up with wrong insert order. Or better to say the timestamp of first messages inserted might be behind in the future after the second one. This leads to a wrong order return for polling operation and, therefore, verification in the test. * Fix `PostgresChannelMessageTableSubscriberTests` inserting `Thread.sleep(100);` between `messageStore.addMessageToGroup()` operations --- .../PostgresChannelMessageTableSubscriberTests.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/postgres/PostgresChannelMessageTableSubscriberTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/postgres/PostgresChannelMessageTableSubscriberTests.java index 9d96f975bc..caebb9debf 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/postgres/PostgresChannelMessageTableSubscriberTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/postgres/PostgresChannelMessageTableSubscriberTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 the original author or authors. + * Copyright 2022-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -88,7 +88,6 @@ public class PostgresChannelMessageTableSubscriberTests implements PostgresConta $BODY$ LANGUAGE PLPGSQL; ^^^ END OF SCRIPT ^^^ - CREATE TRIGGER INT_CHANNEL_MESSAGE_NOTIFY_TRG AFTER INSERT ON INT_CHANNEL_MESSAGE FOR EACH ROW @@ -150,6 +149,8 @@ public class PostgresChannelMessageTableSubscriberTests implements PostgresConta latch.countDown(); }); messageStore.addMessageToGroup(groupId, new GenericMessage<>("1")); + // A little delay to avoid race condition with CREATED_DATE value + Thread.sleep(100); messageStore.addMessageToGroup(groupId, new GenericMessage<>("2")); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(payloads).containsExactly("1", "2"); @@ -165,6 +166,8 @@ public class PostgresChannelMessageTableSubscriberTests implements PostgresConta latch.countDown(); }); messageStore.addMessageToGroup(groupId, new GenericMessage<>("1")); + // A little delay to avoid race condition with CREATED_DATE value + Thread.sleep(100); messageStore.addMessageToGroup(groupId, new GenericMessage<>("2")); postgresChannelMessageTableSubscriber.start(); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); @@ -189,6 +192,8 @@ public class PostgresChannelMessageTableSubscriberTests implements PostgresConta postgresSubscribableChannel.subscribe(messageHandler); messageStore.addMessageToGroup(groupId, new GenericMessage<>("1")); + // A little delay to avoid race condition with CREATED_DATE value + Thread.sleep(100); messageStore.addMessageToGroup(groupId, new GenericMessage<>("2")); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); @@ -294,6 +299,8 @@ public class PostgresChannelMessageTableSubscriberTests implements PostgresConta assertThat(connectionLatch.await(10, TimeUnit.SECONDS)).isTrue(); messageStore.addMessageToGroup(groupId, new GenericMessage<>("1")); + // A little delay to avoid race condition with CREATED_DATE value + Thread.sleep(100); messageStore.addMessageToGroup(groupId, new GenericMessage<>("2")); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(payloads).containsExactlyInAnyOrder("1", "2");