Skip to content

Commit

Permalink
fixup! Add a concurrent write test
Browse files Browse the repository at this point in the history
  • Loading branch information
bchambers committed Jul 27, 2016
1 parent a0c7b6b commit ec43e7b
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Tests for {@link AggregatorContainer}.
Expand Down Expand Up @@ -84,4 +88,27 @@ public void failToAddValueAfterCommitWithPrevious() {
thrown.expect(IllegalStateException.class);
aggregator.addValue(5);
}

@Test
public void concurrentWrites() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(20);
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
final int value = i;
final AggregatorContainer.Mutator mutator = container.createMutator();
executor.submit(new Runnable() {
@Override
public void run() {
mutator.createAggregator("sum_int", new SumIntegerFn()).addValue(value);
mutator.commit();
}
});
}
executor.shutdown();
assertThat("Expected all threads to complete after 5 seconds",
executor.awaitTermination(5, TimeUnit.SECONDS), equalTo(true));

assertThat((Integer) container.getAggregate("sum_int"), equalTo(sum));
}
}

0 comments on commit ec43e7b

Please sign in to comment.