Search in sources :

Example 81 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project components by Talend.

the class CouchbaseStreamingConnectionTest method testStartStreaming.

@Test
public void testStartStreaming() throws InterruptedException {
    Mockito.when(client.initializeState(StreamFrom.BEGINNING, StreamTo.NOW)).thenReturn(Completable.complete());
    Mockito.when(client.startStreaming(Mockito.<Short[]>anyVararg())).thenReturn(Completable.complete());
    SessionState sessionState = Mockito.mock(SessionState.class);
    Mockito.when(sessionState.isAtEnd()).thenReturn(false, false, true);
    Mockito.when(client.sessionState()).thenReturn(sessionState);
    BlockingQueue<ByteBuf> resultsQueue = new ArrayBlockingQueue<>(3);
    streamingConnection.startStreaming(resultsQueue);
    Assert.assertTrue(streamingConnection.isStreaming());
    Thread.sleep(2000);
    Mockito.verify(client, Mockito.times(3)).sessionState();
}
Also used : SessionState(com.couchbase.client.dcp.state.SessionState) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ByteBuf(com.couchbase.client.deps.io.netty.buffer.ByteBuf) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 82 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project photon-model by vmware.

the class AWSImageEnumerationAdapterService method allocateExecutor.

/**
 * Creates an executor specific to AWS public images enum, which by default is single threaded
 * with a queue of size 20.
 */
private ExecutorService allocateExecutor() {
    final int corePoolSize = 1;
    // By default returns 1, so effectively we have single threaded executor;
    // otherwise the pool size varies
    final int imagesMaxConcurrentEnums = getImagesMaxConcurrentEnums();
    final long keepAliveTime = 0L;
    final TimeUnit unit = TimeUnit.MILLISECONDS;
    // Use queue with size 20, which is close to AWS regions count
    final BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(20);
    ThreadFactory tFactory = r -> new Thread(r, "/" + getUri() + "/" + Utils.getSystemNowMicrosUtc());
    return new ThreadPoolExecutor(corePoolSize, imagesMaxConcurrentEnums, keepAliveTime, unit, workQueue, tFactory);
}
Also used : Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) AWSClientManagerFactory.returnClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.returnClientManager) Utils(com.vmware.xenon.common.Utils) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) ImageService(com.vmware.photon.controller.model.resources.ImageService) ThreadFactory(java.util.concurrent.ThreadFactory) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AWS_IMAGE_VIRTUALIZATION_TYPE_PARAVIRTUAL(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_PARAVIRTUAL) StatelessService(com.vmware.xenon.common.StatelessService) DiskConfiguration(com.vmware.photon.controller.model.resources.ImageService.ImageState.DiskConfiguration) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) BlockingQueue(java.util.concurrent.BlockingQueue) DeviceType(com.amazonaws.services.ec2.model.DeviceType) VOLUME_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.VOLUME_TYPE) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) Type(java.lang.reflect.Type) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) PhotonModelUtils.waitToComplete(com.vmware.photon.controller.model.resources.util.PhotonModelUtils.waitToComplete) AWS_IMAGE_VIRTUALIZATION_TYPE_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_FILTER) ImageEnumerateRequest(com.vmware.photon.controller.model.adapterapi.ImageEnumerateRequest) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) AWS_IMAGE_VIRTUALIZATION_TYPE_HVM(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_HVM) Image(com.amazonaws.services.ec2.model.Image) Filter(com.amazonaws.services.ec2.model.Filter) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) Iterator(java.util.Iterator) EndpointEnumerationProcess(com.vmware.photon.controller.model.adapters.util.enums.EndpointEnumerationProcess) Operation(com.vmware.xenon.common.Operation) TimeUnit(java.util.concurrent.TimeUnit) ImageEnumerateRequestType(com.vmware.photon.controller.model.adapterapi.ImageEnumerateRequest.ImageEnumerateRequestType) ImageEnumerationTaskState(com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService.ImageEnumerationTaskState) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Collections(java.util.Collections) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ThreadFactory(java.util.concurrent.ThreadFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 83 with ArrayBlockingQueue

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

the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttpReuseChannel.

@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttpReuseChannel() 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 Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).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));
        assertSame(ch1, ch2);
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) LocalAddress(io.netty.channel.local.LocalAddress) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Channel(io.netty.channel.Channel) Subscription(rx.Subscription) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test)

Example 84 with ArrayBlockingQueue

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

the class DefaultHttpClientTestCase method testInitiatorInteractionNo1NotSendNo2SuccessReuseChannelAsHttps.

@Test(timeout = 5000)
public void testInitiatorInteractionNo1NotSendNo2SuccessReuseChannelAsHttps() 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, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.<HttpObject>error(new RuntimeException("test error")), 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);
                subscriber.awaitTerminalEvent();
                subscriber.assertError(RuntimeException.class);
                subscriber.assertNoValues();
            }
        }).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));
        assertSame(ch1, ch2);
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) 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) 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 85 with ArrayBlockingQueue

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

the class DefaultHttpClientTestCase method testInitiatorMultiInteractionSuccessAsHttp.

@Test(timeout = 5000)
public void testInitiatorMultiInteractionSuccessAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    assertEquals(0, allActiveAllocationsCount(allocator));
    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);
    try (final HttpInitiator initiator = client.initiator().remoteAddress(new LocalAddress(addr)).build().toBlocking().single()) {
        {
            final Observable<? extends DisposableWrapper<HttpObject>> cached = initiator.defineInteraction(Observable.just(fullHttpRequest())).cache();
            cached.subscribe();
            // server side recv req
            final HttpTrade trade = trades.take();
            // recv all request
            trade.inbound().toCompletable().await();
            assertEquals(0, allActiveAllocationsCount(allocator));
            final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
            // send back resp
            trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
            // wait for recv all resp at client side
            cached.toCompletable().await();
            svrRespContent.release();
            assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached.compose(RxNettys.message2fullresp(initiator))), CONTENT));
            cached.doOnNext(DISPOSE_EACH).toCompletable().await();
        }
        assertEquals(0, allActiveAllocationsCount(allocator));
        {
            final Observable<? extends DisposableWrapper<HttpObject>> cached = initiator.defineInteraction(Observable.just(fullHttpRequest())).cache();
            final Observable<HttpObject> resp2 = cached.map(DisposableWrapperUtil.<HttpObject>unwrap());
            resp2.subscribe();
            // server side recv req
            final HttpTrade trade = trades.take();
            // recv all request
            trade.inbound().toCompletable().await();
            // assertEquals(0, allActiveAllocationsCount(allocator));
            final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
            // send back resp
            trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
            // wait for recv all resp at client side
            resp2.toCompletable().await();
            svrRespContent.release();
            assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached.compose(RxNettys.message2fullresp(initiator))), CONTENT));
        }
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) ByteBuf(io.netty.buffer.ByteBuf) ConnectableObservable(rx.observables.ConnectableObservable) Observable(rx.Observable) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) HttpObject(io.netty.handler.codec.http.HttpObject) Subscription(rx.Subscription) Test(org.junit.Test)

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