From 40ded0075f9d3a3302f9bed4f74d2b056392c53f Mon Sep 17 00:00:00 2001 From: dilanSachi Date: Mon, 8 Jan 2024 16:32:33 +0530 Subject: [PATCH] Fix test cases --- ...erverGoAwayMultipleStreamScenarioTest.java | 11 +- ...pServerGoAwaySingleStreamScenarioTest.java | 18 +-- ...rGoAwayWhileReceivingBodyScenarioTest.java | 11 +- ...verGoAwayWhileSendingBodyScenarioTest.java | 121 ------------------ ...rRSTStreamFrameForMultipleStreamsTest.java | 55 ++++++-- ...rverRSTStreamFrameForSingleStreamTest.java | 11 +- ...ameWhenReadingBodyForSingleStreamTest.java | 11 +- ...verSendRequestAfterGoAwayScenarioTest.java | 10 +- .../Http2TcpServerSuccessScenarioTest.java | 11 +- native/src/test/resources/testng.xml | 1 + 10 files changed, 100 insertions(+), 160 deletions(-) delete mode 100644 native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileSendingBodyScenarioTest.java diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayMultipleStreamScenarioTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayMultipleStreamScenarioTest.java index 010671f13e..dd4278489e 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayMultipleStreamScenarioTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayMultipleStreamScenarioTest.java @@ -29,6 +29,7 @@ import io.netty.util.CharsetUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -62,6 +63,7 @@ public class Http2TcpServerGoAwayMultipleStreamScenarioTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerGoAwayMultipleStreamScenarioTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -116,7 +118,6 @@ private void testGoAwayWhenReceivingHeadersInAMultipleStreamScenario() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -126,8 +127,6 @@ private void runTcpServer(int port) { sendGoAwayForASingleStreamInAMultipleStreamScenario(outputStream); } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); @@ -158,4 +157,10 @@ private static void sendGoAwayForASingleStreamInAMultipleStreamScenario(OutputSt outputStream.write(DATA_FRAME_STREAM_03); Thread.sleep(END_SLEEP_TIME); } + + @AfterClass + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwaySingleStreamScenarioTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwaySingleStreamScenarioTest.java index 61e1f58950..6784d0be71 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwaySingleStreamScenarioTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwaySingleStreamScenarioTest.java @@ -30,6 +30,7 @@ import io.netty.util.CharsetUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -58,19 +59,16 @@ public class Http2TcpServerGoAwaySingleStreamScenarioTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerGoAwaySingleStreamScenarioTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + private ServerSocket serverSocket; @BeforeMethod public void setup(Method method) throws InterruptedException { - if (method.getName().equals("testGoAwayWhenReceivingHeadersForASingleStream")) { - runTcpServer(TestUtil.HTTP_SERVER_PORT, 1); - } else { - runTcpServer(TestUtil.HTTP_SERVER_PORT, 2); - } h2ClientWithPriorKnowledge = TestUtils.setupHttp2PriorKnowledgeClient(); } @Test (description = "In this, server sends headers and data for the accepted stream") private void testGoAwayWhenReceivingHeadersForASingleStream() { + runTcpServer(TestUtil.HTTP_SERVER_PORT, 1); HttpCarbonMessage httpCarbonMessage = MessageGenerator.generateRequest(HttpMethod.POST, "Test Http2 Message"); try { CountDownLatch latch = new CountDownLatch(1); @@ -88,6 +86,7 @@ private void testGoAwayWhenReceivingHeadersForASingleStream() { @Test (description = "In this, server exits without sending the headers and data for the accepted stream as well") private void testGoAwayAndServerExitWhenReceivingHeadersForASingleStream() { + runTcpServer(TestUtil.HTTP_SERVER_PORT, 2); HttpCarbonMessage httpCarbonMessage = MessageGenerator.generateRequest(HttpMethod.POST, "Test Http2 Message"); try { CountDownLatch latch = new CountDownLatch(1); @@ -110,7 +109,6 @@ private void testGoAwayAndServerExitWhenReceivingHeadersForASingleStream() { private void runTcpServer(int port, int option) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -124,8 +122,6 @@ private void runTcpServer(int port, int option) { } } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); @@ -158,4 +154,10 @@ private static void sendGoAwayForASingleStream(OutputStream outputStream) throws outputStream.write(DATA_FRAME_STREAM_03); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileReceivingBodyScenarioTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileReceivingBodyScenarioTest.java index e6e62bee99..76bd81ca76 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileReceivingBodyScenarioTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileReceivingBodyScenarioTest.java @@ -29,6 +29,7 @@ import io.netty.handler.codec.http.HttpMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -56,6 +57,7 @@ public class Http2TcpServerGoAwayWhileReceivingBodyScenarioTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerGoAwayWhileReceivingBodyScenarioTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -87,7 +89,6 @@ private void testGoAwayWhenReceivingBodyForASingleStream() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -97,8 +98,6 @@ private void runTcpServer(int port) { sendGoAwayAfterSendingHeadersForASingleStream(outputStream); } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); @@ -118,4 +117,10 @@ private static void sendGoAwayAfterSendingHeadersForASingleStream(OutputStream o outputStream.write(GO_AWAY_FRAME_STREAM_01); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileSendingBodyScenarioTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileSendingBodyScenarioTest.java deleted file mode 100644 index 0a8a5fa7c6..0000000000 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerGoAwayWhileSendingBodyScenarioTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved. - * - * WSO2 LLC. 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.ballerina.stdlib.http.transport.http2.frameleveltests; - -import io.ballerina.stdlib.http.transport.contract.Constants; -import io.ballerina.stdlib.http.transport.contract.HttpClientConnector; -import io.ballerina.stdlib.http.transport.contract.HttpResponseFuture; -import io.ballerina.stdlib.http.transport.message.HttpCarbonMessage; -import io.ballerina.stdlib.http.transport.util.DefaultHttpConnectorListener; -import io.ballerina.stdlib.http.transport.util.TestUtil; -import io.ballerina.stdlib.http.transport.util.client.http2.MessageGenerator; -import io.netty.handler.codec.http.HttpContent; -import io.netty.handler.codec.http.HttpMethod; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.END_SLEEP_TIME; -import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.GO_AWAY_FRAME_STREAM_01; -import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.SETTINGS_FRAME; -import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.SETTINGS_FRAME_WITH_ACK; -import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.SLEEP_TIME; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; - -/** - * This contains a test case where the tcp server sends a GoAway while client sends the body. - */ -public class Http2TcpServerGoAwayWhileSendingBodyScenarioTest { - - private static final Logger LOGGER = - LoggerFactory.getLogger(Http2TcpServerGoAwayWhileSendingBodyScenarioTest.class); - - private HttpClientConnector h2ClientWithPriorKnowledge; - - @BeforeClass - public void setup() throws InterruptedException { - runTcpServer(TestUtil.HTTP_SERVER_PORT); - h2ClientWithPriorKnowledge = TestUtils.setupHttp2PriorKnowledgeClient(); - } - - @Test - private void testGoAwayWhenSendingBodyForASingleStream() { - HttpCarbonMessage httpCarbonMessage = MessageGenerator.generateRequest(HttpMethod.POST, "hello world"); - try { - CountDownLatch latch = new CountDownLatch(1); - DefaultHttpConnectorListener msgListener = new DefaultHttpConnectorListener(latch); - HttpResponseFuture responseFuture = h2ClientWithPriorKnowledge.send(httpCarbonMessage); - responseFuture.setHttpConnectorListener(msgListener); - latch.await(TestUtil.HTTP2_RESPONSE_TIME_OUT, TimeUnit.SECONDS); - responseFuture.sync(); - HttpContent content = msgListener.getHttpResponseMessage().getHttpContent(); - if (content != null) { - assertEquals(content.decoderResult().cause().getMessage(), - Constants.REMOTE_SERVER_CLOSED_WHILE_READING_INBOUND_RESPONSE_BODY); - } else { - fail("Expected http content"); - } - } catch (InterruptedException e) { - LOGGER.error("Interrupted exception occurred"); - } - } - - private void runTcpServer(int port) { - new Thread(() -> { - ServerSocket serverSocket; - try { - serverSocket = new ServerSocket(port); - LOGGER.info("HTTP/2 TCP Server listening on port " + port); - Socket clientSocket = serverSocket.accept(); - LOGGER.info("Accepted connection from: " + clientSocket.getInetAddress()); - try (OutputStream outputStream = clientSocket.getOutputStream()) { - sendGoAwayWhileSendingBodyForASingleStream(outputStream); - } catch (Exception e) { - LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); - } - } catch (IOException e) { - LOGGER.error(e.getMessage()); - } - }).start(); - } - - private static void sendGoAwayWhileSendingBodyForASingleStream(OutputStream outputStream) - throws IOException, InterruptedException { - // Sending settings frame with HEADER_TABLE_SIZE=25700 - outputStream.write(SETTINGS_FRAME); - Thread.sleep(SLEEP_TIME); - outputStream.write(SETTINGS_FRAME_WITH_ACK); - Thread.sleep(SLEEP_TIME); -// outputStream.write(HEADER_FRAME_STREAM_03); -// Thread.sleep(SLEEP_TIME); - outputStream.write(GO_AWAY_FRAME_STREAM_01); - Thread.sleep(END_SLEEP_TIME); - } -} diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForMultipleStreamsTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForMultipleStreamsTest.java index 3ee33f07ad..cdcffdee21 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForMultipleStreamsTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForMultipleStreamsTest.java @@ -30,6 +30,7 @@ import io.netty.util.CharsetUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -38,7 +39,7 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.Semaphore; import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.DATA_FRAME_STREAM_05; import static io.ballerina.stdlib.http.transport.http2.frameleveltests.TestUtils.END_SLEEP_TIME; @@ -60,6 +61,9 @@ public class Http2TcpServerRSTStreamFrameForMultipleStreamsTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerRSTStreamFrameForMultipleStreamsTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + Semaphore readSemaphore = new Semaphore(0); + Semaphore writeSemaphore = new Semaphore(0); + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -68,22 +72,26 @@ public void setup() throws InterruptedException { } @Test - private void testRSTStreamFrameForSingleStream() { + private void testRSTStreamFrameForMultipleStreams() { HttpCarbonMessage httpCarbonMessage1 = MessageGenerator.generateRequest(HttpMethod.POST, "Test Http2 Message"); HttpCarbonMessage httpCarbonMessage2 = MessageGenerator.generateRequest(HttpMethod.POST, "Test Http2 Message"); HttpCarbonMessage httpCarbonMessage3 = MessageGenerator.generateRequest(HttpMethod.POST, "Test Http2 Message"); try { - CountDownLatch latch = new CountDownLatch(3); - DefaultHttpConnectorListener msgListener1 = new DefaultHttpConnectorListener(latch); + DefaultHttpConnectorListener msgListener1 = new DefaultHttpConnectorListener(new CountDownLatch(1)); HttpResponseFuture responseFuture1 = h2ClientWithPriorKnowledge.send(httpCarbonMessage1); responseFuture1.setHttpConnectorListener(msgListener1); - DefaultHttpConnectorListener msgListener2 = new DefaultHttpConnectorListener(latch); + writeSemaphore.release(); + readSemaphore.acquire(); + DefaultHttpConnectorListener msgListener2 = new DefaultHttpConnectorListener(new CountDownLatch(1)); HttpResponseFuture responseFuture2 = h2ClientWithPriorKnowledge.send(httpCarbonMessage2); responseFuture2.setHttpConnectorListener(msgListener2); - DefaultHttpConnectorListener msgListener3 = new DefaultHttpConnectorListener(latch); + writeSemaphore.release(); + readSemaphore.acquire(); + DefaultHttpConnectorListener msgListener3 = new DefaultHttpConnectorListener(new CountDownLatch(1)); HttpResponseFuture responseFuture3 = h2ClientWithPriorKnowledge.send(httpCarbonMessage3); responseFuture3.setHttpConnectorListener(msgListener3); - latch.await(TestUtil.HTTP2_RESPONSE_TIME_OUT, TimeUnit.SECONDS); + writeSemaphore.release(); + readSemaphore.acquire(); responseFuture1.sync(); responseFuture2.sync(); responseFuture3.sync(); @@ -92,7 +100,13 @@ private void testRSTStreamFrameForSingleStream() { assertEquals(throwable.getMessage(), Constants.REMOTE_SERVER_CLOSED_BEFORE_INITIATING_INBOUND_RESPONSE); } else { - fail("Expected an error"); +// HttpContent content1 = msgListener1.getHttpResponseMessage().getHttpContent(); +// if (content1 != null) { +// assertEquals(content1.decoderResult().cause().getMessage(), +// Constants.REMOTE_SERVER_CLOSED_WHILE_READING_INBOUND_RESPONSE_BODY); +// } else { + fail("Expected an error"); +// } } HttpContent content2 = msgListener2.getHttpResponseMessage().getHttpContent(); assertEquals(content2.content().toString(CharsetUtil.UTF_8), "hello world5"); @@ -110,7 +124,6 @@ private void testRSTStreamFrameForSingleStream() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -120,29 +133,43 @@ private void runTcpServer(int port) { sendRSTStream(outputStream); } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); + } finally { + readSemaphore.release(); + writeSemaphore.release(); } }).start(); } // This will send an RST_STREAM frame for stream 3 before sending headers and a successful response for // stream 5 and an RST_STREAM frame for stream 7 after sending headers - private static void sendRSTStream(OutputStream outputStream) throws IOException, InterruptedException { + private void sendRSTStream(OutputStream outputStream) throws IOException, InterruptedException { + Thread.sleep(SLEEP_TIME); outputStream.write(SETTINGS_FRAME); Thread.sleep(SLEEP_TIME); outputStream.write(SETTINGS_FRAME_WITH_ACK); Thread.sleep(SLEEP_TIME); + writeSemaphore.acquire(); outputStream.write(RST_STREAM_FRAME_STREAM_03); + readSemaphore.release(); Thread.sleep(SLEEP_TIME); + writeSemaphore.acquire(); outputStream.write(HEADER_FRAME_STREAM_05); - outputStream.write(HEADER_FRAME_STREAM_07); - Thread.sleep(100); outputStream.write(DATA_FRAME_STREAM_05); + Thread.sleep(SLEEP_TIME); + readSemaphore.release(); + writeSemaphore.acquire(); + outputStream.write(HEADER_FRAME_STREAM_07); outputStream.write(RST_STREAM_FRAME_STREAM_07); + readSemaphore.release(); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForSingleStreamTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForSingleStreamTest.java index 49188b2768..7ebead82ea 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForSingleStreamTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameForSingleStreamTest.java @@ -28,6 +28,7 @@ import io.netty.handler.codec.http.HttpMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -53,6 +54,7 @@ public class Http2TcpServerRSTStreamFrameForSingleStreamTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerRSTStreamFrameForSingleStreamTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -84,7 +86,6 @@ private void testRSTStreamFrameForSingleStream() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -94,8 +95,6 @@ private void runTcpServer(int port) { sendRSTStream(outputStream); } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); @@ -112,4 +111,10 @@ private static void sendRSTStream(OutputStream outputStream) throws IOException, outputStream.write(RST_STREAM_FRAME_STREAM_03); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameWhenReadingBodyForSingleStreamTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameWhenReadingBodyForSingleStreamTest.java index 09b643dbb0..edf850fd15 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameWhenReadingBodyForSingleStreamTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerRSTStreamFrameWhenReadingBodyForSingleStreamTest.java @@ -29,6 +29,7 @@ import io.netty.handler.codec.http.HttpMethod; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -56,6 +57,7 @@ public class Http2TcpServerRSTStreamFrameWhenReadingBodyForSingleStreamTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerRSTStreamFrameWhenReadingBodyForSingleStreamTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -87,7 +89,6 @@ private void testRSTStreamFrameWhenReadingBodyForSingleStream() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -97,8 +98,6 @@ private void runTcpServer(int port) { sendRSTStream(outputStream); } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); @@ -117,4 +116,10 @@ private static void sendRSTStream(OutputStream outputStream) throws IOException, outputStream.write(RST_STREAM_FRAME_STREAM_03); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSendRequestAfterGoAwayScenarioTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSendRequestAfterGoAwayScenarioTest.java index 19b7f54c55..75405ec789 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSendRequestAfterGoAwayScenarioTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSendRequestAfterGoAwayScenarioTest.java @@ -29,6 +29,7 @@ import io.netty.util.CharsetUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -61,6 +62,7 @@ public class Http2TcpServerSendRequestAfterGoAwayScenarioTest { private HttpClientConnector h2ClientWithPriorKnowledge; private AtomicInteger numberOfConnections = new AtomicInteger(0); + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -101,7 +103,6 @@ private void testNewRequestAfterGoAwayReceivedScenario() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -122,7 +123,6 @@ private void runTcpServer(int port) { LOGGER.error(e.getMessage()); } } - serverSocket.close(); } catch (IOException e) { LOGGER.error(e.getMessage()); } @@ -150,4 +150,10 @@ private static void sendSuccessfulResponse(OutputStream outputStream) throws IOE outputStream.write(DATA_FRAME_STREAM_03); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSuccessScenarioTest.java b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSuccessScenarioTest.java index a0dc863474..f39e3e9048 100644 --- a/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSuccessScenarioTest.java +++ b/native/src/test/java/io/ballerina/stdlib/http/transport/http2/frameleveltests/Http2TcpServerSuccessScenarioTest.java @@ -29,6 +29,7 @@ import io.netty.util.CharsetUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -54,6 +55,7 @@ public class Http2TcpServerSuccessScenarioTest { private static final Logger LOGGER = LoggerFactory.getLogger(Http2TcpServerSuccessScenarioTest.class); private HttpClientConnector h2ClientWithPriorKnowledge; + private ServerSocket serverSocket; @BeforeClass public void setup() throws InterruptedException { @@ -80,7 +82,6 @@ private void testSuccessfulConnection() { private void runTcpServer(int port) { new Thread(() -> { - ServerSocket serverSocket; try { serverSocket = new ServerSocket(port); LOGGER.info("HTTP/2 TCP Server listening on port " + port); @@ -90,8 +91,6 @@ private void runTcpServer(int port) { sendSuccessfulResponse(outputStream); } catch (Exception e) { LOGGER.error(e.getMessage()); - } finally { - serverSocket.close(); } } catch (IOException e) { LOGGER.error(e.getMessage()); @@ -110,4 +109,10 @@ private static void sendSuccessfulResponse(OutputStream outputStream) throws IOE outputStream.write(DATA_FRAME_STREAM_03); Thread.sleep(END_SLEEP_TIME); } + + @AfterMethod + public void cleanUp() throws IOException { + h2ClientWithPriorKnowledge.close(); + serverSocket.close(); + } } diff --git a/native/src/test/resources/testng.xml b/native/src/test/resources/testng.xml index 2f73f51d38..ca73553934 100644 --- a/native/src/test/resources/testng.xml +++ b/native/src/test/resources/testng.xml @@ -161,6 +161,7 @@ +