Search in sources :

Example 6 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project camel by apache.

the class MockEndpointTest method testReporter.

public void testReporter() throws Exception {
    final AtomicBoolean reported = new AtomicBoolean(false);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.setExpectedMessageCount(1);
    mock.setReporter(new Processor() {

        public void process(Exchange exchange) throws Exception {
            reported.set(true);
        }
    });
    template.sendBody("direct:a", "Hello World");
    assertMockEndpointsSatisfied();
    assertNotNull(mock.getReporter());
    assertTrue(reported.get());
}
Also used : Exchange(org.apache.camel.Exchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Processor(org.apache.camel.Processor)

Example 7 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project flink by apache.

the class SchedulerSlotSharingTest method testConcurrentAllocateAndRelease.

@Test
public void testConcurrentAllocateAndRelease() {
    final ExecutorService executor = Executors.newFixedThreadPool(20);
    try {
        for (int run = 0; run < 50; run++) {
            final JobVertexID jid1 = new JobVertexID();
            final JobVertexID jid2 = new JobVertexID();
            final JobVertexID jid3 = new JobVertexID();
            final JobVertexID jid4 = new JobVertexID();
            final SlotSharingGroup sharingGroup = new SlotSharingGroup(jid1, jid2, jid3, jid4);
            final Scheduler scheduler = new Scheduler(TestingUtils.defaultExecutionContext());
            scheduler.newInstanceAvailable(getRandomInstance(4));
            final AtomicInteger enumerator1 = new AtomicInteger();
            final AtomicInteger enumerator2 = new AtomicInteger();
            final AtomicBoolean flag3 = new AtomicBoolean();
            final AtomicInteger enumerator4 = new AtomicInteger();
            final Random rnd = new Random();
            // use atomic boolean as a mutable boolean reference
            final AtomicBoolean failed = new AtomicBoolean(false);
            // use atomic integer as a mutable integer reference
            final AtomicInteger completed = new AtomicInteger();
            final Runnable deploy4 = new Runnable() {

                @Override
                public void run() {
                    try {
                        SimpleSlot slot = scheduler.allocateSlot(new ScheduledUnit(getTestVertex(jid4, enumerator4.getAndIncrement(), 4), sharingGroup), false).get();
                        sleepUninterruptibly(rnd.nextInt(5));
                        slot.releaseSlot();
                        if (completed.incrementAndGet() == 13) {
                            synchronized (completed) {
                                completed.notifyAll();
                            }
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                        failed.set(true);
                    }
                }
            };
            final Runnable deploy3 = new Runnable() {

                @Override
                public void run() {
                    try {
                        if (flag3.compareAndSet(false, true)) {
                            SimpleSlot slot = scheduler.allocateSlot(new ScheduledUnit(getTestVertex(jid3, 0, 1), sharingGroup), false).get();
                            sleepUninterruptibly(5);
                            executor.execute(deploy4);
                            executor.execute(deploy4);
                            executor.execute(deploy4);
                            executor.execute(deploy4);
                            slot.releaseSlot();
                            if (completed.incrementAndGet() == 13) {
                                synchronized (completed) {
                                    completed.notifyAll();
                                }
                            }
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                        failed.set(true);
                    }
                }
            };
            final Runnable deploy2 = new Runnable() {

                @Override
                public void run() {
                    try {
                        SimpleSlot slot = scheduler.allocateSlot(new ScheduledUnit(getTestVertex(jid2, enumerator2.getAndIncrement(), 4), sharingGroup), false).get();
                        // wait a bit till scheduling the successor
                        sleepUninterruptibly(rnd.nextInt(5));
                        executor.execute(deploy3);
                        // wait a bit until release
                        sleepUninterruptibly(rnd.nextInt(5));
                        slot.releaseSlot();
                        if (completed.incrementAndGet() == 13) {
                            synchronized (completed) {
                                completed.notifyAll();
                            }
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                        failed.set(true);
                    }
                }
            };
            final Runnable deploy1 = new Runnable() {

                @Override
                public void run() {
                    try {
                        SimpleSlot slot = scheduler.allocateSlot(new ScheduledUnit(getTestVertex(jid1, enumerator1.getAndIncrement(), 4), sharingGroup), false).get();
                        // wait a bit till scheduling the successor
                        sleepUninterruptibly(rnd.nextInt(5));
                        executor.execute(deploy2);
                        // wait a bit until release
                        sleepUninterruptibly(rnd.nextInt(5));
                        slot.releaseSlot();
                        if (completed.incrementAndGet() == 13) {
                            synchronized (completed) {
                                completed.notifyAll();
                            }
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                        failed.set(true);
                    }
                }
            };
            final Runnable deploy0 = new Runnable() {

                @Override
                public void run() {
                    sleepUninterruptibly(rnd.nextInt(10));
                    executor.execute(deploy1);
                }
            };
            executor.execute(deploy0);
            executor.execute(deploy0);
            executor.execute(deploy0);
            executor.execute(deploy0);
            //noinspection SynchronizationOnLocalVariableOrMethodParameter
            synchronized (completed) {
                while (!failed.get() && completed.get() < 13) {
                    completed.wait(1000);
                }
            }
            assertFalse("Thread failed", failed.get());
            while (scheduler.getNumberOfAvailableSlots() < 4) {
                sleepUninterruptibly(5);
            }
            assertEquals(1, scheduler.getNumberOfAvailableInstances());
            assertEquals(1, scheduler.getNumberOfInstancesWithAvailableSlots());
            assertEquals(4, scheduler.getNumberOfAvailableSlots());
            assertEquals(13, scheduler.getNumberOfUnconstrainedAssignments());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JobVertexID(org.apache.flink.runtime.jobgraph.JobVertexID) SimpleSlot(org.apache.flink.runtime.instance.SimpleSlot) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 8 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project flink by apache.

the class SystemProcessingTimeServiceTest method testExceptionReportingScheduleAtFixedRate.

@Test
public void testExceptionReportingScheduleAtFixedRate() throws InterruptedException {
    final AtomicBoolean exceptionWasThrown = new AtomicBoolean(false);
    final OneShotLatch latch = new OneShotLatch();
    final Object lock = new Object();
    ProcessingTimeService timeServiceProvider = new SystemProcessingTimeService(new AsyncExceptionHandler() {

        @Override
        public void handleAsyncException(String message, Throwable exception) {
            exceptionWasThrown.set(true);
            latch.trigger();
        }
    }, lock);
    timeServiceProvider.scheduleAtFixedRate(new ProcessingTimeCallback() {

        @Override
        public void onProcessingTime(long timestamp) throws Exception {
            throw new Exception("Exception in Timer");
        }
    }, 0L, 100L);
    latch.await();
    assertTrue(exceptionWasThrown.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 9 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project flink by apache.

the class SystemProcessingTimeServiceTest method testExceptionReporting.

@Test
public void testExceptionReporting() throws InterruptedException {
    final AtomicBoolean exceptionWasThrown = new AtomicBoolean(false);
    final OneShotLatch latch = new OneShotLatch();
    final Object lock = new Object();
    ProcessingTimeService timeServiceProvider = new SystemProcessingTimeService(new AsyncExceptionHandler() {

        @Override
        public void handleAsyncException(String message, Throwable exception) {
            exceptionWasThrown.set(true);
            latch.trigger();
        }
    }, lock);
    timeServiceProvider.registerTimer(System.currentTimeMillis(), new ProcessingTimeCallback() {

        @Override
        public void onProcessingTime(long timestamp) throws Exception {
            throw new Exception("Exception in Timer");
        }
    });
    latch.await();
    assertTrue(exceptionWasThrown.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Test(org.junit.Test)

Example 10 with AtomicBoolean

use of java.util.concurrent.atomic.AtomicBoolean in project flink by apache.

the class IOManagerAsyncTest method testExceptionInCallbackRead.

@Test
public void testExceptionInCallbackRead() {
    try {
        final AtomicBoolean handlerCalled = new AtomicBoolean();
        ReadRequest regularRequest = new ReadRequest() {

            @Override
            public void requestDone(IOException ioex) {
                synchronized (handlerCalled) {
                    handlerCalled.set(true);
                    handlerCalled.notifyAll();
                }
            }

            @Override
            public void read() {
            }
        };
        ReadRequest exceptionThrower = new ReadRequest() {

            @Override
            public void requestDone(IOException ioex) {
                throw new RuntimeException();
            }

            @Override
            public void read() {
            }
        };
        RequestQueue<ReadRequest> rq = ioManager.getReadRequestQueue(ioManager.createChannel());
        // queue first an exception thrower, then a regular request.
        // we check that the regular request gets successfully handled
        rq.add(exceptionThrower);
        rq.add(regularRequest);
        synchronized (handlerCalled) {
            while (!handlerCalled.get()) {
                handlerCalled.wait();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2412 Test (org.junit.Test)1002 CountDownLatch (java.util.concurrent.CountDownLatch)394 IOException (java.io.IOException)336 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)301 ArrayList (java.util.ArrayList)214 AtomicReference (java.util.concurrent.atomic.AtomicReference)202 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)108 Test (org.testng.annotations.Test)106 List (java.util.List)98 Ignite (org.apache.ignite.Ignite)98 AtomicLong (java.util.concurrent.atomic.AtomicLong)94 HashMap (java.util.HashMap)93 ExecutorService (java.util.concurrent.ExecutorService)90 Map (java.util.Map)88 ExecutionException (java.util.concurrent.ExecutionException)87 File (java.io.File)68 Random (java.util.Random)68 CyclicBarrier (java.util.concurrent.CyclicBarrier)68 HashSet (java.util.HashSet)63