Search in sources :

Example 1 with NonBlockingCreditMap

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

the class NonBlockingCreditMapTest method setup.

@BeforeMethod
protected void setup() {
    map = new NonBlockingCreditMap(MAX_CREDITS, MAX_QUEUE_SIZE, new ReentrantLock(true));
    addAll();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) NonBlockingCreditMap(org.jgroups.util.NonBlockingCreditMap) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with NonBlockingCreditMap

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

the class NonBlockingCreditMapTest method testBlockingDecrement.

public void testBlockingDecrement() {
    map = new NonBlockingCreditMap(MAX_CREDITS, 2500, new ReentrantLock(true));
    addAll();
    Message msg = new BytesMessage(null, new byte[8000]);
    boolean rc = map.decrement(msg, msg.getLength(), 0);
    assert rc && !map.isQueuing();
    new Thread(() -> {
        Util.sleep(2000);
        System.out.println("\n-- replenishing 5000 credits");
        Stream.of(a, b, c, d).forEach(c -> map.replenish(c, 5000));
    }).start();
    for (int i = 1; i <= 5; i++) {
        msg = new BytesMessage(null, new byte[1000]);
        System.out.printf("-- adding msg %d: ", i);
        // message 5 should block, but replenish() should unblock it
        rc = map.decrement(msg, msg.getLength(), 0);
        System.out.printf("rc=%b\n", rc);
        assert rc == i < 3;
    }
    assert !map.isQueuing();
    assert map.getQueuedMessages() == 0;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) BytesMessage(org.jgroups.BytesMessage) Arrays(java.util.Arrays) Stream(java.util.stream.Stream) Message(org.jgroups.Message) Util(org.jgroups.util.Util) ReentrantLock(java.util.concurrent.locks.ReentrantLock) NonBlockingCreditMap(org.jgroups.util.NonBlockingCreditMap) BeforeMethod(org.testng.annotations.BeforeMethod) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) Address(org.jgroups.Address) Collectors(java.util.stream.Collectors) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) NonBlockingCreditMap(org.jgroups.util.NonBlockingCreditMap) BytesMessage(org.jgroups.BytesMessage)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 NonBlockingCreditMap (org.jgroups.util.NonBlockingCreditMap)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 Arrays (java.util.Arrays)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Address (org.jgroups.Address)1 BytesMessage (org.jgroups.BytesMessage)1 Message (org.jgroups.Message)1 Util (org.jgroups.util.Util)1 Test (org.testng.annotations.Test)1