Search in sources :

Example 1 with SchedulerService

use of org.mule.runtime.api.scheduler.SchedulerService in project mule by mulesoft.

the class AbstractMuleContextTestCase method recordSchedulersOnInit.

protected static void recordSchedulersOnInit(MuleContext context) {
    if (context != null) {
        final SchedulerService serviceImpl = context.getSchedulerService();
        schedulersOnInit = serviceImpl.getSchedulers();
    } else {
        schedulersOnInit = emptyList();
    }
}
Also used : SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService)

Example 2 with SchedulerService

use of org.mule.runtime.api.scheduler.SchedulerService in project mule by mulesoft.

the class AbstractMuleContextTestCase method verifyAndStopSchedulers.

protected static void verifyAndStopSchedulers() throws MuleException {
    final SchedulerService serviceImpl = muleContext.getSchedulerService();
    Set<String> schedulersOnInitNames = schedulersOnInit.stream().map(s -> s.getName()).collect(toSet());
    try {
        assertThat(muleContext.getSchedulerService().getSchedulers().stream().filter(s -> !schedulersOnInitNames.contains(s.getName())).collect(toList()), empty());
    } finally {
        if (serviceImpl instanceof SimpleUnitTestSupportSchedulerService) {
            stopIfNeeded(serviceImpl);
        }
    }
}
Also used : Message(org.mule.runtime.api.message.Message) MuleContextNotification(org.mule.runtime.core.api.context.notification.MuleContextNotification) Thread.currentThread(java.lang.Thread.currentThread) Assert.assertThat(org.junit.Assert.assertThat) LifecycleUtils.setMuleContextIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.setMuleContextIfNeeded) Map(java.util.Map) After(org.junit.After) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) StringUtils(org.mule.runtime.core.api.util.StringUtils) Collectors.toSet(java.util.stream.Collectors.toSet) AfterClass(org.junit.AfterClass) MuleContextFactory(org.mule.runtime.core.api.context.MuleContextFactory) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) DefaultMuleConfiguration(org.mule.runtime.core.api.config.DefaultMuleConfiguration) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) Set(java.util.Set) FileUtils.newFile(org.mule.runtime.core.api.util.FileUtils.newFile) List(java.util.List) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) TriggerableMessageSource(org.mule.tck.TriggerableMessageSource) Optional(java.util.Optional) DefaultComponentLocation.fromSingleComponent(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent) QName(javax.xml.namespace.QName) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) Mockito.mock(org.mockito.Mockito.mock) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) TestsLogConfigurationHelper.configureLoggingForTest(org.mule.tck.junit4.TestsLogConfigurationHelper.configureLoggingForTest) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) MuleContextNotificationListener(org.mule.runtime.core.api.context.notification.MuleContextNotificationListener) TestsLogConfigurationHelper.clearLoggingConfig(org.mule.tck.junit4.TestsLogConfigurationHelper.clearLoggingConfig) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) FileUtils.deleteTree(org.mule.runtime.core.api.util.FileUtils.deleteTree) ExpressionExecutor(org.mule.runtime.core.internal.el.ExpressionExecutor) ArrayList(java.util.ArrayList) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) MuleContext(org.mule.runtime.core.api.MuleContext) DefaultsConfigurationBuilder(org.mule.runtime.core.internal.config.builders.DefaultsConfigurationBuilder) MuleException(org.mule.runtime.api.exception.MuleException) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) MuleContextUtils.eventBuilder(org.mule.tck.util.MuleContextUtils.eventBuilder) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) JavaObjectSerializer(org.mule.runtime.core.internal.serialization.JavaObjectSerializer) Properties(java.util.Properties) Logger(org.slf4j.Logger) DataType(org.mule.runtime.api.metadata.DataType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TestServicesConfigurationBuilder(org.mule.tck.config.TestServicesConfigurationBuilder) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) APPLE_FLOW(org.mule.tck.MuleTestUtils.APPLE_FLOW) SchedulerView(org.mule.runtime.api.scheduler.SchedulerView) File(java.io.File) MuleContextBuilder(org.mule.runtime.core.api.context.MuleContextBuilder) Collectors.toList(java.util.stream.Collectors.toList) Latch(org.mule.runtime.api.util.concurrent.Latch) HttpService(org.mule.runtime.http.api.HttpService) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) MuleConfiguration(org.mule.runtime.core.api.config.MuleConfiguration) ObjectSerializer(org.mule.runtime.api.serialization.ObjectSerializer) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TemporaryFolder(org.junit.rules.TemporaryFolder) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService)

Example 3 with SchedulerService

use of org.mule.runtime.api.scheduler.SchedulerService in project mule by mulesoft.

the class MuleContextUtils method mockContextWithServices.

/**
 * Creates and configures a mock {@link MuleContext} to return testing services implementations.
 *
 * @return the created {@code muleContext}.
 */
public static MuleContextWithRegistries mockContextWithServices() {
    final MuleContextWithRegistries muleContext = mockMuleContext();
    SchedulerService schedulerService = spy(new SimpleUnitTestSupportSchedulerService());
    when(muleContext.getSchedulerService()).thenReturn(schedulerService);
    ErrorTypeRepository errorTypeRepository = mock(ErrorTypeRepository.class);
    when(muleContext.getErrorTypeRepository()).thenReturn(errorTypeRepository);
    when(errorTypeRepository.getErrorType(any(ComponentIdentifier.class))).thenReturn(of(mock(ErrorType.class)));
    final MuleRegistry registry = muleContext.getRegistry();
    NotificationListenerRegistry notificationListenerRegistry = mock(NotificationListenerRegistry.class);
    ConfigurationProperties configProps = mock(ConfigurationProperties.class);
    when(configProps.resolveBooleanProperty(any())).thenReturn(empty());
    ConfigurationComponentLocator configurationComponentLocator = mock(ConfigurationComponentLocator.class);
    when(configurationComponentLocator.find(any(Location.class))).thenReturn(empty());
    when(configurationComponentLocator.find(any(ComponentIdentifier.class))).thenReturn(emptyList());
    try {
        when(registry.lookupObject(NotificationListenerRegistry.class)).thenReturn(notificationListenerRegistry);
        Map<Class, Object> injectableObjects = new HashMap<>();
        injectableObjects.put(MuleContext.class, muleContext);
        injectableObjects.put(SchedulerService.class, schedulerService);
        injectableObjects.put(ErrorTypeRepository.class, errorTypeRepository);
        injectableObjects.put(ExtendedExpressionManager.class, muleContext.getExpressionManager());
        injectableObjects.put(StreamingManager.class, muleContext.getRegistry().lookupObject(StreamingManager.class));
        injectableObjects.put(ObjectStoreManager.class, muleContext.getRegistry().lookupObject(OBJECT_STORE_MANAGER));
        injectableObjects.put(NotificationDispatcher.class, muleContext.getRegistry().lookupObject(NotificationDispatcher.class));
        injectableObjects.put(NotificationListenerRegistry.class, notificationListenerRegistry);
        injectableObjects.put(ConfigurationComponentLocator.class, configurationComponentLocator);
        injectableObjects.put(ConfigurationProperties.class, configProps);
        // Ensure injection of consistent mock objects
        when(muleContext.getInjector()).thenReturn(new MocksInjector(injectableObjects));
    } catch (RegistrationException e1) {
        throw new MuleRuntimeException(e1);
    }
    return muleContext;
}
Also used : RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) HashMap(java.util.HashMap) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ConfigurationProperties(org.mule.runtime.api.component.ConfigurationProperties) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) Location(org.mule.runtime.api.component.location.Location)

Example 4 with SchedulerService

use of org.mule.runtime.api.scheduler.SchedulerService in project mule by mulesoft.

the class DefaultSchedulerMessageSourceTestCase method disposeScheduler.

@Test
public void disposeScheduler() throws Exception {
    SchedulerService schedulerService = muleContext.getSchedulerService();
    reset(schedulerService);
    AtomicReference<Scheduler> pollScheduler = new AtomicReference<>();
    doAnswer(invocation -> {
        Scheduler scheduler = (Scheduler) invocation.callRealMethod();
        pollScheduler.set(scheduler);
        return scheduler;
    }).when(schedulerService).cpuLightScheduler();
    DefaultSchedulerMessageSource schedulerMessageSource = createMessageSource();
    verify(schedulerService).cpuLightScheduler();
    schedulerMessageSource.start();
    verify(pollScheduler.get()).scheduleAtFixedRate(any(), anyLong(), anyLong(), any());
    schedulerMessageSource.stop();
    schedulerMessageSource.dispose();
    verify(pollScheduler.get()).stop();
}
Also used : SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) DefaultSchedulerMessageSource(org.mule.runtime.core.internal.source.scheduler.DefaultSchedulerMessageSource) Scheduler(org.mule.runtime.api.scheduler.Scheduler) FixedFrequencyScheduler(org.mule.runtime.core.api.source.scheduler.FixedFrequencyScheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with SchedulerService

use of org.mule.runtime.api.scheduler.SchedulerService in project mule by mulesoft.

the class DefaultMuleContext method getSchedulerService.

@Override
public SchedulerService getSchedulerService() {
    if (this.schedulerService == null) {
        try {
            this.schedulerService = this.getRegistry().lookupObject(SchedulerService.class);
            requireNonNull(schedulerService);
        } catch (RegistrationException e) {
            throw new MuleRuntimeException(e);
        }
    }
    return this.schedulerService;
}
Also used : RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Aggregations

SchedulerService (org.mule.runtime.api.scheduler.SchedulerService)8 Test (org.junit.Test)4 MuleException (org.mule.runtime.api.exception.MuleException)3 SimpleUnitTestSupportSchedulerService (org.mule.tck.SimpleUnitTestSupportSchedulerService)3 IOException (java.io.IOException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ThrowableAssert.catchThrowable (org.assertj.core.api.ThrowableAssert.catchThrowable)2 ConfigurationComponentLocator (org.mule.runtime.api.component.location.ConfigurationComponentLocator)2 ConnectionException (org.mule.runtime.api.connection.ConnectionException)2 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 LifecycleException (org.mule.runtime.api.lifecycle.LifecycleException)2 Scheduler (org.mule.runtime.api.scheduler.Scheduler)2 RetryPolicyExhaustedException (org.mule.runtime.core.api.retry.policy.RetryPolicyExhaustedException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 Thread.currentThread (java.lang.Thread.currentThread)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1