Skip to content

Commit

Permalink
[UNDERTOW-2293] Add tests for asynchronous writing response, and extr…
Browse files Browse the repository at this point in the history
…a tests for writing response on post before reading input

Signed-off-by: Flavia Rainone <[email protected]>
  • Loading branch information
fl4via committed Jul 19, 2023
1 parent f8b8481 commit 6155af8
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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.undertow.servlet.test.response.writer;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
* Asynchronous version of {@link ExceptionWriterServlet}.
*
* @author rmartinc
*/
public class AsyncExceptionWriterServlet extends jakarta.servlet.http.HttpServlet {

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final var asyncContext = req.startAsync();
new Thread(()->{
try {
resp.setContentType("text/plain;charset=UTF-8");
try (PrintWriter writer = resp.getWriter()) {
new Exception("TestException").printStackTrace(writer);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
asyncContext.complete();
}
}).start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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.undertow.servlet.test.response.writer;

import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* Asynchronous version of {@link LargeResponseWriterServlet}.
*
* @author Flavia Rainone
*/
public class AsyncLargeResponseWriterServlet extends LargeResponseWriterServlet {

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final AsyncContext asyncContext = req.startAsync();
new Thread(()->{
try {
String msg = getMessage();
resp.setContentLength(msg.length());
resp.getWriter().write(msg);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
asyncContext.complete();
}
}).start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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.undertow.servlet.test.response.writer;

import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* Asynchronous version of {@link ResponseWriterOnPostServlet}.
*
* @author Flavia Rainone
*/
public class AsyncResponseWriterOnPostServlet extends ResponseWriterOnPostServlet {

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
String test = req.getParameter("test");
if (!test.equals(CONTENT_LENGTH_FLUSH)) {
throw new IllegalArgumentException("not a test " + test);
}
final AsyncContext asyncContext = req.startAsync();
new Thread(()->{
try {
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
contentLengthFlush(response);
// read after writing the response (UNDERTOW-2243)
while (true) {
if (req.getInputStream().readLine(new byte[100], 0, 100) == -1) {
req.getInputStream().close();
break;
}
}
} catch (RuntimeException e) {
exception = e;
throw e;
} catch (Throwable t) {
exception = t;
throw new RuntimeException(t);
} finally {
asyncContext.complete();
}
}).start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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.undertow.servlet.test.response.writer;

import jakarta.servlet.AsyncContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* Asynchronous version of {@link ResponseWriterServlet}.
*
* @author Flavia Rainone
*/
public class AsyncResponseWriterServlet extends ResponseWriterServlet {

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

String test = req.getParameter("test");
if (!test.equals(CONTENT_LENGTH_FLUSH)) {
throw new IllegalArgumentException("not a test " + test);
}
final AsyncContext asyncContext = req.startAsync();
new Thread(()->{
try {
contentLengthFlush((HttpServletResponse) asyncContext.getResponse());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
asyncContext.complete();
}
}).start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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.undertow.servlet.test.response.writer;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
* Servlet that writes response on post, before reading input.
*
* @author Flavia Rainone
*/
public class ResponseWriterOnPostServlet extends ResponseWriterServlet {

protected static Throwable exception = null;

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

String test = req.getParameter("test");
if (test.equals(CONTENT_LENGTH_FLUSH)) {
contentLengthFlush(resp);
// read after writing the response (UNDERTOW-2243)
try {
while (true) {
if (req.getInputStream().readLine(new byte[100], 0, 100) == -1) {
req.getInputStream().close();
break;
}

}
} catch (Throwable e) {
exception = e;
throw e;
}
} else {
throw new IllegalArgumentException("not a test " + test);
}
}

public static Throwable getExceptionIfAny() {
return exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res

String test = req.getParameter("test");
if (test.equals(CONTENT_LENGTH_FLUSH)) {
contentLengthFlush(req, resp);
contentLengthFlush(resp);
} else {
throw new IllegalArgumentException("not a test " + test);
}
}

private void contentLengthFlush(HttpServletRequest req, HttpServletResponse resp) throws IOException {
protected void contentLengthFlush(HttpServletResponse resp) throws IOException {
int size = 10;

PrintWriter pw = resp.getWriter();
Expand All @@ -54,7 +54,7 @@ private void contentLengthFlush(HttpServletRequest req, HttpServletResponse resp
resp.setContentLength(size);
//write more data than the content length
while (i < 20) {
tmp = tmp.append("a");
tmp.append("a");
i = i + 1;
}
pw.println(tmp);
Expand Down
Loading

0 comments on commit 6155af8

Please sign in to comment.