Skip to content

Commit

Permalink
Fixed multiple connection results
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Nov 29, 2023
1 parent da464f0 commit d7a13fa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/main/java/com/github/kerbity/kerb/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ protected void send(@NotNull String data) {
if (this.socket.isClosed()) return;

this.printWriter.println(data);
if (this.getDebugMode()) this.logger.log("&7[DEBUG] Send {data: \"" + data + "\"}");
if (this.getDebugMode()) this.logger
.createExtension("[" + this.socket.getLocalPort() + "] ")
.log("&7[DEBUG] Send {data: \"" + data + "\"}");
}

/**
Expand All @@ -117,7 +119,9 @@ protected void send(byte[] byteArray) {
builder.append(item).append(",");
}
this.send(builder.toString());
if (this.getDebugMode()) this.logger.log("&7[DEBUG] Send {data: \"" + builder + "\"}");
if (this.getDebugMode()) this.logger
.createExtension("[" + this.socket.getLocalPort() + "] ")
.log("&7[DEBUG] Send {data: \"" + builder + "\"}");
}

/**
Expand All @@ -132,8 +136,13 @@ protected String read() throws IOException {
if (socket == null) return null;
if (socket.isClosed()) return null;

this.logger.createExtension("[" + this.socket.getLocalPort() + "] ")
.log("&7[DEBUG] Read wait");

String data = this.bufferedReader.readLine();
if (this.getDebugMode()) this.logger.log("&7[DEBUG] Read {data: \"" + data + "\"}");
if (this.getDebugMode()) this.logger
.createExtension("[" + this.socket.getLocalPort() + "] ")
.log("&7[DEBUG] Read {data: \"" + data + "\"}");
return data;
}

Expand All @@ -151,7 +160,9 @@ protected byte[] readBytes() throws IOException {
index++;
}

if (this.getDebugMode()) this.logger.log("&7[DEBUG] Read {data: \"" + byteList + "\"}");
if (this.getDebugMode()) this.logger
.createExtension("[" + this.socket.getLocalPort() + "] ")
.log("&7[DEBUG] Read {data: \"" + byteList + "\"}");
return byteList;
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/github/kerbity/kerb/client/KerbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,10 @@ private void startLoop() {
return;
}

Packet packet = Packet.getPacket(data);
this.packetManager.interpret(packet);
new Thread(() -> {
Packet packet = Packet.getPacket(data);
this.packetManager.interpret(packet);
}).start();

} catch (Exception exception) {
throw new RuntimeException(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public void interpret(@NotNull Packet packet) {
return;
}

// Set the instance.

// Loop though low-priority events.
for (EventListener<?> listener : this.client.getEventListeners(Priority.LOW)) {
if (listener.isNotCastable(event)) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ public int getSize() {
public @NotNull CompletableResultSet<T> addAmbiguosResult(@Nullable Object result) {
try {
this.addResult((T) result);
} catch (Exception ignored) {
} catch (Exception exception) {
exception.printStackTrace();
}
return this;
}
Expand Down
35 changes: 34 additions & 1 deletion src/test/java/com/github/kerbity/kerb/tests/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import java.util.List;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ConnectionTest {

Expand All @@ -50,7 +52,7 @@ public void testValidation() {
}

@Test
@Order(0)
@Order(1)
public void testPingEvent() {
Server server = ServerCreator.createAndStart().waitForStartup();

Expand All @@ -72,4 +74,35 @@ public void testPingEvent() {
.expect(resultSet.waitForFirstNonNullAssumption().wasReceived())
.expect("Test", resultSet.waitForFirstNonNullAssumption().getSource().getName());
}

@Test
@Order(2)
public void testPingEventMultiple() {
Server server = ServerCreator.createAndStart().waitForStartup();
server.setDebugMode(true);
KerbClient client1 = ClientCreator.create(server.getPort(), server.getAddress());
client1.connect();
KerbClient client2 = ClientCreator.create(server.getPort(), server.getAddress());
client2.connect();

client1.registerListener(Priority.HIGH, (EventListener<PingEvent>) event -> {
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
throw new RuntimeException(exception);
}
event.setWasReceived(true);
return event;
});

client2.registerListener(Priority.HIGH, (EventListener<PingEvent>) event -> {
event.setWasReceived(true);
return null;
});

CompletableResultSet<PingEvent> resultSet = client1.callEvent(new PingEvent("Test"));
List<PingEvent> results = resultSet.waitForFinalResult();

new ResultChecker().expect(results.size() == 1);
}
}

0 comments on commit d7a13fa

Please sign in to comment.