Search in sources :

Example 1 with Matchers

use of org.hamcrest.Matchers in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerViaConsumerEndpointFactoryBean.

@Test
public void testReactiveStreamsConsumerViaConsumerEndpointFactoryBean() throws Exception {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(3);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    ConsumerEndpointFactoryBean endpointFactoryBean = new ConsumerEndpointFactoryBean();
    endpointFactoryBean.setBeanFactory(mock(ConfigurableBeanFactory.class));
    endpointFactoryBean.setInputChannel(testChannel);
    endpointFactoryBean.setHandler(messageHandler);
    endpointFactoryBean.setBeanName("reactiveConsumer");
    endpointFactoryBean.afterPropertiesSet();
    endpointFactoryBean.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    endpointFactoryBean.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    endpointFactoryBean.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result.size(), equalTo(3));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 2 with Matchers

use of org.hamcrest.Matchers in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerFluxMessageChannel.

@Test
public void testReactiveStreamsConsumerFluxMessageChannel() throws InterruptedException {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(2);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    MessageHandler testSubscriber = new MethodInvokingMessageHandler(messageHandler, (String) null);
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    reactiveConsumer.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) Test(org.junit.Test)

Example 3 with Matchers

use of org.hamcrest.Matchers in project beam by apache.

the class GreedyPipelineFuserTest method flattenWithHeterogenousInputsAndOutputsEntirelyMaterialized.

/*
   * goImpulse -> .out -> goRead -> .out \                    -> goParDo -> .out
   *                                      -> flatten -> .out |
   * pyImpulse -> .out -> pyRead -> .out /                    -> pyParDo -> .out
   *
   * becomes
   * (goImpulse.out) -> goRead -> goRead.out -> flatten -> (flatten.out_synthetic0)
   * (pyImpulse.out) -> pyRead -> pyRead.out -> flatten -> (flatten.out_synthetic1)
   * flatten.out_synthetic0 & flatten.out_synthetic1 -> synthetic_flatten -> flatten.out
   * (flatten.out) -> goParDo
   * (flatten.out) -> pyParDo
   */
@Test
public void flattenWithHeterogenousInputsAndOutputsEntirelyMaterialized() {
    Components components = Components.newBuilder().putCoders("coder", Coder.newBuilder().build()).putCoders("windowCoder", Coder.newBuilder().build()).putWindowingStrategies("ws", WindowingStrategy.newBuilder().setWindowCoderId("windowCoder").build()).putTransforms("pyImpulse", PTransform.newBuilder().setUniqueName("PyImpulse").putOutputs("output", "pyImpulse.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.IMPULSE_TRANSFORM_URN)).build()).putPcollections("pyImpulse.out", pc("pyImpulse.out")).putTransforms("pyRead", PTransform.newBuilder().setUniqueName("PyRead").putInputs("input", "pyImpulse.out").putOutputs("output", "pyRead.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.PAR_DO_TRANSFORM_URN).setPayload(ParDoPayload.newBuilder().setDoFn(FunctionSpec.newBuilder()).build().toByteString())).setEnvironmentId("py").build()).putPcollections("pyRead.out", pc("pyRead.out")).putTransforms("goImpulse", PTransform.newBuilder().setUniqueName("GoImpulse").putOutputs("output", "goImpulse.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.IMPULSE_TRANSFORM_URN)).build()).putPcollections("goImpulse.out", pc("goImpulse.out")).putTransforms("goRead", PTransform.newBuilder().setUniqueName("GoRead").putInputs("input", "goImpulse.out").putOutputs("output", "goRead.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.PAR_DO_TRANSFORM_URN).setPayload(ParDoPayload.newBuilder().setDoFn(FunctionSpec.newBuilder()).build().toByteString())).setEnvironmentId("go").build()).putPcollections("goRead.out", pc("goRead.out")).putTransforms("flatten", PTransform.newBuilder().setUniqueName("Flatten").putInputs("goReadInput", "goRead.out").putInputs("pyReadInput", "pyRead.out").putOutputs("output", "flatten.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.FLATTEN_TRANSFORM_URN)).build()).putPcollections("flatten.out", pc("flatten.out")).putTransforms("pyParDo", PTransform.newBuilder().setUniqueName("PyParDo").putInputs("input", "flatten.out").putOutputs("output", "pyParDo.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.PAR_DO_TRANSFORM_URN).setPayload(ParDoPayload.newBuilder().setDoFn(FunctionSpec.newBuilder()).build().toByteString())).setEnvironmentId("py").build()).putPcollections("pyParDo.out", pc("pyParDo.out")).putTransforms("goParDo", PTransform.newBuilder().setUniqueName("GoParDo").putInputs("input", "flatten.out").putOutputs("output", "goParDo.out").setSpec(FunctionSpec.newBuilder().setUrn(PTransformTranslation.PAR_DO_TRANSFORM_URN).setPayload(ParDoPayload.newBuilder().setDoFn(FunctionSpec.newBuilder()).build().toByteString())).setEnvironmentId("go").build()).putPcollections("goParDo.out", pc("goParDo.out")).putEnvironments("go", Environments.createDockerEnvironment("go")).putEnvironments("py", Environments.createDockerEnvironment("py")).build();
    FusedPipeline fused = GreedyPipelineFuser.fuse(Pipeline.newBuilder().setComponents(components).build());
    assertThat(fused.getRunnerExecutedTransforms(), hasSize(3));
    assertThat("The runner should include the impulses for both languages, plus an introduced flatten", fused.getRunnerExecutedTransforms(), hasItems(PipelineNode.pTransform("pyImpulse", components.getTransformsOrThrow("pyImpulse")), PipelineNode.pTransform("goImpulse", components.getTransformsOrThrow("goImpulse"))));
    PTransformNode flattenNode = null;
    for (PTransformNode runnerTransform : fused.getRunnerExecutedTransforms()) {
        if (getOnlyElement(runnerTransform.getTransform().getOutputsMap().values()).equals("flatten.out")) {
            flattenNode = runnerTransform;
        }
    }
    assertThat(flattenNode, not(nullValue()));
    assertThat(flattenNode.getTransform().getSpec().getUrn(), equalTo(PTransformTranslation.FLATTEN_TRANSFORM_URN));
    assertThat(new HashSet<>(flattenNode.getTransform().getInputsMap().values()), hasSize(2));
    Collection<String> introducedOutputs = flattenNode.getTransform().getInputsMap().values();
    AnyOf<String> anyIntroducedPCollection = anyOf(introducedOutputs.stream().map(Matchers::equalTo).collect(Collectors.toSet()));
    assertThat(fused.getFusedStages(), containsInAnyOrder(ExecutableStageMatcher.withInput("goImpulse.out").withOutputs(anyIntroducedPCollection).withTransforms("goRead", "flatten"), ExecutableStageMatcher.withInput("pyImpulse.out").withOutputs(anyIntroducedPCollection).withTransforms("pyRead", "flatten"), ExecutableStageMatcher.withInput("flatten.out").withNoOutputs().withTransforms("goParDo"), ExecutableStageMatcher.withInput("flatten.out").withNoOutputs().withTransforms("pyParDo")));
    Set<String> materializedStageOutputs = fused.getFusedStages().stream().flatMap(executableStage -> executableStage.getOutputPCollections().stream()).map(PCollectionNode::getId).collect(Collectors.toSet());
    assertThat("All materialized stage outputs should be flattened, and no more", materializedStageOutputs, containsInAnyOrder(flattenNode.getTransform().getInputsMap().values().toArray(new String[0])));
}
Also used : Components(org.apache.beam.model.pipeline.v1.RunnerApi.Components) PTransformNode(org.apache.beam.runners.core.construction.graph.PipelineNode.PTransformNode) Matchers(org.hamcrest.Matchers) Test(org.junit.Test)

Aggregations

Matchers (org.hamcrest.Matchers)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)2 Assert.assertSame (org.junit.Assert.assertSame)2 Assert.assertThat (org.junit.Assert.assertThat)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Assert.fail (org.junit.Assert.fail)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 BDDMockito.willAnswer (org.mockito.BDDMockito.willAnswer)2 Mockito (org.mockito.Mockito)2 Mockito.mock (org.mockito.Mockito.mock)2