Skip to content

Commit 32e3d18

Browse files
committed
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
1 parent 8dab740 commit 32e3d18

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/postgres/PostgresChannelMessageTableSubscriberTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,7 +88,6 @@ CREATE FUNCTION INT_CHANNEL_MESSAGE_NOTIFY_FCT()
8888
$BODY$
8989
LANGUAGE PLPGSQL;
9090
^^^ END OF SCRIPT ^^^
91-
9291
CREATE TRIGGER INT_CHANNEL_MESSAGE_NOTIFY_TRG
9392
AFTER INSERT ON INT_CHANNEL_MESSAGE
9493
FOR EACH ROW
@@ -150,6 +149,8 @@ public void testMessagePollMessagesAddedAfterStart() throws Exception {
150149
latch.countDown();
151150
});
152151
messageStore.addMessageToGroup(groupId, new GenericMessage<>("1"));
152+
// A little delay to avoid race condition with CREATED_DATE value
153+
Thread.sleep(100);
153154
messageStore.addMessageToGroup(groupId, new GenericMessage<>("2"));
154155
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
155156
assertThat(payloads).containsExactly("1", "2");
@@ -165,6 +166,8 @@ public void testMessagePollMessagesAddedBeforeStart() throws InterruptedExceptio
165166
latch.countDown();
166167
});
167168
messageStore.addMessageToGroup(groupId, new GenericMessage<>("1"));
169+
// A little delay to avoid race condition with CREATED_DATE value
170+
Thread.sleep(100);
168171
messageStore.addMessageToGroup(groupId, new GenericMessage<>("2"));
169172
postgresChannelMessageTableSubscriber.start();
170173
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
@@ -189,6 +192,8 @@ void testMessagesDispatchedInTransaction() throws InterruptedException {
189192
postgresSubscribableChannel.subscribe(messageHandler);
190193

191194
messageStore.addMessageToGroup(groupId, new GenericMessage<>("1"));
195+
// A little delay to avoid race condition with CREATED_DATE value
196+
Thread.sleep(100);
192197
messageStore.addMessageToGroup(groupId, new GenericMessage<>("2"));
193198

194199
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
@@ -294,6 +299,8 @@ public void testRenewConnection() throws Exception {
294299
assertThat(connectionLatch.await(10, TimeUnit.SECONDS)).isTrue();
295300

296301
messageStore.addMessageToGroup(groupId, new GenericMessage<>("1"));
302+
// A little delay to avoid race condition with CREATED_DATE value
303+
Thread.sleep(100);
297304
messageStore.addMessageToGroup(groupId, new GenericMessage<>("2"));
298305
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
299306
assertThat(payloads).containsExactlyInAnyOrder("1", "2");

0 commit comments

Comments
 (0)