Search in sources :

Example 56 with LinkedBlockingDeque

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));
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ExecutorService(java.util.concurrent.ExecutorService) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 57 with LinkedBlockingDeque

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));
}
Also used : HttpRequest(org.webbitserver.HttpRequest) HttpHandler(org.webbitserver.HttpHandler) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HttpResponse(org.webbitserver.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Rest(org.webbitserver.rest.Rest) HttpControl(org.webbitserver.HttpControl) Writer(java.io.Writer) Test(org.junit.Test)

Example 58 with LinkedBlockingDeque

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());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 59 with LinkedBlockingDeque

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);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) AmqpsMessage(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsMessage) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) Test(org.junit.Test)

Example 60 with LinkedBlockingDeque

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();
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) IOException(java.io.IOException) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) AutobahnAnnotatedEndpoint(io.undertow.websockets.jsr.test.autobahn.AutobahnAnnotatedEndpoint) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) HashSet(java.util.HashSet) Test(org.junit.Test) BinaryEndpointTest(io.undertow.websockets.jsr.test.BinaryEndpointTest)

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