Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void notifyWhenStateChanged(Runnable callback, Executor executor, ConnectivitySt
*/
void gotoState(@Nonnull ConnectivityState newState) {
checkNotNull(newState, "newState");
checkState(state != null, "ConnectivityStateManager is already disabled");
checkState(!isDisabled(), "ConnectivityStateManager is already disabled");
gotoNullableState(newState);
}

Expand Down Expand Up @@ -102,6 +102,13 @@ void disable() {
gotoNullableState(null);
}

/**
* This method is threadsafe.
*/
boolean isDisabled() {
return state == null;
}

private static final class Listener {
final Runnable callback;
final Executor executor;
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/io/grpc/internal/ManagedChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ public ManagedChannelImpl shutdown() {
channelExecutor.executeLater(new Runnable() {
@Override
public void run() {
channelStateManager.gotoState(SHUTDOWN);
if (!channelStateManager.isDisabled()) {
channelStateManager.gotoState(SHUTDOWN);
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
package io.grpc.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.ConnectivityState;
import java.util.LinkedList;
import java.util.concurrent.Executor;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -31,6 +34,9 @@
*/
@RunWith(JUnit4.class)
public class ConnectivityStateManagerTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();

private final FakeClock executor = new FakeClock();
private final ConnectivityStateManager state = new ConnectivityStateManager();
private final LinkedList<ConnectivityState> sink = new LinkedList<ConnectivityState>();
Expand Down Expand Up @@ -223,4 +229,43 @@ public void registerCallbackFromCallbackDirectExecutor() {
assertEquals(1, sink.size());
assertEquals(ConnectivityState.READY, sink.poll());
}

@Test
public void disable() {
state.disable();
assertTrue(state.isDisabled());

thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Channel state API is not implemented");
state.getState();
}

@Test
public void disableThenDisable() {
state.disable();
state.disable();
assertTrue(state.isDisabled());

thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("Channel state API is not implemented");
state.getState();
}

@Test
public void disableThenGotoReady() {
state.disable();

thrown.expect(IllegalStateException.class);
thrown.expectMessage("ConnectivityStateManager is already disabled");
state.gotoState(ConnectivityState.READY);
}

@Test
public void shutdownThenReady() {
state.gotoState(ConnectivityState.SHUTDOWN);
assertEquals(ConnectivityState.SHUTDOWN, state.getState());

state.gotoState(ConnectivityState.READY);
assertEquals(ConnectivityState.SHUTDOWN, state.getState());
}
}