Fix shutting down a never-started ServerImpl

This commit is contained in:
Eric Anderson 2015-08-03 19:11:47 -07:00
parent 7d1e65c111
commit e45c0c53d0
2 changed files with 23 additions and 1 deletions

View File

@ -132,13 +132,21 @@ public final class ServerImpl extends Server {
* Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected. * Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.
*/ */
public ServerImpl shutdown() { public ServerImpl shutdown() {
boolean shutdownTransportServer;
synchronized (lock) { synchronized (lock) {
if (shutdown) { if (shutdown) {
return this; return this;
} }
shutdown = true; shutdown = true;
shutdownTransportServer = started;
if (!shutdownTransportServer) {
transportServerTerminated = true;
checkForTermination();
} }
}
if (shutdownTransportServer) {
transportServer.shutdown(); transportServer.shutdown();
}
SharedResourceHolder.release(TIMER_SERVICE, timeoutService); SharedResourceHolder.release(TIMER_SERVICE, timeoutService);
return this; return this;
} }

View File

@ -115,6 +115,20 @@ public class ServerImplTest {
assertTrue(server.isTerminated()); assertTrue(server.isTerminated());
} }
@Test
public void stopImmediate() {
transportServer = new SimpleServer() {
@Override
public void shutdown() {
throw new AssertionError("Should not be called, because wasn't started");
}
};
ServerImpl server = new ServerImpl(executor, registry, transportServer);
server.shutdown();
assertTrue(server.isShutdown());
assertTrue(server.isTerminated());
}
@Test @Test
public void startStopImmediateWithChildTransport() throws IOException { public void startStopImmediateWithChildTransport() throws IOException {
ServerImpl server = new ServerImpl(executor, registry, transportServer); ServerImpl server = new ServerImpl(executor, registry, transportServer);