use of java.io.Closeable in project hazelcast by hazelcast.
the class EventServiceImpl method close.
@Override
public void close(EventRegistration eventRegistration) {
Registration registration = (Registration) eventRegistration;
Object listener = registration.getListener();
if (!(listener instanceof Closeable)) {
return;
}
try {
((Closeable) listener).close();
} catch (IOException e) {
EmptyStatement.ignore(e);
}
}
use of java.io.Closeable in project fresco by facebook.
the class StatefulProducerRunnableTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mException = new ConcurrentModificationException();
mSuccessMap = new HashMap<>();
mSuccessMap.put("state", "success");
mFailureMap = new HashMap<>();
mFailureMap.put("state", "failure");
mCancellationMap = new HashMap<>();
mCancellationMap.put("state", "cancelled");
mStatefulProducerRunnable = new StatefulProducerRunnable<Closeable>(mConsumer, mProducerListener, PRODUCER_NAME, REQUEST_ID) {
@Override
protected void disposeResult(Closeable result) {
try {
result.close();
} catch (IOException ioe) {
throw new RuntimeException("unexpected IOException", ioe);
}
}
@Override
protected Closeable getResult() throws Exception {
return mResultSupplier.get();
}
@Override
protected Map<String, String> getExtraMapOnCancellation() {
return mCancellationMap;
}
@Override
protected Map<String, String> getExtraMapOnFailure(Exception exception) {
return mFailureMap;
}
@Override
protected Map<String, String> getExtraMapOnSuccess(Closeable result) {
return mSuccessMap;
}
};
}
use of java.io.Closeable in project guava by google.
the class FinalizableReferenceQueueClassLoaderUnloadingTest method doTestUnloadableInStaticFieldIfClosed.
// If you have a FinalizableReferenceQueue that is a static field of one of the classes of your
// app (like the FrqUser class above), then the app's ClassLoader will never be gc'd. The reason
// is that we attempt to run a thread in a separate ClassLoader that will detect when the FRQ
// is no longer referenced, meaning that the app's ClassLoader has been gc'd, and when that
// happens. But the thread's supposedly separate ClassLoader actually has a reference to the app's
// ClasLoader via its AccessControlContext. It does not seem to be possible to make a
// URLClassLoader without capturing this reference, and it probably would not be desirable for
// security reasons anyway. Therefore, the FRQ.close() method provides a way to stop the thread
// explicitly. This test checks that calling that method does allow an app's ClassLoader to be
// gc'd even if there is a still a FinalizableReferenceQueue in a static field. (Setting the field
// to null would also work, but only if there are no references to the FRQ anywhere else.)
private WeakReference<ClassLoader> doTestUnloadableInStaticFieldIfClosed() throws Exception {
final URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
final URL[] urls = myLoader.getURLs();
URLClassLoader sepLoader = new URLClassLoader(urls, myLoader.getParent());
Class<?> frqC = FinalizableReferenceQueue.class;
Class<?> sepFrqC = sepLoader.loadClass(frqC.getName());
assertNotSame(frqC, sepFrqC);
Class<?> sepFrqSystemLoaderC = sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName());
Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled");
disabled.setAccessible(true);
disabled.set(null, true);
Class<?> frqUserC = FrqUser.class;
Class<?> sepFrqUserC = sepLoader.loadClass(frqUserC.getName());
assertNotSame(frqUserC, sepFrqUserC);
assertSame(sepLoader, sepFrqUserC.getClassLoader());
Callable<?> sepFrqUser = (Callable<?>) sepFrqUserC.newInstance();
WeakReference<?> finalizableWeakReference = (WeakReference<?>) sepFrqUser.call();
GcFinalization.awaitClear(finalizableWeakReference);
Field sepFrqUserFinalizedF = sepFrqUserC.getField("finalized");
Semaphore finalizeCount = (Semaphore) sepFrqUserFinalizedF.get(null);
boolean finalized = finalizeCount.tryAcquire(5, TimeUnit.SECONDS);
assertTrue(finalized);
Field sepFrqUserFrqF = sepFrqUserC.getField("frq");
Closeable frq = (Closeable) sepFrqUserFrqF.get(null);
frq.close();
return new WeakReference<ClassLoader>(sepLoader);
}
use of java.io.Closeable in project hazelcast by hazelcast.
the class IOUtilTest method testCloseResource.
@Test
public void testCloseResource() throws Exception {
Closeable closeable = mock(Closeable.class);
closeResource(closeable);
verify(closeable).close();
verifyNoMoreInteractions(closeable);
}
use of java.io.Closeable in project OpenAM by OpenRock.
the class SimpleTaskExecutorTest method testExecute.
@Test
public void testExecute() throws Exception {
// given
Task task = mock(Task.class);
Closeable connection = mock(Closeable.class);
when(connectionFactory.create()).thenReturn(connection);
when(connectionFactory.isValid(connection)).thenReturn(true);
executor.start();
// when
executor.execute(null, task);
// then
verify(task).execute(connection, adapter);
}
Aggregations