Search in sources :

Example 61 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project jetcache by alibaba.

the class AbstractEncoderTest method baseTest.

protected void baseTest() {
    Assert.assertEquals("123", decoder.apply(encoder.apply("123")));
    Assert.assertEquals(123, decoder.apply(encoder.apply(123)));
    Date date = new Date();
    Assert.assertEquals(date, decoder.apply(encoder.apply(date)));
    Assert.assertArrayEquals(new byte[] { 1, 2, 3, -1 }, (byte[]) decoder.apply(encoder.apply(new byte[] { 1, 2, 3, -1 })));
    Assert.assertNull(decoder.apply(encoder.apply(null)));
    testMap(new HashMap());
    testMap(new Hashtable());
    testMap(new ConcurrentHashMap());
    //        testMap(Collections.synchronizedMap(new HashMap()));
    testList(new ArrayList());
    testList(new Vector());
    testList(new LinkedList());
    testSet(new HashSet());
    testQueue(new LinkedBlockingQueue<>());
    //        testQueue(new ArrayBlockingQueue(10));
    testDeque(new LinkedBlockingDeque());
    TestObject q = new TestObject();
    q.setId(100);
    q.setEmail("aaa");
    q.setName("bbb");
    q.setData(new byte[] { 1, 2, 3 });
    Map<String, BigDecimal> m = new HashMap();
    m.put("12345", new BigDecimal(12345));
    byte[] bs = encoder.apply(q);
    TestObject q2 = (TestObject) decoder.apply(bs);
    compareTestObject(q, q2);
    bs = encoder.apply(new Object[] { q, 123 });
    Object[] o = (Object[]) decoder.apply(bs);
    q2 = (TestObject) o[0];
    Assert.assertEquals(123, o[1]);
    compareTestObject(q, q2);
    A a = new A();
    a.setList(new ArrayList<>());
    a.getList().add(q);
    CacheValueHolder<CacheValueHolder<A>> h = new CacheValueHolder(new CacheValueHolder(a, 1000), 1000);
    bs = encoder.apply(h);
    CacheValueHolder<CacheValueHolder<A>> h2 = (CacheValueHolder<CacheValueHolder<A>>) decoder.apply(bs);
    compareTestObject(h.getValue().getValue().getList().get(0), h2.getValue().getValue().getList().get(0));
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CacheValueHolder(com.alicp.jetcache.CacheValueHolder) BigDecimal(java.math.BigDecimal) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 62 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project asterixdb by apache.

the class ConcurrentFramePoolUnitTest method testFixedSizeSubscribtion.

@org.junit.Test
public void testFixedSizeSubscribtion() {
    try {
        ActiveProperties afp = Mockito.mock(ActiveProperties.class);
        Mockito.when(afp.getMemoryComponentGlobalBudget()).thenReturn(FEED_MEM_BUDGET);
        ConcurrentFramePool fmm = new ConcurrentFramePool("TestNode", afp.getMemoryComponentGlobalBudget(), DEFAULT_FRAME_SIZE);
        int i = 0;
        ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_FRAME_SIZE);
        LinkedBlockingDeque<ByteBuffer> buffers = new LinkedBlockingDeque<>();
        FrameAction frameAction = new FrameAction();
        frameAction.setFrame(buffer);
        while (!fmm.subscribe(frameAction)) {
            buffers.put(frameAction.retrieve());
            i++;
        }
        // One subscriber.
        // Check that all frames have been consumed
        Assert.assertEquals(i, NUM_FRAMES);
        // Release a frame (That will be handed out to the subscriber)
        fmm.release(buffers.take());
        // Check that all frames have been consumed (since the released frame have been handed to the consumer)
        Assert.assertEquals(0, fmm.remaining());
    } catch (Throwable th) {
        th.printStackTrace();
        Assert.fail(th.getMessage());
    } finally {
        Assert.assertNull(cause);
    }
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ActiveProperties(org.apache.asterix.common.config.ActiveProperties) FrameAction(org.apache.asterix.common.memory.FrameAction) ByteBuffer(java.nio.ByteBuffer)

Example 63 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project asterixdb by apache.

the class ConcurrentFramePoolUnitTest method testgetWhileSubscribersExist.

@org.junit.Test
public void testgetWhileSubscribersExist() {
    try {
        ActiveProperties afp = Mockito.mock(ActiveProperties.class);
        Mockito.when(afp.getMemoryComponentGlobalBudget()).thenReturn(FEED_MEM_BUDGET);
        ConcurrentFramePool fmm = new ConcurrentFramePool("TestNode", afp.getMemoryComponentGlobalBudget(), DEFAULT_FRAME_SIZE);
        int i = 0;
        ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_FRAME_SIZE);
        LinkedBlockingDeque<ByteBuffer> buffers = new LinkedBlockingDeque<>();
        FrameAction frameAction = new FrameAction();
        frameAction.setFrame(buffer);
        while (!fmm.subscribe(frameAction)) {
            buffers.put(frameAction.retrieve());
            i++;
        }
        // One subscriber.
        // Check that all frames have been consumed
        Assert.assertEquals(i, NUM_FRAMES);
        // Release a frame (That will be handed out to the subscriber)
        fmm.release(buffers.take());
        // Check that all frames have been consumed (since the released frame have been handed to the consumer)
        Assert.assertEquals(fmm.remaining(), 0);
        buffers.put(frameAction.retrieve());
        // Create another subscriber that takes frames of double the size
        ByteBuffer bufferTimes2 = ByteBuffer.allocate(DEFAULT_FRAME_SIZE * 2);
        LinkedBlockingDeque<ByteBuffer> buffersTimes2 = new LinkedBlockingDeque<>();
        FrameAction frameActionTimes2 = new FrameAction();
        frameActionTimes2.setFrame(bufferTimes2);
        Assert.assertEquals(true, fmm.subscribe(frameActionTimes2));
        // release a small one
        fmm.release(buffers.take());
        Assert.assertEquals(fmm.remaining(), 1);
        // Check that a small get fails
        Assert.assertEquals(null, fmm.get());
        // release another small one
        fmm.release(buffers.take());
        // Check that no small frames exists in the pool since subscriber request was satisfied
        Assert.assertEquals(fmm.remaining(), 0);
        buffersTimes2.add(frameActionTimes2.retrieve());
        fmm.release(buffers);
        fmm.release(bufferTimes2);
        Assert.assertEquals(fmm.remaining(), NUM_FRAMES);
    } catch (Throwable th) {
        th.printStackTrace();
        Assert.fail(th.getMessage());
    } finally {
        Assert.assertNull(cause);
    }
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ActiveProperties(org.apache.asterix.common.config.ActiveProperties) FrameAction(org.apache.asterix.common.memory.FrameAction) ByteBuffer(java.nio.ByteBuffer)

Example 64 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project jackrabbit-oak by apache.

the class ManagementOperationTest method running.

@Test
public void running() throws InterruptedException {
    final LinkedBlockingDeque<Thread> thread = new LinkedBlockingDeque<Thread>(1);
    ManagementOperation<Void> op = newManagementOperation("test", new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            thread.add(currentThread());
            sleep(100000);
            return null;
        }
    });
    executor.execute(op);
    Status status = op.getStatus();
    assertEquals(op.getId(), status.getId());
    assertEquals(RUNNING, status.getCode());
    thread.poll(5, SECONDS).interrupt();
    try {
        op.get();
        fail("Expected InterruptedException");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof InterruptedException);
    }
    assertTrue(op.isDone());
    status = op.getStatus();
    assertEquals(op.getId(), status.getId());
    assertEquals(FAILED, status.getCode());
    assertTrue(status.getMessage().contains("test failed: "));
}
Also used : Status(org.apache.jackrabbit.oak.commons.jmx.ManagementOperation.Status) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Thread.currentThread(java.lang.Thread.currentThread) Test(org.junit.Test)

Example 65 with LinkedBlockingDeque

use of java.util.concurrent.LinkedBlockingDeque in project ddf by codice.

the class CswQueryResponseTransformer method init.

public void init() {
    int numThreads = Runtime.getRuntime().availableProcessors();
    LOGGER.debug(QUERY_POOL_NAME + " size: {}", numThreads);
    /*
            - when first two args the same, get fixed size thread pool.
            - 3rd arg, keepAliveTime, ignored when !allowsCoreThreadTimeOut (the default); thus pass zero.
            - fixed (and arbitrarily) size blocking queue.
            - CswThreadFactory gives pool threads a name to ease debug.
            - tried arbitrarily large numThreads/queue-size, but did not see performance gain.
            - big queue + small pool minimizes CPU usage, OS resources, and context-switching overhead,
              but *can* lead to artificially low throughput.
            - todo: externalize config to support runtime tuning.
        */
    queryExecutor = new ThreadPoolExecutor(numThreads, numThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(BLOCKING_Q_INITIAL_SIZE), new CswThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
    queryExecutor.prestartAllCoreThreads();
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)119 Test (org.junit.Test)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 NoSuchElementException (java.util.NoSuchElementException)8 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)7 IOException (java.io.IOException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 ExecutorService (java.util.concurrent.ExecutorService)5 BlockingDeque (java.util.concurrent.BlockingDeque)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)2 AmqpsMessage (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage)2 AmqpsTransport (com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2