【问题标题】:Restassured Testing Reactive Quarkus blocking in IO thread放心测试 IO 线程中的反应性 Quarkus 阻塞
【发布时间】:2022-11-05 02:14:19
【问题描述】:

我有一个 quarkus 应用程序,我正在尝试使用 Restassured 进行测试 但是,我在测试时收到此错误:

[org.jbo.res.rea.ser.cor.RuntimeExceptionMapper] (vert.x-eventloop-thread-1) A blocking operation occurred on the IO thread. This likely means you need to annotate my.package.MyController#update(class my.package.MyRequestDto) with @io.smallrye.common.annotation.Blocking. Alternatively you can annotate the class my.package.MyController to make every method on the class blocking, or annotate your sub class of the javax.ws.rs.core.Application class to make the whole application blocking

如果我运行这个应用程序并使用邮递员手动测试,这似乎不会发生,所以这似乎是一个测试设置问题。

我的测试如下:

@QuarkusTest
public class MyTest {
  @Test
  @TestReactiveTransaction
  public void test_A() {
              Response response = given()
                      .when()
                      .accept(MediaTypes.JSON_TYPE)
                      .contentType(MediaTypes.JSON_TYPE)
                      .body("{myRequestJsonHere}")
                      .patch("/path/to/api")
                      .then()
                      .extract().response();
      // asserts

  }
}

在我的控制器内部:

@Path("/path/to/api")
@ApplicationScoped
public class MyController{
  @Inject MyService service;

  @PATCH
  public Uni<MyResultType> update(@NotNull @RequestBody @Valid MyRequestDto request) {
     return service.update(request);
  }
}

我的服务:

@ApplicationScoped
public class MyService {
  @Inject MyRepository repository;

  @Transactional
  public Uni<MyResultType> update(MyRequestDto request) {
    return repository.findAll().firstResult()
       .onItem().transform(l -> mapping here))
       .flatMap(repository::persistAndFlush);
  }
}

我的存储库:

public interface MyRepository extends PanacheRepository<MyResultType> {
}

我还尝试将完整的测试内容包装在 UniAsserter.execute(() -&gt; { }); 中,但仍然遇到同样的问题。

是否需要进行更多设置以确保正确使用线程?

【问题讨论】:

    标签: quarkus rest-assured quarkus-panache quarkus-reactive


    【解决方案1】:

    @Transactionaldoesn't work with Reactive Panache or Hibernate Reactive。 您可以使用@ReactiveTransactional 或Panache.withTransaction。

    您不应该使用@TestReactiveTransactional 进行测试。您正在模拟 Rest 调用,没有理由打开事务。 我认为正在发生的事情是您正在启动一个反应式事务,然后 RestAssure 将调用 REST API 并阻止客户端,直到收到结果。我怀疑这是导致问题的原因。

    您可以将测试代码更改为:

    @QuarkusTest
    public class MyTest {
    
      @Test
      public void test_A() {
                  Response response = given()
                          .when()
                          .accept(MediaTypes.JSON_TYPE)
                          .contentType(MediaTypes.JSON_TYPE)
                          .body("{myRequestJsonHere}")
                          .patch("/path/to/api")
                          .then()
                          .extract().response();
      }
    }
    

    服务变为:

    @ApplicationScoped
    public class MyService {
      @Inject MyRepository repository;
    
      @ReactiveTransactional
      public Uni<MyResultType> update(MyRequestDto request) {
        return repository
           .findAll()
           .firstResult()
           .map(l -> /* update the entity... */ );
      }
    }
    

    只有在创建新实体时才需要持久化。 如果需要,您可以将其添加回来。

    【讨论】:

      【解决方案2】:

      我相信当您进行一次数据库访问时,您的线程应该定义为blocking。

      正如您的错误消息所述,您可以在方法或类中包含一个 @Blocking 注释。

      Quarkus guide about blocking threads on WRITING REST SERVICES WITH RESTEASY REACTIVE

      我猜你的课可能是这样的:

      import io.smallrye.common.annotation.Blocking;
      
      @Path("/path/to/api")
      @ApplicationScoped
      public class MyController{
        @Inject MyService service;
      
        @Blocking
        @PATCH
        public Uni<MyResultType> update(@NotNull @RequestBody @Valid MyRequestDto request) {
           return service.update(request);
        }
      }
      

      【讨论】:

      • 我的存储库是一个反应性存储库,所以我不需要将其标记为阻塞否?
      • 你说的对。如果您想覆盖默认行为,@Blocking 很有用。如果您使用了正确的依赖项(看起来确实如此),则没有必要。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2016-06-11
      • 1970-01-01
      • 2015-06-25
      • 2013-10-05
      • 2010-10-07
      • 1970-01-01
      相关资源
      最近更新 更多