Skip to content

Commit

Permalink
Clean up tests to use EventCollectionApparatus more widely.
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=106382513
  • Loading branch information
ulfjack authored and fweikert committed Oct 27, 2015
1 parent 52b3868 commit 1a90e50
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.events;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

import java.util.ArrayList;
Expand All @@ -38,6 +39,13 @@ public EventCollector(Set<EventKind> mask) {
this(mask, new ArrayList<Event>());
}

/**
* This collector will collect all events that match the event mask.
*/
public EventCollector(EventKind... mask) {
this(ImmutableSet.copyOf(mask), new ArrayList<Event>());
}

/**
* This collector will save the Event instances in the provided
* collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.events.EventCollector;
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.events.util.EventCollectionApparatus;
import com.google.devtools.build.lib.util.Clock;

import org.junit.Before;
Expand Down Expand Up @@ -66,7 +65,7 @@ public long nanoTime() {
}
}

private EventCollector collector;
private EventCollectionApparatus events;
private ActionExecutionStatusReporter statusReporter;
private EventBus eventBus;
private MockClock clock = new MockClock();
Expand All @@ -85,33 +84,32 @@ private Action mockAction(String progressMessage, boolean remote) {

@Before
public void setUp() throws Exception {
collector = new EventCollector(EventKind.ALL_EVENTS);
Reporter reporter = new Reporter();
reporter.addHandler(collector);
statusReporter = ActionExecutionStatusReporter.create(reporter, clock);
events = new EventCollectionApparatus(EventKind.ALL_EVENTS);
statusReporter = ActionExecutionStatusReporter.create(events.reporter(), clock);
eventBus = new EventBus();
eventBus.register(statusReporter);
}

private void verifyNoOutput() {
collector.clear();
events.clear();
statusReporter.showCurrentlyExecutingActions("");
assertEquals(0, collector.count());
assertThat(events.collector()).isEmpty();
}

private void verifyOutput(String... lines) throws Exception {
collector.clear();
events.clear();
statusReporter.showCurrentlyExecutingActions("");
assertThat(Splitter.on('\n').omitEmptyStrings().trimResults().split(
Iterables.getOnlyElement(collector).getMessage().replaceAll(" +", " ")))
Iterables.getOnlyElement(events.collector()).getMessage().replaceAll(" +", " ")))
.containsExactlyElementsIn(Arrays.asList(lines)).inOrder();
}

private void verifyWarningOutput(String... lines) throws Exception {
collector.clear();
events.setFailFast(false);
events.clear();
statusReporter.warnAboutCurrentlyExecutingActions();
assertThat(Splitter.on('\n').omitEmptyStrings().trimResults().split(
Iterables.getOnlyElement(collector).getMessage().replaceAll(" +", " ")))
Iterables.getOnlyElement(events.collector()).getMessage().replaceAll(" +", " ")))
.containsExactlyElementsIn(Arrays.asList(lines)).inOrder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public class EventCollectorTest extends EventTestTemplate {
@Test
public void usesPassedInCollection() {
Collection<Event> events = new ArrayList<>();
EventCollector collector =
new EventCollector(EventKind.ERRORS_AND_WARNINGS, events);
EventCollector collector = new EventCollector(EventKind.ERRORS_AND_WARNINGS, events);
collector.handle(event);
Event onlyEvent = events.iterator().next();
assertEquals(event.getMessage(), onlyEvent.getMessage());
Expand All @@ -51,8 +50,7 @@ public void usesPassedInCollection() {

@Test
public void collectsEvents() {
EventCollector collector =
new EventCollector(EventKind.ERRORS_AND_WARNINGS);
EventCollector collector = new EventCollector(EventKind.ERRORS_AND_WARNINGS);
collector.handle(event);
Iterator<Event> collectedEventIt = collector.iterator();
Event onlyEvent = collectedEventIt.next();
Expand All @@ -63,5 +61,4 @@ public void collectsEvents() {
onlyEvent.getLocation().getStartOffset());
assertFalse(collectedEventIt.hasNext());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ public Event assertContainsWarning(String expectedMessage) {
return MoreAsserts.assertContainsEvent(eventCollector, expectedMessage, EventKind.WARNING);
}

/**
* Utility method: Assert that the {@link #collector()} has received an event of the given type
* and with the {@code expectedMessage}.
*/
public Event assertContainsEvent(EventKind kind, String expectedMessage) {
return MoreAsserts.assertContainsEvent(eventCollector, expectedMessage, kind);
}

public List<Event> assertContainsEventWithFrequency(String expectedMessage,
int expectedFrequency) {
return MoreAsserts.assertContainsEventWithFrequency(eventCollector, expectedMessage,
Expand Down

0 comments on commit 1a90e50

Please sign in to comment.