summaryrefslogtreecommitdiff
path: root/src/test/java/testing/protocols/BerkeleyProtocolTest.java
blob: 9dc144d6ba8567c8fc23f07d2e9c22b51a2dd945 (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
package testing.protocols;

import testing.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

/**
 * JUnit test for Berkeley time synchronization protocol.
 */
@DisplayName("Berkeley Time Synchronization Protocol Tests")
public class BerkeleyProtocolTest {
    private HeadlessSimulationRunner runner;
    
    @BeforeEach
    public void setup() {
        runner = new HeadlessSimulationRunner();
    }
    
    @AfterEach
    public void teardown() {
        runner.shutdown();
    }
    
    @Test
    @DisplayName("Test Berkeley protocol activation")
    public void testProtocolActivation() throws Exception {
        SimulationResult result = runner.runSimulation(
            "saved-simulations/berkeley.dat", 
            1000
        );
        
        ProtocolVerifier verifier = new ProtocolVerifier()
            .expectLog("Berkley.*activated|Berkeley.*activated")
            .expectNoLog("ERROR");
            
        VerificationResult verification = verifier.verify(result.getAllLogs());
        
        assertTrue(verification.passed(), verification.getFailureMessage());
        assertTrue(result.getMetrics().getTotalLogCount() > 0, 
                  "Should have some log activity");
    }
    
    @Test
    @DisplayName("Test time synchronization messages")
    public void testTimeSynchronization() throws Exception {
        SimulationResult result = runner.runSimulation(
            "saved-simulations/berkeley.dat", 
            5000
        );
        
        // Berkeley protocol involves time requests and adjustments
        ProtocolVerifier verifier = new ProtocolVerifier()
            .expectLog("time|Time|TIME")
            .expectLog("sync|Sync|SYNC")
            .expectLog("Message sent")
            .expectLog("Message received");
            
        VerificationResult verification = verifier.verify(result.getAllLogs());
        assertTrue(verification.passed(), verification.getFailureMessage());
    }
    
    @Test
    @DisplayName("Test master-slave communication")
    public void testMasterSlaveCommunication() throws Exception {
        SimulationResult result = runner.runSimulation(
            "saved-simulations/berkeley.dat", 
            3000
        );
        
        // Berkeley has master and slave nodes
        boolean hasMasterActivity = result.countLogs("master|Master|MASTER") > 0;
        boolean hasSlaveActivity = result.countLogs("slave|Slave|SLAVE") > 0;
        boolean hasTimeExchange = result.countLogs("time|Time") > 0;
        
        assertTrue(hasMasterActivity || hasSlaveActivity || hasTimeExchange, 
                  "Should have master/slave or time-related activity");
    }
}