Skip to content

Timing change in Vertx.setTimer #5771

@ianstewart-bls

Description

@ianstewart-bls

Version

5.0.5

Context

I have used Vertx to write a loop where the looping mechanism uses Vertx.runOnContext(Handler). The loop's condition uses an AtomicBoolean which is updated externally to the loop with Vertx.setTimer(long, Handler). The delay for vertx.setTimer is 100 ms. On Vertx 4.5.15, the timer triggers after 100ms as expected. On 5.0.5, it consistently triggers after 1s. I have attached a JUnit test to reproduce it.

Steps to reproduce

Run the following with JUnit on version 4.5.15 (passes) and 5.0.5 (fails). On 4.5.15, "final delay" is 106 on my machine, as expected.

package x;

import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@ExtendWith(VertxExtension.class)
class VertxTest {
    private final AtomicBoolean closed = new AtomicBoolean(false);
    private Vertx vertx;
    private Promise<Void> promise;

    private void loop() {
        if (closed.get()) {
            System.out.println("Stopping loop");
            promise.complete();
            return;
        }
        vertx.runOnContext(v -> this.loop());
    }

    @Test
    void test(Vertx vertx, VertxTestContext context) {
        this.vertx = vertx;
        this.promise = Promise.promise();
        loop();
        long start = System.currentTimeMillis();
        vertx.setTimer(100, l -> {
            System.out.println("setTimer delay: " + (System.currentTimeMillis() - start));
            closed.set(true);
        });

        promise.future().onComplete(context.succeeding(v -> {
            long finalDelay = System.currentTimeMillis() - start;
            System.out.println("Final delay: " + finalDelay);
            assertThat(finalDelay).isLessThan(1000L);
            context.completeNow();
        }));
    }
}

Do you have a reproducer?

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions