INT-1507, INT-1545: Add groovy processor for payloads

- Remove unneeded config file
- Upgrade groovy
- fix potential permgen leak in groovy payload processor
- Test concurrent execution
This commit is contained in:
Dave Syer
2010-10-18 10:45:57 -07:00
parent 64fec64d40
commit d7bbbaae17
9 changed files with 378 additions and 68 deletions

View File

@@ -22,7 +22,9 @@ import static org.junit.Assert.assertFalse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.core.io.AbstractResource;
import org.springframework.integration.Message;
@@ -31,6 +33,7 @@ import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.scripting.ScriptSource;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.test.annotation.Repeat;
/**
* @author Mark Fisher
@@ -40,15 +43,22 @@ import org.springframework.scripting.support.ResourceScriptSource;
*/
public class GroovyScriptExecutingMessageProcessorTests {
@Rule
public RepeatProcessor repeater = new RepeatProcessor(4);
private AtomicInteger countHolder = new AtomicInteger();
@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").build();
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", result.toString());
assertEquals("payload is foo, header is bar"+count, result.toString());
}
@Test

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.groovy;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.annotation.Repeat;
/**
* @author Dave Syer
* @since 2.0
*/
public class GroovyScriptPayloadMessageProcessorTests {
@Rule
public RepeatProcessor repeater = new RepeatProcessor(4);
private AtomicInteger countHolder = new AtomicInteger();
private GroovyScriptPayloadMessageProcessor processor = new GroovyScriptPayloadMessageProcessor();
@Test
@Repeat(20)
public void testSimpleExecution() throws Exception {
int count = countHolder.getAndIncrement();
Message<?> message = MessageBuilder.withPayload("headers.foo" + count).setHeader("foo" + count, "bar").build();
Object result = processor.processMessage(message);
assertEquals("bar", result.toString());
}
@Test
public void testDoubleExecutionWithNewScript() throws Exception {
Message<?> message = MessageBuilder.withPayload("headers.foo").setHeader("foo", "bar").build();
Object result = processor.processMessage(message);
assertEquals("bar", result.toString());
message = MessageBuilder.withPayload("headers.bar").setHeader("bar", "spam").build();
result = processor.processMessage(message);
assertEquals("spam", result.toString());
}
@Test
public void testSimpleExecutionWithContext() throws Exception {
Message<?> message = MessageBuilder.withPayload("\"spam is $spam foo is $headers.foo\"")
.setHeader("foo", "bar").build();
MessageProcessor<Object> processor = new GroovyScriptPayloadMessageProcessor(Collections.singletonMap("spam",
"bucket"));
Object result = processor.processMessage(message);
assertEquals("spam is bucket foo is bar", result.toString());
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.groovy;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.annotation.Repeat;
/**
* A method rule that looks at Spring repeat annotations on methods and executes the test multiple times (without
* re-initializing the test case).
*
* @author Dave Syer
* @since 2.0
*
*/
public class RepeatProcessor implements MethodRule {
private final int concurrency;
public RepeatProcessor(int concurrency) {
this.concurrency = concurrency < 0 ? 0 : concurrency;
}
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();
}
}
};
}
}