Skip to content

Commit

Permalink
add two test cases so that success and fail happens on the test threa…
Browse files Browse the repository at this point in the history
…d instead of the http request thread
  • Loading branch information
deanhiller committed Aug 28, 2016
1 parent 10be1d5 commit b4aa3a8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

public class Actions {

/*
* For better stack traces to see which controller was involved, use this (only because java
* does not support a FutureContext where we could pass state through the customer code
*/
public static Render renderView(ResponseProcessor processor, String templatePath, Object ... pageArgs) {
RenderImpl renderHtml = new RenderImpl(templatePath, pageArgs);
return processor.createRenderResponse(renderHtml);
}

/**
* Renders the html file at templatePath which is relative to the controller or an
* absolute reference from the start of the classpath
Expand All @@ -22,13 +31,14 @@ public class Actions {
* @return
*/
public static Render renderView(String templatePath, Object ... pageArgs) {
RenderImpl renderHtml = new RenderImpl(templatePath, pageArgs);
ResponseProcessor processor = RequestLocalCtx.get();
if(processor != null) {
//If there is a context (the controller method is synchronous), then we do all calculations on their thread so if there
//is an exception, their code is in the stack trace pointing the developer to the code lines to fix
return processor.createRenderResponse(renderHtml);
return renderView(processor, templatePath, pageArgs);
}

RenderImpl renderHtml = new RenderImpl(templatePath, pageArgs);
return renderHtml;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public enum AsyncRouteId implements RouteId {

//This is where you define the ids of routes that you can use in the controllers to redirect to a route
//or use in the webpages
REDIRECT_PAGE, RENDER_PAGE, SOME_ROUTE, REDIRECT2, THROW_NOT_FOUND
REDIRECT_PAGE, RENDER_PAGE, SOME_ROUTE, REDIRECT2, THROW_NOT_FOUND, ASYNC_SUCCESS, ASYNC_FAIL
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public void configure(Router router, String currentPackage) {
router.addRoute(HttpMethod.GET, "/redirectint/{id}", "../basic/biz/AsyncController.redirectWithInt", AsyncRouteId.REDIRECT2);
router.addRoute(HttpMethod.GET, "/myroute", "../basic/biz/AsyncController.myMethod", AsyncRouteId.RENDER_PAGE);
router.addRoute(HttpMethod.GET, "/throwNotFound", "../basic/biz/AsyncController.throwNotFound", AsyncRouteId.THROW_NOT_FOUND);

router.addRoute(HttpMethod.GET, "/asyncSuccessRoute","../basic/biz/AsyncController.asyncMyMethod", AsyncRouteId.ASYNC_SUCCESS);
router.addRoute(HttpMethod.GET, "/asyncFailRoute", "../basic/biz/AsyncController.asyncFail", AsyncRouteId.ASYNC_FAIL);

router.setPageNotFoundRoute("../basic/biz/AsyncController.notFound");
router.setInternalErrorRoute("../basic/biz/AsyncController.internalError");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.webpieces.webserver.async;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.junit.Assert;
import org.junit.Before;
Expand All @@ -13,26 +14,32 @@
import org.webpieces.util.file.VirtualFileClasspath;
import org.webpieces.webserver.Requests;
import org.webpieces.webserver.WebserverForTest;
import org.webpieces.webserver.basic.biz.SomeOtherLib;
import org.webpieces.webserver.mock.MockSomeOtherLib;
import org.webpieces.webserver.test.FullResponse;
import org.webpieces.webserver.test.MockFrontendSocket;
import org.webpieces.webserver.test.PlatformOverridesForTest;

import com.google.inject.Binder;
import com.google.inject.Module;

public class TestAsyncWebServer {

private MockFrontendSocket socket = new MockFrontendSocket();
private HttpRequestListener server;

private MockSomeOtherLib mockNotFoundLib = new MockSomeOtherLib();

@Before
public void setUp() {
TemplateCompileConfig config = new TemplateCompileConfig(WebserverForTest.CHAR_SET_TO_USE);

VirtualFileClasspath metaFile = new VirtualFileClasspath("asyncMeta.txt", WebserverForTest.class.getClassLoader());
WebserverForTest webserver = new WebserverForTest(new PlatformOverridesForTest(config), null, false, metaFile);
WebserverForTest webserver = new WebserverForTest(new PlatformOverridesForTest(config), new AppOverridesModule(), false, metaFile);
server = webserver.start();
}

@Test
public void testBasic() {
public void testCompletePromiseOnRequestThread() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/myroute");

server.processHttpRequests(socket, req , false);
Expand All @@ -45,6 +52,26 @@ public void testBasic() {
response.assertContains("This is the first raw html page");
}

@Test
public void testCompletePromiseOnAnotherThread() {
CompletableFuture<Integer> future = new CompletableFuture<Integer>();
mockNotFoundLib.queueFuture(future );

HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/asyncSuccessRoute");

server.processHttpRequests(socket, req , false);

//now have the server complete processing
future.complete(5);

List<FullResponse> responses = socket.getResponses();
Assert.assertEquals(1, responses.size());

FullResponse response = responses.get(0);
response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
response.assertContains("Hi Dean Hiller, This is a page");
}

@Test
public void testRedirect() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/");
Expand All @@ -59,4 +86,10 @@ public void testRedirect() {
Assert.assertEquals(0, response.getBody().getReadableSize());
}

private class AppOverridesModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(SomeOtherLib.class).toInstance(mockNotFoundLib);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,26 @@ public void testWebAppHasBugAndRender500HasBug() {
httpPayload.assertContains("The webpieces platform saved them");
}

@Test
public void testCompletePromiseAnotherThreadAndPageParamMissing() {
CompletableFuture<Integer> future = new CompletableFuture<Integer>();
mockNotFoundLib.queueFuture(future );

HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/asyncFailRoute");

server.processHttpRequests(mockResponseSocket, req , false);

//now have the server complete processing
future.complete(5);

List<FullResponse> responses = mockResponseSocket.getResponses();
Assert.assertEquals(1, responses.size());

FullResponse response = responses.get(0);
response.assertStatusCode(KnownStatusCode.HTTP_500_INTERNAL_SVR_ERROR);
response.assertContains("There was a bug in our software");
}

private class AppOverridesModule implements Module {
@Override
public void configure(Binder binder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.webpieces.webserver.basic;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

import javax.inject.Inject;

import org.webpieces.ctx.api.Current;
import org.webpieces.ctx.api.RequestContext;
import org.webpieces.router.api.actions.Action;
import org.webpieces.router.api.actions.Actions;
import org.webpieces.router.api.actions.Redirect;
import org.webpieces.router.api.actions.Render;
import org.webpieces.router.impl.ctx.RequestLocalCtx;
import org.webpieces.router.impl.ctx.ResponseProcessor;
import org.webpieces.webserver.async.AsyncRouteId;

public class AsyncController {
Expand Down Expand Up @@ -38,6 +42,20 @@ public CompletableFuture<Action> myMethod() {
return CompletableFuture.completedFuture(renderThis);
}

public CompletableFuture<Action> asyncMyMethod() {
ResponseProcessor processor = RequestLocalCtx.get();
return notFoundLib.someBusinessLogic().thenApply(s -> {
return Actions.renderView(processor, "userParamPage.html", "user", "Dean Hiller");
});
}

public CompletableFuture<Action> asyncFail() {
ResponseProcessor processor = RequestLocalCtx.get();
return notFoundLib.someBusinessLogic().thenApply(s -> {
return Actions.renderView(processor, "userParamPage.html");
});
}

public CompletableFuture<Action> throwNotFound() {
return notFoundLib.someBusinessLogic().thenApply(s -> Actions.redirect(AsyncRouteId.SOME_ROUTE));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
Hi ${user}$, This is a page
</body>
</html>

0 comments on commit b4aa3a8

Please sign in to comment.