diff options
Diffstat (limited to 'src/main/java/testing/examples')
| -rw-r--r-- | src/main/java/testing/examples/InteractiveTest.java | 66 | ||||
| -rw-r--r-- | src/main/java/testing/examples/QuickTest.java | 40 | ||||
| -rw-r--r-- | src/main/java/testing/examples/TestPingPongSimulation.java | 138 | ||||
| -rw-r--r-- | src/main/java/testing/examples/TestPingPongVerified.java | 132 |
4 files changed, 376 insertions, 0 deletions
diff --git a/src/main/java/testing/examples/InteractiveTest.java b/src/main/java/testing/examples/InteractiveTest.java new file mode 100644 index 0000000..8cc93e8 --- /dev/null +++ b/src/main/java/testing/examples/InteractiveTest.java @@ -0,0 +1,66 @@ +package testing.examples; + +import testing.*; +import java.util.Scanner; + +public class InteractiveTest { + public static void main(String[] args) throws Exception { + Scanner scanner = new Scanner(System.in); + HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); + + System.out.println("=== Interactive Headless Test ==="); + System.out.println("\nAvailable simulations:"); + System.out.println("1. ping-pong.dat"); + System.out.println("2. broadcast.dat"); + System.out.println("3. berkeley.dat"); + System.out.println("4. raft-working.dat"); + + System.out.print("\nEnter simulation filename (or full path): "); + String filename = scanner.nextLine(); + + // Add saved-simulations/ prefix if not present + if (!filename.contains("/")) { + filename = "saved-simulations/" + filename; + } + + System.out.print("Run duration in ms (default 2000): "); + String durationStr = scanner.nextLine(); + long duration = durationStr.isEmpty() ? 2000 : Long.parseLong(durationStr); + + System.out.print("Pattern to search for (optional): "); + String pattern = scanner.nextLine(); + + try { + System.out.println("\nRunning simulation..."); + SimulationResult result = runner.runSimulation(filename, duration); + + System.out.println("\nResults:"); + System.out.println("- Total logs: " + result.getAllLogs().size()); + System.out.println("- Processes: " + result.getMetrics().getNumProcesses()); + + if (!pattern.isEmpty()) { + int count = result.countLogs(pattern); + System.out.println("- Pattern '" + pattern + "' found: " + count + " times"); + + if (count > 0) { + System.out.println("\nMatching logs:"); + result.findAll(pattern).stream() + .limit(5) + .forEach(log -> System.out.println(" " + log)); + } + } + + System.out.println("\nFirst 10 logs:"); + result.getAllLogs().stream() + .limit(10) + .forEach(log -> System.out.println(" [" + log.getTimestamp() + "] " + + log.getMessage())); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + } finally { + runner.shutdown(); + scanner.close(); + } + } +}
\ No newline at end of file diff --git a/src/main/java/testing/examples/QuickTest.java b/src/main/java/testing/examples/QuickTest.java new file mode 100644 index 0000000..f6f3e86 --- /dev/null +++ b/src/main/java/testing/examples/QuickTest.java @@ -0,0 +1,40 @@ +package testing.examples; + +import testing.*; + +public class QuickTest { + public static void main(String[] args) throws Exception { + // Use command line arg or default + String simulationFile = args.length > 0 ? args[0] : "saved-simulations/ping-pong.dat"; + long duration = args.length > 1 ? Long.parseLong(args[1]) : 1000; + + if (args.length == 0) { + System.out.println("=== Quick Headless Test ===\n"); + } + + HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); + + try { + SimulationResult result = runner.runSimulation( + simulationFile, + duration + ); + + System.out.println("Captured " + result.getAllLogs().size() + " logs"); + System.out.println("\nFirst 5 logs:"); + result.getAllLogs().stream() + .limit(5) + .forEach(log -> System.out.println(" " + log)); + + // Simple verification + boolean hasActivation = result.countLogs("activated") > 0; + boolean hasMessages = result.countLogs("Message") > 0; + + System.out.println("\n✓ Protocol activated: " + hasActivation); + System.out.println("✓ Messages exchanged: " + hasMessages); + + } finally { + runner.shutdown(); + } + } +}
\ No newline at end of file diff --git a/src/main/java/testing/examples/TestPingPongSimulation.java b/src/main/java/testing/examples/TestPingPongSimulation.java new file mode 100644 index 0000000..9f16a27 --- /dev/null +++ b/src/main/java/testing/examples/TestPingPongSimulation.java @@ -0,0 +1,138 @@ +package testing.examples; + +import testing.*; +import java.util.List; + +/** + * Test program to verify the headless testing framework with ping-pong simulation. + * This demonstrates how to use the framework to verify protocol behavior. + */ +public class TestPingPongSimulation { + + public static void main(String[] args) { + System.out.println("=== Testing Ping-Pong Protocol ===\n"); + + HeadlessSimulationRunner runner = new HeadlessSimulationRunner(); + + try { + // Run the ping-pong simulation for 2 seconds + SimulationResult result = runner.runSimulation( + "saved-simulations/ping-pong.dat", + 2000 + ); + + // Print summary + System.out.println("\n" + result.generateSummary()); + + // Show first 20 logs + System.out.println("\nFirst 20 log entries:"); + List<LogEntry> logs = result.getAllLogs(); + for (int i = 0; i < Math.min(20, logs.size()); i++) { + System.out.println(" " + logs.get(i)); + } + + // Verify ping-pong behavior + System.out.println("\n=== Verification ==="); + + ProtocolVerifier verifier = new ProtocolVerifier() + // Expect protocol activation + .expectLog("Ping-Pong.*activated") + // Expect ping messages + .expectLog("ping") + // Expect pong responses + .expectLog("pong") + // Expect alternating sequence + .expectSequence("ping", "pong") + // No errors expected + .expectNoLog("ERROR") + .expectNoLog("Exception"); + + 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=== Protocol Analysis ==="); + + // Count ping and pong messages + int pingCount = result.countLogs("ping"); + int pongCount = result.countLogs("pong"); + + System.out.println("Ping messages: " + pingCount); + System.out.println("Pong messages: " + pongCount); + + // Check balance + if (Math.abs(pingCount - pongCount) <= 1) { + System.out.println("✓ Ping/Pong messages are balanced"); + } else { + System.out.println("✗ Ping/Pong imbalance detected"); + } + + // Check for message patterns by process + System.out.println("\n=== Per-Process Analysis ==="); + for (int i = 0; i < result.getMetrics().getNumProcesses(); i++) { + List<LogEntry> processLogs = result.getLogsForProcess(i); + if (!processLogs.isEmpty()) { + System.out.println("Process " + i + ":"); + System.out.println(" Total messages: " + processLogs.size()); + + long pings = processLogs.stream() + .filter(log -> log.getMessage().contains("ping")) + .count(); + long pongs = processLogs.stream() + .filter(log -> log.getMessage().contains("pong")) + .count(); + + System.out.println(" Pings sent: " + pings); + System.out.println(" Pongs sent: " + pongs); + } + } + + // Test real-time monitoring + System.out.println("\n=== Testing Real-time Monitoring ==="); + + HeadlessSimulationRunner runner2 = new HeadlessSimulationRunner(); + + // Add a listener that prints ping/pong messages in real-time + class PingPongMonitor implements LogListener { + private int pingCount = 0; + private int pongCount = 0; + + @Override + public void onLogEntry(LogEntry entry) { + if (entry.getMessage().contains("ping")) { + pingCount++; + if (pingCount <= 5) { + System.out.println(" [MONITOR] Ping #" + pingCount + + " at time " + entry.getTimestamp()); + } + } else if (entry.getMessage().contains("pong")) { + pongCount++; + if (pongCount <= 5) { + System.out.println(" [MONITOR] Pong #" + pongCount + + " at time " + entry.getTimestamp()); + } + } + } + } + + // Note: We'd need to modify HeadlessSimulationRunner to expose + // the LogCapture to add listeners, but this shows the concept + + System.out.println("\n=== Test Complete ==="); + + } catch (Exception e) { + System.err.println("Test failed with error: " + e.getMessage()); + e.printStackTrace(); + } finally { + runner.shutdown(); + } + } +}
\ No newline at end of file diff --git a/src/main/java/testing/examples/TestPingPongVerified.java b/src/main/java/testing/examples/TestPingPongVerified.java new file mode 100644 index 0000000..1a41a6a --- /dev/null +++ b/src/main/java/testing/examples/TestPingPongVerified.java @@ -0,0 +1,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(); + } + } +}
\ No newline at end of file |
