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());
}
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());
}
}
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());
}
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());
}
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());
}
}
Aggregations