-
Notifications
You must be signed in to change notification settings - Fork 6
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
Added test for multiple connections #64
base: main
Are you sure you want to change the base?
Changes from 1 commit
4f4baf8
b165fcb
95f5447
87fc7e8
58a6993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,5 +53,4 @@ public void removeDisconnectedClients() { | |
public List<Socket> getClients() { | ||
return clients; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -26,4 +26,12 @@ public void start() throws IOException { | |||||
e.printStackTrace(); | ||||||
} | ||||||
} | ||||||
public void stop(){ | ||||||
try { | ||||||
serverSocket.close(); | ||||||
System.out.println("Server stopped"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
} catch (IOException e) { | ||||||
e.printStackTrace(); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.fungover.thunder; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class ServerTest { | ||
private static Server server; | ||
@BeforeAll | ||
public static void setUp() { | ||
try { | ||
server = new Server(); | ||
ExecutorService executorService = Executors.newFixedThreadPool(5); | ||
executorService.submit(() -> { | ||
try { | ||
server.start(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
Thread.sleep(1000); | ||
} catch (IOException | InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
@AfterAll | ||
public static void tearDown() { | ||
if (server != null) { | ||
server.stop(); | ||
} | ||
} | ||
@Test | ||
public void testMultipleConnections() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
int numClients = 5; | ||
ExecutorService executorService = Executors.newFixedThreadPool(numClients); | ||
|
||
for (int i = 0; i < numClients; i++) { | ||
final int clientId = i; | ||
executorService.submit(() -> { | ||
try { | ||
Client client = new Client("Client" + clientId); | ||
client.connectToServer("localhost", 1883); | ||
assertTrue(client.isConnected()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
executorService.shutdown(); | ||
while (!executorService.isTerminated()) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
connectToServer
method doesn't actually connect to the server. It just sets theconnected
field totrue
. This could lead to false positives in tests, as the client might not be able to connect to the server in a real-world scenario.