Exemplo n.º 1
0
  public static void testDetermineMergeParticipantsAndMergeCoords4() {
    Address a = Util.createRandomAddress(),
        b = Util.createRandomAddress(),
        c = Util.createRandomAddress(),
        d = Util.createRandomAddress();
    org.jgroups.util.UUID.add(a, "A");
    org.jgroups.util.UUID.add(b, "B");
    org.jgroups.util.UUID.add(c, "C");
    org.jgroups.util.UUID.add(d, "D");

    View v1 = View.create(a, 1, a, b);
    View v2 = View.create(c, 1, c, d);

    Map<Address, View> map = new HashMap<>();
    map.put(a, v1);
    map.put(b, v1);
    map.put(d, v2);

    StringBuilder sb = new StringBuilder("map:\n");
    for (Map.Entry<Address, View> entry : map.entrySet())
      sb.append(entry.getKey() + ": " + entry.getValue() + "\n");
    System.out.println(sb);

    Collection<Address> merge_participants = Util.determineMergeParticipants(map);
    System.out.println("merge_participants = " + merge_participants);
    assert merge_participants.size() == 3;
    assert merge_participants.contains(a)
        && merge_participants.contains(c)
        && merge_participants.contains(d);

    Collection<Address> merge_coords = Util.determineMergeCoords(map);
    System.out.println("merge_coords = " + merge_coords);
    assert merge_coords.size() == 2;
    assert merge_coords.contains(a) && merge_coords.contains(c);
  }
Exemplo n.º 2
0
  public static void testWriteView() throws Exception {
    List<Address> members = new ArrayList<>();
    View v;
    Address a1 = Util.createRandomAddress();
    Address a2 = Util.createRandomAddress();
    Address a4 = Util.createRandomAddress();
    ViewId vid = new ViewId(a1, 12345);
    members.add(a1);
    members.add(a2);
    members.add(a4);
    v = new View(vid, members);

    ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(outstream);
    Util.writeGenericStreamable(v, dos);
    Util.writeStreamable(v, dos);
    dos.close();
    byte[] buf = outstream.toByteArray();
    ByteArrayInputStream instream = new ByteArrayInputStream(buf);
    DataInputStream dis = new DataInputStream(instream);
    View v2 = (View) Util.readGenericStreamable(dis);
    Assert.assertEquals(v, v2);
    v2 = (View) Util.readStreamable(View.class, dis);
    Assert.assertEquals(v, v2);
  }
Exemplo n.º 3
0
  public static void testNewMembers() {
    final Address a = Util.createRandomAddress(),
        b = Util.createRandomAddress(),
        c = Util.createRandomAddress(),
        d = Util.createRandomAddress(),
        e = Util.createRandomAddress();
    List<Address> old = new ArrayList<>();
    List<Address> new_list = new ArrayList<>();

    old.add(a);
    old.add(b);
    old.add(c);
    new_list.add(b);
    new_list.add(a);
    new_list.add(c);
    new_list.add(d);
    new_list.add(e);

    System.out.println("old: " + old);
    System.out.println("new: " + new_list);

    List<Address> new_nodes = Util.newMembers(old, new_list);
    System.out.println("new_nodes = " + new_nodes);
    assert new_nodes.size() == 2 : "list should have d and e";
    assert new_nodes.contains(d);
    assert new_nodes.contains(e);
  }
Exemplo n.º 4
0
 public static void testMessageToByteBuffer() throws Exception {
   _testMessage(new Message());
   _testMessage(new Message(null, null, "hello world"));
   _testMessage(new Message(null, Util.createRandomAddress(), null));
   _testMessage(new Message(null, Util.createRandomAddress(), null));
   _testMessage(new Message(null, Util.createRandomAddress(), "bela"));
 }
Exemplo n.º 5
0
  public static void testChanged() {
    Address a = null, b = null;
    assert !Util.different(a, b);

    a = Util.createRandomAddress("A");
    assert Util.different(a, b);
    assert Util.different(b, a);

    b = Util.createRandomAddress("B");
    assert Util.different(a, b);
    assert Util.different(b, a);

    b = a;
    assert !Util.different(a, b);
    assert !Util.different(b, a);
    a = null;
    assert Util.different(a, b);
    assert Util.different(b, a);
  }
Exemplo n.º 6
0
  public static void testWriteAddress() throws Exception {
    Address a1 = Util.createRandomAddress();
    Address a2 = Util.createRandomAddress();
    Address a4 = Util.createRandomAddress();

    ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(outstream);
    Util.writeAddress(a1, dos);
    Util.writeAddress(a2, dos);
    Util.writeAddress(a4, dos);
    dos.close();
    byte[] buf = outstream.toByteArray();
    ByteArrayInputStream instream = new ByteArrayInputStream(buf);
    DataInputStream dis = new DataInputStream(instream);

    Assert.assertEquals(a1, Util.readAddress(dis));
    Assert.assertEquals(a2, Util.readAddress(dis));
    Assert.assertEquals(a4, Util.readAddress(dis));
  }
Exemplo n.º 7
0
  public static void testWriteAndReadStreamableArray() throws Exception {
    Message[] msgs = {
      new Message(null, "hello world").setFlag(Message.Flag.OOB, Message.Flag.NO_RELIABILITY),
      new Message(Util.createRandomAddress("dest"), "bela ban"),
      new Message(
              Util.createRandomAddress("dest"),
              Util.createRandomAddress("src"),
              "hello world again")
          .setTransientFlag(Message.TransientFlag.DONT_LOOPBACK)
    };

    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(256);
    Util.write(msgs, out);

    ByteArrayDataInputStream in = new ByteArrayDataInputStream(out.buffer(), 0, out.position());
    Message[] tmp = Util.read(Message.class, in);
    for (int i = 0; i < msgs.length; i++) {
      if (msgs[i].dest() == null) assert tmp[i].dest() == null;
      else assert (msgs[i].dest().equals(tmp[i].dest()));
      assert msgs[i].getLength() == tmp[i].getLength();
      assert msgs[i].getObject().equals(tmp[i].getObject());
    }
  }
Exemplo n.º 8
0
  public static void testLeftMembers2() {
    final Address a = Util.createRandomAddress(),
        b = Util.createRandomAddress(),
        c = Util.createRandomAddress(),
        d = Util.createRandomAddress();

    List<Address> v1 = new ArrayList<>();
    v1.add(a);
    v1.add(b);
    v1.add(c);
    v1.add(d);

    List<Address> v2 = new ArrayList<>();
    v2.add(c);
    v2.add(d);
    v2.add(a);
    v2.add(b);

    View one = new View(new ViewId(a, 1), v1), two = new View(new ViewId(b, 2), v2);
    List<Address> left = View.leftMembers(one, two);
    System.out.println("left = " + left);
    assert left != null;
    assert left.isEmpty();
  }
Exemplo n.º 9
0
 public static void testWriteStreamable() throws Exception {
   Message m = new Message(null, null, "Hello");
   ViewId vid2 = new ViewId(Util.createRandomAddress(), 35623);
   ByteArrayOutputStream outstream = new ByteArrayOutputStream();
   DataOutputStream dos = new DataOutputStream(outstream);
   Util.writeGenericStreamable(m, dos);
   Util.writeGenericStreamable(vid2, dos);
   dos.close();
   byte[] buf = outstream.toByteArray();
   ByteArrayInputStream instream = new ByteArrayInputStream(buf);
   DataInputStream dis = new DataInputStream(instream);
   Message m2 = (Message) Util.readGenericStreamable(dis);
   ViewId v3 = (ViewId) Util.readGenericStreamable(dis);
   assert m2.getBuffer() != null;
   Assert.assertEquals(m.getLength(), m2.getLength());
   assert v3 != null;
 }
Exemplo n.º 10
0
  @SuppressWarnings("unchecked")
  public void testObjectToFromByteBuffer() throws Exception {
    byte[] buf;
    Address addr = Util.createRandomAddress(), addr2;
    List<String> list = new ArrayList<>(), list2;
    list.add("Bela");
    list.add("Jeannette");

    buf = Util.objectToByteBuffer(addr);
    addr2 = (Address) Util.objectFromByteBuffer(buf);
    System.out.println("addr=" + addr + ", addr2=" + addr2);
    Assert.assertEquals(addr, addr2);

    buf = Util.objectToByteBuffer(list);
    list2 = (List<String>) Util.objectFromByteBuffer(buf);
    System.out.println("list=" + list + ", list2=" + list2);
    Assert.assertEquals(list, list2);

    byte[] buffer = {'B', 'e', 'l', 'a', ' ', 'B', 'a', 'n'};
    buf = Util.objectToByteBuffer(buffer);

    byte[] buffer2 = (byte[]) Util.objectFromByteBuffer(buf);
    assert buffer2 != null && buffer.length == buffer2.length;
    assert Arrays.equals(buffer, buffer2);

    Object obj = null;
    buf = Util.objectToByteBuffer(obj);
    assert buf != null;
    assert buf.length > 0;
    obj = Util.objectFromByteBuffer(buf);
    assert obj == null;

    Object[] values = {
      Boolean.TRUE,
      true,
      false,
      Boolean.FALSE,
      (byte) 22,
      new Byte("2"),
      '5',
      3.14,
      352.3f,
      0,
      100,
      322649,
      Integer.MAX_VALUE,
      Integer.MIN_VALUE,
      0L,
      322649L,
      Long.MAX_VALUE - 50,
      Long.MAX_VALUE,
      Long.MIN_VALUE,
      (short) 22,
      Short.MAX_VALUE,
      Short.MIN_VALUE,
      "Bela Ban",
      new byte[] {'H', 'e', 'l', 'l', 'o'},
      Util.generateArray(1024)
    };
    for (int i = 0; i < values.length; i++) {
      Object value = values[i];
      marshal(value);
    }
  }