コード例 #1
0
  @Test
  public void getNextNodeNoFiltering() throws Exception {

    Node node1 = new Node(new InetSocketAddress("127.0.0.1", 8181));
    Node node2 = new Node(new InetSocketAddress("127.0.0.2", 8182));
    Node node3 = new Node(new InetSocketAddress("127.0.0.3", 8183));

    Strategy lb =
        new FilteredRoundRobinLoadBalancer(
            new Filter() {

              @Override
              public boolean contains(String serviceName, String hostname, String item) {
                return true;
              }
            });
    ImmutableList<Node> pool = ImmutableList.of(node1, node2, node3);

    assertEquals(node1, lb.getNextNode(pool, ImmutableMap.of(node1.token(), node1)));
    assertEquals(node2, lb.getNextNode(pool, ImmutableMap.of(node2.token(), node2)));
    assertEquals(node3, lb.getNextNode(pool, ImmutableMap.of(node3.token(), node3)));

    // test restarting from idx 0 to make sure no overflow
    assertEquals(node1, lb.getNextNode(pool, ImmutableMap.of(node1.token(), node1)));
    assertEquals(node2, lb.getNextNode(pool, ImmutableMap.of(node2.token(), node2)));
    assertEquals(node3, lb.getNextNode(pool, ImmutableMap.of(node3.token(), node3)));
  }
コード例 #2
0
  @Test
  public void getNextNodeWithFiltering() throws Exception {

    Node node1 =
        new Node(
            new InetSocketAddress("127.0.0.1", 8181),
            ImmutableList.copyOf(new String[] {"msmaster1int"}),
            0,
            "paymentserv");
    Node node2 =
        new Node(
            new InetSocketAddress("127.0.0.2", 8182),
            ImmutableList.copyOf(new String[] {"msmaster2int"}),
            0,
            "paymentserv");
    Node node3 =
        new Node(
            new InetSocketAddress("127.0.0.3", 8183),
            ImmutableList.copyOf(new String[] {"msmaster1int"}),
            0,
            "paymentserv");

    Strategy lb =
        new FilteredRoundRobinLoadBalancer(
            new Filter() {

              @Override
              public boolean contains(String serviceName, String hostname, String item) {
                return "msmaster1int".equals(item);
              }
            });
    ImmutableList<Node> pool = ImmutableList.of(node1, node2, node3);

    assertEquals(node1, lb.getNextNode(pool, ImmutableMap.of(node1.token(), node1)));
    assertEquals(node3, lb.getNextNode(pool, ImmutableMap.of(node3.token(), node3)));

    assertEquals(node1, lb.getNextNode(pool, ImmutableMap.of(node1.token(), node1)));
    assertEquals(node3, lb.getNextNode(pool, ImmutableMap.of(node3.token(), node3)));
  }