Search in sources :

Example 61 with ArrayDeque

use of java.util.ArrayDeque in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method constructApplicationInfo.

private ApplicationInfo constructApplicationInfo(Boolean setQNameTableNameToNull) throws SQLException {
    final ApplicationInfo appInfo = new ApplicationInfo();
    final List<CompositeDescriptorInfo> valueDescriptors = new ArrayList<>();
    final Deque<Object> currentPath = new ArrayDeque<>();
    _app.descriptor().accept(new HierarchicalVisitorAdapter<Object, Object, RuntimeException>() {

        @Override
        public boolean visitEnter(Object visited) throws RuntimeException {
            if (visited instanceof LayerDescriptor || visited instanceof ModuleDescriptor) {
                currentPath.push(visited);
            }
            if (visited instanceof EntityDescriptor || visited instanceof ValueDescriptor) {
                // TODO filter non-visible descriptors away.
                if (visited instanceof EntityDescriptor) {
                    EntityDescriptor entityDescriptor = (EntityDescriptor) visited;
                    if (entityDescriptor.queryable()) {
                        LOGGER.debug("THIS ONE WORKS: {}", entityDescriptor);
                        appInfo.entityDescriptors.put(first(entityDescriptor.types()).getName(), entityDescriptor);
                    }
                } else {
                    valueDescriptors.add(new CompositeDescriptorInfo((LayerDescriptor) Iterables.first(Iterables.skip(1, currentPath)), (ModuleDescriptor) Iterables.first(currentPath), (CompositeDescriptor) visited));
                }
                return false;
            }
            return true;
        }

        @Override
        public boolean visitLeave(Object visited) {
            if (visited instanceof LayerDescriptor || visited instanceof ModuleDescriptor) {
                currentPath.pop();
            }
            return true;
        }
    });
    for (EntityDescriptor descriptor : appInfo.entityDescriptors.values()) {
        Set<QualifiedName> newQNames = new HashSet<>();
        this.extractPropertyQNames(descriptor, this._state.qNameInfos().get(), newQNames, valueDescriptors, appInfo.enumValues, setQNameTableNameToNull);
        this.extractAssociationQNames(descriptor, this._state.qNameInfos().get(), newQNames, setQNameTableNameToNull);
        this.extractManyAssociationQNames(descriptor, this._state.qNameInfos().get(), newQNames, setQNameTableNameToNull);
        this._state.entityUsedQNames().get().put(descriptor, newQNames);
    }
    appInfo.usedValueComposites.addAll(valueDescriptors);
    return appInfo;
}
Also used : ValueDescriptor(org.qi4j.api.value.ValueDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) ModuleDescriptor(org.qi4j.api.structure.ModuleDescriptor) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) LayerDescriptor(org.qi4j.api.structure.LayerDescriptor) HashSet(java.util.HashSet)

Example 62 with ArrayDeque

use of java.util.ArrayDeque in project RxJava by ReactiveX.

the class QueueDrainHelperTest method postCompleteCancelledAfterOne.

@Test
public void postCompleteCancelledAfterOne() {
    final TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            cancel();
        }
    };
    ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
    AtomicLong state = new AtomicLong();
    BooleanSupplier isCancelled = new BooleanSupplier() {

        @Override
        public boolean getAsBoolean() throws Exception {
            return ts.isCancelled();
        }
    };
    ts.onSubscribe(new BooleanSubscription());
    queue.offer(1);
    state.getAndIncrement();
    QueueDrainHelper.postComplete(ts, queue, state, isCancelled);
    ts.assertValue(1).assertNoErrors().assertNotComplete();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BooleanSupplier(io.reactivex.functions.BooleanSupplier) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 63 with ArrayDeque

use of java.util.ArrayDeque in project RxJava by ReactiveX.

the class QueueDrainHelperTest method postCompleteEmpty.

@Test
public void postCompleteEmpty() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
    AtomicLong state = new AtomicLong();
    BooleanSupplier isCancelled = new BooleanSupplier() {

        @Override
        public boolean getAsBoolean() throws Exception {
            return false;
        }
    };
    ts.onSubscribe(new BooleanSubscription());
    QueueDrainHelper.postComplete(ts, queue, state, isCancelled);
    ts.assertResult();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BooleanSupplier(io.reactivex.functions.BooleanSupplier) ArrayDeque(java.util.ArrayDeque) Test(org.junit.Test)

Example 64 with ArrayDeque

use of java.util.ArrayDeque in project netty by netty.

the class ResourceLeakDetectorTest method testConcurentUsage.

@Test(timeout = 60000)
public void testConcurentUsage() throws Throwable {
    final AtomicBoolean finished = new AtomicBoolean();
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    // With 50 threads issue #6087 is reproducible on every run.
    Thread[] threads = new Thread[50];
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    for (int i = 0; i < threads.length; i++) {
        Thread t = new Thread(new Runnable() {

            Queue<LeakAwareResource> resources = new ArrayDeque<LeakAwareResource>(100);

            @Override
            public void run() {
                try {
                    barrier.await();
                    // Run 10000 times or until the test is marked as finished.
                    for (int b = 0; b < 1000 && !finished.get(); b++) {
                        // Allocate 100 LeakAwareResource per run and close them after it.
                        for (int a = 0; a < 100; a++) {
                            DefaultResource resource = new DefaultResource();
                            ResourceLeakTracker<Resource> leak = DefaultResource.detector.track(resource);
                            LeakAwareResource leakAwareResource = new LeakAwareResource(resource, leak);
                            resources.add(leakAwareResource);
                        }
                        if (closeResources(true)) {
                            finished.set(true);
                        }
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } catch (Throwable e) {
                    error.compareAndSet(null, e);
                } finally {
                    // Just close all resource now without assert it to eliminate more reports.
                    closeResources(false);
                }
            }

            private boolean closeResources(boolean checkClosed) {
                for (; ; ) {
                    LeakAwareResource r = resources.poll();
                    if (r == null) {
                        return false;
                    }
                    boolean closed = r.close();
                    if (checkClosed && !closed) {
                        error.compareAndSet(null, new AssertionError("ResourceLeak.close() returned 'false' but expected 'true'"));
                        return true;
                    }
                }
            }
        });
        threads[i] = t;
        t.start();
    }
    // Just wait until all threads are done.
    for (Thread t : threads) {
        t.join();
    }
    // Check if we had any leak reports in the ResourceLeakDetector itself
    DefaultResource.detector.assertNoErrors();
    assertNoErrors(error);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayDeque(java.util.ArrayDeque) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 65 with ArrayDeque

use of java.util.ArrayDeque in project netty by netty.

the class CombinedChannelDuplexHandlerTest method testOutboundEvents.

@Test
public void testOutboundEvents() {
    final Queue<Event> queue = new ArrayDeque<Event>();
    ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter();
    ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_ADDED);
        }

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_REMOVED);
        }

        @Override
        public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            queue.add(Event.BIND);
        }

        @Override
        public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            queue.add(Event.CONNECT);
        }

        @Override
        public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.DISCONNECT);
        }

        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.CLOSE);
        }

        @Override
        public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.DEREGISTER);
        }

        @Override
        public void read(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.READ);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            queue.add(Event.WRITE);
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.FLUSH);
        }
    };
    CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(inboundHandler, outboundHandler);
    EmbeddedChannel channel = new EmbeddedChannel();
    channel.pipeline().addFirst(handler);
    doOutboundOperations(channel);
    assertEquals(Event.HANDLER_ADDED, queue.poll());
    assertEquals(Event.BIND, queue.poll());
    assertEquals(Event.CONNECT, queue.poll());
    assertEquals(Event.WRITE, queue.poll());
    assertEquals(Event.FLUSH, queue.poll());
    assertEquals(Event.READ, queue.poll());
    assertEquals(Event.CLOSE, queue.poll());
    assertEquals(Event.CLOSE, queue.poll());
    assertEquals(Event.DEREGISTER, queue.poll());
    handler.removeOutboundHandler();
    assertEquals(Event.HANDLER_REMOVED, queue.poll());
    // These should not be handled by the inboundHandler anymore as it was removed before
    doOutboundOperations(channel);
    // Should have not received any more events as it was removed before via removeInboundHandler()
    assertTrue(queue.isEmpty());
    assertTrue(channel.finish());
    assertTrue(queue.isEmpty());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ArrayDeque(java.util.ArrayDeque) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

ArrayDeque (java.util.ArrayDeque)217 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)36 IOException (java.io.IOException)27 HashMap (java.util.HashMap)23 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)17 Deque (java.util.Deque)11 Iterator (java.util.Iterator)10 NoSuchElementException (java.util.NoSuchElementException)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 File (java.io.File)7 Path (java.nio.file.Path)7 Random (java.util.Random)7 ByteBuffer (java.nio.ByteBuffer)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpFields (org.eclipse.jetty.http.HttpFields)5 Name (com.github.anba.es6draft.ast.scope.Name)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4