aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java
diff options
context:
space:
mode:
authorJon Marius Venstad <jvenstad@yahoo-inc.com>2019-05-24 13:24:44 +0200
committerJon Marius Venstad <jvenstad@yahoo-inc.com>2019-05-24 15:55:22 +0200
commita9d67ff3662ef5020ce51197b698289753a83399 (patch)
tree4e5abb2e43639e9849002423df946c41dc59e242 /vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java
parenta159b4ce190a42e2c25ad5fae0e010d947bbe89c (diff)
Add StripedExecutor for serialising tasks per key
Diffstat (limited to 'vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java')
-rw-r--r--vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java b/vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java
new file mode 100644
index 00000000000..712f24c79d4
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/concurrent/StripedExecutorTest.java
@@ -0,0 +1,44 @@
+package com.yahoo.concurrent;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author jonmv
+ */
+public class StripedExecutorTest {
+
+ private static final int workers = 1 << 5;
+ private static final int values = 1 << 10;
+
+ @Test
+ public void testSerialization() {
+ AtomicLong counter = new AtomicLong(0);
+ List<Deque<Long>> sequences = new ArrayList<>();
+ for (int j = 0; j < workers; j++)
+ sequences.add(new ConcurrentLinkedDeque<>());
+
+ StripedExecutor<Integer> executor = new StripedExecutor<>();
+ for (int i = 0; i < values; i++)
+ for (int j = 0; j < workers; j++) {
+ Deque<Long> sequence = sequences.get(j);
+ executor.execute(j, () -> sequence.add(counter.incrementAndGet()));
+ }
+ executor.shutdownAndWait();
+
+ for (int j = 0; j < workers; j++) {
+ assertEquals(values, sequences.get(j).size());
+ assertArrayEquals(sequences.get(j).stream().sorted().toArray(Long[]::new),
+ sequences.get(j).toArray(Long[]::new));
+ }
+ }
+
+}