|
| 1 | +/* |
| 2 | + * Copyright 2002-2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.ripc.reactor; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import io.netty.buffer.ByteBuf; |
| 22 | +import io.netty.buffer.Unpooled; |
| 23 | +import io.ripc.reactor.protocol.tcp.ReactorTcpServer; |
| 24 | +import io.ripc.test.SocketTestUtils; |
| 25 | +import io.ripc.transport.netty4.tcp.Netty4TcpServer; |
| 26 | +import org.junit.After; |
| 27 | +import static org.junit.Assert.assertEquals; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | +import reactor.rx.Promise; |
| 31 | +import reactor.rx.Promises; |
| 32 | +import reactor.rx.Streams; |
| 33 | + |
| 34 | +public class ReactorTcpServerTests { |
| 35 | + |
| 36 | + private ReactorTcpServer<ByteBuf, ByteBuf> reactorServer; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setup() { |
| 40 | + reactorServer = ReactorTcpServer.create(Netty4TcpServer.<ByteBuf, ByteBuf>create(0)); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void tearDown() { |
| 45 | + reactorServer.shutdown(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void writeSingleValue() throws IOException { |
| 50 | + reactorServer.start(connection -> connection.writeWith(Streams.just(Unpooled.buffer().writeBytes("test".getBytes())))); |
| 51 | + assertEquals("test", SocketTestUtils.read("localhost", reactorServer.getPort())); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void writeMultipleValues() throws IOException { |
| 56 | + Promise<ByteBuf> chunk1 = Promises.success(Unpooled.buffer().writeBytes("This is".getBytes())); |
| 57 | + Promise<ByteBuf> chunk2 = Promises.success(Unpooled.buffer().writeBytes(" a test!".getBytes())); |
| 58 | + reactorServer.start(connection -> connection.writeWith(Streams.concat(chunk1, chunk2))); |
| 59 | + assertEquals("This is a test!", SocketTestUtils.read("localhost", reactorServer.getPort())); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments