RESOLVED - issue BATCH-1301: ItemStream is not being opened correctly for multi-threaded Step when scope="step"

This commit is contained in:
dsyer
2009-06-21 08:25:15 +00:00
parent 8a39db4815
commit 3d6f1cadbc
5 changed files with 151 additions and 41 deletions

View File

@@ -51,6 +51,7 @@ public class AsyncStepScopeIntegrationTests implements BeanFactoryAware {
@Before
public void countBeans() {
StepSynchronizationManager.release();
beanCount = beanFactory.getBeanDefinitionCount();
}
@@ -71,7 +72,7 @@ public class AsyncStepScopeIntegrationTests implements BeanFactoryAware {
}
@Test
public void testGetMultiple() throws Exception {
public void testGetMultipleInMultipleThreads() throws Exception {
List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
@@ -105,4 +106,44 @@ public class AsyncStepScopeIntegrationTests implements BeanFactoryAware {
}
@Test
public void testGetSameInMultipleThreads() throws Exception {
List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
final StepExecution stepExecution = new StepExecution("foo", new JobExecution(0L), 123L);
ExecutionContext executionContext = stepExecution.getExecutionContext();
executionContext.put("foo", "foo");
StepSynchronizationManager.register(stepExecution);
assertEquals("foo", simple.getName());
for (int i = 0; i < 12; i++) {
final String value = "foo"+i;
FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
public String call() throws Exception {
ExecutionContext executionContext = stepExecution.getExecutionContext();
executionContext.put("foo", value);
StepContext context = StepSynchronizationManager.register(stepExecution);
logger.debug("Registered: "+context.getStepExecutionContext());
try {
return simple.getName();
}
finally {
StepSynchronizationManager.close();
}
}
});
tasks.add(task);
taskExecutor.execute(task);
}
StepSynchronizationManager.close();
int i = 0;
for (FutureTask<String> task : tasks) {
assertEquals("foo", task.get());
i++;
}
}
}

View File

@@ -6,14 +6,17 @@ import static org.junit.Assert.assertNull;
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.FutureTask;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
public class StepSynchronizationManagerTests {
@@ -48,6 +51,30 @@ public class StepSynchronizationManagerTests {
assertEquals(0, list.size());
}
@Test
public void testMultithreaded() throws Exception {
StepContext context = StepSynchronizationManager.register(stepExecution);
ExecutorService executorService = Executors.newFixedThreadPool(2);
FutureTask<StepContext> task = new FutureTask<StepContext>(new Callable<StepContext>() {
public StepContext call() throws Exception {
try {
StepSynchronizationManager.register(stepExecution);
StepContext context = StepSynchronizationManager.getContext();
context.setAttribute("foo", "bar");
return context;
}
finally {
StepSynchronizationManager.close();
}
}
});
executorService.execute(task);
executorService.awaitTermination(1, TimeUnit.SECONDS);
assertEquals(context.attributeNames().length, task.get().attributeNames().length);
StepSynchronizationManager.close();
assertNull(StepSynchronizationManager.getContext());
}
@Test
public void testRelease() {
StepContext context = StepSynchronizationManager.register(stepExecution);