|
11 | 11 | import org.apache.commons.collections4.trie.PatriciaTrie; |
12 | 12 | import org.opensearch.test.OpenSearchTestCase; |
13 | 13 |
|
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Random; |
| 17 | + |
14 | 18 | public class AttributeValueStoreTests extends OpenSearchTestCase { |
15 | 19 |
|
16 | 20 | AttributeValueStore<String, String> subjectUnderTest; |
| 21 | + final static String ALPHA_NUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 22 | + final static Random random = new Random(); |
17 | 23 |
|
18 | 24 | public void setUp() throws Exception { |
19 | 25 | super.setUp(); |
@@ -50,4 +56,73 @@ public void testClear() { |
50 | 56 | subjectUnderTest.clear(); |
51 | 57 | assertEquals(0, subjectUnderTest.size()); |
52 | 58 | } |
| 59 | + |
| 60 | + public void testConcurrentUpdatesAndReads() { |
| 61 | + final List<String> randomStrings = new ArrayList<>(); |
| 62 | + for (int i = 0; i < 100; i++) { |
| 63 | + randomStrings.add(generateRandom(20)); |
| 64 | + } |
| 65 | + List<Thread> readerThreads = new ArrayList<>(); |
| 66 | + List<Thread> writerThreads = new ArrayList<>(); |
| 67 | + for (int i = 0; i < 10; i++) { |
| 68 | + readerThreads.add(new AttributeValueStoreReader(subjectUnderTest, randomStrings)); |
| 69 | + writerThreads.add(new AttributeValueStoreWriter(subjectUnderTest, randomStrings)); |
| 70 | + } |
| 71 | + |
| 72 | + for (int ii = 0; ii < 10; ii++) { |
| 73 | + readerThreads.get(ii).start(); |
| 74 | + writerThreads.get(ii).start(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static String generateRandom(int maxLength) { |
| 79 | + int length = random.nextInt(maxLength) + 1; // +1 to avoid length 0 |
| 80 | + StringBuilder sb = new StringBuilder(length); |
| 81 | + for (int i = 0; i < length; i++) { |
| 82 | + sb.append(ALPHA_NUMERIC.charAt(random.nextInt(ALPHA_NUMERIC.length()))); |
| 83 | + } |
| 84 | + return sb.toString(); |
| 85 | + } |
| 86 | + |
| 87 | + private static class AttributeValueStoreReader extends Thread { |
| 88 | + private final AttributeValueStore<String, String> subjectUnderTest; |
| 89 | + private final List<String> toReadKeys; |
| 90 | + |
| 91 | + public AttributeValueStoreReader(AttributeValueStore<String, String> subjectUnderTest, List<String> toReadKeys) { |
| 92 | + super(); |
| 93 | + this.subjectUnderTest = subjectUnderTest; |
| 94 | + this.toReadKeys = toReadKeys; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void run() { |
| 99 | + try { |
| 100 | + Thread.sleep(random().nextInt(100)); |
| 101 | + for (String key : toReadKeys) { |
| 102 | + subjectUnderTest.get(key); |
| 103 | + } |
| 104 | + } catch (InterruptedException e) {} |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static class AttributeValueStoreWriter extends Thread { |
| 109 | + private final AttributeValueStore<String, String> subjectUnderTest; |
| 110 | + private final List<String> toWriteKeys; |
| 111 | + |
| 112 | + public AttributeValueStoreWriter(AttributeValueStore<String, String> subjectUnderTest, List<String> toWriteKeys) { |
| 113 | + super(); |
| 114 | + this.subjectUnderTest = subjectUnderTest; |
| 115 | + this.toWriteKeys = toWriteKeys; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void run() { |
| 120 | + try { |
| 121 | + Thread.sleep(random().nextInt(100)); |
| 122 | + for (String key : toWriteKeys) { |
| 123 | + subjectUnderTest.put(key, key); |
| 124 | + } |
| 125 | + } catch (InterruptedException e) {} |
| 126 | + } |
| 127 | + } |
53 | 128 | } |
0 commit comments