Skip to content

Commit

Permalink
fixed non-idempotent unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyaok2 committed May 10, 2024
1 parent 5d916cd commit 46ab618
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void test() throws Exception {
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();
cg.setClassName(className);
cg.addInterface(BaseInterface.class);
cg.setSuperClass(BaseClass.class);
Expand Down Expand Up @@ -212,7 +212,7 @@ void testMain0() throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ private void afterExport() {
@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ private void afterInvoke() {
@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ private void afterInvoke() {
@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ private void afterInvoke() {}
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ private void afterExport() {
@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ private void afterInvoke() {
@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ private void afterInvoke() {
@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ReferenceCacheTest {
public void setUp() throws Exception {
DubboBootstrap.reset();
MockReferenceConfig.setCounter(0);
XxxMockReferenceConfig.setCounter(0);
SimpleReferenceCache.CACHE_HOLDER.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PrometheusMetricsReporterTest {
private MetricsConfig metricsConfig;
private ApplicationModel applicationModel;
private FrameworkModel frameworkModel;
HttpServer prometheusExporterHttpServer;

@BeforeEach
public void setup() {
Expand All @@ -64,6 +65,9 @@ public void setup() {
@AfterEach
public void teardown() {
applicationModel.destroy();
if (prometheusExporterHttpServer != null) {
prometheusExporterHttpServer.stop(0);
}
}

@Test
Expand Down Expand Up @@ -146,7 +150,7 @@ void testPushgateway() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class PrometheusMetricsThreadPoolTest {

DefaultMetricsCollector metricsCollector;

HttpServer prometheusExporterHttpServer;

@BeforeEach
public void setup() {
applicationModel = ApplicationModel.defaultModel();
Expand All @@ -77,6 +79,9 @@ public void setup() {
@AfterEach
public void teardown() {
applicationModel.destroy();
if (prometheusExporterHttpServer != null) {
prometheusExporterHttpServer.stop(0);
}
}

@Test
Expand Down Expand Up @@ -121,7 +126,7 @@ void testExporterThreadpoolName() {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
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;
Expand Down Expand Up @@ -139,7 +141,13 @@ void testCount() {
}

@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")
Expand All @@ -163,12 +171,12 @@ void testMonitorFactory() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void setUp() {
@AfterEach
public void reset() {
frameworkModel.destroy();
MockLivenessProbe.setCheckReturnValue(false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
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();
Expand Down Expand Up @@ -138,4 +144,12 @@ public static int getReceivedCount() {
public static int getCaughtCount() {
return caughtCount;
}

public static void reset() {
sentCount = 0;
connectedCount = 0;
disconnectedCount = 0;
receivedCount = 0;
caughtCount = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ void testBeginCountEndCount() {
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);
Expand All @@ -65,6 +69,10 @@ void testBeginCountEndCount() {
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();
Expand Down Expand Up @@ -99,6 +107,10 @@ void testStatistics() {
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);
Expand Down

0 comments on commit 46ab618

Please sign in to comment.