Skip to content

Commit

Permalink
[WFDESC-37] Adjust takeService() method to use (default) timeout valu…
Browse files Browse the repository at this point in the history
…e specified in constructor.
  • Loading branch information
rachmatowicz committed Jan 4, 2021
1 parent 8e54204 commit a089969
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
</property>
</systemProperties>
<enableAssertions>true</enableAssertions>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/org/wildfly/discovery/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServiceURL> queue, final CopyOnWriteArrayList<Throwable> problems, final DiscoveryRequest request, final long time, final TimeUnit timeUnit) {
this.queue = queue;
this.problems = problems;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/org/wildfly/discovery/FilterSpecTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -153,11 +156,11 @@ public void testDiscoverySingleAttribute() {
List<ServiceURL> results = new ArrayList<ServiceURL>();

// 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();
Expand All @@ -182,11 +185,11 @@ public void testDiscoveryMultipleAttributes() {
List<ServiceURL> results = new ArrayList<ServiceURL>();

// 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();
Expand Down

0 comments on commit a089969

Please sign in to comment.