Skip to content

Commit 0d830ed

Browse files
author
innokenty
committed
test quota hot reloading
1 parent 822ffec commit 0d830ed

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

proxy/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@
139139
<version>1.4.01</version>
140140
<scope>test</scope>
141141
</dependency>
142+
<dependency>
143+
<groupId>ru.yandex.qatools.matchers</groupId>
144+
<artifactId>matcher-decorators</artifactId>
145+
<version>1.1</version>
146+
<scope>test</scope>
147+
</dependency>
142148
</dependencies>
143149

144150
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ru.qatools.gridrouter;
2+
3+
import org.junit.After;
4+
import org.junit.AfterClass;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.TestRule;
8+
import ru.qatools.gridrouter.utils.GridRouterRule;
9+
import ru.qatools.gridrouter.utils.HubEmulatorRule;
10+
11+
import static java.util.concurrent.TimeUnit.SECONDS;
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.openqa.selenium.remote.DesiredCapabilities.firefox;
14+
import static ru.qatools.gridrouter.utils.GridRouterRule.HUB_PORT;
15+
import static ru.qatools.gridrouter.utils.GridRouterRule.USER_1;
16+
import static ru.qatools.gridrouter.utils.GridRouterRule.USER_4;
17+
import static ru.qatools.gridrouter.utils.MatcherUtils.canObtain;
18+
import static ru.qatools.gridrouter.utils.QuotaUtils.copyQuotaFile;
19+
import static ru.qatools.gridrouter.utils.QuotaUtils.deleteQuotaFile;
20+
import static ru.qatools.gridrouter.utils.QuotaUtils.replacePortInQuotaFile;
21+
import static ru.yandex.qatools.matchers.decorators.MatcherDecorators.should;
22+
import static ru.yandex.qatools.matchers.decorators.MatcherDecorators.timeoutHasExpired;
23+
24+
/**
25+
* @author Innokenty Shuvalov [email protected]
26+
*/
27+
public class QuotaReloadTest {
28+
29+
private static final int HUB_PORT_2 = HUB_PORT + 1;
30+
31+
@Rule
32+
public TestRule gridRouter = new GridRouterRule();
33+
34+
@Rule
35+
public HubEmulatorRule hub2 = new HubEmulatorRule(HUB_PORT_2) {{
36+
emulate().newSessions(1);
37+
}};
38+
39+
@Test
40+
public void testQuotaIsReloadedOnFileChange() throws Exception {
41+
replacePortInQuotaFile(USER_1, HUB_PORT_2);
42+
Thread.sleep(5000); // just to avoid multiple exceptions in the logs
43+
assertThat(USER_1, should(canObtain(firefox()))
44+
.whileWaitingUntil(timeoutHasExpired().withPollingInterval(SECONDS.toMillis(3))));
45+
}
46+
47+
@Test
48+
public void testNewQuotaFileIsLoaded() throws Exception {
49+
copyQuotaFile(USER_1, USER_4, HUB_PORT_2);
50+
Thread.sleep(5000); // just to avoid multiple exceptions in the logs
51+
assertThat(USER_4, should(canObtain(firefox()))
52+
.whileWaitingUntil(timeoutHasExpired().withPollingInterval(SECONDS.toMillis(3))));
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
hub2.verify().newSessionRequestsCountIs(1);
58+
hub2.verify().totalRequestsCountIs(1);
59+
}
60+
61+
@AfterClass
62+
public static void restoreQuotaFiles() {
63+
replacePortInQuotaFile(USER_1, HUB_PORT);
64+
deleteQuotaFile(USER_4);
65+
}
66+
}

proxy/src/test/java/ru/qatools/gridrouter/utils/GridRouterRule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class GridRouterRule extends JettyRule {
1919
public static final String USER_1 = "user1";
2020
public static final String USER_2 = "user2";
2121
public static final String USER_3 = "user3";
22+
public static final String USER_4 = "user4";
2223
public static final String PASSWORD = "password";
2324
public static final String ROLE = "user";
2425

@@ -40,6 +41,7 @@ public GridRouterRule() {
4041
putUser(USER_1, new Password(PASSWORD), new String[]{ROLE});
4142
putUser(USER_2, new Password(PASSWORD), new String[]{ROLE});
4243
putUser(USER_3, new Password(PASSWORD), new String[]{ROLE});
44+
putUser(USER_4, new Password(PASSWORD), new String[]{ROLE});
4345
}}
4446
);
4547
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.qatools.gridrouter.utils;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeMatcher;
6+
import org.openqa.selenium.remote.DesiredCapabilities;
7+
import org.openqa.selenium.remote.RemoteWebDriver;
8+
9+
import static ru.qatools.gridrouter.utils.GridRouterRule.baseUrl;
10+
import static ru.qatools.gridrouter.utils.GridRouterRule.hubUrl;
11+
12+
/**
13+
* @author Innokenty Shuvalov [email protected]
14+
*/
15+
public final class MatcherUtils {
16+
17+
private MatcherUtils() {
18+
}
19+
20+
/**
21+
* Creates a matcher that tries to obtain a browser
22+
* for a user that it is matched against.
23+
*
24+
* @return A matcher instance that creates a new webdriver
25+
* on {@link Matcher#matches(Object) matches()} method invocation.
26+
*
27+
* @param browser capabilities for the browser to obtain
28+
*/
29+
public static Matcher<String> canObtain(final DesiredCapabilities browser) {
30+
return new TypeSafeMatcher<String>() {
31+
32+
private Exception exception;
33+
34+
@Override
35+
protected boolean matchesSafely(String user) {
36+
try {
37+
new RemoteWebDriver(hubUrl(baseUrl(user)), browser);
38+
return true;
39+
} catch (Exception e) {
40+
exception = e;
41+
}
42+
return false;
43+
}
44+
45+
@Override
46+
public void describeTo(Description description) {
47+
description.appendText("not able to obtain browser because of ")
48+
.appendValue(exception.toString());
49+
}
50+
};
51+
}
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package ru.qatools.gridrouter.utils;
2+
3+
import org.apache.commons.lang3.SerializationUtils;
4+
import ru.qatools.gridrouter.config.Browsers;
5+
6+
import javax.xml.bind.JAXB;
7+
import java.io.File;
8+
9+
import static java.lang.ClassLoader.getSystemResource;
10+
import static ru.qatools.gridrouter.utils.GridRouterRule.USER_1;
11+
12+
/**
13+
* @author Innokenty Shuvalov [email protected]
14+
*/
15+
public final class QuotaUtils {
16+
17+
private QuotaUtils() {
18+
}
19+
20+
public static final String QUOTA_FILE_PATTERN
21+
= getSystemResource("quota/" + USER_1 + ".xml").getPath().replace(USER_1, "%s");
22+
23+
public static void replacePortInQuotaFile(String user, int port) {
24+
copyQuotaFile(user, user, port);
25+
}
26+
27+
public static void copyQuotaFile(String srcUser, String dstUser, int withHubPort) {
28+
Browsers browsers = getQuotaFor(srcUser);
29+
setPort(browsers, withHubPort);
30+
writeQuotaFor(dstUser, browsers);
31+
}
32+
33+
public static Browsers getQuotaFor(String user) {
34+
File quotaFile = getQuotaFile(user);
35+
Browsers browsersOriginal = JAXB.unmarshal(quotaFile, Browsers.class);
36+
return SerializationUtils.clone(browsersOriginal);
37+
}
38+
39+
public static void writeQuotaFor(String user, Browsers browsers) {
40+
JAXB.marshal(browsers, getQuotaFile(user));
41+
}
42+
43+
public static File getQuotaFile(String user) {
44+
return new File(String.format(QUOTA_FILE_PATTERN, user));
45+
}
46+
47+
@SuppressWarnings("ResultOfMethodCallIgnored")
48+
public static void deleteQuotaFile(String user) {
49+
getQuotaFile(user).delete();
50+
}
51+
52+
public static void setPort(Browsers browsers, int port) {
53+
browsers.getBrowsers().get(0)
54+
.getVersions().get(0)
55+
.getRegions().get(0)
56+
.getHosts().get(0)
57+
.setPort(port);
58+
}
59+
}

0 commit comments

Comments
 (0)