public static MocoEventAction async(final MocoEventAction action) { return async(checkNotNull(action, "Action should not be null"), latency(DEFAULT_LATENCY, TimeUnit.MILLISECONDS)); }
public MocoEventAction createTrigger() { if (!async && latency != null) { throw new IllegalArgumentException("Latency only works for async mode"); } MocoEventAction action = doCreateAction(); if (this.async) { return Moco.async(action, latency.asProcedure()); } return action; }
@Test public void should_send_post_request_to_target_on_complete_asyc() throws Exception { final ResponseHandler handler = mock(ResponseHandler.class); server.request(and(by(uri("/target")), by("content"))).response(handler); server.request(by(uri("/event"))).response("event").on(complete(async(post(remoteUrl("/target"), text("content"))))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/event")), is("event")); verify(handler, never()).writeToResponse(any(SessionContext.class)); Idles.idle(2, TimeUnit.SECONDS); } }); verify(handler).writeToResponse(any(SessionContext.class)); }
@Test public void should_send_post_request_to_target_on_complete_async_after_awhile() throws Exception { final ResponseHandler handler = mock(ResponseHandler.class); server.request(and(by(uri("/target")), by("content"))).response(handler); server.request(by(uri("/event"))).response("event").on(complete(async(post(remoteUrl("/target"), text("content")), latency(1, TimeUnit.SECONDS)))); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.get(remoteUrl("/event")), is("event")); verify(handler, never()).writeToResponse(any(SessionContext.class)); Idles.idle(2, TimeUnit.SECONDS); } }); verify(handler).writeToResponse(any(SessionContext.class)); }
public MocoEventAction createTrigger() { if (!async && latency != null) { throw new IllegalArgumentException("Latency only works for async mode"); } MocoEventAction action = doCreateAction(); if (this.async) { return Moco.async(action, latency.asProcedure()); } return action; }