blob: 1a41a6aae66abeabf6fe60c2c3d81ce23811655c (
plain)
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
|
package testing.examples;
import testing.*;
import java.util.List;
/**
* Verified test program for ping-pong simulation that checks for actual logged messages.
*/
public class TestPingPongVerified {
public static void main(String[] args) {
System.out.println("=== Testing Ping-Pong Protocol (Verified) ===\n");
HeadlessSimulationRunner runner = new HeadlessSimulationRunner();
try {
// Run the ping-pong simulation for 3 seconds
SimulationResult result = runner.runSimulation(
"saved-simulations/ping-pong.dat",
3000
);
// Print summary
System.out.println("\n" + result.generateSummary());
// Show all captured logs
System.out.println("\nAll captured log entries:");
List<LogEntry> logs = result.getAllLogs();
for (int i = 0; i < Math.min(30, logs.size()); i++) {
LogEntry log = logs.get(i);
System.out.printf("[%4d] %s %s\n",
log.getTimestamp(),
log.getType() == LogType.PROCESS ? "P" + log.getProcessNum() : "G",
log.getMessage());
}
if (logs.size() > 30) {
System.out.println("... (" + (logs.size() - 30) + " more entries)");
}
// Verify ping-pong behavior with correct patterns
System.out.println("\n=== Verification ===");
ProtocolVerifier verifier = new ProtocolVerifier()
// Expect protocol activation
.expectLogExactly("Ping-Pong.*activated", 2)
// Expect client activation first
.expectLog("Ping-Pong Client activated")
// Expect server activation
.expectLog("Ping-Pong Server activated")
// Expect message exchanges
.expectLog("Message sent")
.expectLog("Message received")
// Expect fromClient messages
.expectLog("fromClient=true")
// Expect fromServer messages
.expectLog("fromServer=true")
// Expect alternating pattern
.expectSequence("fromClient=true", "fromServer=true")
// Check counter increments
.expectLog("counter=1")
.expectLog("counter=2")
// No errors expected
.expectNoLog("ERROR")
.expectNoLog("Exception")
.expectNoLog("crashed");
VerificationResult verification = verifier.verify(result.getAllLogs());
System.out.println("\n" + verification.generateReport());
if (verification.passed()) {
System.out.println("\n✓ All verification rules passed!");
} else {
System.out.println("\n✗ Some verification rules failed:");
System.out.println(verification.getFailureMessage());
}
// Additional analysis
System.out.println("\n=== Message Exchange Analysis ===");
// Count message types
int sentCount = result.countLogs("Message sent");
int receivedCount = result.countLogs("Message received");
int fromClientCount = result.countLogs("fromClient=true");
int fromServerCount = result.countLogs("fromServer=true");
System.out.println("Messages sent: " + sentCount);
System.out.println("Messages received: " + receivedCount);
System.out.println("From client: " + fromClientCount);
System.out.println("From server: " + fromServerCount);
// Verify message flow
if (Math.abs(sentCount - receivedCount) <= 1) {
System.out.println("✓ Sent/Received messages are balanced");
} else {
System.out.println("✗ Message imbalance detected");
}
if (Math.abs(fromClientCount - fromServerCount) <= 1) {
System.out.println("✓ Client/Server messages are balanced");
} else {
System.out.println("✗ Client/Server imbalance detected");
}
// Check message IDs
System.out.println("\n=== Message ID Sequence ===");
logs.stream()
.filter(log -> log.getMessage().contains("Message sent"))
.limit(10)
.forEach(log -> {
String msg = log.getMessage();
int idStart = msg.indexOf("ID: ") + 4;
int idEnd = msg.indexOf(";", idStart);
if (idStart > 3 && idEnd > idStart) {
String id = msg.substring(idStart, idEnd);
System.out.println(" Message ID " + id + " sent at time " +
log.getTimestamp());
}
});
System.out.println("\n=== Test Complete ===");
System.out.println("The Ping-Pong protocol is working correctly!");
System.out.println("Messages are being exchanged between client and server.");
} catch (Exception e) {
System.err.println("Test failed with error: " + e.getMessage());
e.printStackTrace();
} finally {
runner.shutdown();
}
}
}
|