Skip to content
Snippets Groups Projects
Commit 07888aaf authored by Elrabu's avatar Elrabu
Browse files

eb093: update: fixed server.java, added Tests

parent 6800cfd7
No related branches found
No related tags found
5 merge requests!74V1,!73Initial commit,!71Merge DataBase into Development,!54Update: coreMaps.json (added new maps),!51Multiplayer
......@@ -46,16 +46,26 @@ public class Server {
public void startServer() throws IOException {
Server server = new Server();
log.info("server starting...");
server.start(ipaddress,4444);
server.start(4444);
}
public void start(String host_ip, int port) throws IOException {
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
log.info("server started!");
started = true;
while (true){
new ServerHandler(serverSocket.accept()).start();
}
log.info("server accepting Thread starting...");
Thread serverThread = new Thread(() -> {
while (true) {
try {
new ServerHandler(serverSocket.accept()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
serverThread.start();
log.info("requests accepting Thread started!");
}
private static class ServerHandler extends Thread {
......
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.ServerSocket;
import static org.junit.jupiter.api.Assertions.*;
class ServerTest {
private static String ipaddress = "localhost";
private ServerSocket serverSocket;
private int port = 4444;
private boolean started = false;
@Test
void startServer() throws IOException {
Server server = new Server();
server.start(port);
assertTrue(server.isStarted());
}
@Test
void stopServer() {
try {
//start Server somehow so that serverSocket is not null
serverSocket.close(); // Close the server socket to break out of accept() in the main thread
started = false;
assertFalse(started);
} catch (IOException e) {
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment