You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by il...@apache.org on 2019/07/03 06:01:26 UTC
[dubbo-samples] branch master updated: integartion test for
dubbo-samples-transaction
This is an automated email from the ASF dual-hosted git repository.
iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git
The following commit(s) were added to refs/heads/master by this push:
new 940736a integartion test for dubbo-samples-transaction
940736a is described below
commit 940736a503eb2f5dc265978ebc2653925afbe178
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Wed Jul 3 13:59:44 2019 +0800
integartion test for dubbo-samples-transaction
---
dubbo-samples-transaction/pom.xml | 226 ++++++++++++++++++++-
.../apache/dubbo/samples/ApplicationKeeper.java | 77 -------
.../dubbo/samples/service/StorageService.java | 8 +
.../samples/service/impl/StorageServiceImpl.java | 12 ++
.../starter/DubboAccountServiceStarter.java | 9 +-
.../samples/starter/DubboOrderServiceStarter.java | 10 +-
.../starter/DubboStorageServiceStarter.java | 10 +-
.../apache/dubbo/samples/service/BusinessIT.java | 57 ++++++
8 files changed, 318 insertions(+), 91 deletions(-)
diff --git a/dubbo-samples-transaction/pom.xml b/dubbo-samples-transaction/pom.xml
index ff19186..7011594 100644
--- a/dubbo-samples-transaction/pom.xml
+++ b/dubbo-samples-transaction/pom.xml
@@ -31,10 +31,15 @@
<properties>
<source.level>1.8</source.level>
<target.level>1.8</target.level>
- <dubbo.version>2.7.1</dubbo.version>
+ <dubbo.version>2.7.2</dubbo.version>
<seata.version>0.5.0</seata.version>
- <spring.version>4.3.16.RELEASE</spring.version>
<mysql-connector.version>8.0.15</mysql-connector.version>
+ <spring.version>4.3.16.RELEASE</spring.version>
+ <junit.version>4.12</junit.version>
+ <docker-maven-plugin.version>0.30.0</docker-maven-plugin.version>
+ <jib-maven-plugin.version>1.2.0</jib-maven-plugin.version>
+ <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
+ <maven-failsafe-plugin.version>2.21.0</maven-failsafe-plugin.version>
</properties>
<dependencyManagement>
@@ -93,13 +98,230 @@
<version>${mysql-connector.version}</version>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>${spring.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
+ <profiles>
+ <profile>
+ <id>dubbo-integration-test</id>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-maven-address-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>local-address</goal>
+ </goals>
+ <configuration>
+ <localAddress>dubbo-local-address</localAddress>
+ </configuration>
+ <phase>initialize</phase>
+ </execution>
+ </executions>
+ </plugin>
+
+
+ <plugin>
+ <groupId>io.fabric8</groupId>
+ <artifactId>docker-maven-plugin</artifactId>
+ <version>${docker-maven-plugin.version}</version>
+ <configuration>
+ <images>
+ <image>
+ <name>transaction-mysql</name>
+ <build>
+ <contextDir>${project.basedir}/src/main/resources/docker/mysql</contextDir>
+ </build>
+ <run>
+ <ports>
+ <port>3306:3306</port>
+ </ports>
+ <wait>
+ <log>.*ready for connections.*</log>
+ <time>30000</time>
+ </wait>
+ </run>
+ </image>
+
+ <image>
+ <name>transaction-seata</name>
+ <build>
+ <contextDir>${project.basedir}/src/main/resources/docker/seata</contextDir>
+ </build>
+ <run>
+ <ports>
+ <port>8091:8091</port>
+ </ports>
+ <wait>
+ <log>.*Server started.*</log>
+ <time>60000</time>
+ </wait>
+ </run>
+ </image>
+
+ <image>
+ <name>zookeeper:latest</name>
+ <run>
+ <ports>
+ <port>2181:2181</port>
+ </ports>
+ <wait>
+ <tcp>
+ <host>${dubbo-local-address}</host>
+ <ports>
+ <port>2181</port>
+ </ports>
+ </tcp>
+ </wait>
+ </run>
+ </image>
+ </images>
+ </configuration>
+ <executions>
+ <execution>
+ <id>docker-build</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>build</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>start</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>start</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>stop</id>
+ <phase>post-integration-test</phase>
+ <goals>
+ <goal>stop</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <version>1.6.0</version>
+ <executions>
+ <execution>
+ <id>storage-service</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ <configuration>
+ <executable>java</executable>
+ <arguments>
+ <argument>-classpath</argument>
+ <classpath/>
+ <argument>org.apache.dubbo.samples.starter.DubboStorageServiceStarter</argument>
+ </arguments>
+ <async>true</async>
+ <asyncDestroyOnShutdown>true</asyncDestroyOnShutdown>
+ </configuration>
+ </execution>
+
+ <execution>
+ <id>account-service</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ <configuration>
+ <executable>java</executable>
+ <arguments>
+ <argument>-classpath</argument>
+ <classpath/>
+ <argument>org.apache.dubbo.samples.starter.DubboAccountServiceStarter</argument>
+ </arguments>
+ <async>true</async>
+ <asyncDestroyOnShutdown>true</asyncDestroyOnShutdown>
+ </configuration>
+ </execution>
+
+ <execution>
+ <id>order-service</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ <configuration>
+ <executable>java</executable>
+ <arguments>
+ <argument>-classpath</argument>
+ <classpath/>
+ <argument>org.apache.dubbo.samples.starter.DubboOrderServiceStarter</argument>
+ </arguments>
+ <async>true</async>
+ <asyncDestroyOnShutdown>true</asyncDestroyOnShutdown>
+ </configuration>
+ </execution>
+
+ <execution>
+ <id>sleep</id>
+ <phase>pre-integration-test</phase>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ <configuration>
+ <executable>sleep</executable>
+ <arguments>
+ <argument>10</argument>
+ </arguments>
+ </configuration>
+ </execution>
+
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-failsafe-plugin</artifactId>
+ <version>${maven-failsafe-plugin.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>integration-test</goal>
+ <goal>verify</goal>
+ </goals>
+ <configuration>
+ <includes>
+ <include>**/*IT.java</include>
+ </includes>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+ </profiles>
+
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
+ <version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${source.level}</source>
<target>${target.level}</target>
diff --git a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/ApplicationKeeper.java b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/ApplicationKeeper.java
deleted file mode 100644
index cff342c..0000000
--- a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/ApplicationKeeper.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.dubbo.samples;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.support.AbstractApplicationContext;
-
-import java.util.concurrent.locks.Condition;
-import java.util.concurrent.locks.ReentrantLock;
-
-/**
- * The type Application keeper.
- */
-public class ApplicationKeeper {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationKeeper.class);
-
- private final ReentrantLock LOCK = new ReentrantLock();
- private final Condition STOP = LOCK.newCondition();
-
- /**
- * Instantiates a new Application keeper.
- *
- * @param applicationContext the application context
- */
- public ApplicationKeeper(AbstractApplicationContext applicationContext) {
- addShutdownHook(applicationContext);
- }
-
- private void addShutdownHook(final AbstractApplicationContext applicationContext) {
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
- try {
- applicationContext.close();
- LOGGER.info("ApplicationContext " + applicationContext + " is closed.");
- } catch (Exception e) {
- LOGGER.error("Failed to close ApplicationContext", e);
- }
-
- try {
- LOCK.lock();
- STOP.signal();
- } finally {
- LOCK.unlock();
- }
- }));
- }
-
- /**
- * Keep.
- */
- public void keep() {
- synchronized (LOCK) {
- try {
- LOGGER.info("Application is keep running ... ");
- LOCK.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-}
diff --git a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/StorageService.java b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/StorageService.java
index 9afeb5c..b7f07b1 100644
--- a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/StorageService.java
+++ b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/StorageService.java
@@ -29,4 +29,12 @@ public interface StorageService {
* @param count amount to deduct
*/
void deduct(String commodityCode, int count);
+
+ /**
+ * query storage
+ *
+ * @param commodityCode commodity code
+ * @return commodity count
+ */
+ int queryCount(String commodityCode);
}
diff --git a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/impl/StorageServiceImpl.java b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/impl/StorageServiceImpl.java
index a6635c3..bff8b04 100644
--- a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/impl/StorageServiceImpl.java
+++ b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/service/impl/StorageServiceImpl.java
@@ -23,6 +23,7 @@ import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
public class StorageServiceImpl implements StorageService {
@@ -51,4 +52,15 @@ public class StorageServiceImpl implements StorageService {
}
+ @Override
+ public int queryCount(String commodityCode) {
+ SqlRowSet rowSet = jdbcTemplate.queryForRowSet("select count from storage_tbl where commodity_code = ?",
+ commodityCode);
+ if (rowSet.next()) {
+ int count = rowSet.getInt("count");
+ LOGGER.info("Storage has " + count + " for " + commodityCode);
+ return count;
+ }
+ return -1;
+ }
}
diff --git a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboAccountServiceStarter.java b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboAccountServiceStarter.java
index 54f0b72..6b9a0f9 100644
--- a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboAccountServiceStarter.java
+++ b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboAccountServiceStarter.java
@@ -17,11 +17,11 @@
package org.apache.dubbo.samples.starter;
-import org.apache.dubbo.samples.ApplicationKeeper;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
+import java.util.concurrent.CountDownLatch;
+
/**
* The type Dubbo account service starter.
*/
@@ -31,13 +31,14 @@ public class DubboAccountServiceStarter {
*
* @param args the input arguments
*/
- public static void main(String[] args) {
+ public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext accountContext = new ClassPathXmlApplicationContext("spring/dubbo-account-service.xml");
accountContext.getBean("service");
JdbcTemplate accountJdbcTemplate = (JdbcTemplate) accountContext.getBean("jdbcTemplate");
accountJdbcTemplate.update("delete from account_tbl where user_id = 'U100001'");
accountJdbcTemplate.update("insert into account_tbl(user_id, money) values ('U100001', 999)");
- new ApplicationKeeper(accountContext).keep();
+ System.out.println("account service started");
+ new CountDownLatch(1).await();
}
}
diff --git a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboOrderServiceStarter.java b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboOrderServiceStarter.java
index d5edfde..93aa4c6 100644
--- a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboOrderServiceStarter.java
+++ b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboOrderServiceStarter.java
@@ -17,10 +17,10 @@
package org.apache.dubbo.samples.starter;
-import org.apache.dubbo.samples.ApplicationKeeper;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
+import java.util.concurrent.CountDownLatch;
+
/**
* The type Dubbo order service starter.
*/
@@ -30,12 +30,14 @@ public class DubboOrderServiceStarter {
*
* @param args the input arguments
*/
- public static void main(String[] args) {
+ public static void main(String[] args) throws InterruptedException {
/**
* 3. Order service is ready . Waiting for buyers to order
*/
ClassPathXmlApplicationContext orderContext = new ClassPathXmlApplicationContext("spring/dubbo-order-service.xml");
orderContext.getBean("service");
- new ApplicationKeeper(orderContext).keep();
+
+ System.out.println("order service started");
+ new CountDownLatch(1).await();
}
}
diff --git a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboStorageServiceStarter.java b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboStorageServiceStarter.java
index 40b3e1e..212cd0d 100644
--- a/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboStorageServiceStarter.java
+++ b/dubbo-samples-transaction/src/main/java/org/apache/dubbo/samples/starter/DubboStorageServiceStarter.java
@@ -17,11 +17,11 @@
package org.apache.dubbo.samples.starter;
-import org.apache.dubbo.samples.ApplicationKeeper;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
+import java.util.concurrent.CountDownLatch;
+
/**
* The type Dubbo storage service starter.
*/
@@ -31,12 +31,14 @@ public class DubboStorageServiceStarter {
*
* @param args the input arguments
*/
- public static void main(String[] args) {
+ public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext storageContext = new ClassPathXmlApplicationContext("spring/dubbo-storage-service.xml");
storageContext.getBean("service");
JdbcTemplate storageJdbcTemplate = (JdbcTemplate) storageContext.getBean("jdbcTemplate");
storageJdbcTemplate.update("delete from storage_tbl where commodity_code = 'C00321'");
storageJdbcTemplate.update("insert into storage_tbl(commodity_code, count) values ('C00321', 100)");
- new ApplicationKeeper(storageContext).keep();
+
+ System.out.println("storage service started");
+ new CountDownLatch(1).await();
}
}
diff --git a/dubbo-samples-transaction/src/test/java/org/apache/dubbo/samples/service/BusinessIT.java b/dubbo-samples-transaction/src/test/java/org/apache/dubbo/samples/service/BusinessIT.java
new file mode 100644
index 0000000..10354a5
--- /dev/null
+++ b/dubbo-samples-transaction/src/test/java/org/apache/dubbo/samples/service/BusinessIT.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.samples.service;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.junit.Assert.fail;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:/spring/dubbo-business.xml"})
+public class BusinessIT {
+ @Autowired
+ @Qualifier("business")
+ private BusinessService business;
+
+ @Autowired
+ private StorageService storageService;
+
+ @Test
+ public void testRollback() throws Exception {
+ int before = storageService.queryCount("C00321");
+ try {
+ business.purchase("U100001", "C00321", 2);
+ fail("BusinessService.purchase should throw exception.");
+ } catch (Throwable t) {
+ // ignore
+ }
+ int after = storageService.queryCount("C00321");
+ Assert.assertEquals(before, after);
+ }
+
+ @Test
+ public void testCommit() throws Exception {
+ // TODO: need implement a success commit
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org