1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.builder;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.CopyOnWriteArrayList;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.Future;
32 import java.util.concurrent.TimeUnit;
33
34 import org.apache.commons.lang3.AbstractLangTest;
35 import org.apache.commons.lang3.concurrent.UncheckedFuture;
36 import org.junit.jupiter.api.Disabled;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ReflectionToStringBuilderConcurrencyTest extends AbstractLangTest {
54
55 static class CollectionHolder<T extends Collection<?>> {
56 T collection;
57
58 CollectionHolder(final T collection) {
59 this.collection = collection;
60 }
61 }
62
63 private static final int DATA_SIZE = 100000;
64 private static final int REPEAT = 100;
65
66 @Test
67 @Disabled
68 public void testArrayList() throws InterruptedException {
69 this.testConcurrency(new CollectionHolder<>(new ArrayList<>()));
70 }
71
72 private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException {
73 final List<Integer> list = holder.collection;
74
75 for (int i = 0; i < DATA_SIZE; i++) {
76 list.add(Integer.valueOf(i));
77 }
78
79 final ExecutorService threadPool = Executors.newFixedThreadPool(2);
80 try {
81
82 final Callable<Integer> consumer = () -> {
83 for (int i = 0; i < REPEAT; i++) {
84 final String s = ReflectionToStringBuilder.toString(holder);
85 assertNotNull(s);
86 }
87 return Integer.valueOf(REPEAT);
88 };
89
90 final Callable<Integer> producer = () -> {
91 for (int i = 0; i < DATA_SIZE; i++) {
92 list.remove(list.get(0));
93 }
94 return Integer.valueOf(REPEAT);
95 };
96 final Collection<Callable<Integer>> tasks = new ArrayList<>();
97 tasks.add(consumer);
98 tasks.add(producer);
99 final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
100 UncheckedFuture.on(futures).forEach(f -> assertEquals(REPEAT, f.get().intValue()));
101 } finally {
102 threadPool.shutdown();
103 threadPool.awaitTermination(1, TimeUnit.SECONDS);
104 }
105 }
106
107 @Test
108 @Disabled
109 public void testCopyOnWriteArrayList() throws InterruptedException {
110 this.testConcurrency(new CollectionHolder<>(new CopyOnWriteArrayList<>()));
111 }
112
113 @Test
114 @Disabled
115 public void testLinkedList() throws InterruptedException {
116 this.testConcurrency(new CollectionHolder<>(new LinkedList<>()));
117 }
118 }