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

Added test for multiple connections #64

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions src/main/java/org/fungover/thunder/Client.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.fungover.thunder;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -30,4 +34,12 @@ public void subscribeToTopic(String topic) {
public Set<String> getSubscribedTopics() {
return new HashSet<>(subscribedTopics);
}

public void connectToServer(String serverAddress, int serverPort) throws IOException {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connectToServer method creates a new Socket and streams but doesn't use them. This could lead to resource leaks and doesn't actually test the connection. The connection should be tested by sending a message to the server and receiving a response.

try (Socket socket = new Socket(serverAddress, serverPort);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {
this.connected = true;
}
}
}
1 change: 0 additions & 1 deletion src/main/java/org/fungover/thunder/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ public void removeDisconnectedClients() {
public List<Socket> getClients() {
return clients;
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/fungover/thunder/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Main {

private static final Logger logger = Logger.getLogger(Main.class.getName());
static final Logger logger = Logger.getLogger(Main.class.getName());

public static void main(String[] args) {
Server server;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/fungover/thunder/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;

import static org.fungover.thunder.Main.logger;


public class Server {
private final ServerSocket serverSocket;
Expand All @@ -26,4 +30,12 @@ public void start() throws IOException {
e.printStackTrace();
}
}
public void stop(){
try {
serverSocket.close();
System.out.println("Server stopped");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stop method catches an IOException and logs it, but it also prints a message to the console. This is redundant and can lead to confusion. It would be better to only log the exception and remove the console print statement.

Suggested change
System.out.println("Server stopped");
logger.log(Level.INFO, "Server stopped");

} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
6 changes: 6 additions & 0 deletions src/test/java/org/fungover/thunder/ClientHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import org.junit.jupiter.api.Test;

import java.io.*;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutorService;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
63 changes: 63 additions & 0 deletions src/test/java/org/fungover/thunder/ServerTest.java
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() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testMultipleConnections method doesn't actually test the server's ability to handle multiple connections. It just creates multiple clients and calls their connectToServer method, which doesn't actually connect to the server. This could lead to false positives in tests, as the server might not be able to handle multiple connections in a real-world scenario.

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();
}
}
}
}