-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed redundant transaction template bean definition. Extracted opt…
…imistic locking tests to common module, add tests for spring optimistic locking.
- Loading branch information
Showing
33 changed files
with
451 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 6 additions & 38 deletions
44
...onaut/spring/jdbc/optimistic/locking/AbstractEventuateMicronautOptimisticLockingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,17 @@ | ||
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import io.eventuate.tram.consumer.common.MessageHandlerDecorator; | ||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractEventuateOptimisticLockingTest; | ||
|
||
import javax.inject.Inject; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
abstract public class AbstractEventuateMicronautOptimisticLockingTest { | ||
|
||
private static final int UPDATE_THREADS = 3; | ||
abstract public class AbstractEventuateMicronautOptimisticLockingTest extends AbstractEventuateOptimisticLockingTest { | ||
|
||
@Inject | ||
private OptimisticLockingDecorator optimisticLockingDecorator; | ||
|
||
private AtomicInteger attempts = new AtomicInteger(0); | ||
|
||
private CountDownLatch countDownLatch = new CountDownLatch(UPDATE_THREADS); | ||
|
||
protected abstract AbstractTestEntityService testEntityService(); | ||
|
||
@Test | ||
public void shouldRetryOnLockException() throws InterruptedException { | ||
|
||
long testEntityId = testEntityService().createTestEntityInTransaction(); | ||
|
||
for (int i = 0; i < UPDATE_THREADS; i++) updateEntity(testEntityId); | ||
|
||
countDownLatch.await(); | ||
|
||
Assertions.assertTrue(attempts.get() > UPDATE_THREADS); | ||
Assertions.assertEquals(UPDATE_THREADS, testEntityService().getDataInTransaction(testEntityId)); | ||
} | ||
|
||
private void updateEntity(Long testEntityId) { | ||
new Thread(() -> { | ||
|
||
optimisticLockingDecorator.accept(null, subscriberIdAndMessage -> { | ||
attempts.incrementAndGet(); | ||
|
||
testEntityService().incDataInTransaction(testEntityId); | ||
}); | ||
|
||
countDownLatch.countDown(); | ||
|
||
}).start(); | ||
@Override | ||
public MessageHandlerDecorator getOptimisticLockingDecorator() { | ||
return optimisticLockingDecorator; | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
...io/eventuate/tram/micronaut/spring/jdbc/optimistic/locking/AbstractTestEntityService.java
This file was deleted.
Oops, something went wrong.
10 changes: 8 additions & 2 deletions
10
.../optimistic/locking/EventuateMicronautOptimisticLockingWithAnnotationTransactionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking; | ||
|
||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityService; | ||
import io.micronaut.test.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.inject.Inject; | ||
|
||
@MicronautTest(transactional = false) | ||
public class EventuateMicronautOptimisticLockingWithAnnotationTransactionTest extends AbstractEventuateMicronautOptimisticLockingTest { | ||
|
||
|
||
@Inject | ||
private TestEntityServiceTransactionAnnotation testEntityService; | ||
|
||
|
||
@Override | ||
protected AbstractTestEntityService testEntityService() { | ||
return testEntityService; | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldRetryOnLockException() throws InterruptedException { | ||
super.shouldRetryOnLockException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
.../io/eventuate/tram/micronaut/spring/jdbc/optimistic/locking/TestEntityRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking; | ||
|
||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntity; | ||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntityRepository; | ||
|
||
import javax.inject.Singleton; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
@Singleton | ||
public class TestEntityRepositoryImpl implements TestEntityRepository { | ||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Override | ||
public TestEntity find(Long entityId) { | ||
return entityManager.find(TestEntity.class, entityId); | ||
} | ||
|
||
@Override | ||
public void persist(TestEntity testEntity) { | ||
entityManager.persist(testEntity); | ||
} | ||
} |
38 changes: 25 additions & 13 deletions
38
...tram/micronaut/spring/jdbc/optimistic/locking/TestEntityServiceTransactionAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,40 @@ | ||
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking; | ||
|
||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityService; | ||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntityRepository; | ||
import io.micronaut.spring.tx.annotation.Transactional; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
@Singleton | ||
public class TestEntityServiceTransactionAnnotation extends AbstractTestEntityService { | ||
|
||
@Inject | ||
private TestEntityRepository testEntityRepository; | ||
|
||
@Transactional | ||
@Override | ||
public Long createTestEntityInTransaction() { | ||
return createTestEntity(); | ||
} | ||
@Transactional | ||
@Override | ||
public Long createTestEntityInTransaction() { | ||
return createTestEntity(); | ||
} | ||
|
||
@Transactional | ||
@Override | ||
public void incDataInTransaction(Long entityId) { | ||
incData(entityId); | ||
} | ||
@Transactional | ||
@Override | ||
public void incDataInTransaction(Long entityId) { | ||
incData(entityId); | ||
} | ||
|
||
@Transactional | ||
@Override | ||
public long getDataInTransaction(Long entityId) { | ||
@Transactional | ||
@Override | ||
public long getDataInTransaction(Long entityId) { | ||
return getData(entityId); | ||
} | ||
|
||
@Override | ||
public TestEntityRepository getTestEntityRepository() { | ||
return testEntityRepository; | ||
} | ||
} |
25 changes: 10 additions & 15 deletions
25
...e/tram/micronaut/spring/jdbc/optimistic/locking/TestEntityServiceTransactionTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,28 @@ | ||
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking; | ||
|
||
import io.eventuate.common.jdbc.EventuateTransactionTemplate; | ||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityServiceTransactionTemplate; | ||
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntityRepository; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class TestEntityServiceTransactionTemplate extends AbstractTestEntityService { | ||
public class TestEntityServiceTransactionTemplate extends AbstractTestEntityServiceTransactionTemplate { | ||
|
||
@Inject | ||
private EventuateTransactionTemplate eventuateTransactionalTemplate; | ||
private TestEntityRepository testEntityRepository; | ||
|
||
@Override | ||
public Long createTestEntityInTransaction() { | ||
return eventuateTransactionalTemplate.executeInTransaction(this::createTestEntity); | ||
} | ||
@Inject | ||
private EventuateTransactionTemplate eventuateTransactionalTemplate; | ||
|
||
@Override | ||
public void incDataInTransaction(Long entityId) { | ||
eventuateTransactionalTemplate.executeInTransaction(() -> { | ||
incData(entityId); | ||
return null; | ||
}); | ||
public EventuateTransactionTemplate getEventuateTransactionalTemplate() { | ||
return eventuateTransactionalTemplate; | ||
} | ||
|
||
@Override | ||
public long getDataInTransaction(Long entityId) { | ||
return eventuateTransactionalTemplate.executeInTransaction(() -> { | ||
return getData(entityId); | ||
}); | ||
public TestEntityRepository getTestEntityRepository() { | ||
return testEntityRepository; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
dependencies { | ||
compile project(":eventuate-tram-consumer-common") | ||
compile project(":eventuate-tram-consumer-common") | ||
compile "io.eventuate.common:eventuate-common-jdbc:$eventuateCommonVersion" | ||
compile 'javax.persistence:javax.persistence-api:2.2' | ||
|
||
compile "junit:junit:4.12" | ||
} |
46 changes: 46 additions & 0 deletions
46
...uate/tram/jdbc/optimistic/locking/common/test/AbstractEventuateOptimisticLockingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.eventuate.tram.jdbc.optimistic.locking.common.test; | ||
|
||
import io.eventuate.tram.consumer.common.MessageHandlerDecorator; | ||
import org.junit.Assert; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public abstract class AbstractEventuateOptimisticLockingTest { | ||
|
||
private static final int UPDATE_THREADS = 3; | ||
|
||
private AtomicInteger attempts = new AtomicInteger(0); | ||
|
||
private CountDownLatch countDownLatch = new CountDownLatch(UPDATE_THREADS); | ||
|
||
protected abstract AbstractTestEntityService testEntityService(); | ||
|
||
public void shouldRetryOnLockException() throws InterruptedException { | ||
|
||
long testEntityId = testEntityService().createTestEntityInTransaction(); | ||
|
||
for (int i = 0; i < UPDATE_THREADS; i++) updateEntity(testEntityId); | ||
|
||
countDownLatch.await(); | ||
|
||
Assert.assertTrue(attempts.get() > UPDATE_THREADS); | ||
Assert.assertEquals(UPDATE_THREADS, testEntityService().getDataInTransaction(testEntityId)); | ||
} | ||
|
||
public abstract MessageHandlerDecorator getOptimisticLockingDecorator(); | ||
|
||
private void updateEntity(Long testEntityId) { | ||
new Thread(() -> { | ||
|
||
getOptimisticLockingDecorator().accept(null, subscriberIdAndMessage -> { | ||
attempts.incrementAndGet(); | ||
|
||
testEntityService().incDataInTransaction(testEntityId); | ||
}); | ||
|
||
countDownLatch.countDown(); | ||
|
||
}).start(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...java/io/eventuate/tram/jdbc/optimistic/locking/common/test/AbstractTestEntityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.eventuate.tram.jdbc.optimistic.locking.common.test; | ||
|
||
public abstract class AbstractTestEntityService { | ||
|
||
public abstract Long createTestEntityInTransaction(); | ||
public abstract void incDataInTransaction(Long entityId); | ||
public abstract long getDataInTransaction(Long entityId); | ||
|
||
public Long createTestEntity() { | ||
TestEntity testEntity = new TestEntity(); | ||
getTestEntityRepository().persist(testEntity); | ||
return testEntity.getId(); | ||
} | ||
|
||
public void incData(Long entityId) { | ||
TestEntity testEntity = getTestEntityRepository().find(entityId); | ||
testEntity.setData(testEntity.getData() + 1); | ||
sleep(); | ||
getTestEntityRepository().persist(testEntity); | ||
} | ||
|
||
public long getData(Long entityId) { | ||
TestEntity testEntity = getTestEntityRepository().find(entityId); | ||
return testEntity.getData(); | ||
} | ||
|
||
public abstract TestEntityRepository getTestEntityRepository(); | ||
|
||
private void sleep() { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.