Search in sources :

Example 1 with ServerCommunicationSystemImpl

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

the class ConsensusTest_ method mockCsNewLeaderDiscardSync.

private void mockCsNewLeaderDiscardSync(int nodeId) {
    ServerCommunicationSystemImpl mockServerCommunicationSystem = (ServerCommunicationSystemImpl) serverNodes[nodeId].getReplica().getServerCommunicationSystem();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            Object obj = objs[1];
            if ((obj instanceof LCMessage) && (((LCMessage) obj).getType().CODE == LCType.SYNC.CODE) && !newLeaderDiscardSyncMsgEnable) {
                newLeaderDiscardSyncMsgEnable = true;
            } else {
                invocationOnMock.callRealMethod();
            }
            return null;
        }
    }).when(mockServerCommunicationSystem).send(any(), (SystemMessage) any());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ServerCommunicationSystemImpl(bftsmart.communication.ServerCommunicationSystemImpl) LCMessage(bftsmart.tom.leaderchange.LCMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 2 with ServerCommunicationSystemImpl

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

the class ConsensusTest_ method mockMessageHandlerTest4Nodes.

private void mockMessageHandlerTest4Nodes(int nodeId) {
    ServerCommunicationSystemImpl serverCommunicationSystem = (ServerCommunicationSystemImpl) serverNodes[nodeId].getReplica().getServerCommunicationSystem();
    MessageHandler mockMessageHandler = serverCommunicationSystem.getMessageHandler();
    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 ConsensusMessage) {
                // 对于消息不做任何处理
                } else if (obj instanceof LCMessage) {
                    Mockito.reset(mockMessageHandler);
                    invocationOnMock.callRealMethod();
                } else {
                    invocationOnMock.callRealMethod();
                }
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ServerCommunicationSystemImpl(bftsmart.communication.ServerCommunicationSystemImpl) ConsensusMessage(bftsmart.consensus.messages.ConsensusMessage) LCMessage(bftsmart.tom.leaderchange.LCMessage) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 3 with ServerCommunicationSystemImpl

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

the class ConsensusTest_ method mockCsNormalNode5.

private void mockCsNormalNode5(int nodeId) {
    ServerCommunicationSystemImpl mockServerCommunicationSystem = (ServerCommunicationSystemImpl) serverNodes[nodeId].getReplica().getServerCommunicationSystem();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            Object obj = objs[1];
            if (newLeaderDiscardSendMsgEnable) {
            } else {
                invocationOnMock.callRealMethod();
            }
            return null;
        }
    }).when(mockServerCommunicationSystem).send(any(), (SystemMessage) any());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ServerCommunicationSystemImpl(bftsmart.communication.ServerCommunicationSystemImpl) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 4 with ServerCommunicationSystemImpl

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

the class TestNodeServer method startNode.

public ServiceReplica startNode(String realmName) {
    TOMConfiguration config = initConfig();
    try {
        // mock messsageHandler and cs
        MessageHandler messageHandler = new MessageHandler();
        MessageHandler mockMessageHandler = Mockito.spy(messageHandler);
        ClientCommunicationServerSide clientCommunication = ClientCommunicationFactory.createServerSide(new ServerViewController(config, new MemoryBasedViewStorage(latestView)));
        ServerCommunicationSystem cs = new ServerCommunicationSystemImpl(clientCommunication, mockMessageHandler, new ServerViewController(config, new MemoryBasedViewStorage(latestView)), realmName);
        ServerCommunicationSystem mockCs = Mockito.spy(cs);
        replica = new ServiceReplica(mockMessageHandler, mockCs, config, this, this, (int) -1, latestView, realmName);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return replica;
}
Also used : ClientCommunicationServerSide(bftsmart.communication.client.ClientCommunicationServerSide) ServiceReplica(bftsmart.tom.ServiceReplica) ServerCommunicationSystemImpl(bftsmart.communication.ServerCommunicationSystemImpl) MessageHandler(bftsmart.communication.MessageHandler) ServerCommunicationSystem(bftsmart.communication.ServerCommunicationSystem) MemoryBasedViewStorage(bftsmart.reconfiguration.views.MemoryBasedViewStorage) TOMConfiguration(bftsmart.reconfiguration.util.TOMConfiguration) ServerViewController(bftsmart.reconfiguration.ServerViewController) IOException(java.io.IOException)

Example 5 with ServerCommunicationSystemImpl

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

the class ConsensusTest_ method mockMessageHandlerNormalNode5.

private void mockMessageHandlerNormalNode5(int nodeId) {
    ServerCommunicationSystemImpl serverCommunicationSystem = (ServerCommunicationSystemImpl) serverNodes[nodeId].getReplica().getServerCommunicationSystem();
    MessageHandler mockMessageHandler = serverCommunicationSystem.getMessageHandler();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objs = invocationOnMock.getArguments();
            if (newLeaderDiscardSendMsgEnable) {
            // 什么也不做
            } else {
                invocationOnMock.callRealMethod();
            }
            return null;
        }
    }).when(mockMessageHandler).processData(any());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ServerCommunicationSystemImpl(bftsmart.communication.ServerCommunicationSystemImpl) MessageHandler(bftsmart.communication.MessageHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Aggregations

ServerCommunicationSystemImpl (bftsmart.communication.ServerCommunicationSystemImpl)9 Mockito.doAnswer (org.mockito.Mockito.doAnswer)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 Answer (org.mockito.stubbing.Answer)7 MessageHandler (bftsmart.communication.MessageHandler)5 LCMessage (bftsmart.tom.leaderchange.LCMessage)3 ServerCommunicationSystem (bftsmart.communication.ServerCommunicationSystem)1 ClientCommunicationServerSide (bftsmart.communication.client.ClientCommunicationServerSide)1 ConsensusMessage (bftsmart.consensus.messages.ConsensusMessage)1 ServerViewController (bftsmart.reconfiguration.ServerViewController)1 TOMConfiguration (bftsmart.reconfiguration.util.TOMConfiguration)1 MemoryBasedViewStorage (bftsmart.reconfiguration.views.MemoryBasedViewStorage)1 ServiceReplica (bftsmart.tom.ServiceReplica)1 IOException (java.io.IOException)1