Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that server is started in Setup and allow timeout to be config… #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions FitNesseRoot/HttpTestSuite/SuiteSetUp/content.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
|set port |5000 |
|set directory |${PUBLIC_DIR} |
|start server |
|ensure |server is started within |PT10S|
30 changes: 28 additions & 2 deletions src/main/java/Server.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.time.Duration;
import java.time.LocalTime;

public class Server {
private String startCommand;
private String directory;
Expand All @@ -19,10 +26,29 @@ public void setPort(String port) {
public void startServer() throws Exception {
String command = startCommand + " -p " + port + " -d " + directory;
process = Runtime.getRuntime().exec(command);
Thread.sleep(2000);
}

public void stopServer() throws Exception {
public boolean serverIsStartedWithin(String timeout) {
LocalTime abortTime = LocalTime.now().plus(Duration.parse(timeout));
while (LocalTime.now().isBefore(abortTime)) {
if (isServerListeningOn(Integer.parseInt(this.port)))
return true;
}
return false;
}

private boolean isServerListeningOn(int port) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress("localhost", port), 1000);
return true;
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
return false;
}
}

public void stopServer() {
process.destroy();
}
}