Skip to content

Instantly share code, notes, and snippets.

@chefhoobajoob
Created May 25, 2017 13:45
Show Gist options
  • Save chefhoobajoob/1d056d0f5641bc00a49eddbd4be4fc36 to your computer and use it in GitHub Desktop.
Save chefhoobajoob/1d056d0f5641bc00a49eddbd4be4fc36 to your computer and use it in GitHub Desktop.
Reproducer for vertx-unit issue 49 Can't reproduce the behavior with this test.
package vertxunit.tests;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestClient {
private final static Logger _logger = LoggerFactory.getLogger( TestClient.class );
private final Vertx _vertx;
private Long _counter = 0L;
public TestClient( Vertx theVertx ) {
_vertx = theVertx;
}
public void send( JsonObject theData, Handler<AsyncResult<Long>> handler ) {
Long request = _counter++;
_logger.debug( "Sending request: {}", request );
_vertx.eventBus().<JsonObject>send( "vertx-unit.issue.49", theData, ar -> {
if ( ar.failed() ) {
_logger.debug("Request ({}) failed", request, ar.cause() );
handler.handle( Future.failedFuture( ar.cause() ) );
}
else {
Long requestId = ar.result().body().getLong("requestId" );
_logger.debug("Request ({}) succeeded with response: {}", request, requestId );
handler.handle( Future.succeededFuture( requestId ) );
}
} );
_logger.debug( "Sent request: {}", request );
}
}
package vertxunit.tests;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
@RunWith(VertxUnitRunner.class)
public class TestIssue49 {
Vertx vertx;
@Before
public void before(TestContext context) {
vertx = Vertx.vertx();
}
@After
public void after(TestContext context) {
vertx.close( context.asyncAssertSuccess() );
}
@Test(timeout = 3000L)
public void test( TestContext theContext ) {
Async deploy = theContext.async();
vertx.deployVerticle( new TestVerticle(),
theContext.asyncAssertSuccess( deploymentID -> {
deploy.complete();
}));
deploy.await();
TestClient client = new TestClient( vertx );
List<Long> responses = new ArrayList<>();
Async submit = theContext.async(9);
for ( int i = 0; i < 9; i++ ) {
client.send( new JsonObject().put( "requestId", i ), theContext.asyncAssertSuccess( result -> {
responses.add(result);
submit.countDown();
} ) );
}
submit.await();
}
}
package vertxunit.tests;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestVerticle extends AbstractVerticle {
private static final Logger _logger = LoggerFactory.getLogger( TestVerticle.class );
private MessageConsumer<JsonObject> _consumer;
@Override
public void start( Future<Void> theStartFuture ) {
_logger.debug("Starting verticle");
_consumer = vertx.eventBus().consumer( "vertx-unit.issue.49", message -> {
Long request = message.body().getLong( "requestId" );
_logger.debug("Got request: {}", request);
vertx.setTimer( 475, tid -> {
_logger.debug("Sending reply to request: {}", request );
message.reply( new JsonObject().put( "requestId", request ) );
_logger.debug("Reply sent for request: {}", request );
});
} );
_logger.debug( "Setting start timer" );
vertx.setTimer( 500, tid -> {
_logger.debug("Timer elapsed, completing start future");
theStartFuture.complete();
_logger.debug("Verticle started");
} );
}
@Override
public void stop() {
_consumer.unregister();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment