Skip to content

Commit

Permalink
adjusted SegmentationRuleCacheFixTest to the new Event behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
shoeffner committed Oct 12, 2016
1 parent ea7c50c commit 77d7e79
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import javax.ws.rs.core.Response;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.intuit.wasabi.api.APISwaggerResource.DEFAULT_EVENT;
Expand Down Expand Up @@ -141,52 +139,6 @@ public Response recordEvents(
return httpHeader.headers(CREATED).build();
}

/**
* Submit events for users within the context of a specific application
* and experiment. Each event is an impression or action.
*
* @param applicationName the application name
* @param experimentLabel the experiment label
* @param eventList the {@link com.intuit.wasabi.analyticsobjects.EventList} event list
* @throws UnsupportedOperationException UnsupportedOperationException
*/
@POST
@Path("applications/{applicationName}/experiments/{experimentLabel}/users")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Timed
public Response recordUsersEvents(
@PathParam("applicationName")
final Application.Name applicationName,

@PathParam("experimentLabel")
final Experiment.Label experimentLabel,

final Map<User.ID, List<Event>> eventList) {
throw new UnsupportedOperationException("Not implemented");
}

/**
* Submit events for users and experiments within the context of a
* specific application. Each event is an impression or action.
*
* @param applicationName the application name
* @param userID the user id
* @param eventList the {@link com.intuit.wasabi.analyticsobjects.EventList} event list
* @throws UnsupportedOperationException always throws
*/
@POST
@Path("applications/{applicationName}/experiments")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Timed
public Response recordExperimentsEvents(
@PathParam("applicationName") final Application.Name applicationName,
@PathParam("userID") final User.ID userID,
final Map<Experiment.Label, Map<User.ID, List<Event>>> eventList) {
throw new UnsupportedOperationException("Not implemented");
}

/**
* Returns number of events currently in the queue
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -90,24 +92,6 @@ public void recordEvents() throws Exception {
verify(events).recordEvents(any(Application.Name.class), any(Experiment.Label.class), any(User.ID.class), any(EventList.class), any(Set.class));
}

@Test
public void recordUsersEvents() throws UnsupportedOperationException {
Map<User.ID, List<Event>> eventList = new HashMap<>();

thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not implemented");
resource.recordUsersEvents(applicationName, experimentLabel, eventList);
}

@Test
public void recordExperimentsEvents() throws Exception {
Map<Experiment.Label, Map<User.ID, List<Event>>> eventList = new HashMap<>();

thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Not implemented");
resource.recordExperimentsEvents(applicationName, userID, eventList);
}

@Test
public void getEventsQueueLength() throws Exception {
assertNotNull(resource.getEventsQueueLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,38 @@
* By default all events are IMPRESSIONs.
*/
public class Event extends ModelItem {
/** this event's timestamp */
/**
* The serialization strategy for comparisons and JSON serialization.
*/
private static SerializationStrategy serializationStrategy = new DefaultNameExclusionStrategy();
/**
* this event's timestamp
*/
public String timestamp;

/** The event type, by default "IMPRESSION", but can be different to represent an Action. */
/**
* The event type, by default "IMPRESSION", but can be different to represent an Action.
*/
public String name = "IMPRESSION";

/** this event's context */
/**
* this event's context
*/
public String context;

/** The payload coming with the event. */
/**
* The payload coming with the event.
*/
public String payload;

/** This event's value. */
/**
* This event's value.
*/
public String value;

public String userId;

public void setUserId(String userId) {
this.userId = userId;
}

/** The serialization strategy for comparisons and JSON serialization. */
private static SerializationStrategy serializationStrategy = new DefaultNameExclusionStrategy();

/**
* Creates a simple IMPRESSION.
*/
public Event() {
}



/**
* Copies an event.
*
Expand All @@ -64,6 +64,7 @@ public Event(Event other) {
update(other);
}


/**
* Creates a specific Event.
*
Expand All @@ -73,6 +74,10 @@ public Event(String name) {
this.setName(name);
}

public void setUserId(String userId) {
this.userId = userId;
}

/**
* Sets this event's name, thus overwriting the default "IMPRESSION'.
*
Expand Down Expand Up @@ -118,15 +123,13 @@ public Event setPayload(String payload) {
}

/**
* Sets this event's value. Note that this has currently no effect as the value is not implemented.
* TODO: once supported remove the note
* Returns the current SerializationStrategy.
*
* @param value the new value
* @return this
* @return the current SerializationStategy.
*/
public Event setValue(String value) {
this.value = value;
return this;
@Override
public SerializationStrategy getSerializationStrategy() {
return Event.serializationStrategy;
}

/**
Expand All @@ -139,14 +142,4 @@ public Event setValue(String value) {
public void setSerializationStrategy(SerializationStrategy serializationStrategy) {
Event.serializationStrategy = serializationStrategy;
}

/**
* Returns the current SerializationStrategy.
*
* @return the current SerializationStategy.
*/
@Override
public SerializationStrategy getSerializationStrategy() {
return Event.serializationStrategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package com.intuit.wasabi.tests.service;


import com.intuit.wasabi.tests.library.APIServerConnector;
import com.intuit.wasabi.tests.library.TestBase;
import com.intuit.wasabi.tests.library.util.*;
import com.intuit.wasabi.tests.library.util.Constants;
import com.intuit.wasabi.tests.library.util.ModelUtil;
import com.intuit.wasabi.tests.library.util.RetryAnalyzer;
import com.intuit.wasabi.tests.library.util.RetryTest;
import com.intuit.wasabi.tests.library.util.serialstrategies.DefaultNameExclusionStrategy;
import com.intuit.wasabi.tests.library.util.serialstrategies.DefaultNameInclusionStrategy;
import com.intuit.wasabi.tests.library.util.serialstrategies.SerializationStrategy;
Expand All @@ -29,21 +33,24 @@
import com.intuit.wasabi.tests.model.factory.EventFactory;
import com.intuit.wasabi.tests.model.factory.ExperimentFactory;
import com.intuit.wasabi.tests.model.factory.UserFactory;
import com.intuit.wasabi.tests.library.APIServerConnector;
import org.apache.http.HttpStatus;
import org.testng.Assert;
import org.testng.annotations.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.intuit.wasabi.tests.library.util.ModelAssert.assertEqualModelItems;

/**
* Testing the fix for the Segmentation Rule Cache problem.
*
* <p>
* Issue was when a segmentation rule for a running experiment was updated the update would not take.
* Root cause was that the old rule was cached.
* Fix was to reset the cache, but that would have to be done once for each node running behind the load balancer.
Expand All @@ -52,8 +59,8 @@
*/
public class SegmentationRuleCacheFixTest extends TestBase {

private final int repeatCount = 30;
private int nodeCount;
public final int repeatCount = 30;
private Experiment experiment;
private User user;
private List<User> userList;
Expand All @@ -62,6 +69,7 @@ public class SegmentationRuleCacheFixTest extends TestBase {
private String ruleSecond;
private String ruleNonHeader;
private SerializationStrategy experimentSerializationStrategy;
private List<Assignment> assignments;

private int newAssignments = 0;
private int noProfileMatches = 0;
Expand Down Expand Up @@ -92,6 +100,7 @@ private void init() throws NumberFormatException {
public void assertRepeatCount() {
Assert.assertTrue(2 * nodeCount < repeatCount, "Repeat count not big enough.");
}

/**
* Creates an experiment with only one control bucket and the first rule.
* After that the experiment is started.
Expand Down Expand Up @@ -119,7 +128,7 @@ public void createExperiment() {
* Retrieves a running experiment.
*/
@Test(dependsOnMethods = {"createExperiment"}, retryAnalyzer = RetryAnalyzer.class)
@RetryTest(maxTries = 3, warmup =500)
@RetryTest(maxTries = 3, warmup = 500)
public void retrieveRunningExperiment() {
experiment = getExperiment(experiment);
Assert.assertEquals(experiment.state, Constants.EXPERIMENT_STATE_RUNNING);
Expand All @@ -133,18 +142,18 @@ public void retrieveRunningExperiment() {
@DataProvider
public Object[][] assignmentDataProvider() {
return new Object[][]{
new Object[]{user, "NoMatchForThisAgentValue", Constants.ASSIGNMENT_NO_PROFILE_MATCH},
new Object[]{user, "Agent001", Constants.ASSIGNMENT_NEW_ASSIGNMENT},
new Object[]{user, "Agent001", Constants.ASSIGNMENT_EXISTING_ASSIGNMENT},
new Object[]{user, "NoMatchForThisAgentValue", Constants.ASSIGNMENT_NO_PROFILE_MATCH},
new Object[]{user, "Agent001", Constants.ASSIGNMENT_NEW_ASSIGNMENT},
new Object[]{user, "Agent001", Constants.ASSIGNMENT_EXISTING_ASSIGNMENT},
};
}

/**
* Tries to get an assignment, but fails as the rule fails.
*
* @param user the user
* @param userAgent the user agent
* @param expectedStatus the expected status
* @param user the user
* @param userAgent the user agent
* @param expectedStatus the expected status
*/
@Test(dependsOnMethods = {"retrieveRunningExperiment"}, dataProvider = "assignmentDataProvider")
public void assignUser(User user, String userAgent, String expectedStatus) {
Expand Down Expand Up @@ -210,9 +219,9 @@ public void verifyResultsOfAssignments() {
* Retrieves the assignments and checks if the count is as expected.
*/
@Test(dependsOnMethods = {"verifyResultsOfAssignments"}, retryAnalyzer = RetryAnalyzer.class)
@RetryTest(maxTries = 5, warmup =2500)
@RetryTest(maxTries = 5, warmup = 2500)
public void getAssignmentsAfterVerification() {
List<Assignment> assignments = getAssignments(experiment);
assignments = getAssignments(experiment);
// +1 for user 0
Assert.assertEquals(assignments.size(), newAssignments + 1, "Number of assignments does not match.");
}
Expand All @@ -222,8 +231,17 @@ public void getAssignmentsAfterVerification() {
*/
@Test(dependsOnMethods = {"getAssignmentsAfterVerification"})
public void postUserImpressions() {
for (User user : userList) {
postEvent(EventFactory.createImpression(), experiment, user, HttpStatus.SC_CREATED);
for (User currentUser : userList) {
List<Assignment> currentAssignments = assignments.parallelStream()
.filter(a -> a.user_id.equals(currentUser.userID))
.collect(Collectors.toList());
if (currentAssignments.isEmpty()) {
continue;
}

postEvent(EventFactory.createImpression(), experiment, currentUser,
Objects.isNull(currentAssignments.get(0).bucket_label) ?
HttpStatus.SC_NOT_FOUND : HttpStatus.SC_CREATED);
}
}

Expand Down Expand Up @@ -262,7 +280,7 @@ public void assignUsersToBuckets_no_key_get() {
/**
* Assigns a user without a User-Agent (POST). Should result in NO_PROFILE_MATCH.
*/
@Test(dependsOnMethods = { "assignUsersToBuckets_no_key_get" })
@Test(dependsOnMethods = {"assignUsersToBuckets_no_key_get"})
public void assignUsersToBuckets_no_key_post() {
Assignment assignment = postAssignment(experiment, UserFactory.createUser(user.userID + "_no_key"));
Assert.assertEquals(assignment.status, Constants.ASSIGNMENT_NO_PROFILE_MATCH);
Expand All @@ -271,7 +289,7 @@ public void assignUsersToBuckets_no_key_post() {
/**
* Assigns a user with a profile which does not match.
*/
@Test(dependsOnMethods = { "assignUsersToBuckets_no_key_post" })
@Test(dependsOnMethods = {"assignUsersToBuckets_no_key_post"})
public void assignUsersToBuckets_key_post_no_match() {
Map<String, Object> profile = new HashMap<>();
profile.put("subscriber", false);
Expand All @@ -282,7 +300,7 @@ public void assignUsersToBuckets_key_post_no_match() {
/**
* Assigns a user with a profile which does not match.
*/
@Test(dependsOnMethods = { "assignUsersToBuckets_key_post_no_match" }, invocationCount = repeatCount)
@Test(dependsOnMethods = {"assignUsersToBuckets_key_post_no_match"}, invocationCount = repeatCount)
public void assignUsersToBuckets_key_post_match() {
User user = UserFactory.createUser();
userList2.add(user);
Expand All @@ -304,7 +322,7 @@ public void assignUsersToBuckets_key_post_match() {
/**
* Verifies the results from {@link #assignUsersToBuckets_key_post_match()}. Resets the counters.
*/
@Test(dependsOnMethods = { "assignUsersToBuckets_key_post_match" }, retryAnalyzer = RetryAnalyzer.class)
@Test(dependsOnMethods = {"assignUsersToBuckets_key_post_match"}, retryAnalyzer = RetryAnalyzer.class)
@RetryTest(maxTries = 5, warmup = 2500)
public void verifyResultsOfNoHeaderAssignments() {
List<Assignment> assignments = getAssignments(experiment);
Expand Down
1 change: 1 addition & 0 deletions modules/functional-test/testng_integrationTests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<class name="com.intuit.wasabi.tests.service.assignment.BatchAssignment" />
<class name="com.intuit.wasabi.tests.service.assignment.PutAssignment" />
<class name="com.intuit.wasabi.tests.service.assignment.ExportAssignment" />
<class name="com.intuit.wasabi.tests.service.assignment.EventSubmissionBeforeAssignmentTest" />
</classes>
</test>
<test name="IntegrationBucketTests">
Expand Down

0 comments on commit 77d7e79

Please sign in to comment.