fixed non-idempotent unit tests (#14172)

This commit is contained in:
Kaiyao Ke 2024-05-10 11:27:46 -05:00 committed by GitHub
parent 5d916cd2c1
commit 8e2eb332a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 53 additions and 14 deletions

View File

@ -57,7 +57,8 @@ class ClassGeneratorTest {
ClassGenerator cg = ClassGenerator.newInstance();
// add className, interface, superClass
String className = BaseClass.class.getPackage().getName() + ".TestClass";
String className = BaseClass.class.getPackage().getName() + ".TestClass"
+ UUID.randomUUID().toString().replace("-", "");
cg.setClassName(className);
cg.addInterface(BaseInterface.class);
cg.setSuperClass(BaseClass.class);
@ -212,7 +213,7 @@ class ClassGeneratorTest {
fname.setAccessible(true);
ClassGenerator cg = ClassGenerator.newInstance();
cg.setClassName(Bean.class.getName() + "$Builder2");
cg.setClassName(Bean.class.getName() + "$Builder2" + UUID.randomUUID().toString());
cg.addInterface(Builder.class);
cg.addField("FNAME", Modifier.PUBLIC | Modifier.STATIC, java.lang.reflect.Field.class);

View File

@ -172,7 +172,6 @@ class MultipleRegistryCenterExportMetadataIntegrationTest implements Integration
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// The exported service has been unexported
Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());

View File

@ -239,7 +239,6 @@ class MultipleRegistryCenterExportProviderIntegrationTest implements Integration
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// The exported service has been unexported
Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());

View File

@ -185,7 +185,6 @@ class MultipleRegistryCenterInjvmIntegrationTest implements IntegrationTest {
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// The exported service has been unexported
Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());

View File

@ -201,7 +201,6 @@ class MultipleRegistryCenterServiceDiscoveryRegistryIntegrationTest implements I
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// TODO: we need to check whether this scenario is normal
// TODO: the Exporter and ServiceDiscoveryRegistry are same in multiple registry center

View File

@ -169,7 +169,6 @@ class SingleRegistryCenterExportMetadataIntegrationTest implements IntegrationTe
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// The exported service has been unexported
Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());

View File

@ -240,7 +240,6 @@ class SingleRegistryCenterExportProviderIntegrationTest implements IntegrationTe
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// The exported service has been unexported
Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());

View File

@ -185,7 +185,6 @@ class SingleRegistryCenterInjvmIntegrationTest implements IntegrationTest {
@AfterEach
public void tearDown() throws IOException {
DubboBootstrap.reset();
PROVIDER_APPLICATION_NAME = null;
serviceConfig = null;
// The exported service has been unexported
Assertions.assertTrue(serviceListener.getExportedServices().isEmpty());

View File

@ -35,6 +35,7 @@ class ReferenceCacheTest {
public void setUp() throws Exception {
DubboBootstrap.reset();
MockReferenceConfig.setCounter(0);
XxxMockReferenceConfig.setCounter(0);
SimpleReferenceCache.CACHE_HOLDER.clear();
}

View File

@ -51,6 +51,7 @@ class PrometheusMetricsReporterTest {
private MetricsConfig metricsConfig;
private ApplicationModel applicationModel;
private FrameworkModel frameworkModel;
HttpServer prometheusExporterHttpServer;
@BeforeEach
public void setup() {
@ -64,6 +65,9 @@ class PrometheusMetricsReporterTest {
@AfterEach
public void teardown() {
applicationModel.destroy();
if (prometheusExporterHttpServer != null) {
prometheusExporterHttpServer.stop(0);
}
}
@Test
@ -146,7 +150,7 @@ class PrometheusMetricsReporterTest {
private void exportHttpServer(PrometheusMetricsReporter reporter, int port) {
try {
HttpServer prometheusExporterHttpServer = HttpServer.create(new InetSocketAddress(port), 0);
prometheusExporterHttpServer = HttpServer.create(new InetSocketAddress(port), 0);
prometheusExporterHttpServer.createContext("/metrics", httpExchange -> {
reporter.resetIfSamplesChanged();
String response = reporter.getPrometheusRegistry().scrape();

View File

@ -62,6 +62,8 @@ public class PrometheusMetricsThreadPoolTest {
DefaultMetricsCollector metricsCollector;
HttpServer prometheusExporterHttpServer;
@BeforeEach
public void setup() {
applicationModel = ApplicationModel.defaultModel();
@ -77,6 +79,9 @@ public class PrometheusMetricsThreadPoolTest {
@AfterEach
public void teardown() {
applicationModel.destroy();
if (prometheusExporterHttpServer != null) {
prometheusExporterHttpServer.stop(0);
}
}
@Test
@ -121,7 +126,7 @@ public class PrometheusMetricsThreadPoolTest {
private void exportHttpServer(PrometheusMetricsReporter reporter, int port) {
try {
HttpServer prometheusExporterHttpServer = HttpServer.create(new InetSocketAddress(port), 0);
prometheusExporterHttpServer = HttpServer.create(new InetSocketAddress(port), 0);
prometheusExporterHttpServer.createContext("/metrics", httpExchange -> {
reporter.resetIfSamplesChanged();
String response = reporter.getPrometheusRegistry().scrape();

View File

@ -30,6 +30,8 @@ import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@ -139,7 +141,13 @@ class DubboMonitorTest {
}
@Test
void testMonitorFactory() {
void testMonitorFactory() throws IOException {
int port;
try (ServerSocket socket = new ServerSocket(0)) {
port = socket.getLocalPort();
socket.close();
}
MockMonitorService monitorService = new MockMonitorService();
URL statistics = new URLBuilder(DUBBO_PROTOCOL, "10.20.153.10", 0)
.addParameter(APPLICATION_KEY, "morgan")
@ -163,12 +171,12 @@ class DubboMonitorTest {
Exporter<MonitorService> exporter = protocol.export(proxyFactory.getInvoker(
monitorService,
MonitorService.class,
URL.valueOf("dubbo://127.0.0.1:17979/" + MonitorService.class.getName())));
URL.valueOf("dubbo://127.0.0.1:" + port + "/" + MonitorService.class.getName())));
try {
Monitor monitor = null;
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < 60000) {
monitor = monitorFactory.getMonitor(URL.valueOf("dubbo://127.0.0.1:17979?interval=10"));
monitor = monitorFactory.getMonitor(URL.valueOf("dubbo://127.0.0.1:" + port + "?interval=10"));
if (monitor == null) {
continue;
}

View File

@ -35,6 +35,7 @@ class LiveTest {
@AfterEach
public void reset() {
frameworkModel.destroy();
MockLivenessProbe.setCheckReturnValue(false);
}
@Test

View File

@ -24,12 +24,18 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
class ChannelHandlerDispatcherTest {
@AfterEach
public void tearDown() {
MockChannelHandler.reset();
}
@Test
void test() {
ChannelHandlerDispatcher channelHandlerDispatcher = new ChannelHandlerDispatcher();
@ -138,4 +144,12 @@ class MockChannelHandler extends ChannelHandlerAdapter {
public static int getCaughtCount() {
return caughtCount;
}
public static void reset() {
sentCount = 0;
connectedCount = 0;
disconnectedCount = 0;
receivedCount = 0;
caughtCount = 0;
}
}

View File

@ -39,6 +39,10 @@ class RpcStatusTest {
URL url = new ServiceConfigURL("dubbo", "127.0.0.1", 91031, DemoService.class.getName());
String methodName = "testBeginCountEndCount";
int max = 2;
RpcStatus.removeStatus(url);
RpcStatus.removeStatus(url, methodName);
boolean flag = RpcStatus.beginCount(url, methodName, max);
RpcStatus urlRpcStatus = RpcStatus.getStatus(url);
RpcStatus methodRpcStatus = RpcStatus.getStatus(url, methodName);
@ -65,6 +69,10 @@ class RpcStatusTest {
void testBeginCountEndCountInMultiThread() throws Exception {
URL url = new ServiceConfigURL("dubbo", "127.0.0.1", 91032, DemoService.class.getName());
String methodName = "testBeginCountEndCountInMultiThread";
RpcStatus.removeStatus(url);
RpcStatus.removeStatus(url, methodName);
int max = 50;
int threadNum = 10;
AtomicInteger successCount = new AtomicInteger();
@ -99,6 +107,10 @@ class RpcStatusTest {
URL url = new ServiceConfigURL("dubbo", "127.0.0.1", 91033, DemoService.class.getName());
String methodName = "testStatistics";
int max = 0;
RpcStatus.removeStatus(url);
RpcStatus.removeStatus(url, methodName);
RpcStatus.beginCount(url, methodName, max);
RpcStatus.beginCount(url, methodName, max);
RpcStatus.beginCount(url, methodName, max);