-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSupport.java
More file actions
72 lines (63 loc) · 2.4 KB
/
Copy pathTestSupport.java
File metadata and controls
72 lines (63 loc) · 2.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.vaultdb;
import com.vaultdb.commands.CommandHandler;
import com.vaultdb.persistence.AofWriter;
import com.vaultdb.resp.RespParser;
import com.vaultdb.resp.RespWriter;
import com.vaultdb.ttl.TtlSweeper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
final class TestSupport {
private TestSupport() {
}
static CommandHandler newHandler(VaultDBEngine engine) {
return new CommandHandler(engine, null, false);
}
static String execute(VaultDBEngine engine, String... args) throws IOException {
CommandHandler handler = newHandler(engine);
ByteArrayOutputStream out = new ByteArrayOutputStream();
RespWriter writer = new RespWriter(out);
handler.handle(List.of(args), writer);
return out.toString(StandardCharsets.UTF_8);
}
static List<String> parseCommand(String resp) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(resp.getBytes(StandardCharsets.UTF_8));
return new RespParser(in).readCommand();
}
static Path tempAof() throws IOException {
return Files.createTempFile("vaultdb-test-", ".aof");
}
static void runConcurrentReads(VaultDBEngine engine, int threads, int iterations) throws InterruptedException {
engine.set("counter", "0");
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(threads);
ExecutorService pool = Executors.newFixedThreadPool(threads);
for (int i = 0; i < threads; i++) {
pool.submit(() -> {
try {
start.await();
for (int j = 0; j < iterations; j++) {
engine.get("counter");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown();
}
});
}
start.countDown();
done.await();
pool.shutdown();
}
static void runTtlSweeperOnce(VaultDBEngine engine) {
new TtlSweeper(engine).sweepOnce();
}
}