use of java.util.concurrent.LinkedBlockingDeque in project hadoop by apache.
the class TestCodecPool method testMultiThreadedDecompressorPool.
@Test(timeout = 1000)
public void testMultiThreadedDecompressorPool() throws InterruptedException {
final int iterations = 4;
ExecutorService threadpool = Executors.newFixedThreadPool(3);
final LinkedBlockingDeque<Decompressor> queue = new LinkedBlockingDeque<Decompressor>(2 * iterations);
Callable<Boolean> consumer = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Decompressor dc = queue.take();
CodecPool.returnDecompressor(dc);
return dc != null;
}
};
Callable<Boolean> producer = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Decompressor c = CodecPool.getDecompressor(codec);
queue.put(c);
return c != null;
}
};
for (int i = 0; i < iterations; i++) {
threadpool.submit(consumer);
threadpool.submit(producer);
}
// wait for completion
threadpool.shutdown();
threadpool.awaitTermination(1000, TimeUnit.SECONDS);
assertEquals(LEASE_COUNT_ERR, 0, CodecPool.getLeasedDecompressorsCount(codec));
}
use of java.util.concurrent.LinkedBlockingDeque in project cucumber-jvm by cucumber.
the class URLOutputStreamTest method can_http_put.
@Test
public void can_http_put() throws IOException, ExecutionException, InterruptedException {
final BlockingQueue<String> data = new LinkedBlockingDeque<String>();
Rest r = new Rest(webbit);
r.PUT("/.cucumber/stepdefs.json", new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest req, HttpResponse res, HttpControl ctl) throws Exception {
data.offer(req.body());
res.end();
}
});
Writer w = new UTF8OutputStreamWriter(new URLOutputStream(new URL(Utils.toURL("http://localhost:9873/.cucumber"), "stepdefs.json")));
w.write("Hellesøy");
w.flush();
w.close();
assertEquals("Hellesøy", data.poll(1000, TimeUnit.MILLISECONDS));
}
use of java.util.concurrent.LinkedBlockingDeque in project netty by netty.
the class ReplayingDecoderTest method testFireChannelReadCompleteOnInactive.
@Test
public void testFireChannelReadCompleteOnInactive() throws InterruptedException {
final BlockingQueue<Integer> queue = new LinkedBlockingDeque<Integer>();
final ByteBuf buf = Unpooled.buffer().writeBytes(new byte[] { 'a', 'b' });
EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder<Integer>() {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
int readable = in.readableBytes();
assertTrue(readable > 0);
in.skipBytes(readable);
out.add("data");
}
@Override
protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
assertFalse(in.isReadable());
out.add("data");
}
}, new ChannelInboundHandlerAdapter() {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
queue.add(3);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
queue.add(1);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
if (!ctx.channel().isActive()) {
queue.add(2);
}
}
});
assertFalse(channel.writeInbound(buf));
channel.finish();
assertEquals(1, (int) queue.take());
assertEquals(1, (int) queue.take());
assertEquals(2, (int) queue.take());
assertEquals(3, (int) queue.take());
assertTrue(queue.isEmpty());
}
use of java.util.concurrent.LinkedBlockingDeque in project azure-iot-sdk-java by Azure.
the class AmqpsTransportTest method isEmptyReturnsFalseIfWaitingListIsNotEmpty.
// Tests_SRS_AMQPSTRANSPORT_15_035: [The function shall return true if the waiting list,
// in progress list and callback list are all empty, and false otherwise.]
@Test
public void isEmptyReturnsFalseIfWaitingListIsNotEmpty() throws IOException {
AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
Queue<IotHubOutboundPacket> waitingMessages = new LinkedBlockingDeque<>();
waitingMessages.add(new IotHubOutboundPacket(new Message(), mockIotHubEventCallback, new Object()));
Deencapsulation.setField(transport, "waitingMessages", waitingMessages);
Boolean isEmpty = transport.isEmpty();
Assert.assertFalse(isEmpty);
}
use of java.util.concurrent.LinkedBlockingDeque in project undertow by undertow-io.
the class JsrWebsocketExtensionTestCase method testLongTextMessage.
@Test
public void testLongTextMessage() throws Exception {
final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
List<WebSocketExtension> extensionsList = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensionsList);
Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
extensionHandshakes.add(new PerMessageDeflateHandshake(true));
final WebSocketChannel clientChannel = WebSocketClient.connect(DefaultServer.getWorker(), null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
final LinkedBlockingDeque<String> resultQueue = new LinkedBlockingDeque<>();
clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
// WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes.");
resultQueue.addLast(data);
}
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
message.getData().close();
WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
WebSocketLogger.ROOT_LOGGER.info("onError");
super.onError(channel, error);
error.printStackTrace();
resultQueue.add("FAILED " + error);
}
});
clientChannel.resumeReceives();
int LONG_MSG = 125 * 1024;
StringBuilder longMsg = new StringBuilder(LONG_MSG);
for (int i = 0; i < LONG_MSG; i++) {
longMsg.append(Integer.toString(i).charAt(0));
}
String message = longMsg.toString();
for (int j = 0; j < MSG_COUNT; ++j) {
WebSockets.sendTextBlocking(message, clientChannel);
String res = resultQueue.poll(3, TimeUnit.SECONDS);
Assert.assertEquals(message, res);
}
clientChannel.sendClose();
}
Aggregations