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.
This commit is contained in:
Gary Russell
2012-09-20 16:22:36 +01:00
parent 52d042eadc
commit f73522d5d0

View File

@@ -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);