Package

Source Code of TestClient$FakeServer

import junit.framework.Assert;
import net.windwards.dnsfrontend.client.Client;
import net.windwards.dnsfrontend.client.Recipient;
import net.windwards.dnsfrontend.dialog.Request;
import net.windwards.dnsfrontend.dialog.Update;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


public class TestClient {
    public static class FakeServer extends ReceiverAdapter {

        private JChannel channel;
        public List<byte[]> received = new LinkedList<byte[]>();

        public FakeServer() throws Exception {
            this.channel = new JChannel();
            this.channel.setReceiver(this);
            this.channel.connect("dns-frontend");
        }

        public void shutdown() {
            this.channel.disconnect();
        }

        public void send(String json) throws Exception {
            Message message = new Message(null, json.getBytes());
            this.channel.send(message);
        }

        @Override
        public void receive(Message message) {
            this.received.add(message.getBuffer());
        }
    }

    @Test
    public void receiveRequest() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);

        Client client = new Client();
        client.addReceiver(new Recipient() {
            @Override
            public void receive(Request request) {
                latch.countDown();
            }
        });
        client.connect();

        FakeServer server = new FakeServer();
        server.send(
                "{ \"type\": \"Request\"," +
                "  \"moniker\": \"bar\"," +
                "  \"name\": \"foo\" }");

        Assert.assertEquals(1, latch.getCount());
        Assert.assertTrue(latch.await(200, TimeUnit.MILLISECONDS));

        client.disconnect();
        server.shutdown();
    }

    @Test
    public void sendUpdate() throws Exception {
        FakeServer server = new FakeServer();

        Client client = new Client();
        client.connect();

        Update update = new Update();
        update.address = "192.168.0.1";
        update.family = "ipv4";
        update.name = "foo";
        update.moniker = "bar";

        Assert.assertEquals(0, server.received.size());

        client.send(update);

        Assert.assertEquals(0, server.received.size());

        Thread.sleep(200);

        Assert.assertEquals(1, server.received.size());

        client.disconnect();
        server.shutdown();
    }

    @Test
    public void overhearUpdate() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);

        Client client = new Client();
        client.addReceiver(new Recipient() {
            @Override
            public void receive(Request request) {
                latch.countDown();
            }
        });
        client.connect();

        FakeServer server = new FakeServer();
        server.send("{" +
                "\"type\": \"update\", " +
                "\"address\": \"192.168.0.1\", " +
                "\"family\": \"ipv4\", " +
                "\"name\": \"foo\"," +
                "\"moniker\": \"bar\" }");

        Assert.assertEquals(1, latch.getCount());
        Assert.assertFalse(latch.await(200, TimeUnit.MILLISECONDS));

        client.disconnect();
        server.shutdown();

    }
}
TOP

Related Classes of TestClient$FakeServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.