Search in sources :

Example 21 with Reference

use of org.mule.runtime.api.util.Reference in project mule by mulesoft.

the class DefaultExtensionModelFactoryTestCase method blockingExecutionTypes.

@Test
public void blockingExecutionTypes() {
    final List<String> nonBlockingOperations = Arrays.asList("killMany", "executeAnything", "alwaysFailsWrapper", "getChain", "exceptionOnCallbacks", "neverFailsWrapper", "payloadModifier");
    ExtensionModel extensionModel = createExtension(HeisenbergExtension.class);
    Reference<Boolean> cpuIntensive = new Reference<>(false);
    Reference<Boolean> blocking = new Reference<>(false);
    new IdempotentExtensionWalker() {

        @Override
        protected void onOperation(OperationModel operation) {
            String operationName = operation.getName();
            assertThat(operation.isBlocking(), is(!nonBlockingOperations.contains(operationName)));
            if (operationName.equals("approve")) {
                assertThat(operation.getExecutionType(), is(CPU_INTENSIVE));
                cpuIntensive.set(true);
            } else if (operation.requiresConnection()) {
                assertThat(operation.getExecutionType(), is(BLOCKING));
                blocking.set(true);
            } else {
                assertThat(operation.getExecutionType(), is(CPU_LITE));
            }
        }
    }.walk(extensionModel);
    assertThat(cpuIntensive.get(), is(true));
    assertThat(blocking.get(), is(true));
}
Also used : Reference(org.mule.runtime.api.util.Reference) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) IdempotentExtensionWalker(org.mule.runtime.api.meta.model.util.IdempotentExtensionWalker) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 22 with Reference

use of org.mule.runtime.api.util.Reference in project mule by mulesoft.

the class ProcessorChainBenchmark method stream.

@Benchmark
public CountDownLatch stream() throws MuleException, InterruptedException {
    CountDownLatch latch = new CountDownLatch(STREAM_SIZE);
    Reference<FluxSink<CoreEvent>> sinkReference = new Reference<>();
    FluxProcessor.create(sinkReference::set).transform(chain).doOnNext(event -> latch.countDown()).subscribe();
    for (int i = 0; i < STREAM_SIZE; i++) {
        sinkReference.get().next(event);
    }
    sinkReference.get().complete();
    latch.await();
    return latch;
}
Also used : FluxSink(reactor.core.publisher.FluxSink) Setup(org.openjdk.jmh.annotations.Setup) DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) Measurement(org.openjdk.jmh.annotations.Measurement) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) FluxSink(reactor.core.publisher.FluxSink) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) FluxProcessor(reactor.core.publisher.FluxProcessor) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Message.of(org.mule.runtime.api.message.Message.of) Warmup(org.openjdk.jmh.annotations.Warmup) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Benchmark(org.openjdk.jmh.annotations.Benchmark) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MuleException(org.mule.runtime.api.exception.MuleException) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) Reference(org.mule.runtime.api.util.Reference) Reference(org.mule.runtime.api.util.Reference) CountDownLatch(java.util.concurrent.CountDownLatch) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 23 with Reference

use of org.mule.runtime.api.util.Reference in project mule by mulesoft.

the class RoutersExecutionTestCase method concurrentRouterExecution.

/**
 * Executes the same flow concurrently to check that no race condition exists because
 * two different instances of Chain are being used
 */
@Test
public void concurrentRouterExecution() throws Exception {
    executor = newFixedThreadPool(2);
    final Latch beginLatch = new Latch();
    final CountDownLatch assertLatch = new CountDownLatch(2);
    final Consumer<Reference<CoreEvent>> runner = reference -> {
        try {
            beginLatch.await(10000, MILLISECONDS);
            reference.set(flowRunner("singleRouteRouter").withPayload("CustomPayload").run());
            assertLatch.countDown();
        } catch (Exception e) {
            fail(e.getMessage());
        }
    };
    final Reference<CoreEvent> first = new Reference<>();
    final Reference<CoreEvent> second = new Reference<>();
    executor.submit(() -> runner.accept(first));
    executor.submit(() -> runner.accept(second));
    beginLatch.release();
    assertLatch.await(10000, MILLISECONDS);
    CoreEvent firstResult = first.get();
    assertThat(firstResult, is(notNullValue()));
    CoreEvent secondResult = second.get();
    assertThat(secondResult, is(notNullValue()));
    assertThat(secondResult, is(not(sameInstance(firstResult))));
    assertThat(firstResult.getMessage().getPayload().getValue(), is("CustomPayload"));
    assertThat(secondResult.getMessage().getPayload().getValue(), is("CustomPayload"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AbstractExtensionFunctionalTestCase(org.mule.test.module.extension.AbstractExtensionFunctionalTestCase) Ricin(org.mule.test.heisenberg.extension.model.Ricin) Event(org.mule.runtime.api.event.Event) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) ExecutorService(java.util.concurrent.ExecutorService) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) IsNot.not(org.hamcrest.core.IsNot.not) SystemProperty(org.mule.tck.junit4.rule.SystemProperty) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Latch(org.mule.runtime.api.util.concurrent.Latch) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) Rule(org.junit.Rule) Reference(org.mule.runtime.api.util.Reference) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Reference(org.mule.runtime.api.util.Reference) CountDownLatch(java.util.concurrent.CountDownLatch) Latch(org.mule.runtime.api.util.concurrent.Latch) CountDownLatch(java.util.concurrent.CountDownLatch) ExpectedException(org.junit.rules.ExpectedException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) Test(org.junit.Test)

Example 24 with Reference

use of org.mule.runtime.api.util.Reference in project mule by mulesoft.

the class MetadataMediator method getTypedModel.

/**
 * Returns a {@link ComponentModel} with its types resolved.
 *
 * @param inputMetadataDescriptor {@link InputMetadataDescriptor} describes the input parameters of the component
 * @param outputMetadataDescriptor {@link OutputMetadataDescriptor} describes the component output
 * @return model with its types resolved by the metadata resolution process
 */
private T getTypedModel(InputMetadataDescriptor inputMetadataDescriptor, OutputMetadataDescriptor outputMetadataDescriptor) {
    Reference<T> typedModel = new Reference<>();
    component.accept(new ComponentModelVisitor() {

        @Override
        public void visit(ConstructModel constructModel) {
            typedModel.set((T) new ImmutableConstructModel(constructModel.getName(), constructModel.getDescription(), resolveParameterGroupModelType(constructModel.getParameterGroupModels(), inputMetadataDescriptor.getAllParameters()), constructModel.getNestedComponents(), constructModel.allowsTopLevelDeclaration(), constructModel.getDisplayModel().orElse(null), constructModel.getErrorModels(), constructModel.getStereotype(), constructModel.getModelProperties()));
        }

        @Override
        public void visit(OperationModel operationModel) {
            OutputModel typedOutputModel = resolveOutputModelType(operationModel.getOutput(), outputMetadataDescriptor.getPayloadMetadata());
            OutputModel typedAttributesModel = resolveOutputModelType(operationModel.getOutputAttributes(), outputMetadataDescriptor.getAttributesMetadata());
            typedModel.set((T) new ImmutableOperationModel(operationModel.getName(), operationModel.getDescription(), resolveParameterGroupModelType(operationModel.getParameterGroupModels(), inputMetadataDescriptor.getAllParameters()), operationModel.getNestedComponents(), typedOutputModel, typedAttributesModel, operationModel.isBlocking(), operationModel.getExecutionType(), operationModel.requiresConnection(), operationModel.isTransactional(), operationModel.supportsStreaming(), operationModel.getDisplayModel().orElse(null), operationModel.getErrorModels(), operationModel.getStereotype(), operationModel.getModelProperties(), operationModel.getNotificationModels()));
        }

        @Override
        public void visit(SourceModel sourceModel) {
            OutputModel typedOutputModel = resolveOutputModelType(sourceModel.getOutput(), outputMetadataDescriptor.getPayloadMetadata());
            OutputModel typedAttributesModel = resolveOutputModelType(sourceModel.getOutputAttributes(), outputMetadataDescriptor.getAttributesMetadata());
            typedModel.set((T) new ImmutableSourceModel(sourceModel.getName(), sourceModel.getDescription(), sourceModel.hasResponse(), true, resolveParameterGroupModelType(sourceModel.getParameterGroupModels(), inputMetadataDescriptor.getAllParameters()), sourceModel.getNestedComponents(), typedOutputModel, typedAttributesModel, resolveSourceCallbackType(sourceModel.getSuccessCallback(), inputMetadataDescriptor.getAllParameters()), resolveSourceCallbackType(sourceModel.getErrorCallback(), inputMetadataDescriptor.getAllParameters()), resolveSourceCallbackType(sourceModel.getTerminateCallback(), inputMetadataDescriptor.getAllParameters()), sourceModel.requiresConnection(), sourceModel.isTransactional(), sourceModel.supportsStreaming(), sourceModel.getDisplayModel().orElse(null), sourceModel.getStereotype(), sourceModel.getErrorModels(), sourceModel.getModelProperties(), sourceModel.getNotificationModels()));
        }
    });
    return typedModel.get();
}
Also used : SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) ImmutableSourceModel(org.mule.runtime.extension.api.model.source.ImmutableSourceModel) ConstructModel(org.mule.runtime.api.meta.model.construct.ConstructModel) ImmutableConstructModel(org.mule.runtime.extension.api.model.construct.ImmutableConstructModel) ImmutableOperationModel(org.mule.runtime.extension.api.model.operation.ImmutableOperationModel) ImmutableSourceModel(org.mule.runtime.extension.api.model.source.ImmutableSourceModel) Reference(org.mule.runtime.api.util.Reference) ComponentModelVisitor(org.mule.runtime.api.meta.model.ComponentModelVisitor) ImmutableConstructModel(org.mule.runtime.extension.api.model.construct.ImmutableConstructModel) ImmutableOutputModel(org.mule.runtime.extension.api.model.ImmutableOutputModel) OutputModel(org.mule.runtime.api.meta.model.OutputModel) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) ImmutableOperationModel(org.mule.runtime.extension.api.model.operation.ImmutableOperationModel)

Example 25 with Reference

use of org.mule.runtime.api.util.Reference in project mule by mulesoft.

the class ProcessorChainExecutorTestCase method testDoProcessOnErrorGenericException.

@Test
public void testDoProcessOnErrorGenericException() throws InterruptedException {
    when(chain.apply(any())).thenReturn(error(new RuntimeException()));
    ImmutableProcessorChainExecutor chainExecutor = new ImmutableProcessorChainExecutor(coreEvent, chain);
    AtomicInteger successCalls = new AtomicInteger(0);
    AtomicInteger errorCalls = new AtomicInteger(0);
    Reference<Event> errorEvent = new Reference<>();
    doProcessAndWait(chainExecutor, r -> successCalls.incrementAndGet(), (t, r) -> {
        errorCalls.incrementAndGet();
        errorEvent.set(((EventedResult) r).getEvent());
    });
    assertThat(successCalls.get(), is(0));
    assertThat(errorCalls.get(), is(1));
    assertThat(errorEvent.get().getMessage().getPayload().getValue(), is(TEST_PAYLOAD));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Reference(org.mule.runtime.api.util.Reference) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Test(org.junit.Test)

Aggregations

Reference (org.mule.runtime.api.util.Reference)29 ObjectType (org.mule.metadata.api.model.ObjectType)12 List (java.util.List)11 MetadataType (org.mule.metadata.api.model.MetadataType)11 Optional (java.util.Optional)10 ArrayType (org.mule.metadata.api.model.ArrayType)8 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)8 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)8 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)8 Map (java.util.Map)7 ClassTypeLoader (org.mule.metadata.api.ClassTypeLoader)7 ConnectionProviderModel (org.mule.runtime.api.meta.model.connection.ConnectionProviderModel)7 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)7 ComponentModel (org.mule.runtime.api.meta.model.ComponentModel)6 ConfigurationModel (org.mule.runtime.api.meta.model.config.ConfigurationModel)6 ConstructModel (org.mule.runtime.api.meta.model.construct.ConstructModel)6 String.format (java.lang.String.format)5 Optional.empty (java.util.Optional.empty)5 Collectors.toList (java.util.stream.Collectors.toList)5 ExtensionWalker (org.mule.runtime.api.meta.model.util.ExtensionWalker)5