From 856bebd5f8bf46293edc083aa1a1e9fadab25e70 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 4 May 2011 00:20:02 +0000 Subject: Add support for Query controller and timeouts; remove vestigial support for interrupts. --- .../com/amazon/carbonado/cursor/TestCursors.java | 36 ++++++++++++++++++++++ .../carbonado/cursor/TestMergeSortBuffer.java | 25 +++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) (limited to 'src/test/java/com/amazon/carbonado/cursor') diff --git a/src/test/java/com/amazon/carbonado/cursor/TestCursors.java b/src/test/java/com/amazon/carbonado/cursor/TestCursors.java index 7c4e3de..3b8b305 100644 --- a/src/test/java/com/amazon/carbonado/cursor/TestCursors.java +++ b/src/test/java/com/amazon/carbonado/cursor/TestCursors.java @@ -28,6 +28,8 @@ import junit.framework.TestSuite; import com.amazon.carbonado.Cursor; import com.amazon.carbonado.FetchException; +import com.amazon.carbonado.FetchInterruptedException; +import com.amazon.carbonado.Query; import com.amazon.carbonado.stored.Dummy; import com.amazon.carbonado.stored.StorableTestMinimal; @@ -446,6 +448,23 @@ public class TestCursors extends TestCase { compareElements(diff, 1, 1, 1, 1, 2, 3, 3, 3, 3, 4, 4, 4); } + public void testFetchTimeout() throws Exception { + Infinite inf = new Infinite(); + long start = System.nanoTime(); + Cursor cursor = ControllerCursor.apply(inf, Query.Timeout.seconds(2)); + try { + while (cursor.hasNext()) { + cursor.next(); + } + fail(); + } catch (FetchInterruptedException e) { + long end = System.nanoTime(); + assertTrue(inf.mClosed); + double duration = (end - start) / 1000000000.0d; + assertTrue(1.5 <= duration && duration <= 2.5); + } + } + private Cursor createElements(int... ids) { Arrays.sort(ids); Element[] elements = new Element[ids.length]; @@ -511,4 +530,21 @@ public class TestCursors extends TestCase { return 0; } } + + private static class Infinite extends AbstractCursor { + private int mID; + boolean mClosed; + + public boolean hasNext() { + return true; + } + + public Element next() { + return new Element(++mID); + } + + public void close() { + mClosed = true; + } + } } diff --git a/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java b/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java index ea13665..4c27f1f 100644 --- a/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java +++ b/src/test/java/com/amazon/carbonado/cursor/TestMergeSortBuffer.java @@ -18,6 +18,8 @@ package com.amazon.carbonado.cursor; +import java.lang.reflect.UndeclaredThrowableException; + import java.util.Comparator; import java.util.NoSuchElementException; import java.util.Random; @@ -148,6 +150,15 @@ public class TestMergeSortBuffer extends TestCase { testBuffer(null, HUGE_BUFFER_SIZE); } + public void testHugeWithTimeout() throws Exception { + try { + testBuffer(null, HUGE_BUFFER_SIZE, Query.Timeout.millis(100)); + fail(); + } catch (UndeclaredThrowableException e) { + assertTrue(e.getCause() instanceof FetchInterruptedException); + } + } + public void testLobs() throws Exception { Comparator c = BeanComparator.forClass(StorableWithLobs.class) .orderBy("-id"); @@ -179,8 +190,18 @@ public class TestMergeSortBuffer extends TestCase { buffer.close(); } - private void testBuffer(Storage storage, int size) throws Exception { - SortBuffer buffer = new MergeSortBuffer(storage); + private void testBuffer(Storage storage, int size) + throws Exception + { + testBuffer(storage, size, null); + } + + private void testBuffer(Storage storage, int size, + Query.Controller controller) + throws Exception + { + SortBuffer buffer = + new MergeSortBuffer(storage, controller); buffer.prepare(mComparator); if (storage == null) { -- cgit v1.2.3