diff --git a/pom.xml b/pom.xml index f8d6abf..d2d74c1 100644 --- a/pom.xml +++ b/pom.xml @@ -104,7 +104,7 @@ true - false + true false diff --git a/src/main/java/org/wildfly/discovery/Discovery.java b/src/main/java/org/wildfly/discovery/Discovery.java index a6e4898..07e9f04 100644 --- a/src/main/java/org/wildfly/discovery/Discovery.java +++ b/src/main/java/org/wildfly/discovery/Discovery.java @@ -225,6 +225,15 @@ static final class BlockingQueueServicesQueue implements ServicesQueue { this(queue, problems, request, Long.MAX_VALUE, TimeUnit.DAYS); } + /** + * Create a BlockingQueueServicesQueue which makes use of a default timeout value for all calls to takeService() + * + * @param queue the underlying BlockingQueue used to store ServiceURLs + * @param problems the underlying List used to store Throwables encountered by the DiscoveryProvider while generating ServiceURLs + * @param request a DiscoveryRequest instance used for cancellation of the Discovery call + * @param time a default timeout value for calls to takeService() + * @param timeUnit the timout TimeUnit + */ BlockingQueueServicesQueue(final LinkedBlockingQueue queue, final CopyOnWriteArrayList problems, final DiscoveryRequest request, final long time, final TimeUnit timeUnit) { this.queue = queue; this.problems = problems; @@ -275,13 +284,33 @@ public ServiceURL pollService() { } } + /** + * Take a ServiceURL from the queue, using the timeout value specified in the constructor. + * + * @return the next ServiceURL in the queue (or null if the call times out) + * @throws InterruptedException + */ public ServiceURL takeService() throws InterruptedException { - await(); + // timeout <= 0 interpreted as block indefinitely + if (this.timeout <= 0) { + await(Long.MAX_VALUE, this.timeUnit); + } else { + await(this.timeout, this.timeUnit); + } return pollService(); } + /** + * Take a ServiceURL from the queue, using the timeout value specified in te method call. + * + * @param timeout + * @param timeUnit + * @return the next ServiceURL in the queue (or null if the call times out) + * @throws InterruptedException + */ @Override public ServiceURL takeService(long timeout, TimeUnit timeUnit) throws InterruptedException { + // timeout <= interpreted as block indefinitely if (timeout <= 0) timeout = Long.MAX_VALUE; await(timeout, timeUnit); return pollService(); @@ -291,6 +320,9 @@ public boolean isFinished() { return next == null && done; } + /** + * Close the queue, cancelling the ongoing request if required. + */ public void close() { if (! isFinished()) { request.cancel(); diff --git a/src/test/java/org/wildfly/discovery/FilterSpecTestCase.java b/src/test/java/org/wildfly/discovery/FilterSpecTestCase.java index 492fb43..0fba1ad 100644 --- a/src/test/java/org/wildfly/discovery/FilterSpecTestCase.java +++ b/src/test/java/org/wildfly/discovery/FilterSpecTestCase.java @@ -2,6 +2,7 @@ import static org.junit.Assert.*; +import org.jboss.logging.Logger; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -25,6 +26,8 @@ */ public final class FilterSpecTestCase { + private static final Logger logger = Logger.getLogger(FilterSpecTestCase.class); + private static DiscoveryProvider provider = null; private static Discovery discovery = null; @@ -153,11 +156,11 @@ public void testDiscoverySingleAttribute() { List results = new ArrayList(); // call discovery for single attribute - System.out.println("Calling discover for filterspec " + cluster); + logger.info("Calling discover for filterspec " + cluster); try (final ServicesQueue servicesQueue = discover(cluster)) { ServiceURL serviceURL = servicesQueue.takeService(); do { - System.out.println("ServiceURL found = " + serviceURL); + logger.info("ServiceURL found = " + serviceURL); results.add(serviceURL); serviceURL = servicesQueue.takeService(); @@ -182,11 +185,11 @@ public void testDiscoveryMultipleAttributes() { List results = new ArrayList(); // call discovery for single attribute - System.out.println("Calling discover for filterspec " + all); + logger.info("Calling discover for filterspec " + all); try (final ServicesQueue servicesQueue = discover(all)) { ServiceURL serviceURL = servicesQueue.takeService(); do { - System.out.println("ServiceURL found = " + serviceURL); + logger.info("ServiceURL found = " + serviceURL); results.add(serviceURL); serviceURL = servicesQueue.takeService();