end() {
+ return Future.fromCompletionStage(close());
}
@Override
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpCommon.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpCommon.java
index 5b70f72766e..ae4b5bac727 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpCommon.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpCommon.java
@@ -20,27 +20,23 @@
import org.apache.servicecomb.foundation.common.io.AsyncCloseable;
-import io.vertx.core.Context;
+import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientResponse;
-import io.vertx.core.streams.Pump;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
+import io.vertx.core.streams.impl.PipeImpl;
public class PumpCommon {
/**
- *
- * @param context
- * @param readStream
- * @param writeStream
* @return future of save action
* important:
*
if writeStream is AsyncCloseable, future means write complete
*
if writeStream is not AsyncCloseable, future only means read complete
*/
@SuppressWarnings("unchecked")
- public CompletableFuture pump(Context context, ReadStream readStream, WriteStream writeStream,
+ public CompletableFuture pump(ReadStream readStream, WriteStream writeStream,
Handler throwableHandler) {
CompletableFuture readFuture = new CompletableFuture<>();
@@ -62,17 +58,18 @@ public CompletableFuture pump(Context context, ReadStream readStre
readFuture.completeExceptionally(e);
});
readStream.exceptionHandler(readFuture::completeExceptionally);
- // just means read finished, not means write finished
- readStream.endHandler(readFuture::complete);
- // if readStream(HttpClientResponse) and writeStream(HttpServerResponse)
- // belongs to difference eventloop
- // maybe will cause deadlock
- // if happened, vertx will print deadlock stacks
- Pump.pump(readStream, writeStream).start();
- context.runOnContext(v -> readStream.resume());
+ Future pipeResult = new PipeImpl<>(readStream).endOnComplete(false).to(writeStream);
- if (!AsyncCloseable.class.isInstance(writeStream)) {
+ pipeResult.onComplete((s) -> readFuture.complete(null),
+ (f) -> {
+ if (throwableHandler != null) {
+ throwableHandler.handle(f);
+ }
+ readFuture.completeExceptionally(f);
+ });
+
+ if (!(writeStream instanceof AsyncCloseable)) {
return readFuture;
}
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpFromPart.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpFromPart.java
index ba7b3182087..2c5284e12dc 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpFromPart.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/stream/PumpFromPart.java
@@ -69,7 +69,7 @@ private CompletableFuture> prepareReadStream() {
public CompletableFuture toWriteStream(WriteStream writeStream, Handler throwableHandler) {
return prepareReadStream()
- .thenCompose(readStream -> new PumpCommon().pump(context, readStream, writeStream, throwableHandler))
+ .thenCompose(readStream -> new PumpCommon().pump(readStream, writeStream, throwableHandler))
.whenComplete((v, e) -> {
if (e != null) {
LOGGER.error("to write stream failed.", e);
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java
index 15e6977d2eb..fcc22bfd7c9 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java
@@ -77,7 +77,7 @@ public NetSocket getNetSocket() {
public void initNetSocket(NetSocketImpl netSocket) {
this.netSocket = netSocket;
- this.context = netSocket.getContext();
+ this.context = netSocket.context();
}
public void write(Buffer buf) {
diff --git a/foundations/foundation-vertx/src/test/java/io/vertx/ext/web/impl/TestHttpServerRequestUtils.java b/foundations/foundation-vertx/src/test/java/io/vertx/ext/web/impl/TestHttpServerRequestUtils.java
deleted file mode 100644
index 7297a866b2f..00000000000
--- a/foundations/foundation-vertx/src/test/java/io/vertx/ext/web/impl/TestHttpServerRequestUtils.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package io.vertx.ext.web.impl;
-
-import org.apache.servicecomb.foundation.vertx.http.VertxServerRequestToHttpServletRequest;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import io.vertx.core.http.impl.HttpServerRequestInternal;
-import io.vertx.core.net.HostAndPort;
-import io.vertx.ext.web.AllowForwardHeaders;
-import io.vertx.ext.web.RequestBody;
-import io.vertx.ext.web.RoutingContext;
-
-// HttpServerRequestWrapper is a package visible class, so put this test in package io.vertx.ext.web.impl
-public class TestHttpServerRequestUtils {
- @Test
- public void testVertxServerRequestToHttpServletRequest() {
- RoutingContext context = Mockito.mock(RoutingContext.class);
- HttpServerRequestInternal request = Mockito.mock(HttpServerRequestInternal.class);
- HttpServerRequestWrapper wrapper = new HttpServerRequestWrapper(request, AllowForwardHeaders.NONE, null);
- Mockito.when(request.scheme()).thenReturn("http");
- Mockito.when(context.request()).thenReturn(wrapper);
- Mockito.when(request.authority()).thenReturn(HostAndPort.create("localhost", 8080));
- RequestBody requestBody = Mockito.mock(RequestBody.class);
- Mockito.when(context.body()).thenReturn(requestBody);
-
- VertxServerRequestToHttpServletRequest reqEx = new VertxServerRequestToHttpServletRequest(context, "abc");
- Assertions.assertEquals("abc", reqEx.getRequestURI());
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestSharedVertxFactory.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestSharedVertxFactory.java
index 10cc0827a0f..8e77a256251 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestSharedVertxFactory.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestSharedVertxFactory.java
@@ -23,7 +23,7 @@
import org.mockito.Mockito;
import org.springframework.core.env.Environment;
-import io.vertx.core.file.impl.FileResolverImpl;
+import io.vertx.core.impl.SysProps;
public class TestSharedVertxFactory {
Environment environment = Mockito.mock(Environment.class);
@@ -32,7 +32,7 @@ public class TestSharedVertxFactory {
public void setUp() {
Mockito.when(environment.getProperty("servicecomb.transport.eventloop.size", int.class, -1))
.thenReturn(-1);
- Mockito.when(environment.getProperty(FileResolverImpl.DISABLE_CP_RESOLVING_PROP_NAME, boolean.class, true))
+ Mockito.when(environment.getProperty(SysProps.DISABLE_FILE_CP_RESOLVING.name, boolean.class, true))
.thenReturn(true);
LegacyPropertyFactory.setEnvironment(environment);
}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestVertxUtils.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestVertxUtils.java
index f3762fe5f37..9f966cbcbde 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestVertxUtils.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/TestVertxUtils.java
@@ -38,14 +38,14 @@
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
-import io.vertx.core.file.impl.FileResolverImpl;
+import io.vertx.core.impl.SysProps;
public class TestVertxUtils {
Environment environment = Mockito.mock(Environment.class);
@BeforeEach
public void setUp() {
- Mockito.when(environment.getProperty(FileResolverImpl.DISABLE_CP_RESOLVING_PROP_NAME, boolean.class, true))
+ Mockito.when(environment.getProperty(SysProps.DISABLE_FILE_CP_RESOLVING.name, boolean.class, true))
.thenReturn(true);
LegacyPropertyFactory.setEnvironment(environment);
@@ -53,7 +53,7 @@ public void setUp() {
@Test
public void testGetOrCreateVertx() throws InterruptedException {
- Vertx vertx = VertxUtils.getOrCreateVertxByName("ut", null);
+ Vertx vertx = VertxUtils.getOrCreateVertxByName("ut", null, null);
Holder name = new Holder<>();
CountDownLatch latch = new CountDownLatch(1);
@@ -70,25 +70,25 @@ public void testGetOrCreateVertx() throws InterruptedException {
@Test
public void testCreateVertxWithFileCPResolving() {
// create .vertx folder
- Mockito.when(environment.getProperty(FileResolverImpl.DISABLE_CP_RESOLVING_PROP_NAME, boolean.class, true))
+ Mockito.when(environment.getProperty(SysProps.DISABLE_FILE_CP_RESOLVING.name, boolean.class, true))
.thenReturn(false);
deleteCacheFile();
- VertxUtils.getOrCreateVertxByName("testCreateVertxWithFileCPResolvingFalse", null);
+ VertxUtils.getOrCreateVertxByName("testCreateVertxWithFileCPResolvingFalse", null, null);
Assertions.assertTrue(isCacheFileExists());
VertxUtils.blockCloseVertxByName("testCreateVertxWithFileCPResolvingFalse");
// don't create .vertx folder
deleteCacheFile();
Assertions.assertFalse(isCacheFileExists());
- Mockito.when(environment.getProperty(FileResolverImpl.DISABLE_CP_RESOLVING_PROP_NAME, boolean.class, true))
+ Mockito.when(environment.getProperty(SysProps.DISABLE_FILE_CP_RESOLVING.name, boolean.class, true))
.thenReturn(true);
- VertxUtils.getOrCreateVertxByName("testCreateVertxWithFileCPResolvingTrue", null);
+ VertxUtils.getOrCreateVertxByName("testCreateVertxWithFileCPResolvingTrue", null, null);
Assertions.assertFalse(isCacheFileExists());
VertxUtils.blockCloseVertxByName("testCreateVertxWithFileCPResolvingTrue");
}
private void deleteCacheFile() {
- String cacheDirBase = System.getProperty(FileResolverImpl.CACHE_DIR_BASE_PROP_NAME,
+ String cacheDirBase = System.getProperty(SysProps.FILE_CACHE_DIR.name,
System.getProperty("java.io.tmpdir", "."));
File folder = new File(cacheDirBase);
File[] files = folder.listFiles();
@@ -100,7 +100,7 @@ private void deleteCacheFile() {
}
private boolean isCacheFileExists() {
- String cacheDirBase = System.getProperty(FileResolverImpl.CACHE_DIR_BASE_PROP_NAME,
+ String cacheDirBase = System.getProperty(SysProps.FILE_CACHE_DIR.name,
System.getProperty("java.io.tmpdir", "."));
File folder = new File(cacheDirBase);
File[] files = folder.listFiles();
@@ -114,7 +114,7 @@ private boolean isCacheFileExists() {
@Test
public void testVertxUtilsInitNullOptions() {
- Vertx vertx = VertxUtils.init(null, null);
+ Vertx vertx = VertxUtils.init(null, null, null);
Assertions.assertNotEquals(null, vertx);
VertxUtils.blockCloseVertx(vertx);
}
@@ -123,7 +123,7 @@ public void testVertxUtilsInitNullOptions() {
public void testVertxUtilsInitWithOptions() {
VertxOptions oOptions = new VertxOptions();
- Vertx vertx = VertxUtils.init(null, oOptions);
+ Vertx vertx = VertxUtils.init(null, oOptions, null);
Assertions.assertNotEquals(null, vertx);
VertxUtils.blockCloseVertx(vertx);
}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClientPoolFactoryTest.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClientPoolFactoryTest.java
deleted file mode 100644
index d4c52133547..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClientPoolFactoryTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.servicecomb.foundation.vertx.client.http;
-
-import io.vertx.core.Context;
-import io.vertx.core.Vertx;
-import io.vertx.core.http.HttpClient;
-import io.vertx.core.http.HttpClientOptions;
-import io.vertx.core.http.PoolOptions;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-import org.springframework.test.util.ReflectionTestUtils;
-
-public class HttpClientPoolFactoryTest {
- private Context context;
- private Vertx vertx;
- private HttpClientPoolFactory factory;
-
- @BeforeEach
- void setUp() {
- // Mock the context and set up the Vertx instance
- context = Mockito.mock(Context.class);
- vertx = Vertx.vertx();
- Mockito.when(context.owner()).thenReturn(vertx);
-
- // Create HttpClientOptions with a specific max pool size
- HttpClientOptions options = new HttpClientOptions();
- options.setMaxPoolSize(123);
-
- // Initialize the HttpClientPoolFactory with the given options
- factory = new HttpClientPoolFactory(options);
- }
-
- @AfterEach
- void tearDown() {
- // Close the Vertx instance to release resources
- vertx.close();
- }
-
- @Test
- void testCreateClientPool() {
- // Create the client pool and get the HttpClient
- HttpClient httpClient = factory.createClientPool(context).getHttpClient();
-
- // Use ReflectionTestUtils to get the poolOptions field from the HttpClient
- if (ReflectionTestUtils.getField(httpClient, "poolOptions") instanceof PoolOptions poolOptions) {
- // Assert that the http1MaxSize is set to the expected value
- Assertions.assertEquals(123, poolOptions.getHttp1MaxSize());
- } else {
- // Fail the test if the poolOptions field is not found or not of the expected type
- Assertions.fail("poolOptions field not found or not of the expected type");
- }
- }
-
- @Test
- void testCreateClientPoolWithDefaultOptions() {
- // Create HttpClientOptions with default values
- HttpClientOptions defaultOptions = new HttpClientOptions();
-
- // Initialize the HttpClientPoolFactory with the default options
- HttpClientPoolFactory defaultFactory = new HttpClientPoolFactory(defaultOptions);
-
- // Create the client pool and get the HttpClient
- HttpClient defaultHttpClient = defaultFactory.createClientPool(context).getHttpClient();
-
- // Use ReflectionTestUtils to get the poolOptions field from the HttpClient
- if (ReflectionTestUtils.getField(defaultHttpClient, "poolOptions") instanceof PoolOptions defaultPoolOptions) {
- // Assert that the http1MaxSize is set to the default value
- Assertions.assertEquals(HttpClientOptions.DEFAULT_MAX_POOL_SIZE, defaultPoolOptions.getHttp1MaxSize());
- } else {
- // Fail the test if the poolOptions field is not found or not of the expected type
- Assertions.fail("poolOptions field not found or not of the expected type");
- }
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestNetClientWrapper.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestNetClientWrapper.java
deleted file mode 100644
index adbcd3e8138..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestNetClientWrapper.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.servicecomb.foundation.vertx.client.tcp;
-
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
-
-import io.vertx.core.AsyncResult;
-import io.vertx.core.Handler;
-import io.vertx.core.Promise;
-import io.vertx.core.Vertx;
-import io.vertx.core.net.NetClient;
-import io.vertx.core.net.NetSocket;
-import mockit.Expectations;
-import mockit.Mock;
-import mockit.MockUp;
-import mockit.Mocked;
-import org.junit.jupiter.api.Assertions;
-
-public class TestNetClientWrapper {
- @Mocked
- Vertx vertx;
-
- @Mocked
- TcpClientConfig normalClientConfig;
-
- @Mocked
- NetClient normalNetClient;
-
- @Mocked
- TcpClientConfig sslClientConfig;
-
- @Mocked
- NetClient sslNetClient;
-
- NetClientWrapper netClientWrapper;
-
- @Before
- public void setup() {
- new Expectations() {
- {
- vertx.createNetClient(normalClientConfig);
- result = normalNetClient;
- vertx.createNetClient(sslClientConfig);
- result = sslNetClient;
- }
- };
- netClientWrapper = new NetClientWrapper(vertx, normalClientConfig, sslClientConfig);
- }
-
- @Test
- public void getClientConfig() {
- Assertions.assertSame(normalClientConfig, netClientWrapper.getClientConfig(false));
- Assertions.assertSame(sslClientConfig, netClientWrapper.getClientConfig(true));
- }
-
- @Test
- public void connect(@Mocked NetSocket normalSocket, @Mocked NetSocket sslSocket) {
- int port = 8000;
- String host = "localhost";
-
- Promise promiseConnect = Promise.promise();
- new MockUp(normalNetClient) {
- @Mock
- NetClient connect(int port, String host, Handler> connectHandler) {
- promiseConnect.complete(normalSocket);
- connectHandler.handle(promiseConnect.future());
- return null;
- }
- };
-
- Promise sslPromiseConnect = Promise.promise();
- new MockUp(sslNetClient) {
- @Mock
- NetClient connect(int port, String host, Handler> connectHandler) {
- sslPromiseConnect.complete(sslSocket);
- connectHandler.handle(sslPromiseConnect.future());
- return null;
- }
- };
-
- List socks = new ArrayList<>();
- netClientWrapper.connect(false, port, host, asyncSocket -> socks.add(asyncSocket.result()));
- netClientWrapper.connect(true, port, host, asyncSocket -> socks.add(asyncSocket.result()));
-
- MatcherAssert.assertThat(socks, Matchers.contains(normalSocket, sslSocket));
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnection.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnection.java
index de87122f1ba..f072d962000 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnection.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnection.java
@@ -28,8 +28,8 @@
import org.junit.jupiter.api.Assertions;
import io.netty.buffer.ByteBuf;
-import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
+import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
@@ -195,9 +195,9 @@ public void connect_success(@Mocked NetSocketImpl netSocket) {
Promise promise = Promise.promise();
new MockUp(netClientWrapper) {
@Mock
- void connect(boolean ssl, int port, String host, Handler> connectHandler) {
+ public Future connect(boolean ssl, int port, String host) {
promise.complete(netSocket);
- connectHandler.handle(promise.future());
+ return promise.future();
}
};
@@ -216,9 +216,9 @@ public void connect_failed() {
RuntimeException error = new RuntimeExceptionWithoutStackTrace();
new MockUp(netClientWrapper) {
@Mock
- void connect(boolean ssl, int port, String host, Handler> connectHandler) {
+ public Future connect(boolean ssl, int port, String host) {
promise.fail(error);
- connectHandler.handle(promise.future());
+ return promise.future();
}
};
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnectionPool.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnectionPool.java
deleted file mode 100644
index dfaf8ec60a2..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/client/tcp/TestTcpClientConnectionPool.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.servicecomb.foundation.vertx.client.tcp;
-
-import io.vertx.core.impl.ContextInternal;
-import io.vertx.core.impl.VertxInternal;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-public class TestTcpClientConnectionPool {
-
- TcpClientConnectionPool pool;
-
- @BeforeEach
- public void setup() {
- ContextInternal context = Mockito.mock(ContextInternal.class);
- VertxInternal vertx = Mockito.mock(VertxInternal.class);
- Mockito.when(context.owner()).thenReturn(vertx);
- NetClientWrapper netClientWrapper = Mockito.mock(NetClientWrapper.class);
- pool = new TcpClientConnectionPool(context, netClientWrapper);
- }
-
- @Test
- public void create() {
- Assertions.assertTrue(pool.create("rest://localhost:8765") instanceof TcpClientConnection);
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestReadStreamPart.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestReadStreamPart.java
deleted file mode 100644
index 03d425e353c..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestReadStreamPart.java
+++ /dev/null
@@ -1,200 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.servicecomb.foundation.vertx.http;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.UUID;
-import java.util.concurrent.ExecutionException;
-
-import jakarta.ws.rs.core.HttpHeaders;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace;
-import org.apache.servicecomb.foundation.vertx.stream.InputStreamToReadStream;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.jupiter.api.Assertions;
-
-import io.vertx.core.Handler;
-import io.vertx.core.Vertx;
-import io.vertx.core.buffer.Buffer;
-import io.vertx.core.file.FileSystemException;
-import io.vertx.core.file.OpenOptions;
-import io.vertx.core.http.HttpClientResponse;
-import io.vertx.core.impl.SyncContext;
-import io.vertx.core.impl.VertxInternal;
-import io.vertx.core.streams.WriteStream;
-import mockit.Expectations;
-import mockit.Mock;
-import mockit.MockUp;
-import mockit.Mocked;
-
-public class TestReadStreamPart {
- static Vertx vertx = Vertx.vertx();
-
- static SyncContext context = new SyncContext();
-
- static String src = "src";
-
- static InputStream inputStream = new ByteArrayInputStream(src.getBytes());
-
- InputStreamToReadStream readStream = new InputStreamToReadStream(context, inputStream, true);
-
- ReadStreamPart part = new ReadStreamPart(context, readStream);
-
- @Before
- public void setup() throws IOException {
- context.setOwner((VertxInternal) vertx);
- inputStream.reset();
- }
-
- @AfterClass
- public static void teardown() {
- vertx.close();
- }
-
- @Test
- public void constructFromHttpClientResponse_noContentType(@Mocked HttpClientResponse httpClientResponse) {
- new Expectations() {
- {
- httpClientResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION);
- result = "xx;filename=name.txt";
- httpClientResponse.getHeader(HttpHeaders.CONTENT_TYPE);
- result = null;
- }
- };
-
- part = new ReadStreamPart(context, httpClientResponse);
-
- Assertions.assertEquals("name.txt", part.getSubmittedFileName());
- Assertions.assertEquals("text/plain", part.getContentType());
- }
-
- @Test
- public void constructFromHttpClientResponse_hasContentType(@Mocked HttpClientResponse httpClientResponse) {
- new Expectations() {
- {
- httpClientResponse.getHeader(HttpHeaders.CONTENT_DISPOSITION);
- result = "xx;filename=name.txt";
- httpClientResponse.getHeader(HttpHeaders.CONTENT_TYPE);
- result = "type";
- }
- };
-
- part = new ReadStreamPart(context, httpClientResponse);
-
- Assertions.assertEquals("name.txt", part.getSubmittedFileName());
- Assertions.assertEquals("type", part.getContentType());
- }
-
- @Test
- public void saveToWriteStream() throws InterruptedException, ExecutionException {
- Buffer buf = Buffer.buffer();
- WriteStream writeStream = new MockUp>() {
- @Mock
- WriteStream write(Buffer data) {
- buf.appendBuffer(data);
- return null;
- }
- }.getMockInstance();
-
- part.saveToWriteStream(writeStream).get();
-
- Assertions.assertEquals(src, buf.toString());
- }
-
- @Test
- public void saveToWriteStream_writeException() throws InterruptedException, ExecutionException {
- RuntimeException error = new RuntimeExceptionWithoutStackTrace();
- WriteStream writeStream = new MockUp>() {
- Handler exceptionHandler;
-
- @Mock
- WriteStream exceptionHandler(Handler handler) {
- this.exceptionHandler = handler;
- return null;
- }
-
- @Mock
- WriteStream write(Buffer data) {
- exceptionHandler.handle(error);
- return null;
- }
- }.getMockInstance();
-
- ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> part.saveToWriteStream(writeStream).get());
- Assertions.assertTrue(exception.getCause() instanceof RuntimeException);
- }
-
- @Test
- public void saveToWrite_readException(@Mocked WriteStream writeStream) {
- RuntimeException error = new RuntimeExceptionWithoutStackTrace();
- new MockUp(inputStream) {
- @Mock
- int read(byte[] b) throws IOException {
- throw error;
- }
- };
-
- ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> part.saveToWriteStream(writeStream).get());
- Assertions.assertTrue(exception.getCause() instanceof RuntimeException);
- }
-
- @Test
- public void saveAsBytes() throws InterruptedException, ExecutionException {
- Assertions.assertArrayEquals(src.getBytes(), part.saveAsBytes().get());
- }
-
- @Test
- public void saveAsString() throws InterruptedException, ExecutionException {
- Assertions.assertEquals(src, part.saveAsString().get());
- }
-
- @Test
- public void saveToFile() throws InterruptedException, ExecutionException, IOException {
- File dir = new File("target/notExist-" + UUID.randomUUID());
- File file = new File(dir, "a.txt");
-
- Assertions.assertFalse(dir.exists());
-
- part.saveToFile(file.getAbsolutePath()).get();
-
- Assertions.assertEquals(src, FileUtils.readFileToString(file, StandardCharsets.UTF_8));
-
- FileUtils.forceDelete(dir);
- Assertions.assertFalse(dir.exists());
- }
-
- @Test
- public void saveToFile_notExist_notCreate() throws InterruptedException, ExecutionException, IOException {
- File dir = new File("target/notExist-" + UUID.randomUUID());
- File file = new File(dir, "a.txt");
-
- Assertions.assertFalse(dir.exists());
-
- ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> {
- OpenOptions openOptions = new OpenOptions().setCreateNew(false);
- part.saveToFile(file, openOptions).get();
- });
- Assertions.assertTrue(exception.getCause() instanceof FileSystemException);
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
deleted file mode 100644
index 1ab6d81bc1c..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
+++ /dev/null
@@ -1,411 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.servicecomb.foundation.vertx.http;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.UUID;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.servicecomb.foundation.common.http.HttpStatus;
-import org.apache.servicecomb.foundation.common.part.FilePart;
-import org.apache.servicecomb.foundation.vertx.stream.PumpFromPart;
-import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.jupiter.api.Assertions;
-
-import io.vertx.core.AsyncResult;
-import io.vertx.core.Context;
-import io.vertx.core.Handler;
-import io.vertx.core.MultiMap;
-import io.vertx.core.Promise;
-import io.vertx.core.Vertx;
-import io.vertx.core.buffer.Buffer;
-import io.vertx.core.http.HttpServerResponse;
-import io.vertx.core.impl.SyncContext;
-import io.vertx.core.streams.WriteStream;
-import jakarta.servlet.http.Part;
-import jakarta.ws.rs.core.HttpHeaders;
-import jakarta.ws.rs.core.Response.StatusType;
-import mockit.Deencapsulation;
-import mockit.Expectations;
-import mockit.Mock;
-import mockit.MockUp;
-import mockit.Mocked;
-
-public class TestVertxServerResponseToHttpServletResponse {
- MultiMap headers = MultiMap.caseInsensitiveMultiMap();
-
- HttpStatus httpStatus = new HttpStatus(123, "default");
-
- HttpServerResponse serverResponse;
-
- VertxServerResponseToHttpServletResponse response;
-
- boolean flushWithBody;
-
- boolean runOnContextInvoked;
-
- @Mocked
- Vertx vertx;
-
- @Mocked
- Context context;
-
- boolean chunked;
-
- @Before
- public void setup() {
- serverResponse = new MockUp() {
- @Mock
- HttpServerResponse setStatusCode(int statusCode) {
- Deencapsulation.setField(httpStatus, "statusCode", statusCode);
- return serverResponse;
- }
-
- @Mock
- HttpServerResponse setStatusMessage(String statusMessage) {
- Deencapsulation.setField(httpStatus, "reason", statusMessage);
- return serverResponse;
- }
-
- @Mock
- int getStatusCode() {
- return httpStatus.getStatusCode();
- }
-
- @Mock
- String getStatusMessage() {
- return httpStatus.getReasonPhrase();
- }
-
- @Mock
- MultiMap headers() {
- return headers;
- }
-
- @Mock
- HttpServerResponse putHeader(String name, String value) {
- headers.set(name, value);
- return serverResponse;
- }
-
- @Mock
- void end() {
- flushWithBody = false;
- }
-
- @Mock
- void end(Buffer chunk) {
- flushWithBody = true;
- }
-
- @Mock
- HttpServerResponse setChunked(boolean chunked) {
- TestVertxServerResponseToHttpServletResponse.this.chunked = chunked;
- return serverResponse;
- }
-
- @Mock
- boolean isChunked() {
- return chunked;
- }
- }.getMockInstance();
-
- new Expectations() {
- {
- Vertx.currentContext();
- result = context;
- }
- };
-
- new MockUp(context) {
- @Mock
- void runOnContext(Handler action) {
- runOnContextInvoked = true;
- action.handle(null);
- }
-
- @Mock
- void executeBlocking(Handler> blockingCodeHandler, boolean ordered,
- Handler> resultHandler) {
- SyncContext.syncExecuteBlocking(blockingCodeHandler, resultHandler);
- }
-
- @Mock
- Vertx owner() {
- return vertx;
- }
- };
-
- response = new VertxServerResponseToHttpServletResponse(serverResponse);
- }
-
- @Test
- public void construct_invalid() throws IOException {
- new Expectations() {
- {
- Vertx.currentContext();
- result = null;
- }
- };
-
- NullPointerException exception = Assertions.assertThrows(NullPointerException.class,
- () -> new VertxServerResponseToHttpServletResponse(serverResponse));
- Assertions.assertEquals("must run in vertx context.", exception.getMessage());
- }
-
- @Test
- public void setContentType() {
- response.setContentType("json");
- Assertions.assertEquals("json", headers.get(HttpHeaders.CONTENT_TYPE));
- }
-
- @Test
- public void getStatusType() {
- StatusType status = response.getStatusType();
-
- Assertions.assertSame(status, response.getStatusType());
- Assertions.assertEquals(123, httpStatus.getStatusCode());
- Assertions.assertEquals("default", httpStatus.getReasonPhrase());
- }
-
- @Test
- public void addHeader() {
- response.addHeader("n1", "v1_1");
- response.addHeader("n1", "v1_2");
- response.addHeader("n2", "v2");
-
- Assertions.assertEquals(2, headers.size());
- MatcherAssert.assertThat(headers.getAll("n1"), Matchers.contains("v1_1", "v1_2"));
- MatcherAssert.assertThat(headers.getAll("n2"), Matchers.contains("v2"));
- }
-
- @Test
- public void setHeader() {
- response.setHeader("n1", "v1_1");
- response.setHeader("n1", "v1_2");
- response.setHeader("n2", "v2");
-
- Assertions.assertEquals(2, headers.size());
- MatcherAssert.assertThat(headers.getAll("n1"), Matchers.contains("v1_2"));
- MatcherAssert.assertThat(headers.getAll("n2"), Matchers.contains("v2"));
- }
-
- @Test
- public void getStatus() {
- Assertions.assertEquals(123, response.getStatus());
- }
-
- @Test
- public void getContentType() {
- headers.set(HttpHeaders.CONTENT_TYPE, "json");
- Assertions.assertEquals("json", response.getContentType());
- }
-
- @Test
- public void getHeader() {
- headers.set(HttpHeaders.CONTENT_TYPE, "json");
- Assertions.assertEquals("json", response.getHeader(HttpHeaders.CONTENT_TYPE));
- }
-
- @Test
- public void getHeaders() {
- headers.add("h1", "h1_1");
- headers.add("h1", "h1_2");
-
- MatcherAssert.assertThat(response.getHeaders("h1"), Matchers.contains("h1_1", "h1_2"));
- }
-
- @Test
- public void getHeaderNames() {
- headers.add("h1", "h1");
- headers.add("h2", "h2");
-
- MatcherAssert.assertThat(response.getHeaderNames(), Matchers.contains("h1", "h2"));
- }
-
- @Test
- public void flushBuffer_sameContext() throws IOException {
- response.endResponse();
-
- Assertions.assertFalse(runOnContextInvoked);
- }
-
- @Test
- public void flushBuffer_diffContext() throws IOException {
- new Expectations() {
- {
- Vertx.currentContext();
- result = null;
- }
- };
- response.endResponse();
-
- Assertions.assertTrue(runOnContextInvoked);
- }
-
- @Test
- public void internalFlushBufferNoBody() throws IOException {
- response.internalFlushBuffer();
-
- Assertions.assertFalse(flushWithBody);
- }
-
- @Test
- public void internalFlushBufferWithBody() throws IOException {
- response.setBodyBuffer(Buffer.buffer());
- response.internalFlushBuffer();
-
- Assertions.assertFalse(flushWithBody);
- }
-
- @Test
- public void prepareSendPartHeader_update(@Mocked Part part) {
- new Expectations() {
- {
- part.getContentType();
- result = "type";
- part.getSubmittedFileName();
- result = "测 试";
- }
- };
- DownloadUtils.prepareDownloadHeader(response, part);
-
- Assertions.assertTrue(serverResponse.isChunked());
- Assertions.assertEquals("type", response.getHeader(HttpHeaders.CONTENT_TYPE));
- Assertions.assertEquals(
- "attachment;filename=%E6%B5%8B%20%20%20%20%20%E8%AF%95;filename*=utf-8''%E6%B5%8B%20%20%20%20%20%E8%AF%95",
- response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
- }
-
- @Test
- public void prepareSendPartHeader_notUpdate(@Mocked Part part) {
- headers.add(HttpHeaders.CONTENT_LENGTH, "10");
- headers.add(HttpHeaders.CONTENT_TYPE, "type");
- headers.add(HttpHeaders.CONTENT_DISPOSITION, "disposition");
-
- DownloadUtils.prepareDownloadHeader(response, part);
-
- Assertions.assertFalse(serverResponse.isChunked());
- Assertions.assertEquals("type", response.getHeader(HttpHeaders.CONTENT_TYPE));
- Assertions.assertEquals("disposition", response.getHeader(HttpHeaders.CONTENT_DISPOSITION));
- }
-
- @Test
- public void sendPart_openInputStreamFailed(@Mocked Part part)
- throws IOException {
- IOException ioException = new IOException("forbid open stream");
- new Expectations() {
- {
- part.getInputStream();
- result = ioException;
- }
- };
-
- CompletableFuture future = response.sendPart(part);
-
- ExecutionException exception = Assertions.assertThrows(ExecutionException.class, future::get);
- Assertions.assertTrue(exception.getCause() instanceof IOException);
- }
-
- @Test
- public void sendPart_testPartIsNull(@Mocked Part part) throws InterruptedException, ExecutionException {
- CompletableFuture future1 = response.sendPart(null);
- Assertions.assertNull(future1.get());
- }
-
- @Test
- public void sendPart_inputStreamBreak(@Mocked Part part, @Mocked InputStream inputStream)
- throws IOException {
- IOException ioException = new IOException("forbid read");
- new Expectations() {
- {
- part.getInputStream();
- result = inputStream;
- inputStream.read((byte[]) any);
- result = ioException;
- }
- };
-
- CompletableFuture future = response.sendPart(part);
-
- ExecutionException exception = Assertions.assertThrows(ExecutionException.class, future::get);
- Assertions.assertTrue(exception.getCause() instanceof IOException);
- }
-
- @Test
- public void sendPart_ReadStreamPart(@Mocked ReadStreamPart part) {
- CompletableFuture future = new CompletableFuture<>();
- new MockUp() {
- @Mock
- CompletableFuture toWriteStream(WriteStream writeStream, Handler throwableHandler) {
- return future;
- }
- };
-
- Assertions.assertSame(future, response.sendPart(part));
- }
-
- @Test
- public void sendPart_succ(@Mocked Part part, @Mocked InputStream inputStream)
- throws IOException, InterruptedException, ExecutionException {
- new Expectations() {
- {
- part.getInputStream();
- result = inputStream;
- inputStream.read((byte[]) any);
- result = -1;
- }
- };
-
- CompletableFuture future = response.sendPart(part);
-
- Assertions.assertNull(future.get());
- }
-
- @Test
- public void clearPartResource_deleteFile() throws IOException {
- File file = new File("target", UUID.randomUUID() + ".txt");
- FileUtils.write(file, "content", StandardCharsets.UTF_8);
- FilePart part = new FilePart(null, file).setDeleteAfterFinished(true);
-
- Assertions.assertTrue(file.exists());
- DownloadUtils.clearPartResource(part);
- Assertions.assertFalse(file.exists());
- }
-
- @Test
- public void clearPartResource_notDeleteFile() throws IOException {
- File file = new File("target", UUID.randomUUID() + ".txt");
- FileUtils.write(file, "content", StandardCharsets.UTF_8);
- FilePart part = new FilePart(null, file);
-
- Assertions.assertTrue(file.exists());
- DownloadUtils.clearPartResource(part);
- Assertions.assertTrue(file.exists());
-
- file.delete();
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.java
index 1ae56ebee4a..ccc67374c82 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.java
@@ -29,14 +29,12 @@ public class TestDefaultVertxMetricsFactory {
DefaultVertxMetricsFactory factory = new DefaultVertxMetricsFactory();
- @SuppressWarnings("deprecation")
@Test
public void metrics() {
MetricsOptions metricsOptions = factory.newOptions();
options.setMetricsOptions(metricsOptions);
VertxMetrics vertxMetrics = factory.metrics(options);
- Assertions.assertSame(factory, metricsOptions.getFactory());
Assertions.assertTrue(metricsOptions.isEnabled());
Assertions.assertSame(factory.getVertxMetrics(), vertxMetrics);
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/server/TestTcpServer.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/server/TestTcpServer.java
deleted file mode 100644
index 489a21d8a1e..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/server/TestTcpServer.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.servicecomb.foundation.vertx.server;
-
-import static org.mockito.ArgumentMatchers.any;
-
-import java.net.InetSocketAddress;
-
-import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
-import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
-import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
-import org.apache.servicecomb.foundation.vertx.metrics.DefaultTcpServerMetrics;
-import org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultServerEndpointMetric;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-import org.mockito.stubbing.Answer;
-import org.springframework.core.env.Environment;
-
-import io.vertx.core.Handler;
-import io.vertx.core.Vertx;
-import io.vertx.core.net.NetServer;
-import io.vertx.core.net.NetSocket;
-import io.vertx.core.net.SocketAddress;
-import io.vertx.core.net.impl.NetSocketImpl;
-
-public class TestTcpServer {
- static class TcpServerForTest extends TcpServer {
- public TcpServerForTest(URIEndpointObject endpointObject) {
- super(endpointObject);
- }
-
- @Override
- protected TcpServerConnection createTcpServerConnection() {
- return new TcpServerConnection() {
- @Override
- public void init(NetSocket netSocket) {
- super.init(netSocket);
- }
- };
- }
- }
-
- protected Environment environment;
-
- @BeforeEach
- public void setup() {
- environment = Mockito.mock(Environment.class);
- LegacyPropertyFactory.setEnvironment(environment);
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- @Test
- public void testTcpServerNonSSL() {
- Vertx vertx = Mockito.mock(Vertx.class);
- AsyncResultCallback callback = Mockito.mock(AsyncResultCallback.class);
- NetServer netServer = Mockito.mock(NetServer.class);
- Mockito.when(vertx.createNetServer()).thenReturn(netServer);
-
- URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663");
- TcpServer server = new TcpServerForTest(endpointObject);
- // assert done in Expectations
- server.init(vertx, "", callback);
- }
-
- Handler connectHandler;
-
- boolean netSocketClosed;
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- @Test
- public void testConnectionLimit() {
- Vertx vertx = Mockito.mock(Vertx.class);
- AsyncResultCallback callback = Mockito.mock(AsyncResultCallback.class);
- NetServer netServer = Mockito.mock(NetServer.class);
- NetSocketImpl netSocket = Mockito.mock(NetSocketImpl.class);
- Mockito.when(vertx.createNetServer(any())).thenReturn(netServer);
-
- DefaultServerEndpointMetric endpointMetric = new DefaultServerEndpointMetric(null);
- DefaultTcpServerMetrics tcpServerMetrics = new DefaultTcpServerMetrics(endpointMetric);
-
- Mockito.doAnswer((Answer) invocationOnMock -> {
- connectHandler = invocationOnMock.getArgument(0);
- return netServer;
- }).when(netServer).connectHandler(any());
-
- Mockito.doAnswer((Answer) invocationOnMock -> {
- netSocketClosed = true;
- return null;
- }).when(netSocket).close();
- Mockito.when(netSocket.metrics()).thenReturn(tcpServerMetrics);
- SocketAddress socketAddress = Mockito.mock(SocketAddress.class);
- Mockito.when(netSocket.remoteAddress()).thenReturn(socketAddress);
- Mockito.when(socketAddress.toString()).thenReturn("127.0.0.1:6663");
-
- URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true");
- TcpServer server = new TcpServerForTest(endpointObject) {
- @Override
- protected int getConnectionLimit() {
- return 2;
- }
- };
- // assert done in Expectations
- server.init(vertx, "", callback);
-
- // no problem
- endpointMetric.onConnect();
- endpointMetric.onConnect();
- connectHandler.handle(netSocket);
-
- // reject
- endpointMetric.onConnect();
- connectHandler.handle(netSocket);
- Assertions.assertTrue(netSocketClosed);
- Assertions.assertEquals(1, endpointMetric.getRejectByConnectionLimitCount());
- }
-}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/stream/TestPumpFromPart.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/stream/TestPumpFromPart.java
deleted file mode 100644
index b32e790e00b..00000000000
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/stream/TestPumpFromPart.java
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.servicecomb.foundation.vertx.stream;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.concurrent.ExecutionException;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.RandomStringUtils;
-import org.apache.servicecomb.foundation.common.part.InputStreamPart;
-import org.apache.servicecomb.foundation.vertx.stream.InputStreamToReadStream.ReadResult;
-import org.hamcrest.MatcherAssert;
-import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.jupiter.api.Assertions;
-
-import io.vertx.core.Context;
-import io.vertx.core.Promise;
-import io.vertx.core.Vertx;
-import io.vertx.core.impl.SyncContext;
-import io.vertx.core.impl.VertxInternal;
-import jakarta.servlet.http.Part;
-import mockit.Expectations;
-import mockit.Mock;
-import mockit.MockUp;
-
-public class TestPumpFromPart {
- static Vertx vertx = Vertx.vertx();
-
- String src = RandomStringUtils.random(100, true, true);
-
- boolean inputStreamClosed;
-
- InputStream inputStream = new ByteArrayInputStream(src.getBytes()) {
- @Override
- public void close() throws IOException {
- super.close();
- inputStreamClosed = true;
- }
- };
-
- Part part;
-
- boolean outputStreamClosed;
-
- BufferOutputStream outputStream;
-
- IOException error = new IOException();
-
- SyncContext context = new SyncContext();
-
- @Before
- public void setup() throws IOException {
- context.setOwner((VertxInternal) vertx);
- }
-
- private void run(Context context, boolean closeOutput) throws Throwable {
- inputStream.reset();
- part = new InputStreamPart("name", inputStream);
-
- outputStream = new BufferOutputStream() {
- @Override
- public void close() {
- super.close();
- outputStreamClosed = true;
- }
- };
-
- new PumpFromPart(context, part).toOutputStream(outputStream, closeOutput).get();
- }
-
- public void do_pump_succ(Context context) throws Throwable {
- run(context, true);
-
- Assertions.assertEquals(src, outputStream.getBuffer().toString());
- Assertions.assertTrue(inputStreamClosed);
- Assertions.assertTrue(outputStreamClosed);
- }
-
- @Test
- public void pump_succ() throws Throwable {
- do_pump_succ(null);
- do_pump_succ(context);
- }
-
- public void do_pump_outputNotClose(Context context) throws Throwable {
- run(context, false);
-
- Assertions.assertEquals(src, outputStream.getBuffer().toString());
- Assertions.assertFalse(outputStreamClosed);
- }
-
- @Test
- public void pump_outputNotClose() throws Throwable {
- do_pump_outputNotClose(null);
- do_pump_outputNotClose(context);
- }
-
- public void pump_error(Context context) {
- try {
- run(context, true);
- Assertions.fail("must throw exception");
- } catch (Throwable e) {
- MatcherAssert.assertThat(e, Matchers.instanceOf(ExecutionException.class));
- MatcherAssert.assertThat(e.getCause(), Matchers.sameInstance(error));
- }
-
- Assertions.assertTrue(inputStreamClosed);
- Assertions.assertTrue(outputStreamClosed);
- }
-
- @Test
- public void pump_read_error() throws IOException {
- new MockUp() {
- @Mock
- void readInWorker(Promise future) {
- future.fail(error);
- }
- };
- new Expectations(IOUtils.class) {
- {
- IOUtils.copyLarge((InputStream) any, (OutputStream) any);
- result = error;
- }
- };
-
- pump_error(null);
- Assertions.assertTrue(inputStreamClosed);
- Assertions.assertTrue(outputStreamClosed);
-
- inputStreamClosed = false;
- outputStreamClosed = false;
- pump_error(context);
- Assertions.assertTrue(inputStreamClosed);
- Assertions.assertTrue(outputStreamClosed);
- }
-
- @Test
- public void pump_write_error() throws IOException {
- new MockUp() {
- @Mock
- void write(byte[] b) throws IOException {
- throw error;
- }
- };
- new Expectations(IOUtils.class) {
- {
- IOUtils.copyLarge((InputStream) any, (OutputStream) any);
- result = error;
- }
- };
-
- pump_error(null);
- Assertions.assertTrue(inputStreamClosed);
- Assertions.assertTrue(outputStreamClosed);
-
- inputStreamClosed = false;
- outputStreamClosed = false;
- pump_error(context);
- Assertions.assertTrue(inputStreamClosed);
- Assertions.assertTrue(outputStreamClosed);
- }
-}
diff --git a/metrics/metrics-core/pom.xml b/metrics/metrics-core/pom.xml
index d3d71ff8ef1..c68b483fde9 100644
--- a/metrics/metrics-core/pom.xml
+++ b/metrics/metrics-core/pom.xml
@@ -59,16 +59,6 @@
vertx-codegen
provided