|
| 1 | +package io.reactivesocket.integration; |
| 2 | + |
| 3 | +import io.reactivesocket.*; |
| 4 | +import io.reactivesocket.client.ClientBuilder; |
| 5 | +import io.reactivesocket.internal.Publishers; |
| 6 | +import io.reactivesocket.test.TestUtil; |
| 7 | +import io.reactivesocket.transport.tcp.client.TcpReactiveSocketConnector; |
| 8 | +import io.reactivesocket.transport.tcp.server.TcpReactiveSocketServer; |
| 9 | +import io.reactivesocket.util.Unsafe; |
| 10 | +import org.junit.Test; |
| 11 | +import org.reactivestreams.Publisher; |
| 12 | +import org.reactivestreams.Subscriber; |
| 13 | +import org.reactivestreams.Subscription; |
| 14 | + |
| 15 | +import java.net.InetSocketAddress; |
| 16 | +import java.net.SocketAddress; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.ExecutionException; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.TimeoutException; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | + |
| 24 | +import static org.junit.Assert.*; |
| 25 | +import static rx.RxReactiveStreams.toObservable; |
| 26 | + |
| 27 | +public class IntegrationTest { |
| 28 | + |
| 29 | + private interface TestingServer { |
| 30 | + int requestCount(); |
| 31 | + int disconnectionCount(); |
| 32 | + SocketAddress getListeningAddress(); |
| 33 | + } |
| 34 | + |
| 35 | + private TestingServer createServer() { |
| 36 | + AtomicInteger requestCounter = new AtomicInteger(); |
| 37 | + AtomicInteger disconnectionCounter = new AtomicInteger(); |
| 38 | + |
| 39 | + ConnectionSetupHandler setupHandler = (setupPayload, reactiveSocket) -> { |
| 40 | + reactiveSocket.onClose().subscribe(new Subscriber<Void>() { |
| 41 | + @Override |
| 42 | + public void onSubscribe(Subscription s) { |
| 43 | + s.request(Long.MAX_VALUE); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onNext(Void aVoid) {} |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onError(Throwable t) {} |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onComplete() { |
| 54 | + disconnectionCounter.incrementAndGet(); |
| 55 | + } |
| 56 | + }); |
| 57 | + return new RequestHandler.Builder() |
| 58 | + .withRequestResponse( |
| 59 | + payload -> subscriber -> subscriber.onSubscribe(new Subscription() { |
| 60 | + @Override |
| 61 | + public void request(long n) { |
| 62 | + requestCounter.incrementAndGet(); |
| 63 | + subscriber.onNext(TestUtil.utf8EncodedPayload("RESPONSE", "NO_META")); |
| 64 | + subscriber.onComplete(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void cancel() {} |
| 69 | + }) |
| 70 | + ) |
| 71 | + .build(); |
| 72 | + }; |
| 73 | + |
| 74 | + SocketAddress addr = new InetSocketAddress("127.0.0.1", 0); |
| 75 | + TcpReactiveSocketServer.StartedServer server = |
| 76 | + TcpReactiveSocketServer.create(addr).start(setupHandler); |
| 77 | + |
| 78 | + return new TestingServer() { |
| 79 | + @Override |
| 80 | + public int requestCount() { |
| 81 | + return requestCounter.get(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public int disconnectionCount() { |
| 86 | + return disconnectionCounter.get(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public SocketAddress getListeningAddress() { |
| 91 | + return server.getServerAddress(); |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + private ReactiveSocket createClient(SocketAddress addr) throws InterruptedException, ExecutionException, TimeoutException { |
| 97 | + List<SocketAddress> addrs = Collections.singletonList(addr); |
| 98 | + Publisher<List<SocketAddress>> src = Publishers.just(addrs); |
| 99 | + |
| 100 | + ConnectionSetupPayload setupPayload = |
| 101 | + ConnectionSetupPayload.create("UTF-8", "UTF-8", ConnectionSetupPayload.HONOR_LEASE); |
| 102 | + TcpReactiveSocketConnector tcp = TcpReactiveSocketConnector.create(setupPayload, Throwable::printStackTrace); |
| 103 | + |
| 104 | + Publisher<ReactiveSocket> socketPublisher = |
| 105 | + ClientBuilder.<SocketAddress>instance() |
| 106 | + .withSource(src) |
| 107 | + .withConnector(tcp) |
| 108 | + .build(); |
| 109 | + |
| 110 | + return Unsafe.blockingSingleWait(socketPublisher, 5, TimeUnit.SECONDS); |
| 111 | + } |
| 112 | + |
| 113 | + @Test(timeout = 2_000L) |
| 114 | + public void testRequest() throws ExecutionException, InterruptedException, TimeoutException { |
| 115 | + TestingServer server = createServer(); |
| 116 | + ReactiveSocket client = createClient(server.getListeningAddress()); |
| 117 | + |
| 118 | + toObservable(client.requestResponse(TestUtil.utf8EncodedPayload("RESPONSE", "NO_META"))) |
| 119 | + .toBlocking() |
| 120 | + .subscribe(); |
| 121 | + assertTrue("Server see the request", server.requestCount() > 0); |
| 122 | + } |
| 123 | + |
| 124 | + @Test(timeout = 2_000L) |
| 125 | + public void testClose() throws ExecutionException, InterruptedException, TimeoutException { |
| 126 | + TestingServer server = createServer(); |
| 127 | + ReactiveSocket client = createClient(server.getListeningAddress()); |
| 128 | + |
| 129 | + toObservable(client.close()).toBlocking().subscribe(); |
| 130 | + |
| 131 | + Thread.sleep(100); |
| 132 | + assertTrue("Server see disconnection", server.disconnectionCount() > 0); |
| 133 | + } |
| 134 | +} |
0 commit comments