From f73522d5d0175d0948c9135f5e55e09d97bfbb5e Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Thu, 20 Sep 2012 16:22:36 +0100 Subject: [PATCH] INT-2759 Fix Failing JPA Test Race condition for deletion after poll. Also, during development, the batch flag was flipped from default true to false. This resulted in two tests for row deletion. Changed the first test to use batch deletion. --- .../JpaPollingChannelAdapterTests.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapterTests.java b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapterTests.java index 1b4df85283..3ed5c0de8a 100644 --- a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapterTests.java +++ b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapterTests.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Collection; @@ -50,6 +51,7 @@ import org.springframework.test.context.transaction.TransactionConfiguration; * Integration tests for the Jpa Polling Channel Adapter {@link JpaPollingChannelAdapter}. * * @author Gunnar Hillert + * @author Gary Russell * @since 2.2 * */ @@ -270,6 +272,7 @@ public class JpaPollingChannelAdapterTests { final JpaExecutor jpaExecutor = new JpaExecutor(entityManager); jpaExecutor.setJpaQuery("from Student s"); jpaExecutor.setDeleteAfterPoll(true); + jpaExecutor.setDeleteInBatch(true); final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor); @@ -297,12 +300,29 @@ public class JpaPollingChannelAdapterTests { assertTrue(students.size() == 3); - Long studentCount = entityManager.createQuery("select count(*) from Student", Long.class).getSingleResult(); + Long studentCount = waitForDeletes(students); assertEquals(Long.valueOf(0), studentCount); } + private Long waitForDeletes(Collection students) throws InterruptedException { + Long studentCount = Long.valueOf(students.size()); + + int n = 0; + + while (studentCount > 0) { + studentCount = entityManager.createQuery("select count(*) from Student", Long.class).getSingleResult(); + if (studentCount > 0) { + Thread.sleep(100); + if (n++ > 100) { + fail("Failed to delete after poll"); + } + } + } + return studentCount; + } + @Test @DirtiesContext public void testWithJpaQueryButNoResultsAndDelete() throws Exception { @@ -377,7 +397,7 @@ public class JpaPollingChannelAdapterTests { assertTrue(students.size() == 3); - final Long studentCount = entityManager.createQuery("select count(*) from Student", Long.class).getSingleResult(); + Long studentCount = waitForDeletes(students); assertEquals(Long.valueOf(0), studentCount);