Skip to content

Commit

Permalink
Fixes @EnabledInNativeImage failure in some unit tests (#30031)
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian authored Feb 7, 2024
1 parent d1a61ff commit d4b515d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import javax.sql.DataSource;
import java.sql.SQLException;

@EnabledInNativeImage
class PostgresTest {

private TestShardingService testShardingService;

@EnabledInNativeImage
@Test
void assertShardingInLocalTransactions() throws SQLException {
HikariConfig config = new HikariConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import javax.sql.DataSource;
import java.sql.SQLException;

@EnabledInNativeImage
class SQLServerTest {

private TestShardingService testShardingService;

@EnabledInNativeImage
@Test
void assertShardingInLocalTransactions() throws SQLException {
HikariConfig config = new HikariConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.etcd.jetcd.test.EtcdClusterExtension;
import io.etcd.jetcd.launcher.Etcd;
import io.etcd.jetcd.launcher.EtcdCluster;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
Expand All @@ -30,27 +31,20 @@
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledInNativeImage;
import org.junit.jupiter.api.extension.RegisterExtension;

import javax.sql.DataSource;
import java.io.IOException;
import java.net.URI;
import java.sql.SQLException;
import java.time.Duration;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

@EnabledInNativeImage
class EtcdTest {

@RegisterExtension
public static final EtcdClusterExtension CLUSTER = EtcdClusterExtension.builder()
.withNodes(1)
.withMountDirectory(false)
.build();

private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.mode.cluster.etcd.";

private TestShardingService testShardingService;
Expand All @@ -63,16 +57,24 @@ class EtcdTest {
* @see org.apache.shardingsphere.mode.repository.cluster.etcd.EtcdRepository
*/
@Test
@EnabledInNativeImage
void assertShardingInLocalTransactions() throws SQLException {
DataSource dataSource = createDataSource();
testShardingService = new TestShardingService(dataSource);
initEnvironment();
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() -> {
dataSource.getConnection().close();
return true;
});
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
try (
EtcdCluster etcd = Etcd.builder()
.withNodes(1)
.withMountedDataDirectory(false)
.build()) {
etcd.start();
DataSource dataSource = createDataSource(etcd.clientEndpoints());
testShardingService = new TestShardingService(dataSource);
initEnvironment();
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() -> {
dataSource.getConnection().close();
return true;
});
testShardingService.processSuccess();
testShardingService.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand All @@ -84,8 +86,8 @@ private void initEnvironment() throws SQLException {
testShardingService.getAddressRepository().truncateTable();
}

private DataSource createDataSource() {
URI clientEndpoint = CLUSTER.clientEndpoints().get(0);
private DataSource createDataSource(final List<URI> clientEndpoints) {
URI clientEndpoint = clientEndpoints.get(0);
Awaitility.await().atMost(Duration.ofSeconds(30L)).ignoreExceptions().until(() -> verifyEtcdClusterRunning(clientEndpoint));
HikariConfig config = new HikariConfig();
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");
Expand Down

0 comments on commit d4b515d

Please sign in to comment.