diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 4670599c2dc..1aeed1673d9 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -75,6 +75,7 @@ Add changes here for all PR submitted to the 2.x branch. ### test: +- [[#7635](https://github.com/apache/incubator-seata/pull/7635)] fix JUnit test method access modifiers and annotations - [[#7541](https://github.com/seata/seata/pull/7541)] fix jakarta UT failed in jdk17+ - [[#7540](https://github.com/seata/seata/pull/7540)] fix port of mock server - [[#7580](https://github.com/seata/seata/pull/7580)] fix the exception caused by the disorder of test case execution order @@ -118,6 +119,7 @@ Thanks to these contributors for their code commits. Please report an unintended - [WangzJi](https://github.com/WangzJi) - [unifolio0](https://github.com/unifolio0) - [Asuka-star](https://github.com/Asuka-star) +- [contrueCT](https://github.com/contrueCT) - [YoWuwuuuw](https://github.com/YoWuwuuuw) - [jongmin-chung](https://github.com/jongmin-chung) - [jihun4452](https://github.com/jihun4452) diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 8fb0e909a81..1a7bc9977d1 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -73,6 +73,7 @@ ### test: +- [[#7635](https://github.com/apache/incubator-seata/pull/7635)] 修正 JUnit 5 测试方法的访问修饰符及注解使用规范 - [[#7541](https://github.com/seata/seata/pull/7541)] 修复 jakarta 依赖在 jdk17+ 单测失败问题 - [[#7540](https://github.com/seata/seata/pull/7540)] 修复mock server端口冲突问题 - [[#7578](https://github.com/seata/seata/pull/7578)] zstd解压由jni改为ZstdInputStream @@ -115,6 +116,7 @@ - [WangzJi](https://github.com/WangzJi) - [unifolio0](https://github.com/unifolio0) - [Asuka-star](https://github.com/Asuka-star) +- [contrueCT](https://github.com/contrueCT) - [YoWuwuuuw](https://github.com/YoWuwuuuw) - [jongmin-chung](https://github.com/jongmin-chung) - [jihun4452](https://github.com/jihun4452) diff --git a/discovery/seata-discovery-etcd3/pom.xml b/discovery/seata-discovery-etcd3/pom.xml index 53a68488c55..d2c175bb2ed 100644 --- a/discovery/seata-discovery-etcd3/pom.xml +++ b/discovery/seata-discovery-etcd3/pom.xml @@ -64,5 +64,10 @@ native-lib-loader test + + org.mockito + mockito-core + test + diff --git a/discovery/seata-discovery-etcd3/src/test/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImplTest.java b/discovery/seata-discovery-etcd3/src/test/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImplTest.java index bfa91598215..e0829b385fe 100644 --- a/discovery/seata-discovery-etcd3/src/test/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImplTest.java +++ b/discovery/seata-discovery-etcd3/src/test/java/org/apache/seata/discovery/registry/etcd3/EtcdRegistryServiceImplTest.java @@ -18,19 +18,23 @@ import io.etcd.jetcd.ByteSequence; import io.etcd.jetcd.Client; +import io.etcd.jetcd.KV; import io.etcd.jetcd.Watch; -import io.etcd.jetcd.launcher.junit4.EtcdClusterResource; +import io.etcd.jetcd.launcher.EtcdCluster; +import io.etcd.jetcd.launcher.EtcdClusterFactory; import io.etcd.jetcd.options.DeleteOption; import io.etcd.jetcd.options.GetOption; import io.etcd.jetcd.watch.WatchResponse; import org.apache.seata.discovery.registry.RegistryService; -import org.junit.Rule; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.net.InetSocketAddress; +import java.net.URI; import java.util.List; import java.util.concurrent.TimeUnit; @@ -42,34 +46,54 @@ public class EtcdRegistryServiceImplTest { private static final String REGISTRY_KEY_PREFIX = "registry-seata-"; private static final String CLUSTER_NAME = "default"; - - @Rule - private static final EtcdClusterResource etcd = new EtcdClusterResource(CLUSTER_NAME, 1); - - private final Client client = - Client.builder().endpoints(etcd.getClientEndpoints()).build(); private static final String HOST = "127.0.0.1"; private static final int PORT = 8091; + private static EtcdCluster etcd; + private static Client client; + private static List clientEndpoints; + @BeforeAll - public static void beforeClass() throws Exception { - System.setProperty( - EtcdRegistryServiceImpl.TEST_ENDPONT, - etcd.getClientEndpoints().get(0).toString()); + public static void beforeAll() { + etcd = EtcdClusterFactory.buildCluster(CLUSTER_NAME, 1, false); + etcd.start(); + clientEndpoints = etcd.getClientEndpoints(); + client = Client.builder().endpoints(clientEndpoints).build(); } @AfterAll - public static void afterClass() throws Exception { - System.setProperty(EtcdRegistryServiceImpl.TEST_ENDPONT, ""); + public static void afterAll() { + if (client != null) { + client.close(); + } + if (etcd != null) { + etcd.close(); + } + System.clearProperty(EtcdRegistryServiceImpl.TEST_ENDPONT); + } + + @BeforeEach + public void setUp() { + String endpoint = clientEndpoints.get(0).toString(); + System.setProperty(EtcdRegistryServiceImpl.TEST_ENDPONT, endpoint); + } + + @AfterEach + public void tearDown() throws Exception { + KV kvClient = client.getKVClient(); + ByteSequence keyPrefix = buildRegistryKeyPrefix(); + DeleteOption deleteOption = + DeleteOption.newBuilder().withPrefix(keyPrefix).build(); + kvClient.delete(keyPrefix, deleteOption).get(); } @Test public void testRegister() throws Exception { RegistryService registryService = new EtcdRegistryProvider().provide(); InetSocketAddress inetSocketAddress = new InetSocketAddress(HOST, PORT); - // 1.register + // 1. Register the service instance. registryService.register(inetSocketAddress); - // 2.get instance information + // 2. Verify the registration by directly querying etcd. GetOption getOption = GetOption.newBuilder().withPrefix(buildRegistryKeyPrefix()).build(); long count = client.getKVClient().get(buildRegistryKeyPrefix(), getOption).get().getKvs().stream() @@ -87,7 +111,7 @@ public void testUnregister() throws Exception { InetSocketAddress inetSocketAddress = new InetSocketAddress(HOST, PORT); // 1.register registryService.register(inetSocketAddress); - // 2.get instance information + // 2. Verify it was registered successfully. GetOption getOption = GetOption.newBuilder().withPrefix(buildRegistryKeyPrefix()).build(); long count = client.getKVClient().get(buildRegistryKeyPrefix(), getOption).get().getKvs().stream() @@ -97,10 +121,9 @@ public void testUnregister() throws Exception { }) .count(); assertThat(count).isEqualTo(1); - // 3.unregister + // 3. Unregister the instance. registryService.unregister(inetSocketAddress); - // 4.again get instance information - getOption = GetOption.newBuilder().withPrefix(buildRegistryKeyPrefix()).build(); + // 4. Verify it was successfully removed from etcd. count = client.getKVClient().get(buildRegistryKeyPrefix(), getOption).get().getKvs().stream() .filter(keyValue -> { String[] instanceInfo = keyValue.getValue().toString(UTF_8).split(":"); @@ -118,8 +141,8 @@ public void testSubscribe() throws Exception { registryService.register(inetSocketAddress); // 2.subscribe EtcdListener etcdListener = new EtcdListener(); - registryService.subscribe(CLUSTER_NAME, etcdListener); - // 3.delete instance,see if the listener can be notified + registryService.subscribe(DEFAULT_TX_GROUP, etcdListener); + // 3. Delete the instance key and verify the listener is notified. DeleteOption deleteOption = DeleteOption.newBuilder().withPrefix(buildRegistryKeyPrefix()).build(); client.getKVClient().delete(buildRegistryKeyPrefix(), deleteOption).get(); @@ -134,14 +157,14 @@ public void testUnsubscribe() throws Exception { registryService.register(inetSocketAddress); // 2.subscribe EtcdListener etcdListener = new EtcdListener(); - registryService.subscribe(CLUSTER_NAME, etcdListener); + registryService.subscribe(DEFAULT_TX_GROUP, etcdListener); // 3.delete instance,see if the listener can be notified DeleteOption deleteOption = DeleteOption.newBuilder().withPrefix(buildRegistryKeyPrefix()).build(); client.getKVClient().delete(buildRegistryKeyPrefix(), deleteOption).get(); assertThat(etcdListener.isNotified()).isTrue(); // 4.unsubscribe - registryService.unsubscribe(CLUSTER_NAME, etcdListener); + registryService.unsubscribe(DEFAULT_TX_GROUP, etcdListener); // 5.reset etcdListener.reset(); // 6.put instance,the listener should not be notified @@ -159,23 +182,26 @@ public void testLookup() throws Exception { registryService.register(inetSocketAddress); // 2.lookup List inetSocketAddresses = registryService.lookup(DEFAULT_TX_GROUP); - assertThat(inetSocketAddresses).size().isEqualTo(1); + // 3.Verify that the correct instance is returned. + assertThat(inetSocketAddresses).hasSize(1); + assertThat(inetSocketAddresses.get(0).getAddress().getHostAddress()).isEqualTo(HOST); + assertThat(inetSocketAddresses.get(0).getPort()).isEqualTo(PORT); } /** - * build registry key prefix - * - * @return + * Builds the etcd key prefix for a given service group. + * The key prefix includes the transaction service group as is standard in Seata. + * @return ByteSequence of the prefix */ private ByteSequence buildRegistryKeyPrefix() { - return ByteSequence.from(REGISTRY_KEY_PREFIX, UTF_8); + return ByteSequence.from(REGISTRY_KEY_PREFIX + DEFAULT_TX_GROUP, UTF_8); } /** - * etcd listener + * Listener implementation for testing subscription notifications. */ private static class EtcdListener implements Watch.Listener { - private boolean notified = false; + private volatile boolean notified = false; @Override public void onNext(WatchResponse response) { @@ -183,23 +209,29 @@ public void onNext(WatchResponse response) { } @Override - public void onError(Throwable throwable) {} + public void onError(Throwable throwable) { + // No-op for this test + } @Override - public void onCompleted() {} + public void onCompleted() { + // No-op for this test + } /** - * @return + * Waits for a short period to allow the async notification to arrive. + * @return true if a notification was received. */ public boolean isNotified() throws InterruptedException { - TimeUnit.SECONDS.sleep(3); + // Give some time for the watch event to be processed + TimeUnit.SECONDS.sleep(1); return notified; } /** - * reset + * Resets the notification flag for subsequent assertions. */ - private void reset() { + public void reset() { this.notified = false; } } diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/BaseH2Test.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/BaseH2Test.java index d7debd86f32..c4b94e922aa 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/BaseH2Test.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/undo/BaseH2Test.java @@ -77,7 +77,7 @@ public static void stop() { } @BeforeEach - private void prepareTable() { + public void prepareTable() { execSQL("DROP TABLE IF EXISTS table_name"); execSQL("CREATE TABLE table_name ( `id` int(8), `name` varchar(64), PRIMARY KEY (`id`))"); } diff --git a/server/src/test/java/org/apache/seata/server/ParameterParserTest.java b/server/src/test/java/org/apache/seata/server/ParameterParserTest.java index e632c5e91c6..88ce1036549 100644 --- a/server/src/test/java/org/apache/seata/server/ParameterParserTest.java +++ b/server/src/test/java/org/apache/seata/server/ParameterParserTest.java @@ -32,7 +32,7 @@ public class ParameterParserTest extends BaseSpringBootTest { * init */ @BeforeEach - private void init() { + public void init() { String[] args = new String[] {"-h", "127.0.0.1", "-p", "8088", "-m", "file", "-e", "test"}; parameterParser = new ParameterParser(args); } diff --git a/test/src/test/java/org/apache/seata/at/ATModeSupportDataBaseDataTypeTest.java b/test/src/test/java/org/apache/seata/at/ATModeSupportDataBaseDataTypeTest.java index 4a2307883cf..d760d35f9c4 100644 --- a/test/src/test/java/org/apache/seata/at/ATModeSupportDataBaseDataTypeTest.java +++ b/test/src/test/java/org/apache/seata/at/ATModeSupportDataBaseDataTypeTest.java @@ -91,7 +91,6 @@ public void doType(int sqlType, int type, boolean globalCommit) throws Throwable testTypeSql(sqlType, globalCommit, sqlClass.getInsertSql(), sqlClass.getTableName(), sqlClass.getUpdateSql()); } - @Test public void testTypeSql(int sqlType, boolean globalCommit, String insertSql, String tableName, String updateSql) throws Throwable { doExecute(sqlType, insertSql); @@ -249,12 +248,13 @@ private SqlClass dogetType(int sqlType, int type) { case 3: sqlClass.setInsertSql(TEST_DATE_TYPE_INSERT_SQL); sqlClass.setTableName(DATE_TABLE_NAME); - sqlClass.setTableName(TEST_DATE_TYPE_UPDATE_SQL); + sqlClass.setUpdateSql(TEST_DATE_TYPE_UPDATE_SQL); break; case 4: sqlClass.setInsertSql(TEST_BINARY_TYPE_INSERT_SQL); sqlClass.setTableName(BINARY_TABLE_NAME); - sqlClass.setTableName(TEST_BINARY_TYPE_UPDATE_SQL); + sqlClass.setUpdateSql(TEST_BINARY_TYPE_UPDATE_SQL); + break; default: } break; diff --git a/test/src/test/java/org/apache/seata/at/oracle/SupportOracleDataTypeTest.java b/test/src/test/java/org/apache/seata/at/oracle/SupportOracleDataTypeTest.java index 9e3be3e6f53..a6805c0a14e 100644 --- a/test/src/test/java/org/apache/seata/at/oracle/SupportOracleDataTypeTest.java +++ b/test/src/test/java/org/apache/seata/at/oracle/SupportOracleDataTypeTest.java @@ -110,7 +110,6 @@ public void doType(int type, boolean globalCommit) throws Throwable { testTypeSql(ORACLE, globalCommit, insertSql, tableName, updateSql); } - @Test public void testTypeSql(int sqlType, boolean globalCommit, String insertSql, String tableName, String updateSql) throws Throwable { doExecute(sqlType, insertSql);