mirror of https://github.com/apache/jclouds.git
JCLOUDS-127: Added a method to check if the context is open
- Added isOpen in Context interface - Refactoring Closer class: - method close can only call once - method close is threadsafe - added method getState
This commit is contained in:
parent
bcf8e3fe50
commit
ff3405c967
|
@ -67,4 +67,8 @@ public interface Context extends Location, Closeable {
|
||||||
@Override
|
@Override
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if context open
|
||||||
|
*/
|
||||||
|
boolean isOpen();
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ import static com.google.common.base.Objects.toStringHelper;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static com.google.common.io.Closeables.closeQuietly;
|
import static com.google.common.io.Closeables.closeQuietly;
|
||||||
|
|
||||||
|
import static org.jclouds.lifecycle.Closer.State.AVAILABLE;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -72,6 +74,11 @@ public class ContextImpl implements Context {
|
||||||
closeQuietly(closer);
|
closeQuietly(closer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isOpen() {
|
||||||
|
return closer.getState() == AVAILABLE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -20,11 +20,14 @@ import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
import static org.jclouds.lifecycle.Closer.State.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This will close objects in the reverse order that they were added.
|
* This will close objects in the reverse order that they were added.
|
||||||
*
|
*
|
||||||
|
@ -35,14 +38,33 @@ public class Closer implements Closeable {
|
||||||
// guice is single threaded. no need to lock this
|
// guice is single threaded. no need to lock this
|
||||||
List<Closeable> methodsToClose = Lists.<Closeable> newArrayList();
|
List<Closeable> methodsToClose = Lists.<Closeable> newArrayList();
|
||||||
|
|
||||||
|
public enum State {
|
||||||
|
AVAILABLE,
|
||||||
|
PROCESSING,
|
||||||
|
DONE
|
||||||
|
}
|
||||||
|
|
||||||
|
private final AtomicReference<State> state;
|
||||||
|
|
||||||
|
public Closer() {
|
||||||
|
this.state = new AtomicReference<State>(AVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
public void addToClose(Closeable toClose) {
|
public void addToClose(Closeable toClose) {
|
||||||
methodsToClose.add(toClose);
|
methodsToClose.add(toClose);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
if (state.compareAndSet(AVAILABLE, PROCESSING)) {
|
||||||
Collections.reverse(methodsToClose);
|
Collections.reverse(methodsToClose);
|
||||||
for (Closeable toClose : methodsToClose) {
|
for (Closeable toClose : methodsToClose) {
|
||||||
toClose.close();
|
toClose.close();
|
||||||
}
|
}
|
||||||
|
state.set(DONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getState() {
|
||||||
|
return state.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,13 @@ import static com.google.inject.name.Names.named;
|
||||||
import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
|
import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
|
||||||
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
|
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
import org.jclouds.concurrent.config.ExecutorServiceModule;
|
import org.jclouds.concurrent.config.ExecutorServiceModule;
|
||||||
import org.jclouds.lifecycle.Closer;
|
import org.jclouds.lifecycle.Closer;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
@ -66,6 +69,7 @@ public class LifeCycleModuleTest {
|
||||||
void testBindsCloser() {
|
void testBindsCloser() {
|
||||||
Injector i = createInjector();
|
Injector i = createInjector();
|
||||||
assert i.getInstance(Closer.class) != null;
|
assert i.getInstance(Closer.class) != null;
|
||||||
|
assert i.getInstance(Closer.class).getState() == Closer.State.AVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -75,8 +79,10 @@ public class LifeCycleModuleTest {
|
||||||
named(PROPERTY_USER_THREADS)));
|
named(PROPERTY_USER_THREADS)));
|
||||||
assert !executor.isShutdown();
|
assert !executor.isShutdown();
|
||||||
Closer closer = i.getInstance(Closer.class);
|
Closer closer = i.getInstance(Closer.class);
|
||||||
|
assert closer.getState() == Closer.State.AVAILABLE;
|
||||||
closer.close();
|
closer.close();
|
||||||
assert executor.isShutdown();
|
assert executor.isShutdown();
|
||||||
|
assert closer.getState() == Closer.State.DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -89,9 +95,11 @@ public class LifeCycleModuleTest {
|
||||||
named(PROPERTY_IO_WORKER_THREADS)));
|
named(PROPERTY_IO_WORKER_THREADS)));
|
||||||
assert !ioExecutor.isShutdown();
|
assert !ioExecutor.isShutdown();
|
||||||
Closer closer = i.getInstance(Closer.class);
|
Closer closer = i.getInstance(Closer.class);
|
||||||
|
assert closer.getState() == Closer.State.AVAILABLE;
|
||||||
closer.close();
|
closer.close();
|
||||||
assert userExecutor.isShutdown();
|
assert userExecutor.isShutdown();
|
||||||
assert ioExecutor.isShutdown();
|
assert ioExecutor.isShutdown();
|
||||||
|
assert closer.getState() == Closer.State.DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class PostConstructable {
|
static class PostConstructable {
|
||||||
|
@ -112,7 +120,90 @@ public class LifeCycleModuleTest {
|
||||||
});
|
});
|
||||||
PostConstructable postConstructable = i.getInstance(PostConstructable.class);
|
PostConstructable postConstructable = i.getInstance(PostConstructable.class);
|
||||||
assert postConstructable.isStarted;
|
assert postConstructable.isStarted;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCloserClosingState() throws InterruptedException {
|
||||||
|
Injector i = createInjector();
|
||||||
|
final Closer closer = i.getInstance(Closer.class);
|
||||||
|
|
||||||
|
final CountDownLatch closeDone = new CountDownLatch(1);
|
||||||
|
final CountDownLatch closeStart = new CountDownLatch(1);
|
||||||
|
|
||||||
|
closer.addToClose(new Closeable() {
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
try {
|
||||||
|
closeStart.countDown();
|
||||||
|
assert closer.getState() == Closer.State.PROCESSING;
|
||||||
|
closeDone.await();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
assert false : e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread thread = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
closer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false : e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
closeStart.await();
|
||||||
|
|
||||||
|
assert closer.getState() == Closer.State.PROCESSING;
|
||||||
|
|
||||||
|
closeDone.countDown();
|
||||||
|
|
||||||
|
thread.join();
|
||||||
|
|
||||||
|
assert closer.getState() == Closer.State.DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCloserCallOneClose() throws IOException, InterruptedException {
|
||||||
|
Injector i = createInjector();
|
||||||
|
final Closer closer = i.getInstance(Closer.class);
|
||||||
|
|
||||||
|
Closeable closeable = createStrictMock(Closeable.class);
|
||||||
|
|
||||||
|
closeable.close();
|
||||||
|
|
||||||
|
expectLastCall();
|
||||||
|
|
||||||
|
replay(closeable);
|
||||||
|
|
||||||
|
closer.addToClose(closeable);
|
||||||
|
|
||||||
|
Runnable closeContext = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
closer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
assert false : e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Thread thread1 = new Thread(closeContext);
|
||||||
|
Thread thread2 = new Thread(closeContext);
|
||||||
|
|
||||||
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
|
||||||
|
thread1.join();
|
||||||
|
thread2.join();
|
||||||
|
|
||||||
|
verify(closeable);
|
||||||
|
|
||||||
|
assert closer.getState() == Closer.State.DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue