| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* Copyright 2014 Red Hat, Inc., and individual contributors |
| 4 |
* as indicated by the @author tags. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.jboss.modules.ref.util; |
| 20 |
|
| 21 |
import java.util.concurrent.CountDownLatch; |
| 22 |
import java.util.concurrent.ExecutionException; |
| 23 |
import java.util.concurrent.Future; |
| 24 |
import java.util.concurrent.TimeUnit; |
| 25 |
import java.util.concurrent.TimeoutException; |
| 26 |
|
| 27 |
import org.jboss.modules.ref.Reaper; |
| 28 |
import org.jboss.modules.ref.Reference; |
| 29 |
|
| 30 |
/** |
| 31 |
* Reaper used by tests. |
| 32 |
* |
| 33 |
* @author <a href="mailto:[email protected]">Flavia Rainone</a> |
| 34 |
* |
| 35 |
*/ |
| 36 |
public class TestReaper<T, A> implements Reaper<T, A>, Future<Reference<T, A>> { |
| 37 |
|
| 38 |
private Reference<T, A> reaped; |
| 39 |
private final CountDownLatch countDownLatch = new CountDownLatch(1); |
| 40 |
|
| 41 |
@Override |
| 42 |
public void reap(Reference<T, A> reference) { |
| 43 |
reaped = reference; |
| 44 |
countDownLatch.countDown(); |
| 45 |
} |
| 46 |
|
| 47 |
public Reference<T, A> getReapedReference() throws InterruptedException, ExecutionException { |
| 48 |
return get(); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean cancel(boolean mayInterruptIfRunning) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public boolean isCancelled() { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public boolean isDone() { |
| 63 |
return reaped != null; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public Reference<T, A> get() throws InterruptedException, ExecutionException { |
| 68 |
try { |
| 69 |
return get(30L, TimeUnit.SECONDS); |
| 70 |
} catch (TimeoutException e) { |
| 71 |
throw new RuntimeException("Could not get reaped in 30 second timeout."); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public Reference<T, A> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, |
| 77 |
TimeoutException { |
| 78 |
countDownLatch.await(timeout, unit); |
| 79 |
return reaped; |
| 80 |
} |
| 81 |
} |