@Test public void sessionIdleShouldResetIdleTimeAndSendPingIfNotAwaitingPong() throws Exception { Mockery context = new Mockery(); final Logger logger = context.mock(Logger.class); final NextFilter nextFilter = context.mock(NextFilter.class); final IoSessionEx session = context.mock(IoSessionEx.class); final IoSessionConfigEx config = context.mock(IoSessionConfigEx.class); context.checking(new Expectations() { { allowing(logger).isTraceEnabled(); will(returnValue(false)); oneOf(session).getConfig(); will(returnValue(config)); oneOf(config).setIdleTimeInMillis(IdleStatus.READER_IDLE, STANDARD_INACTIVITY_TIMEOUT_MILLIS / 2); oneOf(session).write(with(PING)); oneOf(nextFilter).sessionIdle(session, IdleStatus.READER_IDLE); } }); WsCheckAliveFilter filter = new WsCheckAliveFilter(STANDARD_INACTIVITY_TIMEOUT_MILLIS, logger); filter.doSessionIdle(nextFilter, session, IdleStatus.READER_IDLE); context.assertIsSatisfied(); }
@Override protected void doSessionIdle(NextFilter nextFilter, IoSessionEx session, IdleStatus status) throws Exception { if (status == IdleStatus.READER_IDLE) { switch (nextAction) { case PONG: logger.info("Session {} didn't receive PONG, will close corresponding connection", session); session.getConfig().setReaderIdleTime(0); IoFilterChain filterChain; if (session instanceof AbstractBridgeSession<?,?> && ((AbstractBridgeSession<?,?>) session).getLocalAddress().getOption(WsResourceAddress.LIGHTWEIGHT)) { filterChain = ((AbstractBridgeSession<?,?>) session).getParent().getFilterChain(); } else { filterChain = session.getFilterChain(); } if (filterChain.contains(WsAcceptor.CLOSE_FILTER)) { filterChain.remove(WsAcceptor.CLOSE_FILTER); } IoSession sessionToClose = wsSession != null ? wsSession : session; logger.info("Closing session {} as it didn't receive PONG", sessionToClose); sessionToClose.close(true); break; case PING: writePing(nextFilter, session); } } super.doSessionIdle(nextFilter, session, status); }
WsCheckAliveFilter extends IoFilterAdapter<IoSessionEx> { @Override protected void doSessionIdle(NextFilter nextFilter, IoSessionEx session, IdleStatus status) throws Exception { if (status == IdleStatus.READER_IDLE) { switch (nextAction) { case PONG: logger.info("Session {} didn't receive PONG, will close corresponding connection", session); session.getConfig().setReaderIdleTime(0); IoFilterChain filterChain; if (session instanceof AbstractBridgeSession<?,?> && ((AbstractBridgeSession<?,?>) session).getLocalAddress().getOption(WsResourceAddress.LIGHTWEIGHT)) { filterChain = ((AbstractBridgeSession<?,?>) session).getParent().getFilterChain(); } else { filterChain = session.getFilterChain(); } if (filterChain.contains(WsAcceptor.CLOSE_FILTER)) { filterChain.remove(WsAcceptor.CLOSE_FILTER); } IoSession sessionToClose = wsSession != null ? wsSession : session; logger.info("Closing session {} as it didn't receive PONG", sessionToClose); sessionToClose.close(true); break; case PING: writePing(nextFilter, session); } } super.doSessionIdle(nextFilter, session, status); } }
WsCheckAliveFilter extends IoFilterAdapter<IoSessionEx> { @Override protected void doSessionIdle(NextFilter nextFilter, IoSessionEx session, IdleStatus status) throws Exception { if (status == IdleStatus.READER_IDLE) { switch (nextAction) { case PONG: logger.info("Session {} didn't receive PONG, will close corresponding connection", session); session.getConfig().setReaderIdleTime(0); IoFilterChain filterChain; if (session instanceof AbstractBridgeSession<?,?> && ((AbstractBridgeSession<?,?>) session).getLocalAddress().getOption(WsResourceAddress.LIGHTWEIGHT)) { filterChain = ((AbstractBridgeSession<?,?>) session).getParent().getFilterChain(); } else { filterChain = session.getFilterChain(); } if (filterChain.contains(WsAcceptor.CLOSE_FILTER)) { filterChain.remove(WsAcceptor.CLOSE_FILTER); } IoSession sessionToClose = wsSession != null ? wsSession : session; logger.info("Closing session {} as it didn't receive PONG", sessionToClose); sessionToClose.close(true); break; case PING: writePing(nextFilter, session); } } super.doSessionIdle(nextFilter, session, status); } WsCheckAliveFilter(long inactivityTimeout, Logger logger);   WsCheckAliveFilter(long inactivityTimeout, IoSession wsSession, Logger logger); }
WsCheckAliveFilter extends IoFilterAdapter<IoSessionEx> { @Override protected void doSessionIdle(NextFilter nextFilter, IoSessionEx session, IdleStatus status) throws Exception { if (status == IdleStatus.READER_IDLE) { switch (nextAction) { case PONG: logger.info("Session {} didn't receive PONG, will close corresponding connection", session); session.getConfig().setReaderIdleTime(0); IoFilterChain filterChain; if (session instanceof AbstractBridgeSession<?,?> && ((AbstractBridgeSession<?,?>) session).getLocalAddress().getOption(WsResourceAddress.LIGHTWEIGHT)) { filterChain = ((AbstractBridgeSession<?,?>) session).getParent().getFilterChain(); } else { filterChain = session.getFilterChain(); } if (filterChain.contains(WsAcceptor.CLOSE_FILTER)) { filterChain.remove(WsAcceptor.CLOSE_FILTER); } IoSession sessionToClose = wsSession != null ? wsSession : session; logger.info("Closing session {} as it didn't receive PONG", sessionToClose); sessionToClose.close(true); break; case PING: writePing(nextFilter, session); } } super.doSessionIdle(nextFilter, session, status); } WsCheckAliveFilter(long inactivityTimeout, Logger logger);   WsCheckAliveFilter(long inactivityTimeout, IoSession wsSession, Logger logger); static void validateSystemProperties(Properties configuration, Logger logger); static void addIfFeatureEnabled(IoFilterChain filterChain, String filterName, long inactivityTimeoutIn, Logger logger); static void addIfFeatureEnabled(IoFilterChain filterChain, String filterName, long inactivityTimeoutIn,
                                           IoSessionEx wsSession, Logger logger); static void moveIfFeatureEnabled(IoFilterChain fromChain, IoFilterChain toChain,
                                            String filterName, long inactivityTimeout, Logger logger); static void updateExtensions(IoFilterChain filterChain); @Override void onPostAdd(IoFilterChain filterChain, String name, NextFilter nextFilter); @Override void onPreRemove(IoFilterChain filterChain,
                            String name,
                            NextFilter nextFilter); }
WsCheckAliveFilter extends IoFilterAdapter<IoSessionEx> { @Override protected void doSessionIdle(NextFilter nextFilter, IoSessionEx session, IdleStatus status) throws Exception { if (status == IdleStatus.READER_IDLE) { switch (nextAction) { case PONG: logger.info("Session {} didn't receive PONG, will close corresponding connection", session); session.getConfig().setReaderIdleTime(0); IoFilterChain filterChain; if (session instanceof AbstractBridgeSession<?,?> && ((AbstractBridgeSession<?,?>) session).getLocalAddress().getOption(WsResourceAddress.LIGHTWEIGHT)) { filterChain = ((AbstractBridgeSession<?,?>) session).getParent().getFilterChain(); } else { filterChain = session.getFilterChain(); } if (filterChain.contains(WsAcceptor.CLOSE_FILTER)) { filterChain.remove(WsAcceptor.CLOSE_FILTER); } IoSession sessionToClose = wsSession != null ? wsSession : session; logger.info("Closing session {} as it didn't receive PONG", sessionToClose); sessionToClose.close(true); break; case PING: writePing(nextFilter, session); } } super.doSessionIdle(nextFilter, session, status); } WsCheckAliveFilter(long inactivityTimeout, Logger logger);   WsCheckAliveFilter(long inactivityTimeout, IoSession wsSession, Logger logger); static void validateSystemProperties(Properties configuration, Logger logger); static void addIfFeatureEnabled(IoFilterChain filterChain, String filterName, long inactivityTimeoutIn, Logger logger); static void addIfFeatureEnabled(IoFilterChain filterChain, String filterName, long inactivityTimeoutIn,
                                           IoSessionEx wsSession, Logger logger); static void moveIfFeatureEnabled(IoFilterChain fromChain, IoFilterChain toChain,
                                            String filterName, long inactivityTimeout, Logger logger); static void updateExtensions(IoFilterChain filterChain); @Override void onPostAdd(IoFilterChain filterChain, String name, NextFilter nextFilter); @Override void onPreRemove(IoFilterChain filterChain,
                            String name,
                            NextFilter nextFilter); static final long DISABLE_INACTIVITY_TIMEOUT; static final long DEFAULT_WS_INACTIVITY_TIMEOUT_MILLIS; }