RequestFilter extends AbstractIoFilter { @SuppressWarnings("unchecked") @Override public void messageReceived(IoSession session, Object message, ReadFilterChainController controller) { if (message instanceof Response) { Object id = ((Response) message).requestId(); if (id != null) { Map<?, ?> inFlight = session.getAttribute(IN_FLIGHT_REQUESTS); RequestFuture<REQUEST, RESPONSE> future = (RequestFuture<REQUEST, RESPONSE>) inFlight.remove(id); if (future != null) { future.complete((RESPONSE) message); } } } super.messageReceived(session, message, controller); }  @SuppressWarnings({ "rawtypes", "unchecked" }) IoFuture<RESPONSE> request(IoSession session, REQUEST request, long timeoutInMs); @SuppressWarnings("rawtypes") @Override void sessionOpened(IoSession session); @SuppressWarnings("unchecked") @Override void messageReceived(IoSession session, Object message, ReadFilterChainController controller); @Override void sessionClosed(IoSession session);  }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void receive_a_messagre_and_find_the_future_to_complete() { IoSession session = mock(IoSession.class); Response r = mock(Response.class); when(r.requestId()).thenReturn("ID"); Map m = mock(Map.class); when(session.getAttribute(RequestFilter.IN_FLIGHT_REQUESTS)).thenReturn(m); RequestFuture f = mock(RequestFuture.class); when(m.remove("ID")).thenReturn(f); ReadFilterChainController ctl = mock(ReadFilterChainController.class); rq.messageReceived(session, r, ctl); verify(session).getAttribute(RequestFilter.IN_FLIGHT_REQUESTS); verify(m).remove("ID"); verify(r).requestId(); verify(f).complete(r); verify(ctl).callReadNextFilter(r); verifyNoMoreInteractions(r, m, session, f, ctl); }