-
Notifications
You must be signed in to change notification settings - Fork 8.9k
test: fix JUnit test method access modifiers and annotations #7635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Changes from 23 commits
569241e
13b93a3
85f24e2
0e3d952
aa5114a
a7d4b12
6554f46
b4dfb3a
c518bb3
6f7990c
ad95dfc
53dfaa2
033c627
dab561a
3cf788e
9d2baee
54f9a30
6e403e3
4117cd8
6c2af4e
7adbefb
1716ed2
ac9973c
9d2a0b3
44011fd
2c9e734
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<URI> 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, was there any particular reason for using DEFAULT_TX_GROUP instead of CLUSTER_NAME? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! The subscribe() and lookup() methods expect a transaction service group, not an etcd cluster name.
Even though both values were "default", using DEFAULT_TX_GROUP is semantically correct and matches how Seata actually works - clients look up services by transaction group. |
||
// 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,47 +182,56 @@ public void testLookup() throws Exception { | |
registryService.register(inetSocketAddress); | ||
// 2.lookup | ||
List<InetSocketAddress> 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) { | ||
notified = true; | ||
} | ||
|
||
@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; | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.