diff --git a/src/test/java/test/bftsmart/communication/server/CommunicationtTestMocker.java b/src/test/java/test/bftsmart/communication/server/CommunicationtTestMocker.java index 088ca4a3..515bf311 100644 --- a/src/test/java/test/bftsmart/communication/server/CommunicationtTestMocker.java +++ b/src/test/java/test/bftsmart/communication/server/CommunicationtTestMocker.java @@ -19,26 +19,17 @@ public class CommunicationtTestMocker { public static ViewTopology mockTopology(int currentId, int[] processIds) { ReplicaConfiguration conf = mockConfiguration(currentId, processIds); - + return mockTopology(currentId, processIds, conf); } - + public static ViewTopology mockTopology(int currentId, int[] processIds, ReplicaConfiguration conf) { ViewTopology topology = Mockito.mock(ViewTopology.class); when(topology.getCurrentProcessId()).thenReturn(currentId); when(topology.getCurrentViewProcesses()).thenReturn(processIds); when(topology.isInCurrentView()).thenReturn(contains(currentId, processIds)); - when(topology.isCurrentViewMember(anyInt())).thenAnswer(new Answer() { + when(topology.getStaticConf()).thenReturn(conf); - @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { - int id = invocation.getArgument(0); - return contains(id, processIds); - } - }); - - when(topology.getStaticConf()).thenReturn(conf); - return topology; } @@ -63,17 +54,10 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable { public static ReplicaConfiguration mockConfiguration(int currentId, int[] processIds) { ReplicaConfiguration conf = Mockito.mock(ReplicaConfiguration.class); - when(conf.getProcessId()).thenReturn(currentId); - when(conf.getInitialView()).thenReturn(processIds); when(conf.getInQueueSize()).thenReturn(100000); - when(conf.getOutQueueSize()).thenReturn(100000); - when(conf.getSendRetryCount()).thenReturn(10); - try { DefaultRSAKeyLoader defaultRSAKeyLoader = new DefaultRSAKeyLoader(); - when(conf.getRSAPrivateKey()).thenReturn(defaultRSAKeyLoader.loadPrivateKey(currentId)); - when(conf.getRSAPublicKey(currentId)).thenReturn(defaultRSAKeyLoader.loadPublicKey(currentId)); - } catch (Exception e) { + } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } return conf; @@ -89,15 +73,15 @@ public static ReplicaConfiguration mockDefaultConfiguration(int currentId, int[] hosts.add(processIds[i], "localhost", port, 0); } TOMConfiguration conf = new TOMConfiguration(currentId, new Properties(), hosts); - + conf = Mockito.spy(conf); - + when(conf.getProcessId()).thenReturn(currentId); when(conf.getInQueueSize()).thenReturn(100000); when(conf.getOutQueueSize()).thenReturn(100000); - + return conf; - + // ReplicaConfiguration conf = mockConfiguration(currentId, processIds); // // when(conf.getServerToServerPort(anyInt())).thenAnswer(new Answer() { @@ -125,4 +109,16 @@ private static boolean contains(int v, int[] values) { return false; } + public static ReplicaTopology mockTopologyWithTCP2(int currentId, int[] processIds, int[] ports) { + ReplicaTopology topology = Mockito.mock(ReplicaTopology.class); + when(topology.getCurrentProcessId()).thenReturn(currentId); + when(topology.getCurrentViewProcesses()).thenReturn(processIds); + when(topology.isInCurrentView()).thenReturn(contains(currentId, processIds)); + + ReplicaConfiguration conf = mockDefaultConfiguration(currentId, processIds, ports); + when(topology.getStaticConf()).thenReturn(conf); + + return topology; + } + } diff --git a/src/test/java/test/bftsmart/communication/server/ServerCommunicationLayerTest.java b/src/test/java/test/bftsmart/communication/server/ServerCommunicationLayerTest.java index 78484eae..fc73ac5e 100644 --- a/src/test/java/test/bftsmart/communication/server/ServerCommunicationLayerTest.java +++ b/src/test/java/test/bftsmart/communication/server/ServerCommunicationLayerTest.java @@ -242,7 +242,7 @@ public void testDoubleNettyNodes() throws InterruptedException { int[] viewProcessIds = { 0, 1}; int[] ports = { 14100, 14110}; - CommunicationLayer[] servers = prepareNettyNodes(realmName, viewProcessIds, ports); + CommunicationLayer[] servers = prepareNettyNodes2(realmName, viewProcessIds, ports); MessageCounter[] counters = prepareMessageCounters(servers); for (CommunicationLayer server : servers) { @@ -272,7 +272,7 @@ public void testNettyNodesNetwork() { int[] viewProcessIds = { 0, 1, 2, 3 }; int[] ports = { 15100, 15110, 15120, 15130 }; - CommunicationLayer[] servers = prepareNettyNodes(realmName, viewProcessIds, ports); + CommunicationLayer[] servers = prepareNettyNodes2(realmName, viewProcessIds, ports); MessageCounter[] counters = prepareMessageCounters(servers); for (CommunicationLayer srv : servers) { @@ -435,4 +435,14 @@ public void assertMessagesEquals(SystemMessage... expectedMessages) { } } + private CommunicationLayer[] prepareNettyNodes2(String realmName, int[] viewProcessIds, int[] ports) { + CommunicationLayer[] comLayers = new CommunicationLayer[viewProcessIds.length]; + for (int i = 0; i < comLayers.length; i++) { + ReplicaTopology topology = CommunicationtTestMocker.mockTopologyWithTCP2(viewProcessIds[i], viewProcessIds, ports); + CommunicationLayer comLayer = new NettyServerCommunicationLayer(realmName, topology); + comLayers[i] = comLayer; + } + return comLayers; + } + }