RestDataSender { public synchronized void scheduleGameProgressRequest(ProgressRequest progressRequest) { requests.add(progressRequest); } @Autowired  RestDataSender(ScheduledExecutorService restSenderExecutorService); synchronized void scheduleGameProgressRequest(ProgressRequest progressRequest); synchronized void sendProgressFor(String playerName, String timestamp, int progress);  }
@Test public void shouldSendUpdateWhenOnePlayerRequested() throws UnsupportedEncodingException, InterruptedException { sender.scheduleGameProgressRequest(progressRequest("123", "petya")); sendProgressAndDown("petya", "123", 50); assertProgress(response.getContentAsString(), 50); }
@Test public void shouldCompleteResponseWhenExceptionOnWrite() throws IOException, InterruptedException { response.setCharacterEncoding("NON_EXISTENT_ENCODING_FOR_IO_EXCEPTION"); sender.scheduleGameProgressRequest(progressRequest("345", "petya")); sendProgressAndDown("petya", "345", 22); assertTrue(asyncContext.isComplete()); }
@Test public void shouldSkipSendingWhenAnotherPlayer() throws UnsupportedEncodingException, InterruptedException { sender.scheduleGameProgressRequest(progressRequest("123", "vasya")); sendProgressAndDown("petya", "123", 11); assertFalse(asyncContext.isComplete()); assertTrue(StringUtils.isBlank(response.getContentAsString())); }
@Test public void shouldSkipSendingWhenAnotherTimestamp() throws UnsupportedEncodingException, InterruptedException { sender.scheduleGameProgressRequest(progressRequest("123", "vasya")); sendProgressAndDown("vasya", "321", 11); assertFalse(asyncContext.isComplete()); assertTrue(StringUtils.isBlank(response.getContentAsString())); }
@Test public void shouldHandleRuntimeExceptionsWhenSending() throws InterruptedException { response.setWriterAccessAllowed(false); sender.scheduleGameProgressRequest(progressRequest("345", "petya")); sendProgressAndDown("petya", "345", 22); assertTrue(asyncContext.isComplete()); }