gpt4 book ai didi

java - Vertx 单元测试阻塞主线程

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:50:24 27 4
gpt4 key购买 nike

在尝试通过测试类使用事件总线测试 Vert.x verticle 时,我遇到了“阻塞错误”。下面是 Verticle 和测试类:

EventBusReceiverVerticle:

public class EventBusReceiverVerticle extends AbstractVerticle {

public EventBusReceiverVerticle(String name) {
this.name = name;
}

public void start() {

vertx.eventBus().consumer("geo-service", (Message<JsonObject> handler) -> {
{
try {
System.out.println("sleep 10 seconds");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}

测试类:

@RunWith(VertxUnitRunner.class)
public class MyFirstVerticleTest {
Vertx vertx;
EventBus eb;
private static final Logger logger = LoggerFactory.getLogger(MyFirstVerticleTest.class);


@Before
public void setUp(TestContext context) {
logger.info("@Before");
vertx = Vertx.vertx();
eb = vertx.eventBus();

Async async = context.async();
vertx.deployVerticle(new EventBusReceiverVerticle("R1"), res -> {
if (res.succeeded()) {
async.complete();
} else {
context.fail();
}
});
}

@After
public void tearDown(TestContext context) {
logger.info("@After");
Async async = context.async();
vertx.close(event -> async.complete());
}


@Test
public void testEventBusVerticle(TestContext context) {
Async async = context.async();
JsonObject msg = new JsonObject("{\"action\":\"pos\"}");
eb.send("geo-service", msg, reply -> {
if (reply.succeeded()) {
async.complete();
} else {
context.fail();
}
});
async.complete();
}
}

控制台输出:

Sep 05, 2015 2:20:23 PM com.company.MyFirstVerticleTest
INFO: @Before
sleep 10 seconds
Sep 05, 2015 2:20:24 PM com.company.MyFirstVerticleTest
INFO: @After
Sep 05, 2015 2:20:27 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 2790 ms, time limit is 2000
Sep 05, 2015 2:20:28 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 3794 ms, time limit is 2000
Sep 05, 2015 2:20:29 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 4796 ms, time limit is 2000
Sep 05, 2015 2:20:30 PM io.vertx.core.impl.BlockedThreadChecker
WARNING: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 5799 ms, time limit is 2000
io.vertx.core.VertxException: Thread blocked
at java.lang.Thread.sleep(Native Method)
at com.company.verticals.EventBusReceiverVerticle.lambda$start$1(EventBusReceiverVerticle.java:35)
at com.company.verticals.EventBusReceiverVerticle$$Lambda$10/2029238960.handle(Unknown Source)
at io.vertx.core.eventbus.impl.EventBusImpl$HandlerRegistration.handle(EventBusImpl.java:1108)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$doReceive$189(EventBusImpl.java:755)
at io.vertx.core.eventbus.impl.EventBusImpl$$Lambda$17/1888442711.handle(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:314)
at io.vertx.core.impl.ContextImpl$$Lambda$9/911312317.run(Unknown Source)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
at java.lang.Thread.run(Thread.java:745)

最佳答案

我必须通过以下设置将 Verticle 设置为 worker:

new DeploymentOptions().setWorker(true)

关于java - Vertx 单元测试阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com