/
LaboratoryIT.java
121 lines (94 loc) · 4.27 KB
/
LaboratoryIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.wixpress.petri.test;
import com.wixpress.petri.experiments.domain.*;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.util.UUID;
import static com.wixpress.petri.experiments.domain.ExperimentBuilder.aCopyOf;
import static com.wixpress.petri.experiments.domain.ExperimentSnapshotBuilder.anExperimentSnapshot;
import static com.wixpress.petri.petri.SpecDefinition.ExperimentSpecBuilder.aNewlyGeneratedExperimentSpec;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* User: Dalias
* Date: 8/7/14
* Time: 6:45 PM
*/
public class LaboratoryIT {
public static final int SAMPLE_APP_PORT = 9015;
public static final int PETRI_PORT = 9016;
public static final String THE_KEY = "THE_KEY";
private final SampleAppRunner sampleApp = new SampleAppRunner(SAMPLE_APP_PORT);
private final FakePetriServer petri = new FakePetriServer(PETRI_PORT);
private Experiment originalExperiment;
private final DateTime now = new DateTime();
@Before
public void startServers() throws Exception {
petri.start();
sampleApp.start();
petri.addSpec(
aNewlyGeneratedExperimentSpec(THE_KEY).
withTestGroups(asList("a", "b")).
withScopes(new ScopeDefinition("the scope", false)));
originalExperiment = petri.addExperiment(
snapshotWithGroups(new TestGroup(0, 100, "a"), new TestGroup(1, 0, "b")));
}
@After
public void stopServers() throws Exception {
sampleApp.stop();
petri.stop();
}
private void updateExperimentState(final TestGroup... testGroups) {
final ExperimentBuilder experimentBuilder = aCopyOf(originalExperiment).withExperimentSnapshot(
snapshotWithGroups(testGroups).build()
);
petri.updateExperiment(experimentBuilder);
originalExperiment = experimentBuilder.build();
}
private void updateExperimentState(boolean forLoggedInUsersOnly) {
final ExperimentBuilder experimentBuilder = aCopyOf(originalExperiment).withExperimentSnapshot(
snapshotWithGroups(originalExperiment.getGroups().toArray(new TestGroup[0]))
.withOnlyForLoggedInUsers(forLoggedInUsersOnly).build()
);
petri.updateExperiment(experimentBuilder);
originalExperiment = experimentBuilder.build();
}
private ExperimentSnapshotBuilder snapshotWithGroups(final TestGroup... testGroups) {
return anActiveSnapshot().
withGroups(asList(testGroups)).withOnlyForLoggedInUsers(false);
}
private ExperimentSnapshotBuilder anActiveSnapshot() {
return anExperimentSnapshot().
withStartDate(now.minusMinutes(1)).
withEndDate(now.plusYears(1)).
withKey(THE_KEY);
}
@Test
public void conductingASimpleExperiment() throws Exception {
String testResult = sampleApp.conductExperiment(THE_KEY, "FALLBACK");
assertThat(testResult, is("a"));
}
@Test
public void experimentsResultsArePreservedAcrossDifferentRequests() throws Exception {
// this causes the experiment to be persisted
sampleApp.conductExperiment(THE_KEY, "FALLBACK");
// flip the toggle so that group 'b' is now the winning group
updateExperimentState(new TestGroup(0, 0, "a"), new TestGroup(1, 100, "b"));
assertThat(sampleApp.conductExperiment(THE_KEY, "FALLBACK"), is("a"));
}
@Test
public void experimentsResultsArePreservedAcrossDifferentRequestsForRegisteredUsers() throws Exception {
final boolean forLoggedInUsersOnly = true;
updateExperimentState(forLoggedInUsersOnly);
// this causes the experiment to be persisted
final UUID uuid = UUID.randomUUID();
sampleApp.conductExperimentByUser(THE_KEY, "FALLBACK", uuid);
// flip the toggle so that group 'b' is now the winning group
updateExperimentState(new TestGroup(0, 0, "a"), new TestGroup(1, 100, "b"));
updateExperimentState(forLoggedInUsersOnly);
assertThat(sampleApp.conductExperimentByUser(THE_KEY, "FALLBACK", uuid), is("a"));
}
}