Package co.paralleluniverse.galaxy.core.RefAllocator

Examples of co.paralleluniverse.galaxy.core.RefAllocator.RefAllocationsListener


    /**
     * A put allocates ID and line
     */
    @Test
    public void whenPutThenAllocateId() throws Exception {
        final RefAllocationsListener listener = hasServer ? null : getRefAllocationListener(cache.getRefAllocator());
        Object res;
        Op put;

        put = new Op(PUT, -1L, serialize("1111"), null);
        res = cache.runOp(put);

        assertThat(res, is(PENDING));
        if (hasServer)
            verify(comm).send(argThat(equalTo(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT))));
        else
            verify(cluster).allocateRefs(anyInt());

        if (hasServer)
            cache.receive(Message.ALLOCED_REF(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT), 100, 3));
        else
            listener.refsAllocated(100, 3);

        res = put.getResult();

        assertThat((Long) res, is(100L));

        res = get(100L);
        assertThat((String) res, is("1111"));
        assertModified(100L, true);
        assertThat(cache.getLine(100L).isLocked(), is(true));

        res = doOp(PUT, -1L, serialize("2222"));
        assertThat((Long) res, is(101L));
        assertModified(101L, true);
        assertThat(cache.getLine(101L).isLocked(), is(true));
        res = get(101L);
        assertThat((String) res, is("2222"));

        if (hasServer)
            verify(comm, times(2)).send(argThat(equalTo(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT))));
        else
            verify(cluster, times(2)).allocateRefs(anyInt()); // allocates after id > (min + max) / 2

        res = doOp(PUT, -1L, serialize("3333"));
        assertThat((Long) res, is(102L));
        assertModified(102L, true);
        assertThat(cache.getLine(102L).isLocked(), is(true));
        res = get(102);
        assertThat((String) res, is("3333"));

        put = new Op(PUT, -1L, serialize("4444"), null);
        res = cache.runOp(put);

        assertThat(res, is(PENDING));

        if (hasServer)
            cache.receive(Message.ALLOCED_REF(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT), 200, 3));
        else
            listener.refsAllocated(200, 3);

        res = put.getResult();

        assertThat((Long) res, is(200L));

View Full Code Here


    /**
     *
     */
    @Test
    public void testAlloc() throws Exception {
        final RefAllocationsListener listener = hasServer ? null : getRefAllocationListener(cache.getRefAllocator());
        Object res;
        Op alloc;

        Transaction txn = cache.beginTransaction();
        alloc = new Op(ALLOC, -1L, 10, txn);
        res = cache.runOp(alloc);

        assertThat(res, is(PENDING));
        if (hasServer)
            verify(comm).send(argThat(equalTo(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT))));
        else
            verify(cluster).allocateRefs(anyInt());

        if (hasServer)
            cache.receive(Message.ALLOCED_REF(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT), 100, 3));
        else
            listener.refsAllocated(100, 3);

        assertThat(alloc.getFuture().isDone(), is(false));

        if (hasServer)
            cache.receive(Message.ALLOCED_REF(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT), 200, 30));
        else
            listener.refsAllocated(200, 30);

        res = alloc.getResult();

        assertThat((Long) res, is(200L));

        for (int i = 0; i < 10; i++) {
            res = get(200L + i);
            assertThat(res, is(nullValue()));
            assertModified(200L + i, true);
            assertThat(cache.getLine(200L + i).isLocked(), is(true));
        }

        res = doOp(ALLOC, -1L, 7, txn);
        assertThat((Long) res, is(210L));

        alloc = new Op(ALLOC, -1L, 20, txn);
        res = cache.runOp(alloc);

        assertThat(res, is(PENDING));
        if (hasServer)
            verify(comm, atLeastOnce()).send(argThat(equalTo(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT))));
        else
            verify(cluster, atLeastOnce()).allocateRefs(anyInt());

        if (hasServer)
            cache.receive(Message.ALLOCED_REF(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT), 300, 10));
        else
            listener.refsAllocated(300, 10);

        assertThat(alloc.getFuture().isDone(), is(false));

        if (hasServer)
            cache.receive(Message.ALLOCED_REF(Message.ALLOC_REF(Comm.SERVER, DEFAULT_ALLOC_COUNT), 400, 50));
        else
            listener.refsAllocated(400, 50);

        res = alloc.getResult();

        assertThat((Long) res, is(400L));
    }
View Full Code Here

TOP

Related Classes of co.paralleluniverse.galaxy.core.RefAllocator.RefAllocationsListener

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.