-
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.
Merge pull request #133 from dartartem/wip-db-id-gen
Extracted transaction manager from in-memory tests, added additional …
- Loading branch information
Showing
15 changed files
with
314 additions
and
177 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apply plugin: PublicModulePlugin | ||
|
||
dependencies { | ||
compile project(":eventuate-tram-in-memory") | ||
compile "org.springframework.boot:spring-boot-starter-jdbc:$springBootCdcVersion" | ||
} |
24 changes: 24 additions & 0 deletions
24
...entuate/tram/common/spring/inmemory/EventuateSpringTransactionSynchronizationManager.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.common.spring.inmemory; | ||
|
||
import io.eventuate.tram.inmemory.EventuateTransactionSynchronizationManager; | ||
import org.springframework.transaction.support.TransactionSynchronizationAdapter; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class EventuateSpringTransactionSynchronizationManager | ||
implements EventuateTransactionSynchronizationManager { | ||
|
||
@Override | ||
public boolean isTransactionActive() { | ||
return TransactionSynchronizationManager.isActualTransactionActive(); | ||
} | ||
|
||
@Override | ||
public void executeAfterTransaction(Runnable callback) { | ||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { | ||
@Override | ||
public void afterCommit() { | ||
callback.run(); | ||
} | ||
}); | ||
} | ||
} |
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,7 @@ | ||
apply plugin: PublicModulePlugin | ||
|
||
dependencies { | ||
compile project(":eventuate-tram-in-memory") | ||
|
||
compile "junit:junit:4.12" | ||
} |
152 changes: 152 additions & 0 deletions
152
...st/src/main/java/io/eventuate/tram/inmemory/test/AbstractInMemoryMessageProducerTest.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,152 @@ | ||
package io.eventuate.tram.inmemory.test; | ||
|
||
import io.eventuate.tram.messaging.common.Message; | ||
import io.eventuate.tram.messaging.consumer.MessageConsumer; | ||
import io.eventuate.tram.messaging.consumer.MessageHandler; | ||
import io.eventuate.tram.messaging.producer.MessageBuilder; | ||
import io.eventuate.tram.messaging.producer.MessageProducer; | ||
|
||
import java.util.Collections; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public abstract class AbstractInMemoryMessageProducerTest { | ||
protected String subscriberId; | ||
protected String destination; | ||
protected String payload; | ||
protected MyMessageHandler mh; | ||
|
||
protected abstract MessageProducer getMessageProducer(); | ||
protected abstract MessageConsumer getMessageConsumer(); | ||
protected abstract void executeInTransaction(Consumer<Runnable> callbackWithRollback); | ||
|
||
public void setUp() { | ||
subscriberId = "subscriberId-" + System.currentTimeMillis(); | ||
destination = "destination-" + System.currentTimeMillis(); | ||
payload = "payload-" + System.currentTimeMillis(); | ||
mh = new MyMessageHandler(); | ||
} | ||
|
||
public void shouldDeliverToMatchingSubscribers() { | ||
subscribe(); | ||
Message m = sendMessage(); | ||
assertNotNull(m.getId()); | ||
assertMessageReceived(); | ||
} | ||
|
||
public void shouldSetIdWithinTransaction() { | ||
Message m = makeMessage(); | ||
executeInTransaction(rollbackCallback -> { | ||
getMessageProducer().send(destination, m); | ||
assertNotNull(m.getId()); | ||
}); | ||
} | ||
|
||
public void shouldDeliverToWildcardSubscribers() { | ||
wildcardSubscribe(); | ||
sendMessage(); | ||
assertMessageReceived(); | ||
} | ||
|
||
public void shouldReceiveMessageAfterTransaction() { | ||
subscribe(); | ||
executeInTransaction(rollbackCallback -> sendMessage()); | ||
assertMessageReceived(); | ||
} | ||
|
||
public void shouldNotReceiveMessageBeforeTransaction() { | ||
subscribe(); | ||
|
||
executeInTransaction(rollbackCallback -> { | ||
sendMessage(); | ||
assertMessageNotReceived(); | ||
}); | ||
|
||
assertMessageReceived(); | ||
} | ||
|
||
public void shouldNotReceiveMessageAfterTransactionRollback() { | ||
subscribe(); | ||
|
||
executeInTransaction(rollbackCallback -> { | ||
sendMessage(); | ||
rollbackCallback.run(); | ||
}); | ||
|
||
assertMessageNotReceived(); | ||
} | ||
|
||
protected void assertMessageReceived() { | ||
mh.assertMessageReceived(payload); | ||
} | ||
|
||
protected void assertMessageNotReceived() { | ||
mh.assertMessageNotReceived(payload); | ||
} | ||
|
||
protected Message sendMessage() { | ||
Message m = makeMessage(); | ||
getMessageProducer().send(destination, m); | ||
return m; | ||
} | ||
|
||
protected void wildcardSubscribe() { | ||
subscribe("*"); | ||
} | ||
|
||
protected void subscribe() { | ||
subscribe(destination); | ||
} | ||
|
||
protected void subscribe(String destination) { | ||
getMessageConsumer().subscribe(subscriberId, Collections.singleton(destination), mh); | ||
} | ||
|
||
protected Message makeMessage() { | ||
return MessageBuilder.withPayload(payload).withHeader(Message.DESTINATION, destination).build(); | ||
} | ||
|
||
protected static class MyMessageHandler implements MessageHandler { | ||
|
||
protected BlockingQueue<String> queue = new LinkedBlockingDeque<>(); | ||
|
||
@Override | ||
public void accept(Message message) { | ||
try { | ||
queue.put(message.getPayload()); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
void assertMessageReceived(String payload) { | ||
assertTrue(searchMessage(payload)); | ||
} | ||
|
||
void assertMessageNotReceived(String payload) { | ||
assertFalse(searchMessage(payload)); | ||
} | ||
|
||
boolean searchMessage(String payload) { | ||
String m; | ||
|
||
try { | ||
while ((m = queue.poll(3, TimeUnit.SECONDS)) != null) { | ||
if (payload.equals(m)) { | ||
return true; | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
.../src/main/java/io/eventuate/tram/inmemory/EventuateTransactionSynchronizationManager.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,6 @@ | ||
package io.eventuate.tram.inmemory; | ||
|
||
public interface EventuateTransactionSynchronizationManager { | ||
boolean isTransactionActive(); | ||
void executeAfterTransaction(Runnable runnable); | ||
} |
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
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
Oops, something went wrong.