|
1 | 1 | package ru.qatools.gridrouter;
|
2 | 2 |
|
3 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
4 |
| -import org.apache.http.client.methods.CloseableHttpResponse; |
5 |
| -import org.apache.http.client.methods.HttpGet; |
6 |
| -import org.apache.http.impl.client.HttpClientBuilder; |
7 |
| -import org.junit.ClassRule; |
8 | 3 | import org.junit.Rule;
|
9 | 4 | import org.junit.Test;
|
10 | 5 | import org.junit.rules.TestRule;
|
|
14 | 9 | import ru.qatools.gridrouter.utils.HubEmulatorRule;
|
15 | 10 |
|
16 | 11 | import java.io.IOException;
|
17 |
| -import java.io.InputStream; |
18 | 12 |
|
19 | 13 | import static org.hamcrest.MatcherAssert.assertThat;
|
20 | 14 | import static org.hamcrest.Matchers.is;
|
21 |
| -import static org.hamcrest.Matchers.notNullValue; |
22 | 15 | import static org.openqa.selenium.remote.DesiredCapabilities.firefox;
|
23 |
| -import static ru.qatools.gridrouter.utils.GridRouterRule.BASE_URL_WITH_AUTH; |
24 |
| -import static ru.qatools.gridrouter.utils.GridRouterRule.HUB_PORT; |
25 |
| -import static ru.qatools.gridrouter.utils.GridRouterRule.hubUrl; |
| 16 | +import static ru.qatools.gridrouter.utils.GridRouterRule.*; |
| 17 | +import static ru.qatools.gridrouter.utils.HttpUtils.executeSimpleGet; |
26 | 18 |
|
27 | 19 | /**
|
28 | 20 | * @author Dmitry Baev [email protected]
|
| 21 | + * @author Innokenty Shuvalov [email protected] |
29 | 22 | */
|
30 | 23 | public class StatsServletTest {
|
31 | 24 |
|
32 |
| - @ClassRule |
33 |
| - public static TestRule START_GRID_ROUTER = new GridRouterRule(); |
| 25 | + @Rule |
| 26 | + public TestRule START_GRID_ROUTER = new GridRouterRule(); |
34 | 27 |
|
35 | 28 | @Rule
|
36 | 29 | public HubEmulatorRule hub = new HubEmulatorRule(HUB_PORT);
|
37 | 30 |
|
38 | 31 | @Test
|
39 | 32 | public void testStats() throws IOException {
|
40 |
| - int actual = executeSimpleGet(BASE_URL_WITH_AUTH + "/stats"); |
41 |
| - assertThat(actual, notNullValue()); |
42 |
| - assertThat(actual, is(0)); |
| 33 | + assertThat(getActual(USER_1), is(0)); |
43 | 34 |
|
44 | 35 | hub.emulate().newSessions(1);
|
45 | 36 | hub.emulate().quit();
|
46 | 37 |
|
47 | 38 | WebDriver driver = new RemoteWebDriver(hubUrl(BASE_URL_WITH_AUTH), firefox());
|
48 |
| - |
49 |
| - actual = executeSimpleGet(BASE_URL_WITH_AUTH + "/stats"); |
50 |
| - assertThat(actual, is(1)); |
| 39 | + assertThat(getActual(USER_1), is(1)); |
51 | 40 |
|
52 | 41 | driver.quit();
|
| 42 | + assertThat(getActual(USER_1), is(0)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testStatsForDifferentUsers() throws IOException { |
| 47 | + hub.emulate().newSessions(1); |
| 48 | + new RemoteWebDriver(hubUrl(BASE_URL_WITH_AUTH), firefox()); |
| 49 | + assertThat(getActual(USER_1), is(1)); |
| 50 | + assertThat(getActual(USER_2), is(0)); |
| 51 | + } |
53 | 52 |
|
54 |
| - actual = executeSimpleGet(BASE_URL_WITH_AUTH + "/stats"); |
55 |
| - assertThat(actual, is(0)); |
| 53 | + @Test |
| 54 | + public void testEvictionOfOldSession() throws Exception { |
| 55 | + hub.emulate().newSessions(1); |
| 56 | + new RemoteWebDriver(hubUrl(BASE_URL_WITH_AUTH), firefox()); |
| 57 | + assertThat(getActual(USER_1), is(1)); |
| 58 | + Thread.sleep(6000); |
| 59 | + assertThat(getActual(USER_1), is(0)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testActiveSessionIsNotEvicted() throws Exception { |
| 64 | + hub.emulate().newSessions(1).navigation(); |
| 65 | + WebDriver driver = new RemoteWebDriver(hubUrl(BASE_URL_WITH_AUTH), firefox()); |
| 66 | + for (int i = 0; i < 3; i++) { |
| 67 | + Thread.sleep(2000); |
| 68 | + driver.get("http://yandex.ru"); |
| 69 | + } |
| 70 | + assertThat(getActual(USER_1), is(1)); |
56 | 71 | }
|
57 | 72 |
|
58 |
| - public static int executeSimpleGet(String url) throws IOException { |
59 |
| - CloseableHttpResponse execute = HttpClientBuilder |
60 |
| - .create().build() |
61 |
| - .execute(new HttpGet(url)); |
62 |
| - InputStream content = execute.getEntity().getContent(); |
63 |
| - return new ObjectMapper().readValue(content, Integer.class); |
| 73 | + public int getActual(String user) throws IOException { |
| 74 | + return executeSimpleGet(baseUrl(user) + "/stats", Integer.class); |
64 | 75 | }
|
65 | 76 | }
|
0 commit comments