Search in sources :

Example 1 with UdpServer

use of io.reactivex.netty.protocol.udp.server.UdpServer in project ribbon by Netflix.

the class UdpClientTest method testUdpClientTimeout.

@Test
public void testUdpClientTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 5000).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    Server myServer = new Server("localhost", port);
    lb.setServersList(Lists.newArrayList(myServer));
    MyUDPClient client = new MyUDPClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.submit("Is there anybody out there?").map(new Func1<DatagramPacket, String>() {

            @Override
            public String call(DatagramPacket datagramPacket) {
                return datagramPacket.content().toString(Charset.defaultCharset());
            }
        }).toBlocking().first();
        fail("Exception expected");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TimeoutException);
        assertEquals(1, client.getLoadBalancerContext().getServerStats(myServer).getSuccessiveConnectionFailureCount());
    } finally {
        server.shutdown();
    }
}
Also used : MyUDPClient(com.netflix.ribbon.transport.netty.MyUDPClient) Server(com.netflix.loadbalancer.Server) UdpServer(io.reactivex.netty.protocol.udp.server.UdpServer) DatagramPacket(io.netty.channel.socket.DatagramPacket) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) TimeoutException(java.util.concurrent.TimeoutException) SocketException(java.net.SocketException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 2 with UdpServer

use of io.reactivex.netty.protocol.udp.server.UdpServer in project ribbon by Netflix.

the class UdpClientTest method testUdpClientWithoutTimeout.

@Test
public void testUdpClientWithoutTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
    RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<DatagramPacket>>() {

            @Override
            public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) {
                connection.writeStringAndFlush("Is there anybody out there?");
                return connection.getInput();
            }
        }).take(1).map(new Func1<DatagramPacket, String>() {

            @Override
            public String call(DatagramPacket datagramPacket) {
                return datagramPacket.content().toString(Charset.defaultCharset());
            }
        }).toBlocking().first();
        assertEquals(HelloUdpServer.WELCOME_MSG, response);
    } finally {
        server.shutdown();
    }
}
Also used : Server(com.netflix.loadbalancer.Server) UdpServer(io.reactivex.netty.protocol.udp.server.UdpServer) ObservableConnection(io.reactivex.netty.channel.ObservableConnection) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) Observable(rx.Observable) DatagramPacket(io.netty.channel.socket.DatagramPacket) Test(org.junit.Test)

Aggregations

BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 Server (com.netflix.loadbalancer.Server)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 UdpServer (io.reactivex.netty.protocol.udp.server.UdpServer)2 Test (org.junit.Test)2 MyUDPClient (com.netflix.ribbon.transport.netty.MyUDPClient)1 ObservableConnection (io.reactivex.netty.channel.ObservableConnection)1 SocketException (java.net.SocketException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Observable (rx.Observable)1