diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-31 10:51:12 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-31 10:51:12 +0100 |
commit | 211fd778ac9011ec79d745767b7d23c8de738003 (patch) | |
tree | 9e6f1a804f8218903611325cbff84bb51f5f2975 /same/src/test/java | |
parent | 00f5afe8de3544bf99f4d84c7904ed784f50074c (diff) |
WorkList => WorkQueue – A worker thread helper class.
Diffstat (limited to 'same/src/test/java')
-rw-r--r-- | same/src/test/java/com/orbekk/util/WorkQueueTest.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/same/src/test/java/com/orbekk/util/WorkQueueTest.java b/same/src/test/java/com/orbekk/util/WorkQueueTest.java new file mode 100644 index 0000000..ab6e138 --- /dev/null +++ b/same/src/test/java/com/orbekk/util/WorkQueueTest.java @@ -0,0 +1,37 @@ +package com.orbekk.util; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class WorkQueueTest { + @Test + public void testPerformsWork() throws Exception { + final ArrayList<Integer> doubled = new ArrayList<Integer>(); + WorkQueue<Integer> worker = new WorkQueue<Integer>() { + @Override protected void onChange() { + List<Integer> list = getAndClear(); + for (int x : list) { + doubled.add(x * 2); + } + synchronized (doubled) { + doubled.notifyAll(); + } + } + }; + + synchronized (doubled) { + worker.start(); + worker.add(1); + doubled.wait(); + worker.interrupt(); + } + + worker.join(); + assertEquals(2, (int)doubled.get(0)); + } + +} |