Search in sources :

Example 1 with Repeat

use of org.springframework.test.annotation.Repeat in project spring-integration by spring-projects.

the class GroovyScriptExecutingMessageProcessorTests method testSimpleExecution.

@Test
@Repeat(20)
public void testSimpleExecution() throws Exception {
    int count = countHolder.getAndIncrement();
    String script = "return \"payload is $payload, header is $headers.testHeader\"";
    Message<?> message = MessageBuilder.withPayload("foo").setHeader("testHeader", "bar" + count).build();
    TestResource resource = new TestResource(script, "simpleTest");
    ScriptSource scriptSource = new ResourceScriptSource(resource);
    MessageProcessor<Object> processor = new GroovyScriptExecutingMessageProcessor(scriptSource);
    Object result = processor.processMessage(message);
    assertEquals("payload is foo, header is bar" + count, result.toString());
}
Also used : RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) RefreshableResourceScriptSource(org.springframework.integration.scripting.RefreshableResourceScriptSource) ScriptSource(org.springframework.scripting.ScriptSource) ResourceScriptSource(org.springframework.scripting.support.ResourceScriptSource) Test(org.junit.Test) Repeat(org.springframework.test.annotation.Repeat)

Example 2 with Repeat

use of org.springframework.test.annotation.Repeat in project spring-integration by spring-projects.

the class RepeatProcessor method apply.

public Statement apply(final Statement base, FrameworkMethod method, Object target) {
    Repeat repeat = AnnotationUtils.findAnnotation(method.getMethod(), Repeat.class);
    if (repeat == null) {
        return base;
    }
    final int repeats = repeat.value();
    if (concurrency <= 0) {
        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                for (int i = 0; i < repeats; i++) {
                    try {
                        base.evaluate();
                    } catch (Throwable t) {
                        throw new IllegalStateException("Failed on iteration: " + i, t);
                    }
                }
            }
        };
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            List<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
            ExecutorService executor = Executors.newFixedThreadPool(concurrency);
            try {
                for (int i = 0; i < repeats; i++) {
                    final int count = i;
                    results.add(executor.submit(new Callable<Boolean>() {

                        public Boolean call() {
                            try {
                                base.evaluate();
                            } catch (Throwable t) {
                                throw new IllegalStateException("Failed on iteration: " + count, t);
                            }
                            return true;
                        }
                    }));
                }
                for (Future<Boolean> future : results) {
                    assertTrue("Null result from completer", future.get(10, TimeUnit.SECONDS));
                }
            } finally {
                executor.shutdownNow();
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Repeat(org.springframework.test.annotation.Repeat) Callable(java.util.concurrent.Callable)

Example 3 with Repeat

use of org.springframework.test.annotation.Repeat in project spring-integration by spring-projects.

the class MySqlJdbcMessageStoreTests method testSameMessageToMultipleGroups.

@Test
@Transactional
@Rollback(false)
@Repeat(20)
public void testSameMessageToMultipleGroups() throws Exception {
    final String group1Id = "group1";
    final String group2Id = "group2";
    final Message<String> message = MessageBuilder.withPayload("foo").build();
    final MessageBuilder<String> builder1 = MessageBuilder.fromMessage(message);
    final MessageBuilder<String> builder2 = MessageBuilder.fromMessage(message);
    builder1.setSequenceNumber(1);
    builder2.setSequenceNumber(2);
    final Message<?> message1 = builder1.build();
    final Message<?> message2 = builder2.build();
    messageStore.addMessageToGroup(group1Id, message1);
    messageStore.addMessageToGroup(group2Id, message2);
    final Message<?> messageFromGroup1 = messageStore.pollMessageFromGroup(group1Id);
    final Message<?> messageFromGroup2 = messageStore.pollMessageFromGroup(group2Id);
    assertNotNull(messageFromGroup1);
    assertNotNull(messageFromGroup2);
    LOG.info("messageFromGroup1: " + messageFromGroup1.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromGroup1).getSequenceNumber());
    LOG.info("messageFromGroup2: " + messageFromGroup2.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromGroup2).getSequenceNumber());
    assertEquals(Integer.valueOf(1), messageFromGroup1.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
    assertEquals(Integer.valueOf(2), messageFromGroup2.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Test(org.junit.Test) Repeat(org.springframework.test.annotation.Repeat) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Repeat

use of org.springframework.test.annotation.Repeat in project spring-integration by spring-projects.

the class MySqlJdbcMessageStoreTests method testSameMessageAndGroupToMultipleRegions.

@Test
@Transactional
@Rollback(false)
@Repeat(20)
public void testSameMessageAndGroupToMultipleRegions() throws Exception {
    final String groupId = "myGroup";
    final String region1 = "region1";
    final String region2 = "region2";
    final JdbcMessageStore messageStore1 = new JdbcMessageStore(dataSource);
    messageStore1.setRegion(region1);
    final JdbcMessageStore messageStore2 = new JdbcMessageStore(dataSource);
    messageStore1.setRegion(region2);
    final Message<String> message = MessageBuilder.withPayload("foo").build();
    final MessageBuilder<String> builder1 = MessageBuilder.fromMessage(message);
    final MessageBuilder<String> builder2 = MessageBuilder.fromMessage(message);
    builder1.setSequenceNumber(1);
    builder2.setSequenceNumber(2);
    final Message<?> message1 = builder1.build();
    final Message<?> message2 = builder2.build();
    messageStore1.addMessageToGroup(groupId, message1);
    messageStore2.addMessageToGroup(groupId, message2);
    final Message<?> messageFromRegion1 = messageStore1.pollMessageFromGroup(groupId);
    final Message<?> messageFromRegion2 = messageStore2.pollMessageFromGroup(groupId);
    assertNotNull(messageFromRegion1);
    assertNotNull(messageFromRegion2);
    LOG.info("messageFromRegion1: " + messageFromRegion1.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromRegion1).getSequenceNumber());
    LOG.info("messageFromRegion2: " + messageFromRegion2.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromRegion2).getSequenceNumber());
    assertEquals(Integer.valueOf(1), messageFromRegion1.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
    assertEquals(Integer.valueOf(2), messageFromRegion2.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) JdbcMessageStore(org.springframework.integration.jdbc.store.JdbcMessageStore) Test(org.junit.Test) Repeat(org.springframework.test.annotation.Repeat) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Repeat

use of org.springframework.test.annotation.Repeat in project spring-integration by spring-projects.

the class JdbcMessageStoreChannelIntegrationTests method testTransactionalSendAndReceive.

@Test
@Repeat(2)
public void testTransactionalSendAndReceive() throws Exception {
    boolean result = new TransactionTemplate(transactionManager).execute(status -> {
        synchronized (storeLock) {
            boolean result1 = input.send(new GenericMessage<>("foo"), 100L);
            // This will time out because the transaction has not committed yet
            try {
                Service.await(100);
                fail("Expected timeout");
            } catch (Exception e) {
            // expected
            }
            return result1;
        }
    });
    assertTrue("Could not send message", result);
    waitForMessage();
    StopWatch stopWatch = new StopWatch();
    try {
        stopWatch.start();
        // It might be null or not, but we don't want it to block
        input.receive(100L);
    } finally {
        stopWatch.stop();
    }
    // If the poll blocks in the RDBMS there is no way for the queue to respect the timeout
    assertTrue("Timed out waiting for receive", stopWatch.getTotalTimeMillis() < 10000);
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) SerializationFailedException(org.springframework.core.serializer.support.SerializationFailedException) NotSerializableException(java.io.NotSerializableException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test) Repeat(org.springframework.test.annotation.Repeat)

Aggregations

Repeat (org.springframework.test.annotation.Repeat)7 Test (org.junit.Test)6 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)2 Rollback (org.springframework.test.annotation.Rollback)2 Transactional (org.springframework.transaction.annotation.Transactional)2 File (java.io.File)1 NotSerializableException (java.io.NotSerializableException)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Statement (org.junit.runners.model.Statement)1 DetectFuture (org.opennms.netmgt.provision.DetectFuture)1 AsyncAbstractDetector (org.opennms.netmgt.provision.support.AsyncAbstractDetector)1 SerializationFailedException (org.springframework.core.serializer.support.SerializationFailedException)1 JdbcMessageStore (org.springframework.integration.jdbc.store.JdbcMessageStore)1 RefreshableResourceScriptSource (org.springframework.integration.scripting.RefreshableResourceScriptSource)1 Message (org.springframework.messaging.Message)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1