Search in sources :

Example 16 with MessageBatch

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

the class MessageBatchTest method testIterator.

public void testIterator() {
    List<Message> msgs = createMessages();
    MessageBatch batch = new MessageBatch(msgs);
    int index = 0, count = 0;
    for (Message msg : batch) {
        Message tmp = msgs.get(index++);
        count++;
        assert msg == tmp;
    }
    assert count == msgs.size();
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Example 17 with MessageBatch

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

the class MessageBatchTest method testAddBatchNoResizeOK.

public void testAddBatchNoResizeOK() {
    MessageBatch batch = new MessageBatch(16);
    List<Message> msgs = createMessages();
    MessageBatch other = new MessageBatch(3);
    msgs.forEach(other::add);
    assert other.size() == msgs.size();
    assert batch.isEmpty();
    int added = batch.add(other, false);
    assert added == other.size();
    assert batch.size() == msgs.size() && batch.capacity() == 16;
    assert other.size() == msgs.size();
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Example 18 with MessageBatch

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

the class MessageBatchTest method testAddBatchNoResizeFail.

public void testAddBatchNoResizeFail() {
    MessageBatch batch = new MessageBatch(3);
    List<Message> msgs = createMessages();
    MessageBatch other = new MessageBatch(3);
    msgs.forEach(other::add);
    assert other.size() == msgs.size();
    assert batch.isEmpty();
    int added = batch.add(other, false);
    assert added == batch.size();
    assert batch.size() == 3 && batch.capacity() == 3;
    assert other.size() == msgs.size();
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Message(org.jgroups.Message)

Example 19 with MessageBatch

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

the class RefcountedNioMessageTest method testUnicastRefcounting.

public void testUnicastRefcounting() throws Exception {
    try (JChannel a = new JChannel(Util.getTestStack()).name("A");
        JChannel b = new JChannel(Util.getTestStack()).name("B")) {
        UNICAST3 u = b.getProtocolStack().findProtocol(UNICAST3.class);
        u.setAckThreshold(1);
        a.connect("testUnicastRefcounting");
        b.connect("testUnicastRefcounting");
        Util.waitUntilAllChannelsHaveSameView(10000, 500, a, b);
        Address dest = b.getAddress();
        // populate a fixed pool of POOL_SIZE elements
        for (int i = 0; i < POOL_SIZE; i++) pool.put(DIRECT ? ByteBuffer.allocateDirect(MSG_SIZE) : ByteBuffer.allocate(MSG_SIZE));
        final AtomicInteger received = new AtomicInteger();
        Receiver r = new Receiver() {

            @Override
            public void receive(Message msg) {
                received.incrementAndGet();
            }

            @Override
            public void receive(MessageBatch batch) {
                received.addAndGet(batch.size());
            }
        };
        b.setReceiver(r);
        Sender[] senders = new Sender[NUM_SENDERS];
        for (int i = 0; i < senders.length; i++) {
            senders[i] = new Sender(a, pool, dest);
            new Thread(senders[i], "sender-" + (i + i)).start();
        }
        Util.waitUntil(30000, 500, () -> received.get() == NUM_MSGS * NUM_SENDERS, () -> String.format("received=%d (expected=%d)", received.get(), NUM_MSGS * NUM_SENDERS));
        assert MSGS.size() == NUM_MSGS * NUM_SENDERS;
        assert MSGS.stream().allMatch(m -> m instanceof RefcountedNioMessage);
        Util.waitUntil(10000, 500, () -> MSGS.stream().allMatch(m -> ((RefcountedNioMessage) m).getRefcount() == 0));
        assert MSGS.stream().allMatch(m -> ((RefcountedNioMessage) m).getRefcount() == 0);
        System.out.printf("\n*** pool size: %d, %d msgs\n", pool.size(), MSGS.size());
        assert pool.size() == POOL_SIZE;
    }
}
Also used : MessageBatch(org.jgroups.util.MessageBatch) Util(org.jgroups.util.Util) Collection(java.util.Collection) BeforeMethod(org.testng.annotations.BeforeMethod) Test(org.testng.annotations.Test) BlockingQueue(java.util.concurrent.BlockingQueue) AfterMethod(org.testng.annotations.AfterMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) UNICAST3(org.jgroups.protocols.UNICAST3) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.jgroups(org.jgroups) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) UNICAST3(org.jgroups.protocols.UNICAST3) MessageBatch(org.jgroups.util.MessageBatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 20 with MessageBatch

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

the class MessageBatchDrainTest2 method drain.

protected void drain(int num) {
    if (adders.getAndIncrement() == 0) {
        num_removers.increment();
        int cnt = 0, removed_msgs, total_removed = 0;
        final MessageBatch delivery_batch = new MessageBatch(num);
        do {
            delivery_batch.reset();
            removed_msgs = _transfer(delivery_batch);
            if (removed_msgs > 0) {
                total_removed += removed_msgs;
                removed.add(removed_msgs);
            }
            cnt++;
        // LockSupport.parkNanos(4_000);
        } while (adders.decrementAndGet() != 0);
        avg_remove_loops.add(cnt);
        avg_removed.add(total_removed);
    }
}
Also used : MessageBatch(org.jgroups.util.MessageBatch)

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