use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.
the class TestStandardProcessScheduler method validateEnabledServiceCanOnlyBeDisabledOnce.
/**
* Validates the atomic nature of ControllerServiceNode.disable() method
* which must only trigger @OnDisabled once, regardless of how many threads
* may have a reference to the underlying ProcessScheduler and
* ControllerServiceNode.
*/
@Test
public void validateEnabledServiceCanOnlyBeDisabledOnce() throws Exception {
final StandardProcessScheduler scheduler = createScheduler();
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
final ControllerServiceNode serviceNode = provider.createControllerService(SimpleTestService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
final SimpleTestService ts = (SimpleTestService) serviceNode.getControllerServiceImplementation();
scheduler.enableControllerService(serviceNode);
assertTrue(serviceNode.isActive());
final ExecutorService executor = Executors.newCachedThreadPool();
final AtomicBoolean asyncFailed = new AtomicBoolean();
for (int i = 0; i < 1000; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
scheduler.disableControllerService(serviceNode);
assertFalse(serviceNode.isActive());
} catch (final Exception e) {
e.printStackTrace();
asyncFailed.set(true);
}
}
});
}
// need to sleep a while since we are emulating async invocations on
// method that is also internally async
Thread.sleep(500);
executor.shutdown();
assertFalse(asyncFailed.get());
assertEquals(1, ts.disableInvocationCount());
}
use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.
the class TestStandardProcessScheduler method validateServiceEnablementLogicHappensOnlyOnce.
/**
* Validates the atomic nature of ControllerServiceNode.enable() method
* which must only trigger @OnEnabled once, regardless of how many threads
* may have a reference to the underlying ProcessScheduler and
* ControllerServiceNode.
*/
@Test
public void validateServiceEnablementLogicHappensOnlyOnce() throws Exception {
final StandardProcessScheduler scheduler = createScheduler();
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
final ControllerServiceNode serviceNode = provider.createControllerService(SimpleTestService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
assertFalse(serviceNode.isActive());
final SimpleTestService ts = (SimpleTestService) serviceNode.getControllerServiceImplementation();
final ExecutorService executor = Executors.newCachedThreadPool();
final AtomicBoolean asyncFailed = new AtomicBoolean();
for (int i = 0; i < 1000; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
scheduler.enableControllerService(serviceNode);
assertTrue(serviceNode.isActive());
} catch (final Exception e) {
e.printStackTrace();
asyncFailed.set(true);
}
}
});
}
// need to sleep a while since we are emulating async invocations on
// method that is also internally async
Thread.sleep(500);
executor.shutdown();
assertFalse(asyncFailed.get());
assertEquals(1, ts.enableInvocationCount());
}
use of org.apache.nifi.controller.service.StandardControllerServiceProvider in project nifi by apache.
the class TestStandardProcessScheduler method validateEnabledDisableMultiThread.
/**
* Validates that in multi threaded environment enabling service can still
* be disabled. This test is set up in such way that disabling of the
* service could be initiated by both disable and enable methods. In other
* words it tests two conditions in
* {@link StandardControllerServiceNode#disable(java.util.concurrent.ScheduledExecutorService, Heartbeater)}
* where the disabling of the service can be initiated right there (if
* ENABLED), or if service is still enabling its disabling will be deferred
* to the logic in
* {@link StandardControllerServiceNode#enable(java.util.concurrent.ScheduledExecutorService, long, Heartbeater)}
* IN any even the resulting state of the service is DISABLED
*/
@Test
@Ignore
public void validateEnabledDisableMultiThread() throws Exception {
final StandardProcessScheduler scheduler = createScheduler();
final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
final ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 200; i++) {
final ControllerServiceNode serviceNode = provider.createControllerService(RandomShortDelayEnablingService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
executor.execute(new Runnable() {
@Override
public void run() {
scheduler.enableControllerService(serviceNode);
}
});
// ensure that enable gets initiated before disable
Thread.sleep(10);
executor.execute(new Runnable() {
@Override
public void run() {
scheduler.disableControllerService(serviceNode);
}
});
Thread.sleep(100);
assertFalse(serviceNode.isActive());
assertTrue(serviceNode.getState() == ControllerServiceState.DISABLED);
}
// need to sleep a while since we are emulating async invocations on
// method that is also internally async
Thread.sleep(500);
executor.shutdown();
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
}
Aggregations