-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrencyTest.java
More file actions
50 lines (43 loc) · 1.4 KB
/
Copy pathConcurrencyTest.java
File metadata and controls
50 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.vaultdb;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.*;
class ConcurrencyTest {
private VaultDBEngine engine;
@BeforeEach
void setUp() {
engine = new VaultDBEngine();
}
@Test
void parallelReadsDoNotBlockEachOther() throws InterruptedException {
engine.set("shared", "value");
TestSupport.runConcurrentReads(engine, 50, 100);
assertEquals("value", engine.get("shared"));
}
@Test
void writesAreIsolatedFromConcurrentReaders() throws InterruptedException {
engine.set("key", "initial");
AtomicInteger readValues = new AtomicInteger();
Thread writer = new Thread(() -> {
for (int i = 0; i < 100; i++) {
engine.set("key", "v" + i);
}
});
Thread reader = new Thread(() -> {
for (int i = 0; i < 100; i++) {
String value = engine.get("key");
if (value != null && value.startsWith("v")) {
readValues.incrementAndGet();
}
}
});
writer.start();
reader.start();
writer.join();
reader.join();
assertTrue(readValues.get() > 0);
assertTrue(engine.get("key").startsWith("v"));
}
}