Skip to content

Commit 42558eb

Browse files
author
innokenty
committed
evict sessions older than a couple of minutes
1 parent 0dbd8ef commit 42558eb

File tree

6 files changed

+103
-28
lines changed

6 files changed

+103
-28
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.qatools.gridrouter;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
7+
import org.springframework.scheduling.annotation.Scheduled;
8+
9+
import java.time.Duration;
10+
11+
/**
12+
* @author Innokenty Shuvalov [email protected]
13+
*/
14+
@Configuration
15+
@EnableScheduling
16+
public class SessionStorageEvictionScheduler {
17+
18+
@Value("${grid.router.evict.sessions.timeout.seconds}")
19+
private int timeout;
20+
21+
@Autowired
22+
private SessionStorage sessionStorage;
23+
24+
@Scheduled(cron = "${grid.router.evict.sessions.cron}")
25+
public void expireOldSessions() {
26+
sessionStorage.expireSessionsOlderThan(Duration.ofSeconds(timeout));
27+
}
28+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
grid.config.quota.directory=classpath:quota
2-
grid.config.quota.hotReload=true
2+
grid.config.quota.hotReload=true
3+
grid.router.evict.sessions.cron=0 * * * * *
4+
grid.router.evict.sessions.timeout.seconds=120
Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package ru.qatools.gridrouter;
22

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;
83
import org.junit.Rule;
94
import org.junit.Test;
105
import org.junit.rules.TestRule;
@@ -14,52 +9,68 @@
149
import ru.qatools.gridrouter.utils.HubEmulatorRule;
1510

1611
import java.io.IOException;
17-
import java.io.InputStream;
1812

1913
import static org.hamcrest.MatcherAssert.assertThat;
2014
import static org.hamcrest.Matchers.is;
21-
import static org.hamcrest.Matchers.notNullValue;
2215
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;
2618

2719
/**
2820
* @author Dmitry Baev [email protected]
21+
* @author Innokenty Shuvalov [email protected]
2922
*/
3023
public class StatsServletTest {
3124

32-
@ClassRule
33-
public static TestRule START_GRID_ROUTER = new GridRouterRule();
25+
@Rule
26+
public TestRule START_GRID_ROUTER = new GridRouterRule();
3427

3528
@Rule
3629
public HubEmulatorRule hub = new HubEmulatorRule(HUB_PORT);
3730

3831
@Test
3932
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));
4334

4435
hub.emulate().newSessions(1);
4536
hub.emulate().quit();
4637

4738
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));
5140

5241
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+
}
5352

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));
5671
}
5772

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);
6475
}
6576
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ru.qatools.gridrouter.utils;
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+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
11+
/**
12+
* @author Dmitry Baev [email protected]
13+
* @author Innokenty Shuvalov [email protected]
14+
*/
15+
public final class HttpUtils {
16+
17+
private HttpUtils() {
18+
}
19+
20+
public static <T> T executeSimpleGet(String url, Class<T> clazz) throws IOException {
21+
CloseableHttpResponse execute = HttpClientBuilder
22+
.create().build()
23+
.execute(new HttpGet(url));
24+
InputStream content = execute.getEntity().getContent();
25+
return new ObjectMapper().readValue(content, clazz);
26+
}
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
grid.config.quota.directory=classpath:quota
2+
grid.config.quota.hotReload=true
3+
grid.router.evict.sessions.cron=* * * * * *
4+
grid.router.evict.sessions.timeout.seconds=5
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
# suppress inspection "UnusedProperty" for whole file
12
grid.config.quota.directory=classpath:quota
2-
grid.config.quota.hotReload=true
3+
grid.config.quota.hotReload=true
4+
grid.router.evict.sessions.cron=0 * * * * *
5+
grid.router.evict.sessions.timeout.seconds=120

0 commit comments

Comments
 (0)