vertx-sync icon indicating copy to clipboard operation
vertx-sync copied to clipboard

awaitResult/fiberHandler behaviour

Open varknull opened this issue 7 years ago • 3 comments

Hello,

I was playing with vertx fiberHandler example and I noticed that if two messages are sent on the eventBus (different address, same eventBus) in the same request, then the output is what I would expect it to be from two request.

If you take this example:

EventBus eb = vertx.eventBus();
    eb.consumer(ADDRESS1).handler(msg -> {
      System.out.println("Waiting");
      // reply after one second
      vertx.setTimer(1000, tid -> msg.reply("wibble"));
    });

    eb.consumer(ADDRESS2).handler(msg -> {
      System.out.println("Waiting");
      // reply after one second
      vertx.setTimer(1000, tid -> msg.reply("wibble"));
    });

    // If you want to do sync stuff in an async handler it must be transformed to a fiber handler
    vertx.createHttpServer().requestHandler(fiberHandler(req -> {

      // Send a message to address and wait for a reply
      Message<String> reply1 = awaitResult(h -> eb.send(ADDRESS1, "blah", h));
      Message<String> reply2 = awaitResult(h -> eb.send(ADDRESS2, "blah", h));

      System.out.println("Got reply: " + reply1.body());
      System.out.println("Got reply: " + reply2.body());

      req.response().end("blah");

    })).listen(8080, "localhost");

The output is

Waiting
Waiting
Got reply: wibble
Got reply: wibble
Waiting
Waiting
Got reply: wibble
Got reply: wibble

but I would expect

Waiting
Waiting
Got reply: wibble
Got reply: wibble

In general with N senders and N consumers the behaviour seems unpredictable. Not sure if a bug or I'm misusing the constructs awaitResult/fiberHandler.

Any elucidation on the matter will be appreciated.

varknull avatar Apr 12 '17 08:04 varknull

@varknull Are you confident you didn't inadvertently set ADDRESS1 and ADDRESS2 to the same string by accident? For me, that creates the behavior you're observing.

There's nothing wrong with the code you posted.

doctorpangloss avatar Apr 30 '17 00:04 doctorpangloss

@doctorpangloss ADDRESS1 and ADDRESS2 are two different strings.

It can be easily reproduced modifying this example

varknull avatar May 02 '17 11:05 varknull

I know it's been a while, but are you sure you ran with instrumentation?

That's another way I get the behaviour you're seeing.

doctorpangloss avatar Jun 30 '18 04:06 doctorpangloss