summaryrefslogtreecommitdiff
path: root/src/main/java/testing/ProtocolTestRunner.java
blob: f035325053517891234f1a65740f3dbe9b354949 (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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package testing;

import java.util.*;

/**
 * Runs all protocol tests and reports results.
 * This is a standalone test runner that doesn't require JUnit.
 */
public class ProtocolTestRunner {
    
    private static class TestCase {
        final String name;
        final String simulationFile;
        final long duration;
        final ProtocolVerifier verifier;
        
        TestCase(String name, String simulationFile, long duration, ProtocolVerifier verifier) {
            this.name = name;
            this.simulationFile = simulationFile;
            this.duration = duration;
            this.verifier = verifier;
        }
    }
    
    public static void main(String[] args) {
        System.out.println("=== DS-Sim Protocol Test Runner ===\n");
        
        // Check for verbose flag
        boolean verbose = args.length > 0 && 
            (args[0].equals("-v") || args[0].equals("--verbose"));
        
        List<TestCase> tests = createTestCases();
        int passed = 0;
        int failed = 0;
        
        HeadlessSimulationRunner runner = new HeadlessSimulationRunner();
        runner.setPrintLogs(verbose);
        
        for (TestCase test : tests) {
            System.out.println("\n" + "=".repeat(60));
            System.out.println("Testing " + test.name);
            System.out.println("Simulation: " + test.simulationFile);
            System.out.println("=".repeat(60));
            
            try {
                SimulationResult result = runner.runSimulation(
                    test.simulationFile, 
                    test.duration
                );
                
                if (!verbose) {
                    System.out.println("\nCaptured " + result.getAllLogs().size() + " log entries");
                }
                
                VerificationResult verification = test.verifier.verify(result.getAllLogs());
                
                if (verification.passed()) {
                    System.out.println("\n✓ PASSED");
                    passed++;
                } else {
                    System.out.println("\n✗ FAILED");
                    System.out.println("  " + verification.getFailureMessage());
                    if (!verbose && result.getAllLogs().size() > 0) {
                        System.out.println("\n  First few logs:");
                        result.getAllLogs().stream()
                            .limit(5)
                            .forEach(log -> System.out.println("    " + log));
                    }
                    failed++;
                }
                
            } catch (Exception e) {
                System.out.println("\n✗ ERROR: " + e.getMessage());
                if (verbose) {
                    e.printStackTrace();
                }
                failed++;
            }
        }
        
        runner.shutdown();
        
        System.out.println("\n" + "=".repeat(60));
        System.out.println("=== Summary ===");
        System.out.println("Total tests: " + tests.size());
        System.out.println("Passed: " + passed);
        System.out.println("Failed: " + failed);
        
        if (failed == 0) {
            System.out.println("\n✓ All tests passed!");
            System.exit(0);
        } else {
            System.out.println("\n✗ Some tests failed!");
            System.out.println("\nRun with -v or --verbose to see detailed logs");
            System.exit(1);
        }
    }
    
    private static List<TestCase> createTestCases() {
        List<TestCase> tests = new ArrayList<>();
        
        // Ping-Pong
        tests.add(new TestCase(
            "Ping-Pong",
            "saved-simulations/ping-pong.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Ping-Pong.*activated")
                .expectLog("Message sent")
                .expectLog("Message received")
                .expectNoLog("ERROR")
        ));
        
        // Ping-Pong Sturm
        tests.add(new TestCase(
            "Ping-Pong Sturm",
            "saved-simulations/ping-pong-sturm.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Ping-Pong.*activated")
                .expectLog("Message")
                .expectNoLog("ERROR")
        ));
        
        // Broadcast
        tests.add(new TestCase(
            "Broadcast",
            "saved-simulations/broadcast.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Broadcast.*activated")
                .expectLog("Message")
                .expectNoLog("ERROR")
        ));
        
        // Basic Multicast
        tests.add(new TestCase(
            "Basic Multicast",
            "saved-simulations/basic-multicast.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Basic Multicast.*activated|Multicast.*activated")
                .expectLog("Message")
                .expectNoLog("ERROR")
        ));
        
        // Reliable Multicast
        tests.add(new TestCase(
            "Reliable Multicast",
            "saved-simulations/reliable-multicast.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Reliable Multicast.*activated")
                .expectLog("Message")
                .expectNoLog("ERROR")
        ));
        
        // Berkeley Time Sync
        tests.add(new TestCase(
            "Berkeley Time Sync",
            "saved-simulations/berkeley.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Berkley.*activated|Berkeley.*activated")
                .expectNoLog("ERROR")
        ));
        
        // Internal Time Sync
        tests.add(new TestCase(
            "Internal Time Sync",
            "saved-simulations/int-sync.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("Internal.*sync.*activated")
                .expectNoLog("ERROR")
        ));
        
        // External vs Internal Sync
        tests.add(new TestCase(
            "External vs Internal Sync",
            "saved-simulations/ext-vs-int-sync.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("activated")
                .expectNoLog("ERROR")
        ));
        
        // One-Phase Commit
        tests.add(new TestCase(
            "One-Phase Commit",
            "saved-simulations/one-phase-commit.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("1-Phase Commit.*activated")
                .expectNoLog("ERROR")
        ));
        
        // Two-Phase Commit
        tests.add(new TestCase(
            "Two-Phase Commit",
            "saved-simulations/two-phase-commit.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("2-Phase Commit.*activated")
                .expectNoLog("ERROR")
        ));
        
        // Slow Connection
        tests.add(new TestCase(
            "Slow Connection",
            "saved-simulations/slow-connection.dat",
            2000,
            new ProtocolVerifier()
                .expectLog("activated")
                .expectNoLog("ERROR")
        ));
        
        return tests;
    }
}