|
1 | 1 | package io.rsocket.core;
|
2 | 2 |
|
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.netty.util.CharsetUtil; |
3 | 5 | import io.netty.util.ReferenceCounted;
|
| 6 | +import io.rsocket.ConnectionSetupPayload; |
4 | 7 | import io.rsocket.Payload;
|
5 | 8 | import io.rsocket.RSocket;
|
6 | 9 | import io.rsocket.test.util.TestClientTransport;
|
|
9 | 12 | import org.assertj.core.api.Assertions;
|
10 | 13 | import org.junit.jupiter.api.Test;
|
11 | 14 | import reactor.core.publisher.Mono;
|
| 15 | +import reactor.core.publisher.MonoProcessor; |
12 | 16 | import reactor.test.StepVerifier;
|
13 | 17 |
|
14 | 18 | public class RSocketConnectorTest {
|
15 | 19 |
|
| 20 | + @Test |
| 21 | + public void ensuresThatSetupPayloadCanBeRetained() { |
| 22 | + MonoProcessor<ConnectionSetupPayload> retainedSetupPayload = MonoProcessor.create(); |
| 23 | + TestClientTransport transport = new TestClientTransport(); |
| 24 | + |
| 25 | + ByteBuf data = transport.alloc().buffer(); |
| 26 | + |
| 27 | + data.writeCharSequence("data", CharsetUtil.UTF_8); |
| 28 | + |
| 29 | + RSocketConnector.create() |
| 30 | + .setupPayload(ByteBufPayload.create(data)) |
| 31 | + .acceptor( |
| 32 | + (setup, sendingSocket) -> { |
| 33 | + retainedSetupPayload.onNext(setup.retain()); |
| 34 | + return Mono.just(new RSocket() {}); |
| 35 | + }) |
| 36 | + .connect(transport) |
| 37 | + .block(); |
| 38 | + |
| 39 | + Assertions.assertThat(transport.testConnection().getSent()) |
| 40 | + .hasSize(1) |
| 41 | + .first() |
| 42 | + .matches( |
| 43 | + bb -> { |
| 44 | + DefaultConnectionSetupPayload payload = new DefaultConnectionSetupPayload(bb); |
| 45 | + return !payload.hasMetadata() && payload.getDataUtf8().equals("data"); |
| 46 | + }) |
| 47 | + .matches(buf -> buf.refCnt() == 2) |
| 48 | + .matches( |
| 49 | + buf -> { |
| 50 | + buf.release(); |
| 51 | + return buf.refCnt() == 1; |
| 52 | + }); |
| 53 | + |
| 54 | + retainedSetupPayload |
| 55 | + .as(StepVerifier::create) |
| 56 | + .expectNextMatches( |
| 57 | + setup -> { |
| 58 | + String dataUtf8 = setup.getDataUtf8(); |
| 59 | + return "data".equals(dataUtf8) && setup.release(); |
| 60 | + }) |
| 61 | + .expectComplete() |
| 62 | + .verify(Duration.ofSeconds(5)); |
| 63 | + |
| 64 | + Assertions.assertThat(retainedSetupPayload.peek().refCnt()).isZero(); |
| 65 | + |
| 66 | + transport.alloc().assertHasNoLeaks(); |
| 67 | + } |
| 68 | + |
16 | 69 | @Test
|
17 | 70 | public void ensuresThatMonoFromRSocketConnectorCanBeUsedForMultipleSubscriptions() {
|
18 | 71 | Payload setupPayload = ByteBufPayload.create("TestData", "TestMetadata");
|
|
0 commit comments