Skip to content

Commit

Permalink
Attempt to start a stopped server will fail with IllegalStateException (
Browse files Browse the repository at this point in the history
#2439)

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer committed Oct 14, 2020
1 parent 52734e1 commit f198919
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public MessageBodyWriterContext writerContext() {

@Override
public synchronized Single<WebServer> start() {
if (shutdownThreadGroupsInitiated.get() || (startFuture.isDone() && shutdownFuture.isDone())) {
// if we are shutting down, or shutdown - restart is not an option
throw new IllegalStateException("WebServer cannot be restarted once it has been shutdown, or it failed to start.");
}

if (!started) {

channelsUpFuture.thenAccept(this::started)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public interface WebServer {

/**
* Starts the server. Has no effect if server is running.
* The start will fail on a server that is shut down, or that failed to start.
* In such cases, create a new instance of Web Server.
*
* @return a single to react on startup process
*/
Expand Down
57 changes: 57 additions & 0 deletions webserver/webserver/src/test/java/io/helidon/webserver/Gh377.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.webserver;

import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests correct behavior when webserver is shutdown and an attempt is made to start it again.
* Github issue #377.
*/
class Gh377 {
@Test
void testRestart() {
WebServer webServer = WebServer.builder()
.routing(Routing.builder()
.get("/", (req, res) -> res.send("Hello World"))
.build())
.build()
.start()
.await(10, TimeUnit.SECONDS);

assertThat(webServer.port(), greaterThan(0));

// shutdown
webServer.shutdown()
.await(10, TimeUnit.SECONDS);

assertThat(webServer.port(), is(-1));

// attempt to start again
assertThrows(IllegalStateException.class, () -> webServer.start()
.await(10, TimeUnit.SECONDS));

assertThat(webServer.port(), is(-1));
}
}

0 comments on commit f198919

Please sign in to comment.