Skip to content

Commit

Permalink
UiSessionInitAndDisposeTest: backport test improvements
Browse files Browse the repository at this point in the history
Various improvements were made to UiSessionInitAndDisposeTest on higher
release branches to make the test more stable and deterministic.

Change-Id: Iab01fc35abc2fef5e0bac8c6a7f1a11d9ebff716
  • Loading branch information
bschwarzent committed Mar 30, 2020
1 parent 9b2ab1e commit f4431f6
Showing 1 changed file with 47 additions and 46 deletions.
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 @@ -37,9 +38,6 @@
import org.eclipse.scout.rt.testing.platform.runner.RunWithNewPlatform;
import org.eclipse.scout.rt.testing.shared.TestingUtility;
import org.eclipse.scout.rt.ui.html.UiHtmlConfigProperties.SessionStoreHousekeepingDelayProperty;
import org.eclipse.scout.rt.ui.html.UiHtmlConfigProperties.SessionStoreHousekeepingMaxWaitShutdownProperty;
import org.eclipse.scout.rt.ui.html.UiHtmlConfigProperties.SessionStoreMaxWaitAllShutdownProperty;
import org.eclipse.scout.rt.ui.html.UiHtmlConfigProperties.SessionStoreMaxWaitWriteLockProperty;
import org.eclipse.scout.rt.ui.html.json.JsonMessageRequestHandler;
import org.eclipse.scout.rt.ui.html.json.JsonStartupRequest;
import org.eclipse.scout.rt.ui.html.json.UnloadRequestHandler;
Expand All @@ -50,27 +48,26 @@
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
public void before() {
m_protocol.clear();
m_beans = TestingUtility.registerBeans(
new BeanMetaData(JobCompletionDelayOnSessionShutdown.class).withProducer(new IBeanInstanceProducer<JobCompletionDelayOnSessionShutdown>() {
new BeanMetaData(JobCompletionDelayOnSessionShutdown.class).withInitialInstance(new JobCompletionDelayOnSessionShutdown() {
@Override
public JobCompletionDelayOnSessionShutdown produce(IBean<JobCompletionDelayOnSessionShutdown> bean) {
return new JobCompletionDelayOnSessionShutdown() {
@Override
public Long getDefaultValue() {
return 10L;
}
};
protected Long getDefaultValue() {
return 1L;
}
}),

Expand All @@ -81,27 +78,6 @@ public Integer getDefaultValue() {
}
}),

new BeanMetaData(SessionStoreHousekeepingMaxWaitShutdownProperty.class).withInitialInstance(new SessionStoreHousekeepingMaxWaitShutdownProperty() {
@Override
public Integer getDefaultValue() {
return 10;
}
}),

new BeanMetaData(SessionStoreMaxWaitWriteLockProperty.class).withInitialInstance(new SessionStoreMaxWaitWriteLockProperty() {
@Override
public Integer getDefaultValue() {
return 1;
}
}),

new BeanMetaData(SessionStoreMaxWaitAllShutdownProperty.class).withInitialInstance(new SessionStoreMaxWaitAllShutdownProperty() {
@Override
public Integer getDefaultValue() {
return 1;
}
}),

new BeanMetaData(HttpSessionHelper.class).withProducer(new IBeanInstanceProducer<HttpSessionHelper>() {
@Override
public HttpSessionHelper produce(IBean<HttpSessionHelper> bean) {
Expand Down Expand Up @@ -136,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 @@ -152,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 @@ -216,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 All @@ -226,6 +202,8 @@ public void testStartupThenBrowserTabClose() throws ServletException, IOExceptio
assertEquals(1, store.getUiSessionMap().size());
assertNotNull(store.getUiSessionMap().get(uiSessionId));

final FixtureClientSession clientSession = (FixtureClientSession) store.getClientSessionMap().values().iterator().next();

assertEquals(
Arrays.asList(
"UiSession.init",
Expand All @@ -246,7 +224,7 @@ public void testStartupThenBrowserTabClose() throws ServletException, IOExceptio
JobTestUtil.waitForCondition(new ICondition() {
@Override
public boolean isFulfilled() {
return store.isEmpty() && !store.isHttpSessionValid();
return store.isEmpty() && !store.isHttpSessionValid() && clientSession.isStopped();
}
});

Expand Down Expand Up @@ -277,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 All @@ -287,6 +265,8 @@ public void testStartupThenBrowserTabReload() throws ServletException, IOExcepti
assertEquals(1, store.getUiSessionMap().size());
assertNotNull(store.getUiSessionMap().get(uiSessionId));

final FixtureClientSession clientSession = (FixtureClientSession) store.getClientSessionMap().values().iterator().next();

assertEquals(
Arrays.asList(
"UiSession.init",
Expand All @@ -303,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 @@ -311,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 All @@ -337,7 +328,7 @@ public void testStartupThenBrowserTabReload() throws ServletException, IOExcepti
JobTestUtil.waitForCondition(new ICondition() {
@Override
public boolean isFulfilled() {
return store.isEmpty() && !store.isHttpSessionValid();
return clientSession.isStopped();
}
});

Expand Down Expand Up @@ -368,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 @@ -398,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 All @@ -416,6 +407,8 @@ public void testStartupThenBrowserTabDuplicate() throws ServletException, IOExce
assertNotEquals(uiSessionIdA, uiSessionIdB);
assertEquals(2, store.countUiSessions());

final FixtureClientSession clientSession = (FixtureClientSession) store.getClientSessionMap().values().iterator().next();

//brower tab A closed -> json unload
try (BufferedServletOutputStream out = new BufferedServletOutputStream()) {
final HttpServletRequest req = JsonTestUtility.createHttpServletRequest(httpSession, "/unload/" + uiSessionIdA, null);
Expand All @@ -431,7 +424,7 @@ public void testStartupThenBrowserTabDuplicate() throws ServletException, IOExce
JobTestUtil.waitForCondition(new ICondition() {
@Override
public boolean isFulfilled() {
return store.isEmpty() && !store.isHttpSessionValid();
return clientSession.isStopped();
}
});

Expand Down Expand Up @@ -474,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 All @@ -484,6 +477,8 @@ public void testStartupThenBrowserTabDuplicateThenBrowserTabClose() throws Servl
assertEquals(1, store.getUiSessionMap().size());
assertNotNull(store.getUiSessionMap().get(uiSessionIdA));

final FixtureClientSession clientSession = (FixtureClientSession) store.getClientSessionMap().values().iterator().next();

assertEquals(
Arrays.asList(
"UiSession.init",
Expand All @@ -504,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 Expand Up @@ -560,7 +555,7 @@ public boolean isFulfilled() {
JobTestUtil.waitForCondition(new ICondition() {
@Override
public boolean isFulfilled() {
return store.isEmpty() && !store.isHttpSessionValid();
return clientSession.isStopped();
}
});

Expand Down Expand Up @@ -615,6 +610,7 @@ public void dispose() {

@IgnoreBean
private class FixtureClientSession extends AbstractClientSession {
private boolean m_stopped;

public FixtureClientSession() {
super(true);
Expand Down Expand Up @@ -642,6 +638,11 @@ public void stop(int exitCode) {
writeToProtocol("ClientSession.stopping");
super.stop(exitCode);
writeToProtocol("ClientSession.stopped");
m_stopped = true;
}

public boolean isStopped() {
return m_stopped;
}
}

Expand Down

0 comments on commit f4431f6

Please sign in to comment.