From bd7ad855d1d9a2e8c306427c86a1f80d404ef669 Mon Sep 17 00:00:00 2001 From: funkye Date: Wed, 17 Apr 2024 22:20:50 +0800 Subject: [PATCH] test: support Redis integration testing (#6466) --- .github/workflows/build.yml | 8 ++- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + .../seata/config/FileConfigurationTest.java | 16 ++++-- dependencies/pom.xml | 6 -- discovery/seata-discovery-redis/pom.xml | 5 -- .../redis/RedisRegisterServiceImplTest.java | 21 ++----- server/pom.xml | 6 -- .../lock/redis/RedisLockManagerTest.java | 6 +- .../lock/redis/RedisLuaLockManagerTest.java | 4 +- .../server/session/redis/MockRedisServer.java | 57 ------------------- .../redis/RedisDistributedLockerTest.java | 3 +- .../RedisLuaTransactionStoreManagerTest.java | 3 +- .../session/redis/RedisQueryConsolTest.java | 2 + .../redis/RedisSessionManagerTest.java | 3 +- .../RedisTransactionStoreManagerTest.java | 3 +- 16 files changed, 38 insertions(+), 107 deletions(-) delete mode 100644 server/src/test/java/org/apache/seata/server/session/redis/MockRedisServer.java diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index db561ebe1a5..d57645eb419 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,6 +10,12 @@ jobs: # job 1: Test based on java 8 and 17. Do not checkstyle. build: name: "build" + services: + redis: + image: redis:7.2 + ports: + - 6379:6379 + options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3 runs-on: ubuntu-latest strategy: fail-fast: false @@ -34,7 +40,7 @@ jobs: run: | ./mvnw -T 4C clean test \ -Dcheckstyle.skip=false -Dpmd.skip=false -Dlicense.skip=false \ - -Dmaven.git-commit-id.skip=true \ + -Dmaven.git-commit-id.skip=true -DredisCaseEnabled=true \ -e -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn; # step 4.2 - name: "Test with Maven and Java${{ matrix.java }}" diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 135db8ab894..cd2c4546b6e 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -148,6 +148,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6325](https://github.com/apache/incubator-seata/pull/6325)] fix mockServerTest fail cause using same port with seata-server - [[#6430](https://github.com/apache/incubator-seata/pull/6430)] increase common module unit test coverage - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] adjust the test cases related to dynamic configuration +- [[#6466](https://github.com/apache/incubator-seata/pull/6466)] support redis integration testing ### refactor: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 0e258f449be..667e968ce52 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -144,6 +144,7 @@ - [[#6325](https://github.com/apache/incubator-seata/pull/6325)] 修复mock-server相关测试用例 - [[#6430](https://github.com/apache/incubator-seata/pull/6430)] 增加 common 模块单元测试覆盖率 - [[#6456](https://github.com/apache/incubator-seata/pull/6456)] 调整动态配置监听测试用例 +- [[#6466](https://github.com/apache/incubator-seata/pull/6466)] 支持redis的集成测试 ### refactor: diff --git a/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java b/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java index ee0b3a83b75..cee604a17b9 100644 --- a/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java +++ b/config/seata-config-core/src/test/java/org/apache/seata/config/FileConfigurationTest.java @@ -23,20 +23,23 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; class FileConfigurationTest { - + Logger logger = LoggerFactory.getLogger(FileConfigurationTest.class); @BeforeEach void setUp() { + System.setProperty("file.listener.enabled", "true"); ConfigurationCache.clear(); } @AfterEach void tearDown() { ConfigurationCache.clear(); + System.setProperty("file.listener.enabled", "true"); } @Test @@ -48,13 +51,14 @@ void addConfigListener() throws InterruptedException { fileConfig.addConfigListener(dataId, (CachedConfigurationChangeListener)event -> { Assertions.assertEquals(Boolean.parseBoolean(event.getNewValue()), !Boolean.parseBoolean(event.getOldValue())); - System.out.println("oldValue:" + event.getOldValue() + ",newValue:" + event.getNewValue()); + logger.info("dataId: {}, oldValue: {}, newValue: {}", event.getDataId(), event.getOldValue(), + event.getNewValue()); countDownLatch.countDown(); }); System.setProperty(dataId, String.valueOf(!value)); - countDownLatch.await(10, TimeUnit.SECONDS); - System.out.println(fileConfig.getBoolean(dataId)); - System.out.println(value); + countDownLatch.await(60, TimeUnit.SECONDS); + logger.info("dataId: {}, oldValue: {}", dataId, value); + logger.info("dataId: {}, currenValue: {}", dataId, fileConfig.getBoolean(dataId)); Assertions.assertNotEquals(fileConfig.getBoolean(dataId), value); //wait for loop safety, loop time is LISTENER_CONFIG_INTERVAL=1s CountDownLatch countDownLatch2 = new CountDownLatch(1); diff --git a/dependencies/pom.xml b/dependencies/pom.xml index 4b0b3729e31..02a016f36c5 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -52,7 +52,6 @@ 3.7.2 5.1.0 1.0.2 - 0.3.1 2.0.1 1.10.18 1.5.4 @@ -416,11 +415,6 @@ jedis ${jedis.version} - - com.github.microwww - redis-server - ${mock-jedis.version} - com.netflix.eureka eureka-client diff --git a/discovery/seata-discovery-redis/pom.xml b/discovery/seata-discovery-redis/pom.xml index e716ff639d7..969979b1173 100644 --- a/discovery/seata-discovery-redis/pom.xml +++ b/discovery/seata-discovery-redis/pom.xml @@ -39,10 +39,5 @@ redis.clients jedis - - com.github.microwww - redis-server - test - diff --git a/discovery/seata-discovery-redis/src/test/java/org/apache/seata/discovery/registry/redis/RedisRegisterServiceImplTest.java b/discovery/seata-discovery-redis/src/test/java/org/apache/seata/discovery/registry/redis/RedisRegisterServiceImplTest.java index d1a66a9adfd..36079b12e8e 100644 --- a/discovery/seata-discovery-redis/src/test/java/org/apache/seata/discovery/registry/redis/RedisRegisterServiceImplTest.java +++ b/discovery/seata-discovery-redis/src/test/java/org/apache/seata/discovery/registry/redis/RedisRegisterServiceImplTest.java @@ -16,11 +16,12 @@ */ package org.apache.seata.discovery.registry.redis; -import com.github.microwww.redis.RedisServer; import org.apache.seata.common.util.NetUtil; import org.apache.seata.config.Configuration; import org.apache.seata.config.ConfigurationFactory; import org.junit.jupiter.api.*; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.mockito.MockedStatic; import org.mockito.internal.util.collections.Sets; @@ -38,23 +39,19 @@ import static org.mockito.Mockito.when; +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisRegisterServiceImplTest { private static RedisRegistryServiceImpl redisRegistryService; - private static RedisServer server; - - @BeforeAll public static void init() throws IOException { System.setProperty("config.type", "file"); System.setProperty("config.file.name", "file.conf"); System.setProperty("txServiceGroup", "default_tx_group"); System.setProperty("service.vgroupMapping.default_tx_group", "default"); - System.setProperty("registry.redis.serverAddr", "127.0.0.1:6789"); + System.setProperty("registry.redis.serverAddr", "127.0.0.1:6379"); System.setProperty("registry.redis.cluster", "default"); - RedisServer server = new RedisServer(); - server.listener("127.0.0.1", 6789); redisRegistryService = RedisRegistryServiceImpl.getInstance(); } @@ -103,14 +100,4 @@ public void testRemoveServerAddressByPushEmptyProtection() Assertions.assertEquals(0, CLUSTER_ADDRESS_MAP.get("cluster").size()); } - @AfterAll - public static void afterAll() { - if (server != null) { - try { - server.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } } diff --git a/server/pom.xml b/server/pom.xml index fd7f6de18d4..af05df095e5 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -231,12 +231,6 @@ jedis - - com.github.microwww - redis-server - test - - com.alibaba fastjson diff --git a/server/src/test/java/org/apache/seata/server/lock/redis/RedisLockManagerTest.java b/server/src/test/java/org/apache/seata/server/lock/redis/RedisLockManagerTest.java index 0526294de40..f4924e405d5 100644 --- a/server/src/test/java/org/apache/seata/server/lock/redis/RedisLockManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/lock/redis/RedisLockManagerTest.java @@ -24,13 +24,13 @@ import org.apache.seata.core.model.LockStatus; import org.apache.seata.server.lock.LockManager; import org.apache.seata.server.session.BranchSession; -import org.apache.seata.server.session.redis.MockRedisServer; import org.apache.seata.server.storage.redis.JedisPooledFactory; import org.apache.seata.server.storage.redis.lock.RedisLockManager; import org.apache.seata.server.storage.redis.lock.RedisLocker; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import redis.clients.jedis.Jedis; @@ -40,6 +40,7 @@ /** */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisLockManagerTest { static LockManager lockManager = null; @@ -54,11 +55,10 @@ public class RedisLockManagerTest { */ @BeforeAll public static void start(ApplicationContext context) throws IOException { - MockRedisServer.getInstance(); JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMinIdle(1); poolConfig.setMaxIdle(10); - JedisPooledFactory.getJedisPoolInstance(new JedisPool(poolConfig, "127.0.0.1", 6789, 60000)).getResource(); + JedisPooledFactory.getJedisPoolInstance(new JedisPool(poolConfig, "127.0.0.1", 6379, 60000)).getResource(); lockManager = new RedisLockManagerForTest(); } diff --git a/server/src/test/java/org/apache/seata/server/lock/redis/RedisLuaLockManagerTest.java b/server/src/test/java/org/apache/seata/server/lock/redis/RedisLuaLockManagerTest.java index 0cc4e65adea..4effb309e44 100644 --- a/server/src/test/java/org/apache/seata/server/lock/redis/RedisLuaLockManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/lock/redis/RedisLuaLockManagerTest.java @@ -21,11 +21,11 @@ import org.apache.seata.common.loader.EnhancedServiceLoader; import org.apache.seata.core.lock.Locker; import org.apache.seata.server.session.BranchSession; -import org.apache.seata.server.session.redis.MockRedisServer; import org.apache.seata.server.storage.redis.lock.RedisLockManager; import org.apache.seata.server.storage.redis.lock.RedisLuaLocker; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; @@ -34,6 +34,7 @@ * */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisLuaLockManagerTest extends RedisLockManagerTest { /** @@ -46,7 +47,6 @@ public class RedisLuaLockManagerTest extends RedisLockManagerTest { */ @BeforeAll public static void start(ApplicationContext context) throws IOException { - MockRedisServer.getInstance(); EnhancedServiceLoader.unloadAll(); lockManager = new RedisLuaLockManagerTest.RedisLockManagerForTest(); } diff --git a/server/src/test/java/org/apache/seata/server/session/redis/MockRedisServer.java b/server/src/test/java/org/apache/seata/server/session/redis/MockRedisServer.java deleted file mode 100644 index c51f793bcca..00000000000 --- a/server/src/test/java/org/apache/seata/server/session/redis/MockRedisServer.java +++ /dev/null @@ -1,57 +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.seata.server.session.redis; - -import java.io.IOException; -import com.github.microwww.redis.RedisServer; -import org.apache.seata.server.storage.redis.JedisPooledFactory; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; - -/** - */ -public class MockRedisServer { - - static { - RedisServer server = new RedisServer(); - try { - server.listener("127.0.0.1", 6789); - } catch (IOException e) { - e.printStackTrace(); - } - JedisPoolConfig poolConfig = new JedisPoolConfig(); - poolConfig.setMinIdle(10); - poolConfig.setMaxIdle(100); - JedisPooledFactory.getJedisPoolInstance(new JedisPool(poolConfig, "127.0.0.1", 6789, 2000)); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - try { - server.close(); - } catch (IOException e) { - e.printStackTrace(); - } - })); - } - - public static MockRedisServer getInstance() { - return Instance.mockredis; - } - - private static class Instance { - private static final MockRedisServer mockredis = new MockRedisServer(); - } - -} diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java index 0d6652c244e..b571a9a40d6 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisDistributedLockerTest.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import redis.clients.jedis.Jedis; @@ -40,6 +41,7 @@ * */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisDistributedLockerTest { private String retryRollbacking = "RetryRollbacking"; @@ -52,7 +54,6 @@ public class RedisDistributedLockerTest { @BeforeAll public static void start(ApplicationContext context) throws IOException { EnhancedServiceLoader.unload(DistributedLocker.class); - MockRedisServer.getInstance(); DistributedLockerFactory.cleanLocker(); distributedLocker = DistributedLockerFactory.getDistributedLocker(StoreMode.REDIS.getName()); jedis = JedisPooledFactory.getJedisInstance(); diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisLuaTransactionStoreManagerTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisLuaTransactionStoreManagerTest.java index 7fe0cf04ff8..b52d23528c9 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisLuaTransactionStoreManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisLuaTransactionStoreManagerTest.java @@ -23,6 +23,7 @@ import org.apache.seata.server.storage.redis.store.RedisLuaTransactionStoreManager; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; @@ -31,6 +32,7 @@ * */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisLuaTransactionStoreManagerTest extends RedisTransactionStoreManagerTest { /** @@ -43,7 +45,6 @@ public class RedisLuaTransactionStoreManagerTest extends RedisTransactionStoreMa */ @BeforeAll public static void start(ApplicationContext context) throws IOException { - MockRedisServer.getInstance(); EnhancedServiceLoader.unloadAll(); redisTransactionStoreManager = new RedisLuaTransactionStoreManager(); RedisSessionManager redisSessionManager = new RedisSessionManager(); diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisQueryConsolTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisQueryConsolTest.java index 563f96f7e10..86de42afe04 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisQueryConsolTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisQueryConsolTest.java @@ -27,11 +27,13 @@ import org.apache.seata.server.console.vo.GlobalLockVO; import org.apache.seata.server.console.vo.GlobalSessionVO; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.springframework.boot.test.context.SpringBootTest; /** */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisQueryConsolTest { /** diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisSessionManagerTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisSessionManagerTest.java index a7e40a0a1c4..7a75f60a772 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisSessionManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisSessionManagerTest.java @@ -37,6 +37,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; @@ -48,6 +49,7 @@ /** */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisSessionManagerTest { private static final Logger LOGGER = LoggerFactory.getLogger(RedisSessionManagerTest.class); @@ -55,7 +57,6 @@ public class RedisSessionManagerTest { @BeforeAll public static void start(ApplicationContext context) throws IOException { - MockRedisServer.getInstance(); EnhancedServiceLoader.unloadAll(); RedisTransactionStoreManager transactionStoreManager = RedisTransactionStoreManager.getInstance(); RedisSessionManager redisSessionManager = new RedisSessionManager(); diff --git a/server/src/test/java/org/apache/seata/server/session/redis/RedisTransactionStoreManagerTest.java b/server/src/test/java/org/apache/seata/server/session/redis/RedisTransactionStoreManagerTest.java index 460023b0e83..25755fe1cfb 100644 --- a/server/src/test/java/org/apache/seata/server/session/redis/RedisTransactionStoreManagerTest.java +++ b/server/src/test/java/org/apache/seata/server/session/redis/RedisTransactionStoreManagerTest.java @@ -41,6 +41,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; @@ -51,6 +52,7 @@ /** */ @SpringBootTest +@EnabledIfSystemProperty(named = "redisCaseEnabled", matches = "true") public class RedisTransactionStoreManagerTest { private static final Logger LOGGER = LoggerFactory.getLogger(RedisTransactionStoreManagerTest.class); @@ -60,7 +62,6 @@ public class RedisTransactionStoreManagerTest { @BeforeAll public static void start(ApplicationContext context) throws IOException { - MockRedisServer.getInstance(); EnhancedServiceLoader.unloadAll(); JedisPooledFactory.getJedisInstance().flushAll(); redisTransactionStoreManager = RedisTransactionStoreManager.getInstance();