Skip to content

Commit

Permalink
NXP-10860: make tests robust when async events are fast (or waited fo…
Browse files Browse the repository at this point in the history
…r) before the test
  • Loading branch information
Florent Guillaume committed Jan 24, 2013
1 parent 7486db9 commit 1e62349
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
75 changes: 50 additions & 25 deletions src/test/java/org/nuxeo/ecm/activity/TestActivityStreamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ public void serviceRegistration() {
assertNotNull(activityStreamService);
}

protected int getOffset() {
return activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null).size();
}

@Test
public void shouldStoreAnActivity() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb("test");
Expand All @@ -81,7 +88,7 @@ public void shouldStoreAnActivity() {
activityStreamService.addActivity(activity);

List<Activity> activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());

Expand All @@ -93,6 +100,8 @@ public void shouldStoreAnActivity() {

@Test
public void shouldCallRegisteredActivityStreamFilter() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb("test");
Expand All @@ -104,7 +113,7 @@ public void shouldCallRegisteredActivityStreamFilter() {
assertEquals(2, filters.size());

List<Activity> activities = activityStreamService.query(
DummyActivityStreamFilter.ID, null);
DummyActivityStreamFilter.ID, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());

Expand Down Expand Up @@ -132,31 +141,33 @@ public void shouldThrowExceptionIfFilterIsNotRegistered() {

@Test
public void shouldHandlePagination() {
int offset = getOffset();

addTestActivities(10);

List<Activity> activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 0, 5);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 5);
assertEquals(5, activities.size());
for (int i = 0; i < 5; i++) {
assertEquals("activity" + i, activities.get(i).getObject());
}

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 5, 5);
ActivityStreamService.ALL_ACTIVITIES, null, offset + 5, 5);
assertEquals(5, activities.size());
for (int i = 5; i < 10; i++) {
assertEquals("activity" + i, activities.get(i - 5).getObject());
}

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 0, 15);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 15);
assertEquals(10, activities.size());
for (int i = 0; i < 10; i++) {
assertEquals("activity" + i, activities.get(i).getObject());
}

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 15, 5);
ActivityStreamService.ALL_ACTIVITIES, null, offset + 15, 5);
assertEquals(0, activities.size());
}

Expand All @@ -173,30 +184,32 @@ protected void addTestActivities(int activitiesCount) {

@Test
public void shouldRemoveActivities() {
int offset = getOffset();

addTestActivities(10);

List<Activity> allActivities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 0, 0);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertEquals(10, allActivities.size());

Activity firstActivity = allActivities.get(0);
activityStreamService.removeActivities(Collections.singleton(firstActivity));

allActivities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 0, 0);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertEquals(9, allActivities.size());
assertFalse(allActivities.contains(firstActivity));

List<Activity> activities = allActivities.subList(0, 4);
activityStreamService.removeActivities(activities);
allActivities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 0, 0);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertEquals(5, allActivities.size());

activities = allActivities.subList(0, 5);
activityStreamService.removeActivities(activities);
allActivities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null, 0, 0);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertTrue(allActivities.isEmpty());
}

Expand Down Expand Up @@ -233,6 +246,8 @@ public void shouldStoreActivityVerbs() {

@Test
public void shouldStoreTweetActivities() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb(TweetActivityStreamFilter.TWEET_VERB);
Expand All @@ -243,7 +258,7 @@ public void shouldStoreTweetActivities() {
Map<String, Serializable> parameters = new HashMap<String, Serializable>();
parameters.put("seenBy", "Bob");
List<Activity> activities = activityStreamService.query(
TweetActivityStreamFilter.ID, parameters);
TweetActivityStreamFilter.ID, parameters, offset, 999);
assertEquals(1, activities.size());
Activity storedActivity = activities.get(0);
assertEquals(activity.getActor(), storedActivity.getActor());
Expand All @@ -253,7 +268,7 @@ public void shouldStoreTweetActivities() {
parameters = new HashMap<String, Serializable>();
parameters.put("seenBy", "Joe");
activities = activityStreamService.query(TweetActivityStreamFilter.ID,
parameters);
parameters, offset, 999);
assertEquals(1, activities.size());
storedActivity = activities.get(0);
assertEquals(activity.getActor(), storedActivity.getActor());
Expand All @@ -263,7 +278,7 @@ public void shouldStoreTweetActivities() {
parameters = new HashMap<String, Serializable>();
parameters.put("seenBy", "John");
activities = activityStreamService.query(TweetActivityStreamFilter.ID,
parameters);
parameters, offset, 999);
assertEquals(1, activities.size());
storedActivity = activities.get(0);
assertEquals(activity.getActor(), storedActivity.getActor());
Expand All @@ -273,6 +288,8 @@ public void shouldStoreTweetActivities() {

@Test
public void shouldRemoveTweets() throws ClientException {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb(TweetActivityStreamFilter.TWEET_VERB);
Expand All @@ -283,19 +300,19 @@ public void shouldRemoveTweets() throws ClientException {
Map<String, Serializable> parameters = new HashMap<String, Serializable>();
parameters.put("seenBy", "Bob");
List<Activity> activities = activityStreamService.query(
TweetActivityStreamFilter.ID, parameters);
TweetActivityStreamFilter.ID, parameters, offset, 999);
assertEquals(1, activities.size());

List<TweetActivity> tweets = getAllTweetActivities();
assertEquals(3, tweets.size());

activityStreamService.removeActivities(Collections.singleton(activity));
activities = activityStreamService.query(TweetActivityStreamFilter.ID,
parameters);
parameters, offset, 999);
assertTrue(activities.isEmpty());

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertTrue(activities.isEmpty());

tweets = getAllTweetActivities();
Expand Down Expand Up @@ -342,6 +359,8 @@ public void shouldStoreActivityStreams() {

@Test
public void shouldStoreAnActivityReply() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb("test");
Expand All @@ -350,7 +369,7 @@ public void shouldStoreAnActivityReply() {
activityStreamService.addActivity(activity);

List<Activity> activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());

Expand All @@ -366,7 +385,7 @@ public void shouldStoreAnActivityReply() {
assertEquals(storedActivity.getId() + "-reply-1", storedReply.getId());

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());
storedActivity = activities.get(0);
Expand All @@ -384,6 +403,8 @@ public void shouldStoreAnActivityReply() {

@Test
public void shouldStoreMultipleActivityReplies() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb("test");
Expand All @@ -392,7 +413,7 @@ public void shouldStoreMultipleActivityReplies() {
activityStreamService.addActivity(activity);

List<Activity> activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());

Expand All @@ -416,7 +437,7 @@ public void shouldStoreMultipleActivityReplies() {
activityStreamService.addActivityReply(storedActivity.getId(), fourthReply);

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());
storedActivity = activities.get(0);
Expand Down Expand Up @@ -453,6 +474,8 @@ public void shouldStoreMultipleActivityReplies() {

@Test
public void shouldRemoveActivityReply() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb("test");
Expand All @@ -461,7 +484,7 @@ public void shouldRemoveActivityReply() {
activityStreamService.addActivity(activity);

List<Activity> activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());

Expand All @@ -485,7 +508,7 @@ public void shouldRemoveActivityReply() {
activityStreamService.addActivityReply(storedActivity.getId(), fourthReply);

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());
storedActivity = activities.get(0);
Expand Down Expand Up @@ -521,7 +544,7 @@ public void shouldRemoveActivityReply() {

activityStreamService.removeActivityReply(activity.getId(), thirdReply.getId());
activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());
storedActivity = activities.get(0);
Expand All @@ -537,6 +560,8 @@ public void shouldRemoveActivityReply() {

@Test
public void testActivityUpgraders() {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Bender");
activity.setVerb("hi");
Expand All @@ -549,7 +574,7 @@ public void testActivityUpgraders() {
activityStreamService.addActivity(activity);

ActivitiesList activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(2, activities.size());

Expand All @@ -561,7 +586,7 @@ public void testActivityUpgraders() {
((ActivityStreamServiceImpl) activityStreamService).upgradeActivities();

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(2, activities.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public class TestActivityOperations {
@Inject
protected AutomationService automationService;

protected int getOffset() {
return activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null).size();
}

@Test
@Ignore
public void shouldAddAnActivityReply() throws Exception {
Expand Down Expand Up @@ -119,6 +124,8 @@ public void shouldAddAnActivityReply() throws Exception {

@Test
public void shouldRemoveAnActivityReply() throws Exception {
int offset = getOffset();

Activity activity = new ActivityImpl();
activity.setActor("Administrator");
activity.setVerb("test");
Expand All @@ -138,7 +145,7 @@ public void shouldRemoveAnActivityReply() throws Exception {
activity.getId(), secondReply);

List<Activity> activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());
activity = activities.get(0);
Expand All @@ -156,7 +163,7 @@ public void shouldRemoveAnActivityReply() throws Exception {
automationService.run(ctx, chain);

activities = activityStreamService.query(
ActivityStreamService.ALL_ACTIVITIES, null);
ActivityStreamService.ALL_ACTIVITIES, null, offset, 999);
assertNotNull(activities);
assertEquals(1, activities.size());
activity = activities.get(0);
Expand Down

0 comments on commit 1e62349

Please sign in to comment.