Skip to content

Commit

Permalink
Removed redundant transaction template bean definition. Extracted opt…
Browse files Browse the repository at this point in the history
…imistic locking tests to common module, add tests for spring optimistic locking.
  • Loading branch information
dartartem committed Feb 25, 2021
1 parent bddf3e4 commit 8161447
Show file tree
Hide file tree
Showing 33 changed files with 451 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ apply plugin: PublicModulePlugin

dependencies {
compile project(":eventuate-tram-consumer-common")
compile project(":eventuate-tram-optimistic-locking-common-test")
compile "io.eventuate.common:eventuate-common-micronaut-spring-jdbc:$eventuateCommonVersion"

compile "io.micronaut.configuration:micronaut-hibernate-jpa"
Expand Down
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;
}
}

This file was deleted.

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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;

Expand All @@ -14,4 +16,10 @@ public class EventuateMicronautOptimisticLockingWithTransactionTemplateTransacti
protected AbstractTestEntityService testEntityService() {
return testEntityService;
}

@Override
@Test
public void shouldRetryOnLockException() throws InterruptedException {
super.shouldRetryOnLockException();
}
}
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);
}
}
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;
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ datasources:
jpa:
default:
packages-to-scan:
- 'io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking'
- 'io.eventuate.tram.jdbc.optimistic.locking.common.test'
properties:
hibernate:
hbm2ddl:
Expand Down
8 changes: 8 additions & 0 deletions eventuate-tram-optimistic-locking-common-test/build.gradle
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"
}
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();
}
}
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);
}
}
}
Loading

0 comments on commit 8161447

Please sign in to comment.