summaryrefslogtreecommitdiff
path: root/src/main/java/testing/HeadlessProtocolRunner.java
blob: a6098de1187b6e2f413707cd4bce354cf19f7919 (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
133
134
135
136
137
138
139
140
141
142
143
package testing;

import java.io.File;
import java.util.*;

/**
 * Runs protocol tests in headless mode without GUI errors.
 * This replaces the old test runners that had GUI dependency issues.
 */
public class HeadlessProtocolRunner {
    
    public static void main(String[] args) throws Exception {
        System.out.println("=== DS-Sim Headless Protocol Test Runner ===\n");
        
        // Check for verbose mode
        boolean verbose = Boolean.getBoolean("ds.sim.verbose");
        
        if (args.length > 0) {
            // Run specific simulation
            runSingleSimulation(args[0], verbose);
        } else {
            // Run all simulations
            runAllSimulations(verbose);
        }
    }
    
    private static void runSingleSimulation(String simFile, boolean verbose) throws Exception {
        System.out.println("Running simulation: " + simFile);
        System.out.println("-".repeat(50));
        
        HeadlessSimulationRunner runner = new HeadlessSimulationRunner();
        runner.setPrintLogs(verbose); // This will print logs in real-time during simulation
        
        if (verbose) {
            System.out.println("\n--- Real-Time Simulation Log Output ---");
            System.out.println("(Logs show protocol actions as they happen)\n");
        }
        
        try {
            long startTime = System.currentTimeMillis();
            SimulationResult result = runner.runSimulation(simFile, 10000); // 10 second timeout
            long duration = System.currentTimeMillis() - startTime;
            
            if (verbose) {
                System.out.println("--- End of Simulation ---\n");
            }
            
            System.out.println("✓ Completed in " + duration + "ms");
            System.out.println("  Processes: " + result.getMetrics().getNumProcesses());
            System.out.println("  Log entries: " + result.getMetrics().getTotalLogCount());
            System.out.println("  Messages per process: " + result.getMetrics().getProcessMessageCounts());
            
            // Count total messages sent
            int totalMessages = result.getMetrics().getTotalMessageCount();
            System.out.println("  Total messages sent: " + totalMessages);
            
            // Check if any messages were sent
            if (totalMessages == 0) {
                System.err.println("\n⚠️  WARNING: No messages were sent during simulation!");
                System.err.println("   This indicates the protocol may not be functioning correctly.");
                if (!verbose) {
                    System.err.println("   Re-run with -Dds.sim.verbose=true for detailed output.");
                }
                // Mark as failure
                throw new RuntimeException("Protocol test failed: No messages sent");
            }
            
            System.out.println();
        } catch (Exception e) {
            System.err.println("✗ FAILED: " + e.getMessage());
            if (verbose) {
                e.printStackTrace();
            }
        } finally {
            runner.shutdown();
        }
        
        // Exit explicitly when running single simulation
        System.exit(0);
    }
    
    private static void runAllSimulations(boolean verbose) throws Exception {
        File simDir = new File("saved-simulations");
        File[] simFiles = simDir.listFiles((dir, name) -> name.endsWith(".dat"));
        
        if (simFiles == null || simFiles.length == 0) {
            System.out.println("No simulation files found in saved-simulations/");
            return;
        }
        
        Arrays.sort(simFiles);
        
        System.out.println("Found " + simFiles.length + " simulations to test\n");
        
        int passed = 0;
        int failed = 0;
        List<String> failures = new ArrayList<>();
        
        for (File simFile : simFiles) {
            System.out.println("Testing: " + simFile.getName());
            System.out.println("-".repeat(50));
            
            HeadlessSimulationRunner runner = new HeadlessSimulationRunner();
            runner.setPrintLogs(verbose); // Print logs if verbose mode is enabled
            
            try {
                long startTime = System.currentTimeMillis();
                SimulationResult result = runner.runSimulation(simFile.getPath(), 10000); // 10 second timeout
                long duration = System.currentTimeMillis() - startTime;
                
                System.out.println("✓ PASSED in " + duration + "ms");
                System.out.println("  Logs: " + result.getMetrics().getTotalLogCount());
                passed++;
                
            } catch (Exception e) {
                System.err.println("✗ FAILED: " + e.getMessage());
                failed++;
                failures.add(simFile.getName() + " - " + e.getMessage());
            } finally {
                runner.shutdown();
            }
            
            System.out.println();
        }
        
        // Summary
        System.out.println("=".repeat(60));
        System.out.println("Test Summary:");
        System.out.println("  Total: " + simFiles.length);
        System.out.println("  Passed: " + passed);
        System.out.println("  Failed: " + failed);
        
        if (!failures.isEmpty()) {
            System.out.println("\nFailures:");
            for (String failure : failures) {
                System.out.println("  - " + failure);
            }
        }
        
        System.out.println();
        System.exit(failed > 0 ? 1 : 0);
    }
}