Search in sources :

Example 6 with MessageBatch

use of org.jgroups.util.MessageBatch in project JGroups by belaban.

the class MessageBatchTest method testIterator7.

public void testIterator7() {
    List<Message> msgs = createMessages();
    MessageBatch batch = new MessageBatch(msgs);
    int index = 0;
    final Message MSG = new Message();
    for (Message msg : batch) {
        if (index % 2 == 0)
            batch.replace(msg, MSG);
        index++;
    }
    index = 0;
    for (Message msg : batch) {
        // every even index has MSG
        assert index % 2 != 0 || msg == MSG;
        index++;
    }
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Example 7 with MessageBatch

use of org.jgroups.util.MessageBatch in project JGroups by belaban.

the class MessageBatchTest method testCreationWithFilter2.

public void testCreationWithFilter2() {
    List<Message> msgs = new ArrayList<>(20);
    for (int i = 1; i <= 20; i++) {
        Message msg = new Message(null, i);
        if (i <= 10) {
            msg.setFlag(Message.Flag.OOB);
            if (i % 2 == 0)
                msg.setTransientFlag(Message.TransientFlag.OOB_DELIVERED);
        }
        msgs.add(msg);
    }
    Predicate<Message> filter = msg -> msg != null && (!msg.isFlagSet(Message.Flag.OOB) || msg.setTransientFlagIfAbsent(Message.TransientFlag.OOB_DELIVERED));
    MessageBatch batch = new MessageBatch(null, null, null, true, msgs, filter);
    System.out.println("batch = " + batch.map(print_numbers));
    assert batch.size() == 15;
    for (Message msg : batch) {
        int num = msg.getObject();
        assert num > 10 || msg.isTransientFlagSet(Message.TransientFlag.OOB_DELIVERED);
    }
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) DataInputStream(java.io.DataInputStream) java.util(java.util) Util(org.jgroups.util.Util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Predicate(java.util.function.Predicate) BiFunction(java.util.function.BiFunction) Test(org.testng.annotations.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Message(org.jgroups.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) Global(org.jgroups.Global) ClassConfigurator(org.jgroups.conf.ClassConfigurator) Address(org.jgroups.Address) org.jgroups.protocols(org.jgroups.protocols) MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Example 8 with MessageBatch

use of org.jgroups.util.MessageBatch in project JGroups by belaban.

the class MessageBatchTest method testReplace3.

public void testReplace3() {
    MessageBatch batch = new MessageBatch(1).add(new Message(null, "Bela")).add(new Message(null, "Michi")).add(new Message(null, "Nicole"));
    System.out.println("batch = " + batch);
    for (Message msg : batch) {
        if ("Michi".equals(msg.getObject())) {
            msg.setObject("Michelle");
            // tests replacing the message with itself (with changed buffer though)
            batch.replace(msg, msg);
        }
    }
    Queue<String> names = new LinkedBlockingQueue<>(Arrays.asList("Bela", "Michelle", "Nicole"));
    for (Message msg : batch) {
        String expected = names.poll();
        String name = msg.getObject();
        System.out.println("found=" + name + ", expected=" + expected);
        assert name.equals(expected) : "found=" + name + ", expected=" + expected;
    }
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 9 with MessageBatch

use of org.jgroups.util.MessageBatch in project JGroups by belaban.

the class MessageBatchTest method testForEach.

public void testForEach() {
    MessageBatch batch = new MessageBatch(10);
    for (int i = 0; i < 10; i++) batch.add(new Message(a, i));
    System.out.println("batch = " + batch);
    assert batch.size() == 10;
    batch.remove(msg -> {
        // removes all msgs with even-numbered payloads
        int num = msg.getObject();
        return num % 2 == 0;
    });
    System.out.println("batch = " + batch);
    assert batch.size() == 5;
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Example 10 with MessageBatch

use of org.jgroups.util.MessageBatch in project JGroups by belaban.

the class MessageBatchTest method testIterator2.

public void testIterator2() {
    List<Message> msgs = createMessages();
    MessageBatch batch = new MessageBatch(msgs);
    int count = 0;
    for (Message ignored : batch) count++;
    assert count == msgs.size();
    remove(batch, 3, 5, 10);
    count = 0;
    for (Message ignored : batch) count++;
    assert count == msgs.size() - 3;
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Aggregations

MessageBatch (org.jgroups.util.MessageBatch)46 Message (org.jgroups.Message)37 Util (org.jgroups.util.Util)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)5 ClassConfigurator (org.jgroups.conf.ClassConfigurator)5 Test (org.testng.annotations.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 java.util (java.util)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BiFunction (java.util.function.BiFunction)4 Predicate (java.util.function.Predicate)4 Collectors (java.util.stream.Collectors)4 Address (org.jgroups.Address)4 Global (org.jgroups.Global)4 org.jgroups.protocols (org.jgroups.protocols)4 Collection (java.util.Collection)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2