Search in sources :

Example 91 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionSendPartRequestThenFailedAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionSendPartRequestThenFailedAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
        req.headers().set(HttpHeaderNames.CONTENT_LENGTH, 100);
        final ConnectableObservable<HttpObject> errorOfEnd = Observable.<HttpObject>error(new RuntimeException("test error")).publish();
        final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.concat(Observable.<HttpObject>just(req), errorOfEnd), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                getresp.subscribe(subscriber);
                // server side recv req
                final HttpTrade trade = trades.take();
                assertTrue(trade.isActive());
                // fire error
                errorOfEnd.connect();
                subscriber.awaitTerminalEvent();
                subscriber.assertError(RuntimeException.class);
                subscriber.assertNoValues();
                TerminateAware.Util.awaitTerminated(trade);
                assertTrue(!trade.isActive());
            }
        }, new Action1<WriteCtrl>() {

            @Override
            public void call(final WriteCtrl writeCtrl) {
                writeCtrl.setFlushPerWrite(true);
            }
        }).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        assertNotSame(ch1, ch2);
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) WriteCtrl(org.jocean.http.WriteCtrl) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) LocalAddress(io.netty.channel.local.LocalAddress) Channel(io.netty.channel.Channel) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 92 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionClientCanceledAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionClientCanceledAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                final Subscription subscription = getresp.subscribe(subscriber);
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv request from client side
                trade.inbound().doOnNext(DISPOSE_EACH).toCompletable().await();
                // server not send response, and client cancel this interaction
                subscription.unsubscribe();
                TerminateAware.Util.awaitTerminated(trade);
                TerminateAware.Util.awaitTerminated(initiator);
                assertTrue(!initiator.isActive());
                subscriber.assertNoTerminalEvent();
                subscriber.assertNoValues();
            }
        });
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TestSubscriber(rx.observers.TestSubscriber) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 93 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project jocean-http by isdom.

the class Nettys4Test method createLocalConnection4Http.

public static Pair<Channel, Channel> createLocalConnection4Http(final String addr) throws Exception {
    final BlockingQueue<Channel> serverChannels = new ArrayBlockingQueue<Channel>(1);
    final Bootstrap clientbootstrap = new Bootstrap().group(Nettys4Test.EVENTLOOP4CLIENT).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            Nettys.applyHandler(ch.pipeline(), HttpHandlers.LOGGING);
            Nettys.applyHandler(ch.pipeline(), HttpHandlers.HTTPCLIENT);
        }
    }).remoteAddress(new LocalAddress(addr));
    final Channel acceptorChannel = new ServerBootstrap().group(EVENTLOOP4BOSS, EVENTLOOP4SERVER).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) throws Exception {
            Nettys.applyHandler(ch.pipeline(), HttpHandlers.LOGGING);
            Nettys.applyHandler(ch.pipeline(), HttpHandlers.HTTPSERVER);
            serverChannels.offer(ch);
        }
    }).localAddress(new LocalAddress(addr)).bind().sync().channel();
    try {
        final Channel client = clientbootstrap.connect().sync().channel();
        final Channel server = serverChannels.take();
        return Pair.of(client, server);
    } finally {
        acceptorChannel.close().sync();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 94 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project cdap by caskdata.

the class MessageCacheTest method testAddError.

@Test
public void testAddError() throws Exception {
    // Test to verify various error situations are being safeguarded
    final MessageCache<Integer> cache = new MessageCache<>(new IntComparator(), new UnitWeigher<Integer>(), new MessageCache.Limits(5, 7, 10), NOOP_METRICS);
    // 1. Adding out of order should result in error
    try {
        cache.addAll(Arrays.asList(5, 2, 3, 4).iterator());
        Assert.fail("Expected failure for adding out of order");
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 2. Adding entries that are smaller than the largest one in the cache
    cache.addAll(Arrays.asList(5, 6, 7, 8).iterator());
    try {
        cache.addAll(Arrays.asList(1, 2, 3, 4).iterator());
        Assert.fail("Expected failure for adding out of order");
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 3. Adding duplicate entry
    try {
        cache.addAll(Arrays.asList(1, 1).iterator());
        Assert.fail("Expected failure for adding out of order");
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 4. Adding entry that is already exist in the cache
    cache.addAll(Arrays.asList(1, 2).iterator());
    try {
        cache.addAll(Arrays.asList(2, 3).iterator());
    } catch (IllegalArgumentException e) {
        // Expected. The cache should be cleared
        Assert.assertEquals(0, cache.getCurrentWeight());
    }
    // 5. Multiple threads calling addAll at the same time
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch produceLatch = new CountDownLatch(1);
    // Starts the the first thread and block inside the addAll call
    new Thread() {

        @Override
        public void run() {
            cache.addAll(new AbstractIterator<Integer>() {

                private boolean produced;

                @Override
                protected Integer computeNext() {
                    try {
                        barrier.await();
                    } catch (Exception e) {
                    // This shouldn't happen
                    }
                    Uninterruptibles.awaitUninterruptibly(produceLatch);
                    if (!produced) {
                        produced = true;
                        return 1;
                    }
                    return endOfData();
                }
            });
        }
    }.start();
    // Starts the second thread that tries to call addAll after the first thread is blocked inside the addAll call
    final BlockingQueue<Exception> exception = new ArrayBlockingQueue<>(1);
    new Thread() {

        @Override
        public void run() {
            try {
                barrier.await();
            } catch (Exception e) {
            // This shouldn't happen
            }
            try {
                cache.addAll(Arrays.asList(1, 2, 3).iterator());
            } catch (Exception e) {
                exception.add(e);
            }
        }
    }.start();
    Exception e = exception.poll(10, TimeUnit.SECONDS);
    Assert.assertNotNull(e);
    Assert.assertTrue(e instanceof ConcurrentModificationException);
    // Unblock the first thread
    produceLatch.countDown();
    final MessageFilter<Integer> filter = MessageFilter.alwaysAccept();
    Tasks.waitFor(Collections.singletonList(1), new Callable<List<Integer>>() {

        @Override
        public List<Integer> call() throws Exception {
            try (MessageCache.Scanner<Integer> scanner = cache.scan(0, true, 10, filter)) {
                return Lists.newArrayList(scanner);
            }
        }
    }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentModificationException(java.util.ConcurrentModificationException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) AbstractIterator(com.google.common.collect.AbstractIterator) Test(org.junit.Test)

Example 95 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project spring_boot by hryou0922.

the class RpcClient method execute.

public static void execute(String host, String userName, String password, String message) {
    // 配置连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    // 需要在管理后台增加一个hry帐号
    factory.setUsername(userName);
    factory.setPassword(password);
    Connection connection = null;
    Channel channel = null;
    try {
        // 建立TCP连接
        connection = factory.newConnection();
        // 在TCP连接的基础上创建通道
        channel = connection.createChannel();
        // 定义临时队列,并返回生成的队列名称
        String replyQueueName = channel.queueDeclare().getQueue();
        // 唯一标志本次请求
        String corrId = UUID.randomUUID().toString();
        // 生成发送消息的属性
        AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().correlationId(// 唯一标志本次请求
        corrId).replyTo(// 设置回调队列
        replyQueueName).build();
        // 发送消息,发送到默认交换机
        channel.basicPublish("", RPC_QUEUE_NAME, props, message.getBytes("UTF-8"));
        System.out.println(" [RpcClient] Requesting : " + message);
        // 阻塞队列,用于存储回调结果
        final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1);
        // 定义消息的回退方法
        channel.basicConsume(replyQueueName, true, new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                if (properties.getCorrelationId().equals(corrId)) {
                    response.offer(new String(body, "UTF-8"));
                }
            }
        });
        // 获取回调的结果
        String result = response.take();
        System.out.println(" [RpcClient] Result:'" + result + "'");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            // 空值判断,为了代码简洁略
            channel.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) AMQP(com.rabbitmq.client.AMQP)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)440 Test (org.junit.Test)158 ArrayList (java.util.ArrayList)75 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)74 IOException (java.io.IOException)66 CountDownLatch (java.util.concurrent.CountDownLatch)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 BlockingQueue (java.util.concurrent.BlockingQueue)34 ExecutorService (java.util.concurrent.ExecutorService)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 List (java.util.List)29 LocalAddress (io.netty.channel.local.LocalAddress)27 HashMap (java.util.HashMap)25 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)25 Subscription (rx.Subscription)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)23 File (java.io.File)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 LinkedList (java.util.LinkedList)21