-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathCacheLockTestBase.java
More file actions
194 lines (155 loc) · 6.43 KB
/
CacheLockTestBase.java
File metadata and controls
194 lines (155 loc) · 6.43 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4jextensions;
import com.microsoft.aad.msal4jextensions.persistence.ICacheAccessor;
import com.sun.jna.Platform;
import org.junit.Assert;
import org.junit.BeforeClass;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class CacheLockTestBase {
static String folder;
static String testFilePath;
static String lockFilePath;
static String testClassesPath;
static String lockHoldingIntervalsFilePath;
@BeforeClass
public static void setup() {
// get proper file paths
String currDir = System.getProperty("user.dir");
String home = System.getProperty("user.home");
java.nio.file.Path classes = java.nio.file.Paths.get(currDir, "target", "classes");
testClassesPath = java.nio.file.Paths.get(currDir, "target", "test-classes").toString();
testFilePath = java.nio.file.Paths.get(home, "test.txt").toString();
lockFilePath = java.nio.file.Paths.get(home, "testLock.lockfile").toString();
lockHoldingIntervalsFilePath = java.nio.file.Paths.get(home, "lockHoldingIntervals.txt").toString();
String delimiter = ":";
if (Platform.isWindows()) {
delimiter = ";";
}
folder = classes.toString() + delimiter + testClassesPath;
}
void waitForProcess(Process process) throws InterruptedException {
if (process.waitFor() != 0) {
throw new RuntimeException(new BufferedReader(new InputStreamReader(process.getErrorStream()))
.lines().collect(Collectors.joining("\n")));
}
}
void validateResult(String data, int expectedNum) {
System.out.println("DATA TO VALIDATE: ");
System.out.println(data);
String prevTag = null;
String prevProcId = null;
int count = 0;
for (String line : data.split("\\r?\\n")) {
String[] tokens = line.split(" ");
String tag = tokens[0];
String procId = tokens[1];
switch (tag) {
case ("<"):
if ("<".equals(prevTag)) {
Assert.fail("Unexpected Token");
}
break;
case (">"):
count++;
if (!"<".equals(prevTag) || !prevProcId.equals(procId)) {
Assert.fail("Unexpected Token");
}
break;
default:
Assert.fail("Unexpected Token");
}
prevTag = tag;
prevProcId = procId;
}
if (!">".equals(prevTag)) {
Assert.fail("Unexpected Token");
}
Assert.assertEquals(expectedNum, count);
}
void validateLockUsageIntervals(int expected_size) throws IOException {
List<Long[]> list = new ArrayList<>();
String data = readFile(lockHoldingIntervalsFilePath);
for (String line : data.split("\\r?\\n")) {
String[] split = line.split("-");
list.add(new Long[]{Long.parseLong(split[0]), Long.parseLong(split[1])});
}
//Assert.assertEquals(expected_size, list.size());
if (expected_size != list.size()) {
System.out.println("lock intervals NUM = " + list.size());
}
list.sort(Comparator.comparingLong(a -> a[0]));
long sum = 0L;
Long[] prev = null;
for (Long[] interval : list) {
Assert.assertTrue(interval[0] <= interval[1]);
sum += interval[1] - interval[0];
if (prev != null) {
if (interval[0] < prev[1]) {
System.out.println("lock acquisition intersection detected");
//Assert.fail();
}
}
prev = interval;
}
System.out.println("average lock holding time in ms - " + sum/list.size());
}
private String readFile(String filePath) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
return new String(bytes, StandardCharsets.UTF_8);
}
void validateResultInCache(ICacheAccessor keyChainAccessor, int expectedNum) throws IOException {
validateResult(new String(keyChainAccessor.read(), StandardCharsets.UTF_8), expectedNum);
}
void multipleThreadsWriting(ICacheAccessor cacheAccessor, int num,
IRunnableFactory runnableFactory) throws IOException, InterruptedException {
clearFile(lockHoldingIntervalsFilePath);
cacheAccessor.delete();
List<Thread> writersThreads = new ArrayList<>();
for (int i = 0; i < num; i++) {
Thread t = new Thread(runnableFactory.create("thread_" + i));
t.start();
writersThreads.add(t);
}
for (Thread t : writersThreads) {
t.join();
}
validateLockUsageIntervals(num);
validateResultInCache(cacheAccessor, num);
}
private void clearFile(String filePath) throws IOException {
new FileOutputStream(filePath).close();
}
void multipleProcessesWriting(ICacheAccessor cacheAccessor, int num,
String writerClass,
String writerClassArgs)
throws IOException, InterruptedException {
clearFile(lockHoldingIntervalsFilePath);
cacheAccessor.delete();
List<Process> processes = new ArrayList<>();
for (int i = 0; i < num; i++) {
String mvnArgs = ("Process_" + i) + " " + writerClassArgs;
String mvn = Platform.isWindows() ? "mvn.bat" : "mvn";
String[] mvnCommand =
new String[]{mvn, "exec:java",
"-Dexec.mainClass=" + writerClass,
"-Dexec.classpathScope=test",
"-Dexec.args=" + mvnArgs};
Process process = new ProcessBuilder(mvnCommand).inheritIO().start();
processes.add(process);
}
for (Process process : processes) {
waitForProcess(process);
}
validateLockUsageIntervals(num);
validateResultInCache(cacheAccessor, num);
}
}