@Test(timeOut = 1000, expectedExceptions = RuntimeException.class) public void test_semaphore_is_released_in_case_of_RuntimeException_in_getAsyncRemote() throws Exception { when(session.getAsyncRemote()).thenThrow(new IllegalArgumentException("Invalid argument")).thenReturn(asyncRemoteEndpoint); webSocket.write("Hello1"); webSocket.write("Hello2"); verify(asyncRemoteEndpoint).sendText(eq("Hello2"), any(SendHandler.class)); }
@Override public WebSocket write(String s) throws IOException { if (!isOpen()) { throw new IOException("Socket closed {}"); } boolean acquired = false; try { acquired = semaphore.tryAcquire(writeTimeout, TimeUnit.MILLISECONDS); if (acquired) { session.getAsyncRemote().sendText(s, new WriteResult(resource(), s)); } else { throw new IOException("Socket closed"); } } catch (Throwable e) { if (IOException.class.isAssignableFrom(e.getClass())) { throw (IOException) e; } handleError(e, acquired); } return this; }
JSR356WebSocket extends WebSocket { @Override public WebSocket write(String s) throws IOException { if (!isOpen()) { throw new IOException("Socket closed {}"); } boolean acquired = false; try { acquired = semaphore.tryAcquire(writeTimeout, TimeUnit.MILLISECONDS); if (acquired) { session.getAsyncRemote().sendText(s, new WriteResult(resource(), s)); } else { throw new IOException("Socket closed"); } } catch (Throwable e) { if (IOException.class.isAssignableFrom(e.getClass())) { throw (IOException) e; } handleError(e, acquired); } return this; } }
JSR356WebSocket extends WebSocket { @Override public WebSocket write(String s) throws IOException { if (!isOpen()) { throw new IOException("Socket closed {}"); } boolean acquired = false; try { acquired = semaphore.tryAcquire(writeTimeout, TimeUnit.MILLISECONDS); if (acquired) { session.getAsyncRemote().sendText(s, new WriteResult(resource(), s)); } else { throw new IOException("Socket closed"); } } catch (Throwable e) { if (IOException.class.isAssignableFrom(e.getClass())) { throw (IOException) e; } handleError(e, acquired); } return this; } JSR356WebSocket(Session session, AtmosphereConfig config); }
JSR356WebSocket extends WebSocket { @Override public WebSocket write(String s) throws IOException { if (!isOpen()) { throw new IOException("Socket closed {}"); } boolean acquired = false; try { acquired = semaphore.tryAcquire(writeTimeout, TimeUnit.MILLISECONDS); if (acquired) { session.getAsyncRemote().sendText(s, new WriteResult(resource(), s)); } else { throw new IOException("Socket closed"); } } catch (Throwable e) { if (IOException.class.isAssignableFrom(e.getClass())) { throw (IOException) e; } handleError(e, acquired); } return this; } JSR356WebSocket(Session session, AtmosphereConfig config); @Override boolean isOpen(); @Override WebSocket write(String s); @Override WebSocket write(byte[] data, int offset, int length); @Override void close(); }
JSR356WebSocket extends WebSocket { @Override public WebSocket write(String s) throws IOException { if (!isOpen()) { throw new IOException("Socket closed {}"); } boolean acquired = false; try { acquired = semaphore.tryAcquire(writeTimeout, TimeUnit.MILLISECONDS); if (acquired) { session.getAsyncRemote().sendText(s, new WriteResult(resource(), s)); } else { throw new IOException("Socket closed"); } } catch (Throwable e) { if (IOException.class.isAssignableFrom(e.getClass())) { throw (IOException) e; } handleError(e, acquired); } return this; } JSR356WebSocket(Session session, AtmosphereConfig config); @Override boolean isOpen(); @Override WebSocket write(String s); @Override WebSocket write(byte[] data, int offset, int length); @Override void close();  }