Fix disconnect notifiers
Build and Test j3270 / Build JAR & Run Tests (Java 11) (push) Successful in 53s
Build and Test j3270 / Build JAR & Run Tests (Java 21) (push) Successful in 1m30s
Build and Test j3270 / Build JAR & Run Tests (Java 17) (push) Successful in 1m37s

This commit is contained in:
2026-09-28 13:01:03 +00:00
parent daac11472d
commit fac6ac8bfb
5 changed files with 70 additions and 3 deletions
@@ -385,6 +385,9 @@ public class Telnet3270Client {
* Check if connected (any state past TCP pending).
*/
public boolean isConnected() {
if (fsm != null && fsm.getConnectionState() == ConnectionState.NOT_CONNECTED) {
return false;
}
return connection != null && connection.isConnected();
}
@@ -599,7 +599,7 @@ public class TelnetConnection {
* Check if the socket is connected.
*/
public boolean isConnected() {
return socket != null && socket.isConnected() && !socket.isClosed();
return running && socket != null && socket.isConnected() && !socket.isClosed();
}
/**
@@ -654,7 +654,13 @@ public class TelnetConnection {
fsm.onError("Network loop error: " + t.getMessage());
}
} finally {
running = false;
stopKeepAlive();
try {
if (socket != null && !socket.isClosed()) {
socket.close();
}
} catch (IOException ignored) {}
}
}
@@ -335,4 +335,24 @@ public class Mock3270ServerIntegrationTest {
}
assertTrue(receivedWontOrDontTn3270e, "Server should have received Telnet de-negotiation for TN3270E");
}
@Test
public void testServerSideDisconnectTransitionsClientToNotConnectedAndClosesSocket() throws Exception {
ConnectionConfig config = new ConnectionConfig("127.0.0.1", server.getPort(), TerminalModel.IBM_3279_2);
client = new Telnet3270Client(config);
assertTrue(client.connect(8000));
assertTrue(server.waitForHandshake(5, TimeUnit.SECONDS));
waitForState(client, ConnectionState.CONNECTED_TN3270E, 5000);
assertTrue(client.isConnected());
// Server drops connection (host disconnect)
server.disconnectClient();
// Wait for client to detect disconnect
waitForState(client, ConnectionState.NOT_CONNECTED, 5000);
assertEquals(ConnectionState.NOT_CONNECTED, client.getConnectionState());
assertFalse(client.isConnected(), "Client must report isConnected() == false after host disconnect");
assertNotNull(client.getConnection());
assertFalse(client.getConnection().isConnected(), "TelnetConnection must report isConnected() == false after host disconnect");
}
}