public void run() { try { out = new PrintWriter(this.socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); while (true) { synchronized (this) { out.println(HEARTBEAT); } reallySleep(NORMAL_HEARTBEAT_INTERVAL); } } catch (SocketException e) { log("Socket Exception: client may have already shutdown."); log(e.getClass() + ": " + Arrays.asList(e.getStackTrace())); } catch (Exception e) { log("Heartbeat thread for child process (port " + port + ") got exception"); log(e.getClass() + ": " + Arrays.asList(e.getStackTrace())); } finally { log("Heartbeat thread for child process (port " + port + ") terminating."); try { socket.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
@Test public void testConnectThrowsSocketException() throws Throwable { LogChannel logChannel = new LogChannel(new ISO87APackagerBBitmap()); Socket socket = new Socket(); socket.close(); try { logChannel.connect(socket); fail("Expected SocketException to be thrown"); } catch (SocketException ex) { assertEquals("ex.getClass()", SocketException.class, ex.getClass()); assertEquals( "logChannel.getOriginalRealm()", "org.jpos.iso.channel.LogChannel", logChannel.getOriginalRealm()); assertEquals("logChannel.getCounters().length", 3, logChannel.getCounters().length); assertNull("logChannel.getRealm()", logChannel.getRealm()); assertSame("logChannel.getSocket()", socket, logChannel.getSocket()); assertFalse("logChannel.isConnected()", logChannel.isConnected()); assertNull("logChannel.getLogger()", logChannel.getLogger()); assertNull("logChannel.reader", logChannel.reader); assertNull("socket.getChannel()", socket.getChannel()); } }