Repository navigation
Expand file tree
/
Copy pathNativeJfrTest.java
More file actions
118 lines (114 loc) 路 4.77 KB
/
Copy pathNativeJfrTest.java
File metadata and controls
118 lines (114 loc) 路 4.77 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
import dev.elide.bemo.transport.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingFile;
/** Read actual recordings; assertions concern causal operations, not timing or batch boundaries. */
public final class NativeJfrTest {
private static final List<String> TYPES =
List.of(
"dev.elide.TransportBatch",
"dev.elide.TransportTlsHandshake",
"dev.elide.TransportCopy",
"dev.elide.TransportShutdown");
public static void main(String[] args) throws Exception {
verify(
new BackendTransport(new FfmTransportNative(Path.of(args[0]))),
Files.readAllBytes(Path.of(args[1])),
Files.readAllBytes(Path.of(args[2])));
}
public static void verify(TransportNative api, byte[] cert, byte[] key) throws Exception {
Path file = Files.createTempFile("native-transport-", ".jfr");
try {
try (Recording recording = new Recording()) {
for (String type : TYPES) recording.disable(type);
recording.start();
NativeChannelTest.verify(api);
recording.stop();
recording.dump(file);
}
if (RecordingFile.readAllEvents(file).stream()
.anyMatch(event -> TYPES.contains(event.getEventType().getName())))
throw new AssertionError("Disabled transport events were recorded");
try (Recording recording = new Recording()) {
for (String type : TYPES) recording.enable(type).withThreshold(Duration.ZERO);
recording.start();
NativeChannelTest.verify(api);
NativeTlsChannelTest.verify(api, cert, key);
recording.stop();
recording.dump(file);
}
List<RecordedEvent> events = RecordingFile.readAllEvents(file);
Set<Long> drivers = new HashSet<>();
Set<String> channels = new HashSet<>();
Set<String> copies = new HashSet<>();
long read = 0;
long written = 0;
int handshakes = 0;
int shutdowns = 0;
for (RecordedEvent event : events) {
switch (event.getEventType().getName()) {
case "dev.elide.TransportBatch" -> {
drivers.add(event.getLong("driverId"));
if (event.getString("backend").equals("unknown"))
throw new AssertionError("Missing actual backend");
int count = event.getInt("completions");
if (count < 0
|| count > event.getInt("capacity")
|| count
!= event.getInt("reads")
+ event.getInt("writes")
+ event.getInt("accepts")
+ event.getInt("connects")
|| event.getLong("nativeBytes") < 0
|| event.getLong("pollNanos") < 0
|| event.getLong("dispatchNanos") < 0)
throw new AssertionError("Invalid batch accounting: " + event);
read += event.getLong("receivedBytes");
written += event.getLong("writtenBytes");
}
case "dev.elide.TransportTlsHandshake" -> {
if (!event.getString("outcome").equals("success")
|| !event.getString("protocol").equals("h2")
|| !channels.add(event.getString("channelId")))
throw new AssertionError("Incorrect or duplicate handshake: " + event);
handshakes++;
}
case "dev.elide.TransportCopy" -> {
if (event.getLong("bytes") <= 0
|| event.getLong("socketId") == 0
|| event.getString("channelId").isEmpty())
throw new AssertionError("Invalid copy accounting: " + event);
copies.add(event.getString("reason"));
}
case "dev.elide.TransportShutdown" -> {
if (event.getInt("status") != 0 || event.getLong("retainedBytes") != 0)
throw new AssertionError("Driver failed to reclaim test buffers: " + event);
shutdowns++;
}
default -> {}
}
}
if (drivers.isEmpty()
|| read < 8
|| written < 8
|| handshakes != 2
|| shutdowns < 2
|| !copies.containsAll(List.of("tcp-write", "tls-write", "tls-read")))
throw new AssertionError("Missing transport events: " + events);
for (RecordedEvent event : events) {
if (event.getEventType().getName().equals("dev.elide.TransportTlsHandshake")
&& !drivers.contains(event.getLong("driverId")))
throw new AssertionError("Handshake has no correlated batch driver");
}
System.out.println("Native transport JFR accounting and disabled-event checks passed");
} finally {
Files.deleteIfExists(file);
}
}
}