Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests.
- Loading branch information
jodzga
committed
Dec 14, 2015
1 parent
b784147
commit fc27e66
Showing
4 changed files
with
402 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
contrib/parseq-batching/src/test/java/com/linkedin/parseq/batching/RecordingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.linkedin.parseq.batching; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
import com.linkedin.parseq.batching.BatchImpl.BatchEntry; | ||
import com.linkedin.parseq.function.Tuple2; | ||
import com.linkedin.parseq.function.Tuples; | ||
import com.linkedin.parseq.promise.SettablePromise; | ||
|
||
public class RecordingStrategy<G, K, T> extends BatchingStrategy<G, K, T> { | ||
|
||
final List<K> _classifiedKeys = new ArrayList<>(); | ||
final List<Batch<K, T>> _executedBatches = new ArrayList<>(); | ||
final List<Tuple2<K, BatchEntry<T>>> _executedSingletons = new ArrayList<>(); | ||
|
||
final BiConsumer<K, SettablePromise<T>> _completer; | ||
final Function<K, G> _classifier; | ||
|
||
public RecordingStrategy(BiConsumer<K, SettablePromise<T>> completer,Function<K, G> classifier) { | ||
_completer = completer; | ||
_classifier = classifier; | ||
} | ||
|
||
@Override | ||
public void executeBatch(G group, Batch<K, T> batch) { | ||
_executedBatches.add(batch); | ||
batch.foreach(_completer); | ||
} | ||
|
||
@Override | ||
public void executeSingleton(G group, K key, BatchEntry<T> entry) { | ||
_executedSingletons.add(Tuples.tuple(key, entry)); | ||
_completer.accept(key, entry.getPromise()); | ||
} | ||
|
||
@Override | ||
public G classify(K key) { | ||
_classifiedKeys.add(key); | ||
return _classifier.apply(key); | ||
} | ||
|
||
public List<K> getClassifiedKeys() { | ||
return _classifiedKeys; | ||
} | ||
|
||
public List<Batch<K, T>> getExecutedBatches() { | ||
return _executedBatches; | ||
} | ||
|
||
public List<Tuple2<K, BatchEntry<T>>> getExecutedSingletons() { | ||
return _executedSingletons; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
contrib/parseq-batching/src/test/java/com/linkedin/parseq/batching/TestBatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.linkedin.parseq.batching; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.Function; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import com.linkedin.parseq.batching.BatchImpl.BatchBuilder; | ||
import com.linkedin.parseq.promise.Promises; | ||
import com.linkedin.parseq.promise.SettablePromise; | ||
import com.linkedin.parseq.trace.ShallowTraceBuilder; | ||
|
||
public class TestBatch { | ||
|
||
@Test | ||
public void testEmptyBatch() { | ||
BatchBuilder<Integer, String> builder = Batch.builder(); | ||
Batch<Integer, String> empty = builder.build(); | ||
|
||
assertEquals(empty.size(), 0); | ||
assertEquals(empty.values().size(), 0); | ||
assertEquals(empty.entries().size(), 0); | ||
} | ||
|
||
@Test | ||
public void testBatch() { | ||
|
||
final AtomicInteger counter = new AtomicInteger(0); | ||
|
||
final Function<String, SettablePromise<String>> createPromise = expected -> { | ||
SettablePromise<String> promise = Promises.settable(); | ||
promise.addListener(p -> { | ||
if (p.get().equals(expected)) { | ||
counter.incrementAndGet(); | ||
} | ||
}); | ||
return promise; | ||
}; | ||
|
||
BatchBuilder<Integer, String> builder = Batch.builder(); | ||
builder.add(0, new ShallowTraceBuilder(0L), createPromise.apply("0")); | ||
builder.add(1, new ShallowTraceBuilder(1L), createPromise.apply("1")); | ||
final SettablePromise<String> p2 = createPromise.apply("2"); | ||
builder.add(2, new ShallowTraceBuilder(2L), p2); | ||
final SettablePromise<String> p3 = Promises.settable(); | ||
builder.add(3, new ShallowTraceBuilder(2L), p3); | ||
builder.add(0, new ShallowTraceBuilder(3L), createPromise.apply("0")); //duplicate | ||
Batch<Integer, String> batch = builder.build(); | ||
|
||
assertEquals(batch.size(), 4); | ||
assertEquals(batch.values().size(), 4); | ||
assertEquals(batch.entries().size(), 4); | ||
assertEquals(batch.keys().size(), 4); | ||
|
||
assertTrue(batch.keys().contains(0)); | ||
assertTrue(batch.keys().contains(1)); | ||
assertTrue(batch.keys().contains(2)); | ||
assertTrue(batch.keys().contains(3)); | ||
|
||
batch.done(0, "0"); | ||
batch.done(1, "1"); | ||
batch.fail(3, new Exception()); | ||
|
||
assertEquals(counter.get(), 3); // 0 with duplicate + 1 | ||
assertFalse(p2.isDone()); | ||
assertTrue(p3.isDone()); | ||
assertTrue(p3.isFailed()); | ||
} | ||
|
||
} |
Oops, something went wrong.