This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git

commit 13bbe7bd6b991aef6de701d988d65e06760710ca
Author: seanyinx <sean....@huawei.com>
AuthorDate: Wed Jan 3 11:54:58 2018 +0800

    SCB-100 ensured context passing through akka actors
    
    Signed-off-by: seanyinx <sean....@huawei.com>
---
 omega/omega-spring-tx/pom.xml                      | 20 +++++++++
 .../spring/TransactionInterceptionTest.java        | 48 ++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/omega/omega-spring-tx/pom.xml b/omega/omega-spring-tx/pom.xml
index 815e080..022f0ba 100644
--- a/omega/omega-spring-tx/pom.xml
+++ b/omega/omega-spring-tx/pom.xml
@@ -82,6 +82,26 @@
       <version>2.1.8</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.typesafe.akka</groupId>
+      <artifactId>akka-actor_2.12</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.typesafe.akka</groupId>
+      <artifactId>akka-slf4j_2.12</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.typesafe.akka</groupId>
+      <artifactId>akka-testkit_2.12</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.scalatest</groupId>
+      <artifactId>scalatest_2.12</artifactId>
+      <scope>test</scope>
+    </dependency>
 
   </dependencies>
 
diff --git 
a/omega/omega-spring-tx/src/test/java/org/apache/servicecomb/saga/omega/transaction/spring/TransactionInterceptionTest.java
 
b/omega/omega-spring-tx/src/test/java/org/apache/servicecomb/saga/omega/transaction/spring/TransactionInterceptionTest.java
index 6dee1d8..253e636 100644
--- 
a/omega/omega-spring-tx/src/test/java/org/apache/servicecomb/saga/omega/transaction/spring/TransactionInterceptionTest.java
+++ 
b/omega/omega-spring-tx/src/test/java/org/apache/servicecomb/saga/omega/transaction/spring/TransactionInterceptionTest.java
@@ -17,9 +17,11 @@
 
 package org.apache.servicecomb.saga.omega.transaction.spring;
 
+import static akka.actor.ActorRef.noSender;
 import static com.seanyinx.github.unit.scaffolding.AssertUtils.expectFailing;
 import static com.seanyinx.github.unit.scaffolding.Randomness.uniquify;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.SECONDS;
 import static 
org.apache.servicecomb.saga.omega.transaction.spring.TransactionalUserService.ILLEGAL_USER;
 import static org.awaitility.Awaitility.await;
 import static org.hamcrest.CoreMatchers.nullValue;
@@ -56,6 +58,10 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 import io.reactivex.Flowable;
 import io.reactivex.schedulers.Schedulers;
+import akka.actor.AbstractLoggingActor;
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.Props;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = {TransactionTestMain.class, MessageConfig.class})
@@ -96,6 +102,7 @@ public class TransactionInterceptionTest {
   @After
   public void tearDown() throws Exception {
     messages.clear();
+    userRepository.deleteAll();
   }
 
   @AfterClass
@@ -215,6 +222,47 @@ public class TransactionInterceptionTest {
     );
   }
 
+  @Test
+  public void passesOmegaContextAmongActors() throws Exception {
+    // TODO: 2018/1/3 if actor system starts before omega context initialized, 
this test will fail
+    ActorSystem actorSystem = ActorSystem.create();
+
+    User user = new User(username, email);
+
+    ActorRef actorRef = 
actorSystem.actorOf(UserServiceActor.props(userService));
+    actorRef.tell(user, noSender());
+
+    await().atMost(1, SECONDS).until(() -> 
userRepository.findByUsername(username) != null);
+
+    assertArrayEquals(
+        new String[] {
+            new TxStartedEvent(globalTxId, localTxId, parentTxId, 
compensationMethod, user).toString(),
+            new TxEndedEvent(globalTxId, localTxId, parentTxId, 
compensationMethod).toString()},
+        toArray(messages)
+    );
+
+    actorSystem.terminate();
+  }
+
+  private static class UserServiceActor extends AbstractLoggingActor {
+    private final TransactionalUserService userService;
+
+    private UserServiceActor(TransactionalUserService userService) {
+      this.userService = userService;
+    }
+
+    static Props props(TransactionalUserService userService) {
+      return Props.create(UserServiceActor.class, () -> new 
UserServiceActor(userService));
+    }
+
+    @Override
+    public Receive createReceive() {
+      return receiveBuilder()
+          .match(User.class, userService::add)
+          .build();
+    }
+  }
+
   private String[] toArray(List<String> messages) {
     return messages.toArray(new String[messages.size()]);
   }

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <commits@servicecomb.apache.org>.

Reply via email to