Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dilanSachi committed Jan 8, 2024
1 parent 0f72561 commit 92e702b
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -158,4 +157,12 @@ private static void sendGoAwayForASingleStreamInAMultipleStreamScenario(OutputSt
outputStream.write(DATA_FRAME_STREAM_03);
Thread.sleep(END_SLEEP_TIME);
}

@AfterMethod
public void cleanUp() throws IOException {
h2ClientWithPriorKnowledge.close();
if (serverSocket != null) {
serverSocket.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,6 +59,7 @@ 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 {
Expand Down Expand Up @@ -110,7 +112,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);
Expand All @@ -124,9 +125,8 @@ private void runTcpServer(int port, int option) {
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
serverSocket.close();
}
serverSocket.close();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
Expand Down Expand Up @@ -158,4 +158,9 @@ 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,6 +57,7 @@ public class Http2TcpServerGoAwayWhileSendingBodyScenarioTest {
LoggerFactory.getLogger(Http2TcpServerGoAwayWhileSendingBodyScenarioTest.class);

private HttpClientConnector h2ClientWithPriorKnowledge;
private ServerSocket serverSocket;

@BeforeClass
public void setup() throws InterruptedException {
Expand Down Expand Up @@ -87,7 +89,6 @@ private void testGoAwayWhenSendingBodyForASingleStream() {

private void runTcpServer(int port) {
new Thread(() -> {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port);
LOGGER.info("HTTP/2 TCP Server listening on port " + port);
Expand All @@ -97,8 +98,6 @@ private void runTcpServer(int port) {
sendGoAwayWhileSendingBodyForASingleStream(outputStream);
} catch (Exception e) {
LOGGER.error(e.getMessage());
} finally {
serverSocket.close();
}
} catch (IOException e) {
LOGGER.error(e.getMessage());
Expand All @@ -118,4 +117,10 @@ private static void sendGoAwayWhileSendingBodyForASingleStream(OutputStream outp
outputStream.write(GO_AWAY_FRAME_STREAM_01);
Thread.sleep(END_SLEEP_TIME);
}

@AfterMethod
public void cleanUp() throws IOException {
h2ClientWithPriorKnowledge.close();
serverSocket.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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();
}
}
Loading

0 comments on commit 92e702b

Please sign in to comment.