Skip to content

Commit cb77340

Browse files
committed
Update Server so it no longer uses futures as this was actually blocking thread execution
1 parent 62a4524 commit cb77340

File tree

2 files changed

+2
-17
lines changed

2 files changed

+2
-17
lines changed

src/main/java/httpserver/Server.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.io.IOException;
44
import java.net.ServerSocket;
55
import java.net.Socket;
6-
import java.util.concurrent.ExecutionException;
76
import java.util.concurrent.ExecutorService;
8-
import java.util.concurrent.Future;
97

108
public class Server {
119
private final ServerSocket serverSocket;
@@ -20,10 +18,8 @@ public void acceptConnection(ExecutorService executorService, SocketHandlerFacto
2018
try {
2119
Socket clientSocket = serverSocket.accept();
2220
SocketHandler socketHandler = socketHandlerFactory.newSocketHandler(appConfig, clientSocket);
23-
Future<?> future = executorService.submit(socketHandler);
24-
future.get();
25-
clientSocket.close();
26-
} catch (IOException | InterruptedException | ExecutionException e) {
21+
executorService.submit(socketHandler);
22+
} catch (IOException e) {
2723
e.printStackTrace();
2824
}
2925
}

src/test/java/httpserver/ServerTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.net.ServerSocket;
77
import java.net.Socket;
88
import java.util.concurrent.ExecutorService;
9-
import java.util.concurrent.Future;
109

1110
import static org.mockito.Mockito.*;
1211

@@ -19,7 +18,6 @@ public class ServerTest {
1918
private Server server;
2019
private final ExecutorService executorServiceMock;
2120
private final SocketHandler socketHandlerMock;
22-
private final Future futureMock;
2321

2422
public ServerTest() throws IOException {
2523
socket = new Socket();
@@ -34,8 +32,6 @@ public ServerTest() throws IOException {
3432
when(socketHandlerFactoryMock.newSocketHandler(any(), any())).thenReturn(socketHandlerMock);
3533

3634
executorServiceMock = mock(ExecutorService.class);
37-
futureMock = mock(Future.class);
38-
when(executorServiceMock.submit(socketHandlerMock)).thenReturn(futureMock);
3935
}
4036

4137
@Test
@@ -58,11 +54,4 @@ public void callsExecutorSubmitWithSocketHandler() throws Exception {
5854

5955
verify(executorServiceMock).submit(socketHandlerMock);
6056
}
61-
62-
@Test
63-
public void callsGetOnFuture() throws Exception {
64-
server.acceptConnection(executorServiceMock, socketHandlerFactoryMock);
65-
66-
verify(futureMock).get();
67-
}
6857
}

0 commit comments

Comments
 (0)