Skip to content

Commit 43fa18f

Browse files
hpoettkerfmbenhassine
authored andcommitted
Use thread-safe collections in multithreaded test
1 parent f4f00fb commit 43fa18f

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplateAsynchronousTests.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import java.util.HashSet;
2929
import java.util.List;
3030
import java.util.Set;
31+
import java.util.concurrent.ConcurrentHashMap;
3132

3233
import org.junit.jupiter.api.Test;
3334

@@ -164,6 +165,7 @@ public RepeatStatus doInIteration(RepeatContext context) throws Exception {
164165
}
165166

166167
@Test
168+
@SuppressWarnings("removal")
167169
void testThrottleLimit() {
168170

169171
int throttleLimit = 600;
@@ -174,30 +176,27 @@ void testThrottleLimit() {
174176
template.setTaskExecutor(taskExecutor);
175177
template.setThrottleLimit(throttleLimit);
176178

177-
final String threadName = Thread.currentThread().getName();
178-
final Set<String> threadNames = new HashSet<>();
179-
final List<String> items = new ArrayList<>();
180-
181-
final RepeatCallback callback = new RepeatCallback() {
182-
@Override
183-
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
184-
assertNotSame(threadName, Thread.currentThread().getName());
185-
Trade item = provider.read();
186-
threadNames.add(Thread.currentThread().getName() + " : " + item);
187-
items.add("" + item);
188-
if (item != null) {
189-
processor.write(Chunk.of(item));
190-
// Do some more I/O
191-
for (int i = 0; i < 10; i++) {
192-
TradeItemReader provider = new TradeItemReader(resource);
193-
provider.open(new ExecutionContext());
194-
while (provider.read() != null)
195-
continue;
196-
provider.close();
197-
}
179+
String threadName = Thread.currentThread().getName();
180+
Set<String> threadNames = ConcurrentHashMap.newKeySet();
181+
List<String> items = Collections.synchronizedList(new ArrayList<>());
182+
183+
RepeatCallback callback = context -> {
184+
assertNotSame(threadName, Thread.currentThread().getName());
185+
Trade item = provider.read();
186+
threadNames.add(Thread.currentThread().getName() + " : " + item);
187+
items.add("" + item);
188+
if (item != null) {
189+
processor.write(Chunk.of(item));
190+
// Do some more I/O
191+
for (int i = 0; i < 10; i++) {
192+
TradeItemReader provider = new TradeItemReader(resource);
193+
provider.open(new ExecutionContext());
194+
while (provider.read() != null)
195+
continue;
196+
provider.close();
198197
}
199-
return RepeatStatus.continueIf(item != null);
200198
}
199+
return RepeatStatus.continueIf(item != null);
201200
};
202201

203202
template.iterate(callback);

0 commit comments

Comments
 (0)