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

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

/**
 * Integration test for Raft consensus protocol.
 */
public class RaftProtocolTest extends BaseProtocolTest {
    
    @Test
    @DisplayName("Test Raft protocol activation and message sending")
    public void testRaftActivation() {
        SimulationResult result = runSimulation(
            "saved-simulations/raft.dat", 
            2000  // 2 seconds should be enough for elections
        );
        
        ProtocolVerifier verifier = new ProtocolVerifier()
            .expectLogExactly("Raft Consensus Server activated", 3)
            .expectLog("FOLLOWER.*initialized")
            .expectLog("Starting election")
            .expectLog("CANDIDATE")
            .expectMessages()  // Must have messages
            .expectAtLeastNMessages(10);  // Should have many election messages
            
        VerificationResult verification = verifier.verify(result.getAllLogs());
        
        assertTrue(verification.passed(), verification.getFailureMessage());
        assertEquals(3, result.getMetrics().getNumProcesses(), 
                    "Should have 3 processes");
    }
    
    @Test
    @DisplayName("Test Raft election messages")
    public void testRaftElectionMessages() {
        SimulationResult result = runSimulation(
            "saved-simulations/raft.dat", 
            3000
        );
        
        ProtocolVerifier verifier = new ProtocolVerifier()
            .expectLog("REQUEST_VOTE")
            .expectLog("Message sent.*REQUEST_VOTE")
            .expectAtLeastNMessages(15);  // Multiple election rounds
            
        VerificationResult verification = verifier.verify(result.getAllLogs());
        assertTrue(verification.passed(), verification.getFailureMessage());
        
        // Verify term progression
        assertTrue(result.findFirst("term=1").isPresent(), "Should have term 1");
        assertTrue(result.findFirst("term=2").isPresent(), "Should progress to term 2");
    }
    
    @Test
    @DisplayName("Test Raft with clients")
    public void testRaftWithClients() {
        // Skip if file doesn't exist
        if (!new java.io.File("saved-simulations/raft-with-clients.dat").exists()) {
            return;
        }
        
        SimulationResult result = runSimulation(
            "saved-simulations/raft-with-clients.dat", 
            5000
        );
        
        ProtocolVerifier verifier = new ProtocolVerifier()
            .expectLogExactly("Raft Consensus Server activated", 3)
            .expectLogExactly("Raft Consensus Client activated", 2)
            .expectMessages();  // Must have messages
            
        VerificationResult verification = verifier.verify(result.getAllLogs());
        assertTrue(verification.passed(), verification.getFailureMessage());
    }
}