Search in sources :

Example 1 with ServerPoller

use of io.mantisrx.runtime.source.http.ServerPoller in project mantis by Netflix.

the class DefaultHttpServerProviderTest method testOverlappingSetsAreHandled.

@Test
public void testOverlappingSetsAreHandled() throws Exception {
    final List<ServerInfo> added = new ArrayList<>();
    final Queue<ServerInfo> removed = new ConcurrentLinkedQueue<>();
    final CountDownLatch done = new CountDownLatch(1);
    final int min = 1;
    final int max = 8;
    ServerPoller poller = new ServerPoller() {

        @Override
        public Observable<Set<ServerInfo>> servers() {
            return Observable.range(min, max).buffer(3, 1).map(new Func1<List<Integer>, Set<ServerInfo>>() {

                @Override
                public Set<ServerInfo> call(List<Integer> ports) {
                    Set<ServerInfo> s = new HashSet<>();
                    for (int port : ports) {
                        s.add(new ServerInfo("host", port));
                    }
                    return s;
                }
            }).doAfterTerminate(new Action0() {

                @Override
                public void call() {
                    done.countDown();
                }
            });
        }

        @Override
        public Set<ServerInfo> getServers() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    DefaultHttpServerProvider provider = new DefaultHttpServerProvider(poller);
    provider.getServersToAdd().doOnNext(new Action1<ServerInfo>() {

        @Override
        public void call(ServerInfo server) {
            added.add(server);
        }
    }).subscribe();
    provider.getServersToRemove().doOnNext(new Action1<ServerInfo>() {

        @Override
        public void call(ServerInfo server) {
            removed.offer(server);
        }
    }).subscribe();
    done.await();
    int port = min - 1;
    added.sort(new Comparator<ServerInfo>() {

        @Override
        public int compare(ServerInfo o1, ServerInfo o2) {
            return o1.getPort() - o2.getPort();
        }
    });
    for (ServerInfo server : added) {
        port += 1;
        assertEquals(port, server.getPort());
    }
    assertEquals(max, port);
    port = 0;
    for (ServerInfo server : removed) {
        port += 1;
        assertEquals(port, server.getPort());
    }
    assertEquals("The very last element should not be removed. ", max - 1, port);
}
Also used : Action0(rx.functions.Action0) Set(java.util.Set) HashSet(java.util.HashSet) Action1(rx.functions.Action1) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerPoller(io.mantisrx.runtime.source.http.ServerPoller) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Func1(rx.functions.Func1) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ServerPoller

use of io.mantisrx.runtime.source.http.ServerPoller in project mantis by Netflix.

the class DefaultHttpServerProviderTest method testTheSameSetWillBeIdempotent.

@Test
public void testTheSameSetWillBeIdempotent() throws Exception {
    final List<ServerInfo> added = new ArrayList<>();
    final List<ServerInfo> removed = new ArrayList<>();
    final CountDownLatch done = new CountDownLatch(1);
    final int min = 1;
    final int max = 8;
    ServerPoller poller = new ServerPoller() {

        @Override
        public Observable<Set<ServerInfo>> servers() {
            return Observable.range(min, max).map(new Func1<Integer, Set<ServerInfo>>() {

                @Override
                public Set<ServerInfo> call(Integer port) {
                    Set<ServerInfo> s = new HashSet<>();
                    s.add(new ServerInfo("host", 1));
                    s.add(new ServerInfo("host", 2));
                    return s;
                }
            }).doAfterTerminate(new Action0() {

                @Override
                public void call() {
                    done.countDown();
                }
            });
        }

        @Override
        public Set<ServerInfo> getServers() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    DefaultHttpServerProvider provider = new DefaultHttpServerProvider(poller);
    provider.getServersToAdd().doOnNext(new Action1<ServerInfo>() {

        @Override
        public void call(ServerInfo server) {
            added.add(server);
        }
    }).subscribe();
    provider.getServersToRemove().doOnNext(new Action1<ServerInfo>() {

        @Override
        public void call(ServerInfo server) {
            removed.add(server);
        }
    }).subscribe();
    done.await();
    int port = min - 1;
    // Have to sort because items in a single batch may not come in order
    Collections.sort(added, new Comparator<ServerInfo>() {

        @Override
        public int compare(ServerInfo o1, ServerInfo o2) {
            return o1.getPort() - o2.getPort();
        }
    });
    for (ServerInfo server : added) {
        port += 1;
        assertEquals(port, server.getPort());
    }
    assertEquals(2, port);
    assertEquals("No element should be removed. ", 0, removed.size());
}
Also used : Action0(rx.functions.Action0) Set(java.util.Set) HashSet(java.util.HashSet) Action1(rx.functions.Action1) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerPoller(io.mantisrx.runtime.source.http.ServerPoller) Func1(rx.functions.Func1) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ServerPoller

use of io.mantisrx.runtime.source.http.ServerPoller in project mantis by Netflix.

the class DefaultHttpServerProviderTest method testNewSetAlwaysReplacesOldSet.

@Test
public void testNewSetAlwaysReplacesOldSet() throws Exception {
    final List<ServerInfo> added = new ArrayList<>();
    final List<ServerInfo> removed = new ArrayList<>();
    final CountDownLatch done = new CountDownLatch(1);
    final int min = 1;
    final int max = 8;
    ServerPoller poller = new ServerPoller() {

        @Override
        public Observable<Set<ServerInfo>> servers() {
            return Observable.range(min, max).buffer(2).map(new Func1<List<Integer>, Set<ServerInfo>>() {

                @Override
                public Set<ServerInfo> call(List<Integer> ports) {
                    Set<ServerInfo> s = new HashSet<>();
                    for (int port : ports) {
                        s.add(new ServerInfo("host", port));
                    }
                    return s;
                }
            }).doAfterTerminate(new Action0() {

                @Override
                public void call() {
                    done.countDown();
                }
            });
        }

        @Override
        public Set<ServerInfo> getServers() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    DefaultHttpServerProvider provider = new DefaultHttpServerProvider(poller);
    provider.getServersToAdd().doOnNext(new Action1<ServerInfo>() {

        @Override
        public void call(ServerInfo server) {
            added.add(server);
        }
    }).subscribe();
    provider.getServersToRemove().doOnNext(new Action1<ServerInfo>() {

        @Override
        public void call(ServerInfo server) {
            removed.add(server);
        }
    }).subscribe();
    done.await();
    int port = min - 1;
    // Have to sort because items in a single batch may not come in order
    Collections.sort(added, new Comparator<ServerInfo>() {

        @Override
        public int compare(ServerInfo o1, ServerInfo o2) {
            return o1.getPort() - o2.getPort();
        }
    });
    for (ServerInfo server : added) {
        port += 1;
        assertEquals(port, server.getPort());
    }
    assertEquals(max, port);
    Collections.sort(removed, new Comparator<ServerInfo>() {

        @Override
        public int compare(ServerInfo o1, ServerInfo o2) {
            return o1.getPort() - o2.getPort();
        }
    });
    port = 0;
    for (ServerInfo server : removed) {
        port += 1;
        assertEquals(port, server.getPort());
    }
    assertEquals("The very last two elements should not be removed. ", max - 2, port);
}
Also used : Action0(rx.functions.Action0) Set(java.util.Set) HashSet(java.util.HashSet) Action1(rx.functions.Action1) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerPoller(io.mantisrx.runtime.source.http.ServerPoller) ArrayList(java.util.ArrayList) List(java.util.List) Func1(rx.functions.Func1) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServerPoller (io.mantisrx.runtime.source.http.ServerPoller)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ServerInfo (mantis.io.reactivex.netty.client.RxClient.ServerInfo)3 Test (org.junit.Test)3 Action0 (rx.functions.Action0)3 Action1 (rx.functions.Action1)3 Func1 (rx.functions.Func1)3 List (java.util.List)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1