WebTestClient有时多次使用返回空体

时间:2018-03-08 16:12:35

标签: spring-webflux spring-boot-test

不确定,为什么这可能是一个问题,但我无法稳定我的单位测试。

这里是我的测试类的一些片段:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.main.web-application-type=reactive" })
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class SolrControllerV1Test {
  @Inject
  ApplicationContext context;
  @LocalServerPort
  int port;

  private WebTestClient client;
  @TestConfiguration
  static class TestConfig {

    @Bean
    public TestingAuthenticationProvider testAuthentiationManager() {
      return new TestingAuthenticationProvider();
    }

    @Bean
    public SecurityWebFilterChain securityConfig(ServerHttpSecurity http, ReactiveAuthenticationManager authenticationManager) {
      AuthenticationWebFilter webFilter = new AuthenticationWebFilter(authenticationManager);
      return http.addFilterAt(webFilter, SecurityWebFiltersOrder.AUTHENTICATION)
          .authorizeExchange()
            .anyExchange()
            .authenticated()
          .and()
          .build();
    }
  }
    @Before
    public void setUp() {
      this.client = WebTestClient.bindToApplicationContext(context).configureClient().responseTimeout(Duration.ofDays(1L)).baseUrl("http://localhost:" + port).build();
    }

    private void defaultCheck(ResponseSpec spec) {
      spec.expectStatus().isOk().expectBody().jsonPath("$.response.numFound").hasJsonPath();
    }
  @Test
  @WithMockUser(roles = { "ADMIN" })
  public void simpleUsrSelect() throws Exception {
    ResponseSpec spec = this.client.get().uri("/" + serviceVersion + "/usr/select?q=*:*&fq=*:*&fl=USRTYP,USRKEY,USRCID&rows=1&start=10&sort=last_update desc").exchange();
    defaultCheck(spec);
  }

  @Test
  @WithMockUser(roles = { "ADMIN" })
  public void simpleCvdSelect() throws Exception {
    ResponseSpec spec = this.client.get().uri("/" + serviceVersion + "/cvd/select?q=*:*&rows=10000").exchange();
    defaultCheck(spec);
  }
.
.
.
}

还有一些单元测试,其中一些是长时间运行(> 1秒)。如果我在课堂上有足够的单元测试(~5-8),其中1或2需要更长的时间,单元测试就会开始中断。这看起来像线程安全问题,但我不知道,我做错了什么。有什么想法吗?

修改 这里出现问题的服务器部分:

@PreAuthorize("hasAnyRole('ADMIN','TENANT')")
public Mono<ServerResponse> select(ServerRequest request) {
  return request.principal().flatMap((principal) -> {
    return client.get().uri(f -> {
              URI u = f.path(request.pathVariable("collection")).path("/select/").queryParams(
                  queryModifier.modify(principal, request.pathVariable("collection"), request.queryParams())
                  .onErrorMap(NoSuchFieldException.class, t -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Collection not found"))
                  .block()).build();
              return u;
            })
        .exchange()
          .flatMap((ClientResponse mapper) -> {
            return ServerResponse.status(mapper.statusCode())
              .headers(c -> mapper.headers().asHttpHeaders().forEach((name, value) -> c.put(name, value)))
              .body(mapper.bodyToFlux(DataBuffer.class), DataBuffer.class);
          })
          .doOnError(t -> handleAuthxErrors(t, principal, request.uri()));
  });
}

如果我在.exchange()部分之后添加publishOn(Schedulers.elastic),它似乎正在工作。由于这是试验和错误,我并不真正理解为什么publishOn修复了问题,其他人都知道吗?我甚至不确定,在这种情况下是否使用弹簧反应式Webclient是否阻塞......

谢谢,亨宁

0 个答案:

没有答案