Search in sources :

Example 1 with MessageHandler

use of bftsmart.communication.MessageHandler in project bftsmart by blockchain-jd-com.

the class HeartBeatForOtherSizeTest_ method leaderHbTimeoutAndRestartAndNextLeaderRepeatTest.

/**
 * leader超时重启,然后下一个被选中的leader也如此,检查是否可正常
 */
@Test
public void leaderHbTimeoutAndRestartAndNextLeaderRepeatTest() {
    int nodeNum = 5;
    initNode(nodeNum);
    // 重新设置leader的消息处理方式
    MessageHandler mockMessageHandler = spy(serverCommunicationSystems[0].getMessageHandler());
    // mock messageHandler对消息应答的处理
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if (obj instanceof LCMessage) {
                    // 走我们设计的逻辑,即不处理
                    System.out.println("0 receive leader change message !");
                } else if (obj instanceof LeaderResponseMessage) {
                    System.out.println("0 receive leader response message !");
                    invocationOnMock.callRealMethod();
                } else if (obj instanceof HeartBeatMessage) {
                    System.out.printf("0 receive heart beat message from %s !\r\n", ((HeartBeatMessage) obj).getSender());
                    invocationOnMock.callRealMethod();
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
    serverCommunicationSystems[0].setMessageHandler(mockMessageHandler);
    // 领导者心跳停止
    stopLeaderHeartBeat(serviceReplicas);
    System.out.println("-- stopLeaderHeartBeat --");
    try {
        // 休眠20s
        Thread.sleep(60000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 重启之前领导者心跳服务
    restartLeaderHeartBeat(serviceReplicas, 0);
    System.out.println("-- restartLeaderHeartBeat --");
    // 重置mock操作
    reset(mockMessageHandler);
    try {
        Thread.sleep(30000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 当前领导者异常,当前领导者为peer1
    mockMessageHandler = spy(serverCommunicationSystems[1].getMessageHandler());
    // mock messageHandler对消息应答的处理
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if (obj instanceof LCMessage) {
                    // 走我们设计的逻辑,即不处理
                    System.out.println("1 receive leader change message !");
                } else if (obj instanceof LeaderResponseMessage) {
                    System.out.println("1 receive leader response message !");
                    invocationOnMock.callRealMethod();
                } else if (obj instanceof HeartBeatMessage) {
                    System.out.printf("1 receive heart beat message from %s !\r\n", ((HeartBeatMessage) obj).getSender());
                    invocationOnMock.callRealMethod();
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
    serverCommunicationSystems[1].setMessageHandler(mockMessageHandler);
    // 领导者心跳停止
    stopLeaderHeartBeat(serviceReplicas);
    System.out.println("-- stopLeaderHeartBeat --");
    try {
        // 休眠20s
        Thread.sleep(60000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 重启之前领导者心跳服务
    restartLeaderHeartBeat(serviceReplicas, 1);
    System.out.println("-- restartLeaderHeartBeat --");
    // 重置mock操作
    reset(mockMessageHandler);
    try {
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) LCMessage(bftsmart.tom.leaderchange.LCMessage) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LeaderResponseMessage(bftsmart.tom.leaderchange.LeaderResponseMessage) HeartBeatMessage(bftsmart.tom.leaderchange.HeartBeatMessage) Test(org.junit.Test)

Example 2 with MessageHandler

use of bftsmart.communication.MessageHandler in project bftsmart by blockchain-jd-com.

the class LCForTOMMessageTest_ method test6NodeLeaderChangeBut2ContinueException.

@Test
public void test6NodeLeaderChangeBut2ContinueException() {
    int nodeNums = 6;
    initNode(nodeNums);
    // 停掉第5个节点
    MessageHandler mockMessageHandler4 = spy(serverCommunicationSystems[4].getMessageHandler());
    // mock messageHandler4对消息应答的处理, 让节点4网络异常
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if ((obj instanceof LCMessage) || (obj instanceof LeaderResponseMessage) || (obj instanceof HeartBeatMessage)) {
                // 走我们设计的逻辑,即不处理
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler4).processData(any());
    serverCommunicationSystems[4].setMessageHandler(mockMessageHandler4);
    // 停止节点4因为心跳超时而重传STOP消息
    serviceReplicas[4].getTomLayer().heartBeatTimer.stopAll();
    // 停掉第6个节点
    MessageHandler mockMessageHandler5 = spy(serverCommunicationSystems[5].getMessageHandler());
    // mock messageHandler5对消息应答的处理, 让节点5网络异常
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if ((obj instanceof LCMessage) || (obj instanceof LeaderResponseMessage) || (obj instanceof HeartBeatMessage)) {
                // 走我们设计的逻辑,即不处理
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler5).processData(any());
    serverCommunicationSystems[5].setMessageHandler(mockMessageHandler5);
    // 停止节点4因为心跳超时而重传STOP消息
    serviceReplicas[5].getTomLayer().heartBeatTimer.stopAll();
    /**
     * 预期结果:
     * 第1个节点触发异常,则会选举2,直到第4个节点异常,则重新选举5,但是因为第5,6个节点一直异常,所以一会儿超时会重新选举0
     */
    for (int i = 0; i < nodeNums - 2; i++) {
        final int index = i;
        MessageHandler mockMessageHandler = stopNode(index);
        // 重启之前领导者心跳服务
        restartLeaderHeartBeat(serviceReplicas, index);
        System.out.printf("-- restart %s LeaderHeartBeat -- \r\n", index);
        // 重置mock操作
        reset(mockMessageHandler);
        try {
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 第5个节点网络恢复
    System.out.println("-- Node 5 network recovery --");
    reset(mockMessageHandler4);
    // 第6个节点网络恢复
    System.out.println("-- Node 6 network recovery --");
    reset(mockMessageHandler5);
    try {
        System.out.println("-- total node has complete change --");
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) LCMessage(bftsmart.tom.leaderchange.LCMessage) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LeaderResponseMessage(bftsmart.tom.leaderchange.LeaderResponseMessage) HeartBeatMessage(bftsmart.tom.leaderchange.HeartBeatMessage) Test(org.junit.Test)

Example 3 with MessageHandler

use of bftsmart.communication.MessageHandler in project bftsmart by blockchain-jd-com.

the class LCForTOMMessageTest_ method test7NodeLeaderChange.

/**
 * 测试7个节点循环进行一次领导者切换,看最终是否可以恢复正常
 */
@Test
public void test7NodeLeaderChange() {
    int nodeNums = 7;
    initNode(nodeNums);
    for (int i = 0; i < nodeNums; i++) {
        final int index = i;
        // 重新设置leader的消息处理方式
        MessageHandler mockMessageHandler = spy(serverCommunicationSystems[i].getMessageHandler());
        // mock messageHandler对消息应答的处理
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                Object[] objs = invocationOnMock.getArguments();
                if (objs == null || objs.length != 1) {
                    invocationOnMock.callRealMethod();
                } else {
                    Object obj = objs[0];
                    if (obj instanceof LCMessage) {
                        // 走我们设计的逻辑,即不处理
                        System.out.println(index + " receive leader change message !");
                    } else if (obj instanceof LeaderResponseMessage) {
                        System.out.println(index + " receive leader response message !");
                        invocationOnMock.callRealMethod();
                    } else if (obj instanceof HeartBeatMessage) {
                        System.out.printf(index + " receive heart beat message from %s !\r\n", ((HeartBeatMessage) obj).getSender());
                        invocationOnMock.callRealMethod();
                    } else {
                        invocationOnMock.callRealMethod();
                    }
                }
                return null;
            }
        }).when(mockMessageHandler).processData(any());
        serverCommunicationSystems[index].setMessageHandler(mockMessageHandler);
        // 领导者心跳停止
        stopLeaderHeartBeat(serviceReplicas);
        System.out.printf("-- stop %s LeaderHeartBeat -- \r\n", index);
        try {
            // 休眠40s,等待领导者切换完成
            Thread.sleep(40000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 重启之前领导者心跳服务
        restartLeaderHeartBeat(serviceReplicas, index);
        System.out.printf("-- restart %s LeaderHeartBeat -- \r\n", index);
        // 重置mock操作
        reset(mockMessageHandler);
        try {
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    try {
        System.out.println("-- total node has complete change --");
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) LCMessage(bftsmart.tom.leaderchange.LCMessage) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LeaderResponseMessage(bftsmart.tom.leaderchange.LeaderResponseMessage) HeartBeatMessage(bftsmart.tom.leaderchange.HeartBeatMessage) Test(org.junit.Test)

Example 4 with MessageHandler

use of bftsmart.communication.MessageHandler in project bftsmart by blockchain-jd-com.

the class HeartBeatTest_ method leaderStopButUnTimeoutTest.

// @Test
// public void oneUnleaderHbTimeoutTest() {
// Mockito.doAnswer(new Answer() {
// @Override
// public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
// Thread.sleep(30000);
// return invocationOnMock.callRealMethod();
// }
// }).when(mockHbTimers[3]).receiveHeartBeatMessage(any());
// 
// 
// try {
// Thread.sleep(Integer.MAX_VALUE);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// @Test
// public void twoUnleaderHbTimeoutTest() {
// isTestTurnOn.set(true);
// 
// Mockito.doAnswer(new Answer() {
// @Override
// public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
// if (isTestTurnOn.get()) {
// Thread.sleep(30000);
// }
// isTestTurnOn.set(false);
// return invocationOnMock.callRealMethod();
// }
// }).when(mockHbTimers[3]).receiveHeartBeatMessage(any());
// 
// isTestTurnOn1.set(true);
// Mockito.doAnswer(new Answer() {
// @Override
// public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
// if (isTestTurnOn1.get()) {
// Thread.sleep(30000);
// }
// isTestTurnOn1.set(false);
// return invocationOnMock.callRealMethod();
// }
// }).when(mockHbTimers[2]).receiveHeartBeatMessage(any());
// 
// try {
// Thread.sleep(Integer.MAX_VALUE);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/**
 * 领导者少发了部分消息,但是不会触发超时
 * 理论上不应该触发领导者切换
 */
@Test
public void leaderStopButUnTimeoutTest() {
    // 重新设置leader的消息处理方式
    MessageHandler mockMessageHandler = spy(serverCommunicationSystems[0].getMessageHandler());
    // mock messageHandler对消息应答的处理
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if (obj instanceof LCMessage) {
                    // 走我们设计的逻辑,即不处理
                    System.out.println("receive leader change message !");
                } else if (obj instanceof LeaderResponseMessage) {
                    System.out.println("receive leader response message !");
                    invocationOnMock.callRealMethod();
                } else if (obj instanceof HeartBeatMessage) {
                    System.out.printf("0 receive heart beat message from %s !\r\n", ((HeartBeatMessage) obj).getSender());
                    invocationOnMock.callRealMethod();
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
    serverCommunicationSystems[0].setMessageHandler(mockMessageHandler);
    // 领导者心跳停止
    stopLeaderHeartBeat(serviceReplicas);
    System.out.println("-- stopLeaderHeartBeat");
    try {
        // 休眠20s
        Thread.sleep(10000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 重启之前领导者心跳服务
    restartLeaderHeartBeat(serviceReplicas, 0);
    System.out.println("-- restartLeaderHeartBeat");
    try {
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) LCMessage(bftsmart.tom.leaderchange.LCMessage) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LeaderResponseMessage(bftsmart.tom.leaderchange.LeaderResponseMessage) HeartBeatMessage(bftsmart.tom.leaderchange.HeartBeatMessage) Test(org.junit.Test)

Example 5 with MessageHandler

use of bftsmart.communication.MessageHandler in project bftsmart by blockchain-jd-com.

the class HeartBeatTest_ method leaderHbTimeoutAndRestartAndNextLeaderRepeatTest.

/**
 * leader超时重启,然后下一个被选中的leader也如此,检查是否可正常
 */
@Test
public void leaderHbTimeoutAndRestartAndNextLeaderRepeatTest() {
    // 重新设置leader的消息处理方式
    MessageHandler mockMessageHandler = spy(serverCommunicationSystems[0].getMessageHandler());
    // mock messageHandler对消息应答的处理
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if (obj instanceof LCMessage) {
                    // 走我们设计的逻辑,即不处理
                    System.out.println("0 receive leader change message !");
                } else if (obj instanceof LeaderResponseMessage) {
                    System.out.println("0 receive leader response message !");
                    invocationOnMock.callRealMethod();
                } else if (obj instanceof HeartBeatMessage) {
                    System.out.printf("0 receive heart beat message from %s !\r\n", ((HeartBeatMessage) obj).getSender());
                    invocationOnMock.callRealMethod();
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
    serverCommunicationSystems[0].setMessageHandler(mockMessageHandler);
    // 领导者心跳停止
    stopLeaderHeartBeat(serviceReplicas);
    System.out.println("-- stopLeaderHeartBeat --");
    try {
        // 休眠20s
        Thread.sleep(60000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 重启之前领导者心跳服务
    restartLeaderHeartBeat(serviceReplicas, 0);
    System.out.println("-- restartLeaderHeartBeat --");
    // 重置mock操作
    reset(mockMessageHandler);
    try {
        Thread.sleep(30000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 当前领导者异常,当前领导者为peer1
    mockMessageHandler = spy(serverCommunicationSystems[1].getMessageHandler());
    // mock messageHandler对消息应答的处理
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (objs == null || objs.length != 1) {
                invocationOnMock.callRealMethod();
            } else {
                Object obj = objs[0];
                if (obj instanceof LCMessage) {
                    // 走我们设计的逻辑,即不处理
                    System.out.println("1 receive leader change message !");
                } else if (obj instanceof LeaderResponseMessage) {
                    System.out.println("1 receive leader response message !");
                    invocationOnMock.callRealMethod();
                } else if (obj instanceof HeartBeatMessage) {
                    System.out.printf("1 receive heart beat message from %s !\r\n", ((HeartBeatMessage) obj).getSender());
                    invocationOnMock.callRealMethod();
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
    serverCommunicationSystems[1].setMessageHandler(mockMessageHandler);
    // 领导者心跳停止
    stopLeaderHeartBeat(serviceReplicas);
    System.out.println("-- stopLeaderHeartBeat --");
    try {
        // 休眠20s
        Thread.sleep(60000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 重启之前领导者心跳服务
    restartLeaderHeartBeat(serviceReplicas, 1);
    System.out.println("-- restartLeaderHeartBeat --");
    // 重置mock操作
    reset(mockMessageHandler);
    try {
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) LCMessage(bftsmart.tom.leaderchange.LCMessage) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LeaderResponseMessage(bftsmart.tom.leaderchange.LeaderResponseMessage) HeartBeatMessage(bftsmart.tom.leaderchange.HeartBeatMessage) Test(org.junit.Test)

Aggregations

MessageHandler (bftsmart.communication.MessageHandler)39 Test (org.junit.Test)29 Mockito.doAnswer (org.mockito.Mockito.doAnswer)26 InvocationOnMock (org.mockito.invocation.InvocationOnMock)26 Answer (org.mockito.stubbing.Answer)26 LCMessage (bftsmart.tom.leaderchange.LCMessage)24 HeartBeatMessage (bftsmart.tom.leaderchange.HeartBeatMessage)23 LeaderResponseMessage (bftsmart.tom.leaderchange.LeaderResponseMessage)23 ServerCommunicationSystemImpl (bftsmart.communication.ServerCommunicationSystemImpl)5 ConsensusMessage (bftsmart.consensus.messages.ConsensusMessage)2 TOMLayer (bftsmart.tom.core.TOMLayer)2 CommunicationLayer (bftsmart.communication.CommunicationLayer)1 ServerCommunicationSystem (bftsmart.communication.ServerCommunicationSystem)1 ClientCommunicationServerSide (bftsmart.communication.client.ClientCommunicationServerSide)1 ServerViewController (bftsmart.reconfiguration.ServerViewController)1 TOMConfiguration (bftsmart.reconfiguration.util.TOMConfiguration)1 MemoryBasedViewStorage (bftsmart.reconfiguration.views.MemoryBasedViewStorage)1 ServiceReplica (bftsmart.tom.ServiceReplica)1 TOMMessage (bftsmart.tom.core.messages.TOMMessage)1 IOException (java.io.IOException)1