Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Updates a counter when a test run fails
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiZaitcev committed Aug 1, 2018
1 parent e8a0838 commit 476e39d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,68 @@

package com.shazam.fork.model;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;

import static com.google.common.collect.FluentIterable.from;
import java.util.function.Predicate;

import static com.google.common.collect.HashMultimap.create;

/**
* Class that keeps track of the number of times each testCase is executed for device.
*/
public class PoolTestCaseFailureAccumulator implements PoolTestCaseAccumulator {
private SetMultimap<Pool, TestCaseEventCounter> map = Multimaps.synchronizedSetMultimap(create());
private final SetMultimap<Pool, TestCaseEventCounter> testCaseCounters = create();

@Override
public void record(Pool pool, TestCaseEvent testCaseEvent) {
if (!map.containsKey(pool)) {
map.put(pool, createNew(testCaseEvent));
}
synchronized (testCaseCounters) {
if (!testCaseCounters.containsKey(pool)) {
testCaseCounters.put(pool, createNew(testCaseEvent));
}

if (!from(map.get(pool)).anyMatch(isSameTestCase(testCaseEvent))) {
map.get(pool).add(
createNew(testCaseEvent)
.withIncreasedCount());
} else {
from(map.get(pool))
.firstMatch(isSameTestCase(testCaseEvent)).get()
.increaseCount();
boolean hasCountedBefore = testCaseCounters.get(pool).stream()
.anyMatch(isSameTestCase(testCaseEvent));
if (hasCountedBefore) {
testCaseCounters.get(pool).stream()
.filter(isSameTestCase(testCaseEvent))
.findFirst()
.ifPresent(TestCaseEventCounter::increaseCount);
} else {
testCaseCounters.get(pool).add(createNew(testCaseEvent).withIncreasedCount());
}
}
}

@Override
public int getCount(Pool pool, TestCaseEvent testCaseEvent) {
if (map.containsKey(pool)) {
return from(map.get(pool))
.firstMatch(isSameTestCase(testCaseEvent)).or(TestCaseEventCounter.EMPTY)
.getCount();
if (testCaseCounters.containsKey(pool)) {
synchronized (testCaseCounters) {
return testCaseCounters.get(pool).stream()
.filter(isSameTestCase(testCaseEvent))
.findFirst()
.orElse(TestCaseEventCounter.EMPTY)
.getCount();
}
} else {
return 0;
}
}

@Override
public int getCount(TestCaseEvent testCaseEvent) {
int result = 0;
ImmutableList<TestCaseEventCounter> counters = from(map.values())
.filter(isSameTestCase(testCaseEvent)).toList();
for (TestCaseEventCounter counter : counters) {
result += counter.getCount();
synchronized (testCaseCounters) {
return testCaseCounters.values().stream()
.filter(isSameTestCase(testCaseEvent))
.mapToInt(TestCaseEventCounter::getCount)
.sum();
}
return result;
}

private static TestCaseEventCounter createNew(final TestCaseEvent testCaseEvent) {
return new TestCaseEventCounter(testCaseEvent, 0);
}

private static Predicate<TestCaseEventCounter> isSameTestCase(final TestCaseEvent testCaseEvent) {
return new Predicate<TestCaseEventCounter>() {
@Override
public boolean apply(TestCaseEventCounter input) {
return input.getTestCaseEvent() != null
&& testCaseEvent.equals(input.getTestCaseEvent());
}
};
private static Predicate<TestCaseEventCounter> isSameTestCase(TestCaseEvent testCaseEvent) {
return testCaseEventCounter -> testCaseEventCounter.getTestCaseEvent().equals(testCaseEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {

@Override
public void testRunFailed(String errorMessage) {

poolProgressTracker.failedTest();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class PoolTestCaseAccumulatorTestFailure {
public class PoolTestCaseFailureAccumulatorTest {
private final Device A_DEVICE = aDevice()
.withSerial("a_device")
.build();
Expand Down

0 comments on commit 476e39d

Please sign in to comment.