Example usage for io.netty.buffer AbstractByteBufAllocator buffer

List of usage examples for io.netty.buffer AbstractByteBufAllocator buffer

Introduction

In this page you can find the example usage for io.netty.buffer AbstractByteBufAllocator buffer.

Prototype

@Override
    public ByteBuf buffer(int initialCapacity) 

Source Link

Usage

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void testLedgerCreateAdvByteBufRefCnt() throws Exception {
    long ledgerId = rng.nextLong();
    ledgerId &= Long.MAX_VALUE;
    if (!baseConf.getLedgerManagerFactoryClass().equals(LongHierarchicalLedgerManagerFactory.class)) {
        // since LongHierarchicalLedgerManager supports ledgerIds of
        // decimal length upto 19 digits but other
        // LedgerManagers only upto 10 decimals
        ledgerId %= 9999999999L;//from  w w  w.j av  a2  s . co m
    }

    final LedgerHandle lh = bkc.createLedgerAdv(ledgerId, 5, 3, 2, digestType, ledgerPassword, null);

    final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true),
            new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true),
            new UnpooledByteBufAllocator(false));

    long entryId = 0;
    for (AbstractByteBufAllocator alloc : allocs) {
        final ByteBuf data = alloc.buffer(10);
        data.writeBytes(("fragment0" + entryId).getBytes());
        assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt());

        CompletableFuture<Integer> cf = new CompletableFuture<>();
        lh.asyncAddEntry(entryId, data, (rc, handle, eId, qwcLatency, ctx) -> {
            CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx;
            future.complete(rc);
        }, cf);

        int rc = cf.get();
        assertEquals("rc code is OK", BKException.Code.OK, rc);

        for (int i = 0; i < 10; i++) {
            if (data.refCnt() == 0) {
                break;
            }
            TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously
        }
        assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0,
                data.refCnt());

        org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId);
        assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes()));
        entryId++;
    }

    bkc.deleteLedger(lh.ledgerId);
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void testLedgerCreateByteBufRefCnt() throws Exception {
    final LedgerHandle lh = bkc.createLedger(5, 3, 2, digestType, ledgerPassword, null);

    final List<AbstractByteBufAllocator> allocs = Lists.newArrayList(new PooledByteBufAllocator(true),
            new PooledByteBufAllocator(false), new UnpooledByteBufAllocator(true),
            new UnpooledByteBufAllocator(false));

    int entryId = 0;
    for (AbstractByteBufAllocator alloc : allocs) {
        final ByteBuf data = alloc.buffer(10);
        data.writeBytes(("fragment0" + entryId).getBytes());
        assertEquals("ref count on ByteBuf should be 1", 1, data.refCnt());

        CompletableFuture<Integer> cf = new CompletableFuture<>();
        lh.asyncAddEntry(data, (rc, handle, eId, ctx) -> {
            CompletableFuture<Integer> future = (CompletableFuture<Integer>) ctx;
            future.complete(rc);//from  w  w  w.j ava  2  s.  com
        }, cf);

        int rc = cf.get();
        assertEquals("rc code is OK", BKException.Code.OK, rc);

        for (int i = 0; i < 10; i++) {
            if (data.refCnt() == 0) {
                break;
            }
            TimeUnit.MILLISECONDS.sleep(250); // recycler runs asynchronously
        }
        assertEquals("writing entry with id " + entryId + ", ref count on ByteBuf should be 0 ", 0,
                data.refCnt());

        org.apache.bookkeeper.client.api.LedgerEntry e = lh.read(entryId, entryId).getEntry(entryId);
        assertEquals("entry data is correct", "fragment0" + entryId, new String(e.getEntryBytes()));
        entryId++;
    }

    bkc.deleteLedger(lh.ledgerId);
}