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 java.util.LinkedList;
21 import java.util.Random;
22
23 import org.apache.commons.lang3.AbstractLangTest;
24 import org.junit.jupiter.api.Disabled;
25 import org.junit.jupiter.api.Test;
26
27
28
29
30
31
32
33
34
35
36
37
38 public class ReflectionToStringBuilderMutateInspectConcurrencyTest extends AbstractLangTest {
39
40 final class InspectingClient implements Runnable {
41 private final TestFixture testFixture;
42
43 InspectingClient(final TestFixture testFixture) {
44 this.testFixture = testFixture;
45 }
46
47 @Override
48 public void run() {
49 ReflectionToStringBuilder.toString(testFixture);
50 }
51 }
52
53 final class MutatingClient implements Runnable {
54 private final TestFixture testFixture;
55 private final Random random = new Random();
56
57 MutatingClient(final TestFixture testFixture) {
58 this.testFixture = testFixture;
59 }
60
61 @Override
62 public void run() {
63 if (random.nextBoolean()) {
64 testFixture.add();
65 } else {
66 testFixture.delete();
67 }
68 }
69 }
70
71 final class TestFixture {
72 private final LinkedList<Integer> listField = new LinkedList<>();
73 private final Random random = new Random();
74 private final int N = 100;
75
76 TestFixture() {
77 synchronized (this) {
78 for (int i = 0; i < N; i++) {
79 listField.add(Integer.valueOf(i));
80 }
81 }
82 }
83
84 public synchronized void add() {
85 listField.add(Integer.valueOf(random.nextInt(N)));
86 }
87
88 public synchronized void delete() {
89 listField.remove(Integer.valueOf(random.nextInt(N)));
90 }
91 }
92
93 @Test
94 @Disabled
95 public void testConcurrency() {
96 final TestFixture testFixture = new TestFixture();
97 final int numMutators = 10;
98 final int numIterations = 10;
99 for (int i = 0; i < numIterations; i++) {
100 for (int j = 0; j < numMutators; j++) {
101 final Thread t = new Thread(new MutatingClient(testFixture));
102 t.start();
103 final Thread s = new Thread(new InspectingClient(testFixture));
104 s.start();
105 }
106 }
107 }
108 }