consensus-raft/src/test/java/com/mboysan/consensus/RaftNodeTest.java
/**
     * Tests append event during a broken quorum and the leader is changed after the quorum is reestablished.
     * Append will fail.
     */
Tests append event during a broken quorum and the leader is changed after the quorum is reestablished.
Append will fail.
Tests append event during a broken quorum and the leader is changed after the quorum is reestablished . Append will fail .
@Test
    void testAppendWhenQuorumNotFormed1() throws Exception {
        int numServers = 5;
        init(numServers);
        int oldLeaderId = assertOneLeader();
        disconnect((oldLeaderId + 1) % numServers);
        disconnect((oldLeaderId + 2) % numServers);
        disconnect((oldLeaderId + 3) % numServers);
        advanceTimeForElections();
        List<String> expectedCommands = List.of("cmd0");
        Future<Boolean> result0 = getNode(oldLeaderId).append(expectedCommands.get(0));
        assertThrows(TimeoutException.class, () -> result0.get(1, TimeUnit.SECONDS));
        kill(oldLeaderId);
        connect((oldLeaderId + 1) % numServers);
        connect((oldLeaderId + 2) % numServers);
        connect((oldLeaderId + 3) % numServers);
        advanceTimeForElections();
        revive(oldLeaderId);    
        advanceTimeForElections();
        assertLeaderChanged(oldLeaderId, true /* oldLeader is aware */);
        synchronized (getNode(oldLeaderId)) {
            getNode(oldLeaderId).notifyAll();
        }
        assertFalse(result0.get());
        expectedCommands = new ArrayList<>();
        assertLogsEquals(expectedCommands);
    }
@ Test void testAppendWhenQuorumNotFormed1 ( ) throws Exception { int numServers = 5 ; init ( numServers ) ; int oldLeaderId = assertOneLeader ( ) ; disconnect ( ( oldLeaderId + 1 ) % numServers ) ; disconnect ( ( oldLeaderId + 2 ) % numServers ) ; disconnect ( ( oldLeaderId + 3 ) % numServers ) ; advanceTimeForElections ( ) ; List < String > expectedCommands = List . of ( "cmd0" ) ; Future < Boolean > result0 = getNode ( oldLeaderId ) . append ( expectedCommands . get ( 0 ) ) ; assertThrows ( TimeoutException . class , ( ) -> result0 . get ( 1 , TimeUnit . SECONDS ) ) ; kill ( oldLeaderId ) ; connect ( ( oldLeaderId + 1 ) % numServers ) ; connect ( ( oldLeaderId + 2 ) % numServers ) ; connect ( ( oldLeaderId + 3 ) % numServers ) ; advanceTimeForElections ( ) ; revive ( oldLeaderId ) ; advanceTimeForElections ( ) ; assertLeaderChanged ( oldLeaderId , true /* oldLeader is aware */ ) ; synchronized ( getNode ( oldLeaderId ) ) { getNode ( oldLeaderId ) . notifyAll ( ) ; } assertFalse ( result0 . get ( ) ) ; expectedCommands = new ArrayList < > ( ) ; assertLogsEquals ( expectedCommands ) ; }
Tests append event during a broken quorum and the leader is changed after the quorum is reestablished.
Tests append event during a broken quorum and the leader is changed after the quorum is reestablished .
// old leader will sync changes // since a new leader will be elected, the old leader will discard the uncommitted command