From 6d87b1e8efbf88dc8b7ec32c397d3c50fdfa5379 Mon Sep 17 00:00:00 2001 From: Jakub Skoczen Date: Mon, 31 Aug 2015 14:13:25 +0200 Subject: [PATCH] Handle errors and do not hard-code count --- src/main/java/org/yaz4j/AsyncConnection.java | 27 ++++++--- src/main/java/org/yaz4j/AsyncConnections.java | 3 + src/test/java/org/yaz4j/AsyncConnectionsTest.java | 67 ++++++++++++++++++++- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/yaz4j/AsyncConnection.java b/src/main/java/org/yaz4j/AsyncConnection.java index 107a00f..4ae2303 100644 --- a/src/main/java/org/yaz4j/AsyncConnection.java +++ b/src/main/java/org/yaz4j/AsyncConnection.java @@ -6,7 +6,6 @@ package org.yaz4j; import org.yaz4j.exception.ZoomException; -import org.yaz4j.jni.yaz4jlib; import static org.yaz4j.jni.yaz4jlib.*; /** @@ -16,6 +15,9 @@ import static org.yaz4j.jni.yaz4jlib.*; public class AsyncConnection extends Connection { private ResultSet lastResultSet; ErrorHandler eh; + //make sure error is only handled once + boolean errorHandled = false; + ErrorHandler reh; SearchHandler sh; RecordHandler rh; @@ -34,14 +36,12 @@ public class AsyncConnection extends Connection { public AsyncConnection(String host, int port) { super(host, port); ZOOM_connection_option_set(zoomConnection, "async", "1"); - //what about piggy back? - ZOOM_connection_option_set(zoomConnection, "count", "100"); - ZOOM_connection_option_set(zoomConnection, "step", "20"); closed = false; } @Override public ResultSet search(Query query) throws ZoomException { + errorHandled = false; lastResultSet = super.search(query); return null; } @@ -61,6 +61,11 @@ public class AsyncConnection extends Connection { return this; } + public AsyncConnection onRecordError(ErrorHandler reh) { + this.reh = reh; + return this; + } + //actuall handler, pkg-private void handleSearch() { @@ -70,10 +75,11 @@ public class AsyncConnection extends Connection { } void handleRecord() { + //TODO clone the record to detach it from the result set try { if (rh != null) rh.handle(lastResultSet.getRecord(lastResultSet.asyncRecordOffset)); } catch (ZoomException ex) { - if (eh != null) eh.handle(ex); + if (reh != null) reh.handle(ex); } finally { lastResultSet.asyncRecordOffset++; } @@ -81,9 +87,14 @@ public class AsyncConnection extends Connection { void handleError() { //handle error - ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); - if (err != null) { - if (eh != null) eh.handle(err); + if (!errorHandled) { + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) { + if (eh != null) { + eh.handle(err); + errorHandled = true; + } + } } } diff --git a/src/main/java/org/yaz4j/AsyncConnections.java b/src/main/java/org/yaz4j/AsyncConnections.java index a7bdd44..b331729 100644 --- a/src/main/java/org/yaz4j/AsyncConnections.java +++ b/src/main/java/org/yaz4j/AsyncConnections.java @@ -39,6 +39,9 @@ public class AsyncConnections { switch (last) { case ZOOM_EVENT_RECV_SEARCH: conn.handleSearch(); break; case ZOOM_EVENT_RECV_RECORD: conn.handleRecord(); break; + //TODO this will make handle error twice + case ZOOM_EVENT_END: conn.handleError(); break; + //TODO should we simply handle error for any event? } } } finally { diff --git a/src/test/java/org/yaz4j/AsyncConnectionsTest.java b/src/test/java/org/yaz4j/AsyncConnectionsTest.java index 4053e81..648da66 100644 --- a/src/test/java/org/yaz4j/AsyncConnectionsTest.java +++ b/src/test/java/org/yaz4j/AsyncConnectionsTest.java @@ -58,6 +58,68 @@ public class AsyncConnectionsTest { @After public void tearDown() { } + + /** + * Test async ZOOM operation. + */ + @Test + public void testBadDatabaseTarget() { + out.println("Trying bad async connection..."); + AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/doesnotexist", 0); + conn.option("count", "100"); + AsyncConnections conns = new AsyncConnections(); + conns.add(conn); + final Box hadError = new Box(false); + try { + conn.setSyntax("sutrs"); + conn.connect(); + conn.search(new PrefixQuery("@attr 1=4 utah")); + conn + .onError(new AsyncConnection.ErrorHandler() { + + public void handle(ZoomException e) { + out.println("There was an error "+e.getMessage()); + hadError.setItem(true); + } + }); + + } catch (ZoomException ex) { + fail(ex.getMessage()); + } + conns.start(); + assertTrue(hadError.item); + } + + /** + * Test async ZOOM operation. + */ + @Test + public void testBadTarget() { + out.println("Trying bad target async connection..."); + AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:70000/doesnotexist", 0); + conn.option("count", "100"); + AsyncConnections conns = new AsyncConnections(); + conns.add(conn); + final Box hadError = new Box(false); + try { + conn.setSyntax("sutrs"); + conn.connect(); + conn.search(new PrefixQuery("@attr 1=4 utah")); + conn + .onError(new AsyncConnection.ErrorHandler() { + + public void handle(ZoomException e) { + out.println("There was an error "+e.getMessage()); + hadError.setItem(true); + } + }); + + } catch (ZoomException ex) { + fail(ex.getMessage()); + } + conns.start(); + assertTrue(hadError.item); + } /** * Test async ZOOM operation. @@ -66,6 +128,7 @@ public class AsyncConnectionsTest { public void testSingleTarget() { out.println("Trying async connection..."); AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0); + conn.option("count", "100"); AsyncConnections conns = new AsyncConnections(); conns.add(conn); int expectedHitCount = 9; @@ -107,8 +170,10 @@ public class AsyncConnectionsTest { out.println("Trying async with multile connections..."); AsyncConnections conns = new AsyncConnections(); AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0); + conn.option("count", "100"); conns.add(conn); AsyncConnection conn2 = new AsyncConnection("z3950.indexdata.dk:210/marc", 0); + conn2.option("count", "100"); conns.add(conn2); int expectedHitCount = 19; //for both final Box actualHitCount = new Box(0L); @@ -146,7 +211,7 @@ public class AsyncConnectionsTest { out.println("Received a record of type "+r.getSyntax()); actualRecordCounter.setItem(actualRecordCounter.getItem()+1); } - }) + }) .onError(new AsyncConnection.ErrorHandler() { public void handle(ZoomException e) { -- 1.7.10.4