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));
}
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);
}
}
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);
}
}
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: "));
}
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();
}
Aggregations