Skip to content

Commit

Permalink
Fix potential race condition in UiSessionInitAndDisposeTest
Browse files Browse the repository at this point in the history
Add missing waitForCondition to make sure the protocol entries are
written in deterministic order.

Change-Id: Ieb14a4e3398ee513a337cbd2c01110d66443225a
  • Loading branch information
bschwarzent committed Mar 16, 2020
1 parent 5796c2c commit 6afa53b
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -47,12 +48,16 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RunWithNewPlatform()
@RunWith(PlatformTestRunner.class)
public class UiSessionInitAndDisposeTest {
private List<IBean<?>> m_beans;

private static final Logger LOG = LoggerFactory.getLogger(UiSessionInitAndDisposeTest.class);

private List<IBean<?>> m_beans;
private List<String> m_protocol = Collections.synchronizedList(new ArrayList<String>());

@Before
Expand Down Expand Up @@ -107,7 +112,7 @@ public void after() {
}

private void writeToProtocol(String line) {
System.out.println("protocol: " + line);
LOG.info("New protocol entry: '{}' ", line);
m_protocol.add(line);
}

Expand All @@ -123,7 +128,7 @@ public void testStartupThenModelStop() throws ServletException, IOException {
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", "{\"startup\":true}");
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -187,7 +192,7 @@ public void testStartupThenBrowserTabClose() throws ServletException, IOExceptio
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -250,7 +255,7 @@ public void testStartupThenBrowserTabReload() throws ServletException, IOExcepti
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -278,6 +283,17 @@ public void testStartupThenBrowserTabReload() throws ServletException, IOExcepti
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
unloadHandler.handlePost(req, resp);
}
JobTestUtil.waitForCondition(new ICondition() {
@Override
public boolean isFulfilled() {
int n = Jobs.getJobManager()
.getFutures(Jobs.newFutureFilterBuilder()
.andMatchExecutionSemaphore(clientSession.getModelJobSemaphore())
.toFilter())
.size();
return n == 0;
}
});

//json startup with same client session
try (BufferedServletOutputStream out = new BufferedServletOutputStream()) {
Expand All @@ -286,7 +302,7 @@ public void testStartupThenBrowserTabReload() throws ServletException, IOExcepti
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionId = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -343,7 +359,7 @@ public void testStartupThenBrowserTabDuplicate() throws ServletException, IOExce
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionIdA = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionIdA = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -373,7 +389,7 @@ public void testStartupThenBrowserTabDuplicate() throws ServletException, IOExce
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionIdB = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionIdB = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -451,7 +467,7 @@ public void testStartupThenBrowserTabDuplicateThenBrowserTabClose() throws Servl
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionIdA = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionIdA = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down Expand Up @@ -483,7 +499,7 @@ public void testStartupThenBrowserTabDuplicateThenBrowserTabClose() throws Servl
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/json", jsonData);
final HttpServletResponse resp = JsonTestUtility.createHttpServletResponse(out);
messageHandler.handlePost(req, resp);
String respContent = new String(out.getContent(), "UTF-8");
String respContent = new String(out.getContent(), StandardCharsets.UTF_8);
uiSessionIdB = new JSONObject(respContent).getJSONObject("startupData").getString("uiSessionId");
clientSessionIdB = new JSONObject(respContent).getJSONObject("startupData").getString("clientSessionId");
}
Expand Down

0 comments on commit 6afa53b

Please sign in to comment.