1

我有多个测试来测试不同的路由和单独的类,这些测试需要设置 RSocket 客户端然后处置客户端,下面是一个示例。

@SpringBootTest
public class RSocketClientToServerITest {

private static RSocketRequester requester;

 @BeforeAll
    public static void setupOnce(@Autowired RSocketRequester.Builder builder, @Value("${spring.rsocket.server.port}") Integer port) {
        requester = builder
                .connectTcp("localhost", port)
                .block();
    }

 @Test
     public void testRequestGetsResponse() {
         // Send a request message (1)
          Mono<Message> result = requester
                 .route("request-response")
                 .data(new Message("TEST", "Request"))
                 .retrieveMono(Message.class);
 
        // Verify that the response message contains the expected data (2)
        StepVerifier
                  .create(result)
                  .consumeNextWith(message -> { 
assertThat(message.getOrigin()).isEqualTo(RSocketController.SERVER); assertThat(message.getInteraction()).isEqualTo(RSocketController.RESPONSE);
assertThat(message.getIndex()).isEqualTo(0);})
                 .verifyComplete();
     }  

     @AfterAll
      public static void tearDownOnce() {
          requester.rsocket().dispose();
      }
}

但是问题是,它们都使用相同的 RSocket 端口,并且一旦测试完成,它们都会处理 RSocket 客户端,这会影响其他测试。我怎样才能避免这种情况?

谢谢!

4

0 回答 0