Search code examples
javavert.xvertx-verticlevertxoptions

Vert.x: how to test how many instances of a specific verticle are deployed?


I have a Verticle which prints something during startup

package com.somepackage;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;

public class MyVerticle extends AbstractVerticle
{
  @Override
  public void start(Promise<Void> startPromise)
  {
    System.out.println("starting Verticle");
    startPromise.complete();
  }
}

and I can run a test where I specify the number of instances:

package com.somepackage;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;

@ExtendWith(VertxExtension.class)
class MyVerticleTest
{
  @BeforeAll
  public static void setUp(Vertx vertx, VertxTestContext context)
  {
    DeploymentOptions options = new DeploymentOptions().setInstances(2);
    vertx.deployVerticle("com.somepackage.MyVerticle", options)
      .onFailure(err -> context.failNow(err))
      .onSuccess(id -> context.completeNow());
  }

  @Test
  void testMyVerticleCount(Vertx vertx, VertxTestContext context)
  {
    // test for number of instances
  }
}

I can read that two verticles are deployed.

How can I test how many instances of MyVerticle exist?


Solution

  • This is not possible with public API but you can use io.vertx.core.impl.VertxInternal to get this information. In order to track how many verticles you have you need to store the Deployment id that you get from deployVerticle method. As mentioned VertxInternal is not public API so it can change at any moment without notice.

    ((VertxInternal)vertx).getDeployment(id).getVerticles().size();