From 84d3478506abd5c7245e8698e0795f20a464a699 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 11 Mar 2022 15:15:01 +0300 Subject: [PATCH 01/51] Continue to StubNetwork --- src/test/java/networking/Hub.java | 18 ++++- src/test/java/networking/MockEngine.java | 15 ++++- src/test/java/networking/StubNetwork.java | 67 +++++++++++++++++-- src/test/java/networking/StubNetworkTest.java | 53 ++++++++++++--- .../java/networking/StubNetworkThread.java | 29 ++++++++ 5 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 src/test/java/networking/StubNetworkThread.java diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index a6c0655a..fa573f8f 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -9,4 +9,20 @@ public class Hub { private ConcurrentHashMap networks; private ConcurrentHashMap entities; -} + + public Hub() { + this.networks=new ConcurrentHashMap<>(); + this.entities=new ConcurrentHashMap<>(); + } + + public void registerNetwork(Identifier key, Network network) { + networks.put( key, network); + + } + public StubNetwork getNetwork(Identifier key){ + + return (StubNetwork) networks.get(key); + + } + +} \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 78adb922..cf1d14dd 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -1,5 +1,6 @@ package networking; +import java.util.HashSet; import java.util.Set; import model.Entity; @@ -9,8 +10,20 @@ public class MockEngine implements Engine { private Set receivedEntityIds; + public MockEngine() { + this.receivedEntityIds = new HashSet<>(); + } + + @Override + public String toString() { + return "MockEngine{" + + "receivedEntityIds=" + receivedEntityIds + + '}'; + } + @Override public void process(Entity e) throws IllegalArgumentException { // TODO: put e.Id() in the set. + receivedEntityIds.add(e.id()); } -} +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index e64ac747..9f210755 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -2,21 +2,80 @@ import java.util.concurrent.ConcurrentHashMap; +import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.exceptions.LightChainNetworkingException; +import model.lightchain.Identifier; import network.Conduit; import network.Network; import protocol.Engine; +import unittest.fixtures.IdentifierFixture; public class StubNetwork implements Network { private ConcurrentHashMap engines; + private ConcurrentHashMap conduits; private Hub hub; + private Identifier identifier; - public StubNetwork(Hub hub){ + + + public StubNetwork(Hub hub) { this.engines = new ConcurrentHashMap<>(); + this.conduits= new ConcurrentHashMap<>(); this.hub = hub; + this.identifier = IdentifierFixture.NewIdentifier(); + this.hub.registerNetwork(identifier, this); + } + + public Identifier id() { + return this.identifier; + } + public void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { + Conduit conduit = conduits.get(ch); + conduit.unicast(entity,stubNetworkR.id()); + } @Override - public Conduit register(Engine e, String channel) throws IllegalStateException { - return null; + public Conduit register(Engine en, String channel) throws IllegalStateException { + Conduit conduit = new Conduit() { + @Override + public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { + StubNetwork net = hub.getNetwork(target); + net.deliverEntity(channel, e); + + } + + @Override + public void put(Entity e) throws LightChainDistributedStorageException { + + } + + @Override + public Entity get(Identifier identifier) throws LightChainDistributedStorageException { + return null; + } + }; + try { + if (engines.containsKey(channel)) { + throw new IllegalStateException(); + } + + engines.put(channel, en); + + conduits.put(channel, conduit); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return conduit; + } + + public void deliverEntity(String ch, Entity en) { + System.out.println(ch); + + Engine engine = engines.get(ch); + engine.process(en); + } -} +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index cab21f7e..595292b4 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,14 +1,45 @@ package networking; +import model.exceptions.LightChainNetworkingException; +import network.Conduit; +import network.Network; +import org.junit.jupiter.api.Test; +import protocol.Engine; +import unittest.fixtures.EntityFixture; + +import java.util.Iterator; + public class StubNetworkTest { - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. -} + + + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + + @Test + void test() throws LightChainNetworkingException { + Hub hub = new Hub(); + StubNetwork stubNetwork1 = new StubNetwork(hub); + StubNetwork stubNetwork2 = new StubNetwork(hub); + String channelA1="ChannelA1"; + + Engine A1 = new MockEngine(); + Engine B1 = new MockEngine(); + stubNetwork1.register(A1, channelA1); + stubNetwork2.register(B1,channelA1); + EntityFixture e1 = new EntityFixture(); + EntityFixture e2 = new EntityFixture(); + StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channelA1); + t1.start(); + //c1.unicast(e1, stubNetwork2.id()); + System.out.println(e1.id()); + System.out.println(B1); + } +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkThread.java b/src/test/java/networking/StubNetworkThread.java new file mode 100644 index 00000000..b88611b5 --- /dev/null +++ b/src/test/java/networking/StubNetworkThread.java @@ -0,0 +1,29 @@ +package networking; + +import model.Entity; +import model.exceptions.LightChainNetworkingException; + +public class StubNetworkThread extends Thread{ + StubNetwork stubNetworkT; + StubNetwork stubNetworkR; + Entity entity; + String ch; + + public StubNetworkThread(StubNetwork stubNetworkT, StubNetwork stubNetworkR, Entity entity, String ch) { + this.stubNetworkT = stubNetworkT; + this.stubNetworkR = stubNetworkR; + this.entity = entity; + this.ch = ch; + } + + @Override + public void run() { + try { + + stubNetworkT.sendUnicast(ch,stubNetworkR,entity); + } catch (LightChainNetworkingException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file From 1ecfaf43148647bbe8f6c6a25c8bcb23cd93485a Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 11 Mar 2022 16:13:02 +0300 Subject: [PATCH 02/51] TestUnicastOneToAll_Sequentially implemented --- src/test/java/networking/StubNetwork.java | 2 +- src/test/java/networking/StubNetworkTest.java | 83 ++++++++++++++++++- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 9f210755..e9ec42c6 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -72,7 +72,7 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce } public void deliverEntity(String ch, Entity en) { - System.out.println(ch); + Engine engine = engines.get(ch); engine.process(en); diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 595292b4..0d6ac7e9 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -22,12 +22,45 @@ public class StubNetworkTest { // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B // and B must be on another same channel. // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + Hub hub = new Hub(); + StubNetwork stubNetwork1 = new StubNetwork(hub); + StubNetwork stubNetwork2 = new StubNetwork(hub); + StubNetwork stubNetwork3 = new StubNetwork(hub); + StubNetwork stubNetwork4 = new StubNetwork(hub); + StubNetwork stubNetwork5 = new StubNetwork(hub); + StubNetwork stubNetwork6 = new StubNetwork(hub); + StubNetwork stubNetwork7 = new StubNetwork(hub); + StubNetwork stubNetwork8 = new StubNetwork(hub); + StubNetwork stubNetwork9 = new StubNetwork(hub); + StubNetwork stubNetwork10 = new StubNetwork(hub); + String channel1="test-network-channel-1"; + String channel2="test-network-channel-2"; + Engine A1 = new MockEngine(); + Engine A2 = new MockEngine(); + Engine B1 = new MockEngine(); + Engine B2 = new MockEngine(); + Engine C1 = new MockEngine(); + Engine C2 = new MockEngine(); + Engine D1 = new MockEngine(); + Engine D2 = new MockEngine(); + Engine E1 = new MockEngine(); + Engine E2 = new MockEngine(); + Engine F1 = new MockEngine(); + Engine F2 = new MockEngine(); + Engine G1 = new MockEngine(); + Engine G2 = new MockEngine(); + Engine H1 = new MockEngine(); + Engine H2 = new MockEngine(); + Engine I1 = new MockEngine(); + Engine I2 = new MockEngine(); + Engine J1 = new MockEngine(); + Engine J2 = new MockEngine(); + @Test + void test() throws LightChainNetworkingException { - Hub hub = new Hub(); - StubNetwork stubNetwork1 = new StubNetwork(hub); - StubNetwork stubNetwork2 = new StubNetwork(hub); + String channelA1="ChannelA1"; Engine A1 = new MockEngine(); @@ -42,4 +75,48 @@ void test() throws LightChainNetworkingException { System.out.println(e1.id()); System.out.println(B1); } + + @Test + void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { + Conduit c1 =stubNetwork1.register(A1,channel1); + EntityFixture e1 =new EntityFixture(); + + stubNetwork1.register(A2,channel2); + stubNetwork2.register(B1,channel1); + stubNetwork2.register(B2,channel2); + stubNetwork3.register(C1,channel1); + stubNetwork3.register(C2,channel2); + stubNetwork4.register(D1,channel1); + stubNetwork4.register(D2,channel2); + stubNetwork5.register(E1,channel1); + stubNetwork5.register(E2,channel2); + stubNetwork6.register(F1,channel1); + stubNetwork6.register(F2,channel2); + stubNetwork7.register(G1,channel1); + stubNetwork7.register(G2,channel2); + stubNetwork8.register(H1,channel1); + stubNetwork8.register(H2,channel2); + stubNetwork9.register(I1,channel1); + stubNetwork9.register(I2,channel2); + stubNetwork10.register(J1,channel1); + stubNetwork10.register(J2,channel2); + c1.unicast(e1,stubNetwork2.id()); + c1.unicast(e1,stubNetwork3.id()); + c1.unicast(e1,stubNetwork4.id()); + c1.unicast(e1,stubNetwork5.id()); + c1.unicast(e1,stubNetwork6.id()); + c1.unicast(e1,stubNetwork7.id()); + c1.unicast(e1,stubNetwork8.id()); + c1.unicast(e1,stubNetwork9.id()); + c1.unicast(e1,stubNetwork10.id()); + System.out.println(e1.id()); + System.out.println(B1); + System.out.println(B2); + System.out.println(C1); + System.out.println(C2); + System.out.println(D1); + System.out.println(D2); + + + } } \ No newline at end of file From 29ec7daa615ff68d9a54b2aada104e9959bef7d2 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 11 Mar 2022 16:29:36 +0300 Subject: [PATCH 03/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/StubNetworkTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 0d6ac7e9..4702a9da 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -119,4 +119,57 @@ void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { } + + @Test + void TestUnicastOneToAll_Concurrently() { + + EntityFixture e1 =new EntityFixture(); + stubNetwork1.register(A1,channel1); + stubNetwork1.register(A2,channel2); + stubNetwork2.register(B1,channel1); + stubNetwork2.register(B2,channel2); + stubNetwork3.register(C1,channel1); + stubNetwork3.register(C2,channel2); + stubNetwork4.register(D1,channel1); + stubNetwork4.register(D2,channel2); + stubNetwork5.register(E1,channel1); + stubNetwork5.register(E2,channel2); + stubNetwork6.register(F1,channel1); + stubNetwork6.register(F2,channel2); + stubNetwork7.register(G1,channel1); + stubNetwork7.register(G2,channel2); + stubNetwork8.register(H1,channel1); + stubNetwork8.register(H2,channel2); + stubNetwork9.register(I1,channel1); + stubNetwork9.register(I2,channel2); + stubNetwork10.register(J1,channel1); + stubNetwork10.register(J2,channel2); + StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channel1); + StubNetworkThread t2 = new StubNetworkThread(stubNetwork1,stubNetwork3,e1,channel1); + StubNetworkThread t3 = new StubNetworkThread(stubNetwork1,stubNetwork4,e1,channel1); + StubNetworkThread t4 = new StubNetworkThread(stubNetwork1,stubNetwork5,e1,channel1); + StubNetworkThread t5 = new StubNetworkThread(stubNetwork1,stubNetwork6,e1,channel1); + StubNetworkThread t6 = new StubNetworkThread(stubNetwork1,stubNetwork7,e1,channel1); + StubNetworkThread t7 = new StubNetworkThread(stubNetwork1,stubNetwork8,e1,channel1); + StubNetworkThread t8 = new StubNetworkThread(stubNetwork1,stubNetwork9,e1,channel1); + StubNetworkThread t9 = new StubNetworkThread(stubNetwork1,stubNetwork10,e1,channel1); + + t1.start(); + t2.start(); + t3.start(); + t4.start(); + t5.start(); + t6.start(); + t7.start(); + t8.start(); + t9.start(); + System.out.println(e1.id()); + System.out.println(B1); + System.out.println(B2); + System.out.println(C1); + System.out.println(C2); + System.out.println(D1); + System.out.println(D2); + + } } \ No newline at end of file From c52b3387dc392972157f062c5756711246f7657b Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Fri, 11 Mar 2022 07:23:11 -0800 Subject: [PATCH 04/51] wip --- src/test/java/networking/Hub.java | 13 +- src/test/java/networking/MockEngine.java | 6 +- src/test/java/networking/StubNetwork.java | 14 +- src/test/java/networking/StubNetworkTest.java | 307 +++++++++--------- 4 files changed, 162 insertions(+), 178 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index fa573f8f..5f7c4130 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -7,19 +7,20 @@ import network.Network; public class Hub { - private ConcurrentHashMap networks; - private ConcurrentHashMap entities; + private final ConcurrentHashMap networks; + private final ConcurrentHashMap entities; public Hub() { - this.networks=new ConcurrentHashMap<>(); - this.entities=new ConcurrentHashMap<>(); + this.networks = new ConcurrentHashMap<>(); + this.entities = new ConcurrentHashMap<>(); } public void registerNetwork(Identifier key, Network network) { - networks.put( key, network); + networks.put(key, network); } - public StubNetwork getNetwork(Identifier key){ + + public StubNetwork getNetwork(Identifier key) { return (StubNetwork) networks.get(key); diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index cf1d14dd..20b1f559 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -8,7 +8,7 @@ import protocol.Engine; public class MockEngine implements Engine { - private Set receivedEntityIds; + private final Set receivedEntityIds; public MockEngine() { this.receivedEntityIds = new HashSet<>(); @@ -17,8 +17,8 @@ public MockEngine() { @Override public String toString() { return "MockEngine{" + - "receivedEntityIds=" + receivedEntityIds + - '}'; + "receivedEntityIds=" + receivedEntityIds + + '}'; } @Override diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index e9ec42c6..1e134461 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -12,16 +12,15 @@ import unittest.fixtures.IdentifierFixture; public class StubNetwork implements Network { - private ConcurrentHashMap engines; - private ConcurrentHashMap conduits; - private Hub hub; - private Identifier identifier; - + private final ConcurrentHashMap engines; + private final ConcurrentHashMap conduits; + private final Hub hub; + private final Identifier identifier; public StubNetwork(Hub hub) { this.engines = new ConcurrentHashMap<>(); - this.conduits= new ConcurrentHashMap<>(); + this.conduits = new ConcurrentHashMap<>(); this.hub = hub; this.identifier = IdentifierFixture.NewIdentifier(); this.hub.registerNetwork(identifier, this); @@ -30,9 +29,10 @@ public StubNetwork(Hub hub) { public Identifier id() { return this.identifier; } + public void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { Conduit conduit = conduits.get(ch); - conduit.unicast(entity,stubNetworkR.id()); + conduit.unicast(entity, stubNetworkR.id()); } diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 4702a9da..887eac9b 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,175 +1,158 @@ package networking; +import java.util.ArrayList; +import java.util.Hashtable; + import model.exceptions.LightChainNetworkingException; import network.Conduit; -import network.Network; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import protocol.Engine; import unittest.fixtures.EntityFixture; -import java.util.Iterator; - public class StubNetworkTest { - - - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + + @Test + void TestTwoStubNetworks_TwoEngines(){ + String channel1 = "test-network-channel-1"; Hub hub = new Hub(); - StubNetwork stubNetwork1 = new StubNetwork(hub); - StubNetwork stubNetwork2 = new StubNetwork(hub); - StubNetwork stubNetwork3 = new StubNetwork(hub); - StubNetwork stubNetwork4 = new StubNetwork(hub); - StubNetwork stubNetwork5 = new StubNetwork(hub); - StubNetwork stubNetwork6 = new StubNetwork(hub); - StubNetwork stubNetwork7 = new StubNetwork(hub); - StubNetwork stubNetwork8 = new StubNetwork(hub); - StubNetwork stubNetwork9 = new StubNetwork(hub); - StubNetwork stubNetwork10 = new StubNetwork(hub); - String channel1="test-network-channel-1"; - String channel2="test-network-channel-2"; - Engine A1 = new MockEngine(); - Engine A2 = new MockEngine(); - Engine B1 = new MockEngine(); - Engine B2 = new MockEngine(); - Engine C1 = new MockEngine(); - Engine C2 = new MockEngine(); - Engine D1 = new MockEngine(); - Engine D2 = new MockEngine(); - Engine E1 = new MockEngine(); - Engine E2 = new MockEngine(); - Engine F1 = new MockEngine(); - Engine F2 = new MockEngine(); - Engine G1 = new MockEngine(); - Engine G2 = new MockEngine(); - Engine H1 = new MockEngine(); - Engine H2 = new MockEngine(); - Engine I1 = new MockEngine(); - Engine I2 = new MockEngine(); - Engine J1 = new MockEngine(); - Engine J2 = new MockEngine(); - - - @Test - - void test() throws LightChainNetworkingException { - - String channelA1="ChannelA1"; - Engine A1 = new MockEngine(); - Engine B1 = new MockEngine(); - stubNetwork1.register(A1, channelA1); - stubNetwork2.register(B1,channelA1); - EntityFixture e1 = new EntityFixture(); - EntityFixture e2 = new EntityFixture(); - StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channelA1); - t1.start(); - //c1.unicast(e1, stubNetwork2.id()); - System.out.println(e1.id()); - System.out.println(B1); - } - - @Test - void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { - Conduit c1 =stubNetwork1.register(A1,channel1); - EntityFixture e1 =new EntityFixture(); - - stubNetwork1.register(A2,channel2); - stubNetwork2.register(B1,channel1); - stubNetwork2.register(B2,channel2); - stubNetwork3.register(C1,channel1); - stubNetwork3.register(C2,channel2); - stubNetwork4.register(D1,channel1); - stubNetwork4.register(D2,channel2); - stubNetwork5.register(E1,channel1); - stubNetwork5.register(E2,channel2); - stubNetwork6.register(F1,channel1); - stubNetwork6.register(F2,channel2); - stubNetwork7.register(G1,channel1); - stubNetwork7.register(G2,channel2); - stubNetwork8.register(H1,channel1); - stubNetwork8.register(H2,channel2); - stubNetwork9.register(I1,channel1); - stubNetwork9.register(I2,channel2); - stubNetwork10.register(J1,channel1); - stubNetwork10.register(J2,channel2); - c1.unicast(e1,stubNetwork2.id()); - c1.unicast(e1,stubNetwork3.id()); - c1.unicast(e1,stubNetwork4.id()); - c1.unicast(e1,stubNetwork5.id()); - c1.unicast(e1,stubNetwork6.id()); - c1.unicast(e1,stubNetwork7.id()); - c1.unicast(e1,stubNetwork8.id()); - c1.unicast(e1,stubNetwork9.id()); - c1.unicast(e1,stubNetwork10.id()); - System.out.println(e1.id()); - System.out.println(B1); - System.out.println(B2); - System.out.println(C1); - System.out.println(C2); - System.out.println(D1); - System.out.println(D2); - - - } - - @Test - void TestUnicastOneToAll_Concurrently() { - - EntityFixture e1 =new EntityFixture(); - stubNetwork1.register(A1,channel1); - stubNetwork1.register(A2,channel2); - stubNetwork2.register(B1,channel1); - stubNetwork2.register(B2,channel2); - stubNetwork3.register(C1,channel1); - stubNetwork3.register(C2,channel2); - stubNetwork4.register(D1,channel1); - stubNetwork4.register(D2,channel2); - stubNetwork5.register(E1,channel1); - stubNetwork5.register(E2,channel2); - stubNetwork6.register(F1,channel1); - stubNetwork6.register(F2,channel2); - stubNetwork7.register(G1,channel1); - stubNetwork7.register(G2,channel2); - stubNetwork8.register(H1,channel1); - stubNetwork8.register(H2,channel2); - stubNetwork9.register(I1,channel1); - stubNetwork9.register(I2,channel2); - stubNetwork10.register(J1,channel1); - stubNetwork10.register(J2,channel2); - StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channel1); - StubNetworkThread t2 = new StubNetworkThread(stubNetwork1,stubNetwork3,e1,channel1); - StubNetworkThread t3 = new StubNetworkThread(stubNetwork1,stubNetwork4,e1,channel1); - StubNetworkThread t4 = new StubNetworkThread(stubNetwork1,stubNetwork5,e1,channel1); - StubNetworkThread t5 = new StubNetworkThread(stubNetwork1,stubNetwork6,e1,channel1); - StubNetworkThread t6 = new StubNetworkThread(stubNetwork1,stubNetwork7,e1,channel1); - StubNetworkThread t7 = new StubNetworkThread(stubNetwork1,stubNetwork8,e1,channel1); - StubNetworkThread t8 = new StubNetworkThread(stubNetwork1,stubNetwork9,e1,channel1); - StubNetworkThread t9 = new StubNetworkThread(stubNetwork1,stubNetwork10,e1,channel1); - - t1.start(); - t2.start(); - t3.start(); - t4.start(); - t5.start(); - t6.start(); - t7.start(); - t8.start(); - t9.start(); - System.out.println(e1.id()); - System.out.println(B1); - System.out.println(B2); - System.out.println(C1); - System.out.println(C2); - System.out.println(D1); - System.out.println(D2); + StubNetwork network1 = new StubNetwork(hub); + Engine A1 = new MockEngine(); + network1.register(A1, channel1); - } + StubNetwork network2 = new StubNetwork(hub); + Engine A2 = new MockEngine(); + network2.register(A2, channel1); + + + network1.sendUnicast(); + + } + + +// @Test +// void test() throws LightChainNetworkingException { +// +// String channelA1 = "ChannelA1"; +// +// Engine A1 = new MockEngine(); +// Engine B1 = new MockEngine(); +// stubNetwork1.register(A1, channelA1); +// stubNetwork2.register(B1, channelA1); +// EntityFixture e1 = new EntityFixture(); +// EntityFixture e2 = new EntityFixture(); +// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channelA1); +// t1.start(); +// //c1.unicast(e1, stubNetwork2.id()); +// System.out.println(e1.id()); +// System.out.println(B1); +// } +// +// @Test +// void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { +// Conduit c1 = stubNetwork1.register(A1, channel1); +// EntityFixture e1 = new EntityFixture(); +// +// stubNetwork1.register(A2, channel2); +// stubNetwork2.register(B1, channel1); +// stubNetwork2.register(B2, channel2); +// stubNetwork3.register(C1, channel1); +// stubNetwork3.register(C2, channel2); +// stubNetwork4.register(D1, channel1); +// stubNetwork4.register(D2, channel2); +// stubNetwork5.register(E1, channel1); +// stubNetwork5.register(E2, channel2); +// stubNetwork6.register(F1, channel1); +// stubNetwork6.register(F2, channel2); +// stubNetwork7.register(G1, channel1); +// stubNetwork7.register(G2, channel2); +// stubNetwork8.register(H1, channel1); +// stubNetwork8.register(H2, channel2); +// stubNetwork9.register(I1, channel1); +// stubNetwork9.register(I2, channel2); +// stubNetwork10.register(J1, channel1); +// stubNetwork10.register(J2, channel2); +// c1.unicast(e1, stubNetwork2.id()); +// c1.unicast(e1, stubNetwork3.id()); +// c1.unicast(e1, stubNetwork4.id()); +// c1.unicast(e1, stubNetwork5.id()); +// c1.unicast(e1, stubNetwork6.id()); +// c1.unicast(e1, stubNetwork7.id()); +// c1.unicast(e1, stubNetwork8.id()); +// c1.unicast(e1, stubNetwork9.id()); +// c1.unicast(e1, stubNetwork10.id()); +// System.out.println(e1.id()); +// System.out.println(B1); +// System.out.println(B2); +// System.out.println(C1); +// System.out.println(C2); +// System.out.println(D1); +// System.out.println(D2); +// +// +// } +// +// @Test +// void TestUnicastOneToAll_Concurrently() { +// +// EntityFixture e1 = new EntityFixture(); +// stubNetwork1.register(A1, channel1); +// stubNetwork1.register(A2, channel2); +// stubNetwork2.register(B1, channel1); +// stubNetwork2.register(B2, channel2); +// stubNetwork3.register(C1, channel1); +// stubNetwork3.register(C2, channel2); +// stubNetwork4.register(D1, channel1); +// stubNetwork4.register(D2, channel2); +// stubNetwork5.register(E1, channel1); +// stubNetwork5.register(E2, channel2); +// stubNetwork6.register(F1, channel1); +// stubNetwork6.register(F2, channel2); +// stubNetwork7.register(G1, channel1); +// stubNetwork7.register(G2, channel2); +// stubNetwork8.register(H1, channel1); +// stubNetwork8.register(H2, channel2); +// stubNetwork9.register(I1, channel1); +// stubNetwork9.register(I2, channel2); +// stubNetwork10.register(J1, channel1); +// stubNetwork10.register(J2, channel2); +// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channel1); +// StubNetworkThread t2 = new StubNetworkThread(stubNetwork1, stubNetwork3, e1, channel1); +// StubNetworkThread t3 = new StubNetworkThread(stubNetwork1, stubNetwork4, e1, channel1); +// StubNetworkThread t4 = new StubNetworkThread(stubNetwork1, stubNetwork5, e1, channel1); +// StubNetworkThread t5 = new StubNetworkThread(stubNetwork1, stubNetwork6, e1, channel1); +// StubNetworkThread t6 = new StubNetworkThread(stubNetwork1, stubNetwork7, e1, channel1); +// StubNetworkThread t7 = new StubNetworkThread(stubNetwork1, stubNetwork8, e1, channel1); +// StubNetworkThread t8 = new StubNetworkThread(stubNetwork1, stubNetwork9, e1, channel1); +// StubNetworkThread t9 = new StubNetworkThread(stubNetwork1, stubNetwork10, e1, channel1); +// +// t1.start(); +// t2.start(); +// t3.start(); +// t4.start(); +// t5.start(); +// t6.start(); +// t7.start(); +// t8.start(); +// t9.start(); +// System.out.println(e1.id()); +// System.out.println(B1); +// System.out.println(B2); +// System.out.println(C1); +// System.out.println(C2); +// System.out.println(D1); +// System.out.println(D2); +// +// } } \ No newline at end of file From e506da7d156a143eeba8ced2f03aff0fd7bacf1a Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Fri, 11 Mar 2022 07:41:59 -0800 Subject: [PATCH 05/51] implements test for two engines --- .../java/model/lightchain/Identifier.java | 13 ++++++++++++ src/test/java/networking/Hub.java | 2 -- src/test/java/networking/MockEngine.java | 15 +++++++------- src/test/java/networking/StubNetwork.java | 10 ++++------ src/test/java/networking/StubNetworkTest.java | 17 ++++++++++++---- .../java/networking/StubNetworkThread.java | 20 +++++++++---------- 6 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/main/java/model/lightchain/Identifier.java b/src/main/java/model/lightchain/Identifier.java index 2b83703c..b104104b 100644 --- a/src/main/java/model/lightchain/Identifier.java +++ b/src/main/java/model/lightchain/Identifier.java @@ -49,4 +49,17 @@ public int comparedTo(Identifier other) { int result = Arrays.compare(this.value, other.value); return Integer.compare(result, 0); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Identifier)) return false; + Identifier that = (Identifier) o; + return Arrays.equals(value, that.value); + } + + @Override + public int hashCode() { + return Arrays.hashCode(value); + } } diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 5f7c4130..0fbfa358 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -21,9 +21,7 @@ public void registerNetwork(Identifier key, Network network) { } public StubNetwork getNetwork(Identifier key) { - return (StubNetwork) networks.get(key); - } } \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 20b1f559..7ffde796 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -8,22 +8,21 @@ import protocol.Engine; public class MockEngine implements Engine { - private final Set receivedEntityIds; + private Set receivedEntityIds; public MockEngine() { this.receivedEntityIds = new HashSet<>(); } - @Override - public String toString() { - return "MockEngine{" + - "receivedEntityIds=" + receivedEntityIds + - '}'; - } - @Override public void process(Entity e) throws IllegalArgumentException { // TODO: put e.Id() in the set. receivedEntityIds.add(e.id()); } + + public boolean hasReceived(Entity e) { + Identifier id = e.id(); + boolean ok = this.receivedEntityIds.contains(id); + return ok; + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 1e134461..0dbd69ce 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -30,20 +30,19 @@ public Identifier id() { return this.identifier; } - public void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { + private void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { Conduit conduit = conduits.get(ch); conduit.unicast(entity, stubNetworkR.id()); - } @Override public Conduit register(Engine en, String channel) throws IllegalStateException { + // TODO: this should be a separate class. Conduit conduit = new Conduit() { @Override public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { StubNetwork net = hub.getNetwork(target); net.deliverEntity(channel, e); - } @Override @@ -56,6 +55,8 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce return null; } }; + + try { if (engines.containsKey(channel)) { throw new IllegalStateException(); @@ -72,10 +73,7 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce } public void deliverEntity(String ch, Entity en) { - - Engine engine = engines.get(ch); engine.process(en); - } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 887eac9b..4dd2fd0c 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,8 +3,10 @@ import java.util.ArrayList; import java.util.Hashtable; +import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import protocol.Engine; @@ -28,16 +30,23 @@ void TestTwoStubNetworks_TwoEngines(){ Hub hub = new Hub(); StubNetwork network1 = new StubNetwork(hub); - Engine A1 = new MockEngine(); - network1.register(A1, channel1); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); StubNetwork network2 = new StubNetwork(hub); - Engine A2 = new MockEngine(); + MockEngine A2 = new MockEngine(); network2.register(A2, channel1); - network1.sendUnicast(); + Entity entity = new EntityFixture(); + try { + c1.unicast(entity, network2.id()); + } catch (LightChainNetworkingException e) { + Assertions.fail(); + } + + Assertions.assertTrue(A2.hasReceived(entity)); } diff --git a/src/test/java/networking/StubNetworkThread.java b/src/test/java/networking/StubNetworkThread.java index b88611b5..00e20117 100644 --- a/src/test/java/networking/StubNetworkThread.java +++ b/src/test/java/networking/StubNetworkThread.java @@ -16,14 +16,14 @@ public StubNetworkThread(StubNetwork stubNetworkT, StubNetwork stubNetworkR, Ent this.ch = ch; } - @Override - public void run() { - try { - - stubNetworkT.sendUnicast(ch,stubNetworkR,entity); - } catch (LightChainNetworkingException e) { - e.printStackTrace(); - } - - } +// @Override +// public void run() { +// try { +// +// // stubNetworkT.sendUnicast(ch,stubNetworkR,entity); +// } catch (LightChainNetworkingException e) { +// e.printStackTrace(); +// } +// +// } } \ No newline at end of file From cc6109cf15982ee9a18c961271cbafe0ec5477f0 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Mon, 14 Mar 2022 17:10:35 +0300 Subject: [PATCH 06/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/MockConduit.java | 62 +++++++++++++++++++ src/test/java/networking/StubNetwork.java | 35 +++-------- src/test/java/networking/StubNetworkTest.java | 57 ++++++++++------- 3 files changed, 104 insertions(+), 50 deletions(-) create mode 100644 src/test/java/networking/MockConduit.java diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java new file mode 100644 index 00000000..48d00450 --- /dev/null +++ b/src/test/java/networking/MockConduit.java @@ -0,0 +1,62 @@ +package networking; + +import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.exceptions.LightChainNetworkingException; +import model.lightchain.Identifier; +import network.Conduit; +import network.Network; +import protocol.Engine; + +public class MockConduit implements Conduit { + private String channel; + private Engine engine; + private Hub hub; + + public MockConduit(String channel, Engine engine, Hub hub) { + this.channel = channel; + this.engine = engine; + this.hub = hub; + } + + /** + * Sends the Entity through the Network to the remote target. + * + * @param e the Entity to be sent over the network. + * @param target Identifier of the receiver. + * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. + */ + @Override + public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { + StubNetwork net = hub.getNetwork(target); + deliverEntity(net,channel , e); + } + + /** + * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. + * + * @param e the Entity to be stored over the network. + * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. + */ + @Override + public void put(Entity e) throws LightChainDistributedStorageException { + + } + + /** + * Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table + * (DHT) of nodes. + * + * @param identifier identifier of the entity to be retrieved. + * @return the retrieved entity or null if it does not exist. + * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. + */ + @Override + public Entity get(Identifier identifier) throws LightChainDistributedStorageException { + return null; + } + public void deliverEntity(StubNetwork network,String channel, Entity en) { + Engine eng =network.getEngine(channel); + eng.process(en); + } +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 0dbd69ce..2002bfd5 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -1,9 +1,6 @@ package networking; -import java.util.concurrent.ConcurrentHashMap; - import model.Entity; -import model.exceptions.LightChainDistributedStorageException; import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; @@ -11,6 +8,8 @@ import protocol.Engine; import unittest.fixtures.IdentifierFixture; +import java.util.concurrent.ConcurrentHashMap; + public class StubNetwork implements Network { private final ConcurrentHashMap engines; private final ConcurrentHashMap conduits; @@ -36,27 +35,9 @@ private void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) thr } @Override - public Conduit register(Engine en, String channel) throws IllegalStateException { + public MockConduit register(Engine en, String channel) throws IllegalStateException { // TODO: this should be a separate class. - Conduit conduit = new Conduit() { - @Override - public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { - StubNetwork net = hub.getNetwork(target); - net.deliverEntity(channel, e); - } - - @Override - public void put(Entity e) throws LightChainDistributedStorageException { - - } - - @Override - public Entity get(Identifier identifier) throws LightChainDistributedStorageException { - return null; - } - }; - - + Conduit conduit = new MockConduit(channel,en, hub); try { if (engines.containsKey(channel)) { throw new IllegalStateException(); @@ -69,11 +50,11 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce ex.printStackTrace(); } - return conduit; + return (MockConduit) conduit; } - - public void deliverEntity(String ch, Entity en) { + public Engine getEngine(String ch){ Engine engine = engines.get(ch); - engine.process(en); + return engine; } + } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 4dd2fd0c..0aaacefe 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -2,10 +2,12 @@ import java.util.ArrayList; import java.util.Hashtable; +import java.util.concurrent.ConcurrentHashMap; import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; +import network.Network; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -13,6 +15,8 @@ import unittest.fixtures.EntityFixture; public class StubNetworkTest { + private ArrayList networkArrayList; + private ConcurrentHashMap> conduitConcurrentHashMap; // TODO: add a test for each of the following scenarios: // Use mock engines. // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. @@ -23,7 +27,28 @@ public class StubNetworkTest { // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B // and B must be on another same channel. // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + @BeforeEach + void setup(){ + this.networkArrayList = new ArrayList(); + this.conduitConcurrentHashMap=new ConcurrentHashMap>(); + ArrayList conduitArrayList = new ArrayList(); + String channel1 = "test-network-channel-1"; + String channel2 = "test-network-channel-2"; + Hub hub = new Hub(); + for (int i =0; i<10; i++){ + StubNetwork stubNetwork = new StubNetwork(hub); + Engine A1 =new MockEngine(); + Engine A2= new MockEngine(); + Conduit c1 =stubNetwork.register(A1,channel1); + Conduit c2 =stubNetwork.register(A2,channel2); + conduitArrayList.add(c1); + conduitArrayList.add(c2); + conduitConcurrentHashMap.put(stubNetwork,conduitArrayList); + networkArrayList.add(stubNetwork); + } + + } @Test void TestTwoStubNetworks_TwoEngines(){ String channel1 = "test-network-channel-1"; @@ -50,26 +75,13 @@ void TestTwoStubNetworks_TwoEngines(){ } -// @Test -// void test() throws LightChainNetworkingException { -// -// String channelA1 = "ChannelA1"; -// -// Engine A1 = new MockEngine(); -// Engine B1 = new MockEngine(); -// stubNetwork1.register(A1, channelA1); -// stubNetwork2.register(B1, channelA1); -// EntityFixture e1 = new EntityFixture(); -// EntityFixture e2 = new EntityFixture(); -// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channelA1); -// t1.start(); -// //c1.unicast(e1, stubNetwork2.id()); -// System.out.println(e1.id()); -// System.out.println(B1); -// } -// -// @Test -// void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { + + + @Test + void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { + + + // Conduit c1 = stubNetwork1.register(A1, channel1); // EntityFixture e1 = new EntityFixture(); // @@ -108,9 +120,8 @@ void TestTwoStubNetworks_TwoEngines(){ // System.out.println(C2); // System.out.println(D1); // System.out.println(D2); -// -// -// } + + } // // @Test // void TestUnicastOneToAll_Concurrently() { From 2160b51358d67b1607784a41d119a95b28bb8e6d Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Mon, 14 Mar 2022 07:35:04 -0700 Subject: [PATCH 07/51] adds concurrent test --- src/test/java/networking/StubNetworkTest.java | 146 ++++++------------ 1 file changed, 50 insertions(+), 96 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 0aaacefe..d6ec679a 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,6 +3,9 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import model.Entity; import model.exceptions.LightChainNetworkingException; @@ -74,105 +77,56 @@ void TestTwoStubNetworks_TwoEngines(){ Assertions.assertTrue(A2.hasReceived(entity)); } + @Test + void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + String channel1 = "test-network-channel-1"; + Thread[] unicastThreads = new Thread[concurrencyDegree]; + Hub hub = new Hub(); + + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); - @Test - void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { - - - -// Conduit c1 = stubNetwork1.register(A1, channel1); -// EntityFixture e1 = new EntityFixture(); -// -// stubNetwork1.register(A2, channel2); -// stubNetwork2.register(B1, channel1); -// stubNetwork2.register(B2, channel2); -// stubNetwork3.register(C1, channel1); -// stubNetwork3.register(C2, channel2); -// stubNetwork4.register(D1, channel1); -// stubNetwork4.register(D2, channel2); -// stubNetwork5.register(E1, channel1); -// stubNetwork5.register(E2, channel2); -// stubNetwork6.register(F1, channel1); -// stubNetwork6.register(F2, channel2); -// stubNetwork7.register(G1, channel1); -// stubNetwork7.register(G2, channel2); -// stubNetwork8.register(H1, channel1); -// stubNetwork8.register(H2, channel2); -// stubNetwork9.register(I1, channel1); -// stubNetwork9.register(I2, channel2); -// stubNetwork10.register(J1, channel1); -// stubNetwork10.register(J2, channel2); -// c1.unicast(e1, stubNetwork2.id()); -// c1.unicast(e1, stubNetwork3.id()); -// c1.unicast(e1, stubNetwork4.id()); -// c1.unicast(e1, stubNetwork5.id()); -// c1.unicast(e1, stubNetwork6.id()); -// c1.unicast(e1, stubNetwork7.id()); -// c1.unicast(e1, stubNetwork8.id()); -// c1.unicast(e1, stubNetwork9.id()); -// c1.unicast(e1, stubNetwork10.id()); -// System.out.println(e1.id()); -// System.out.println(B1); -// System.out.println(B2); -// System.out.println(C1); -// System.out.println(C2); -// System.out.println(D1); -// System.out.println(D2); + for(int i = 0; i < concurrencyDegree; i++){ + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + + try { + c1.unicast(entity, network2.id()); + if(!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t: unicastThreads){ + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); } -// -// @Test -// void TestUnicastOneToAll_Concurrently() { -// -// EntityFixture e1 = new EntityFixture(); -// stubNetwork1.register(A1, channel1); -// stubNetwork1.register(A2, channel2); -// stubNetwork2.register(B1, channel1); -// stubNetwork2.register(B2, channel2); -// stubNetwork3.register(C1, channel1); -// stubNetwork3.register(C2, channel2); -// stubNetwork4.register(D1, channel1); -// stubNetwork4.register(D2, channel2); -// stubNetwork5.register(E1, channel1); -// stubNetwork5.register(E2, channel2); -// stubNetwork6.register(F1, channel1); -// stubNetwork6.register(F2, channel2); -// stubNetwork7.register(G1, channel1); -// stubNetwork7.register(G2, channel2); -// stubNetwork8.register(H1, channel1); -// stubNetwork8.register(H2, channel2); -// stubNetwork9.register(I1, channel1); -// stubNetwork9.register(I2, channel2); -// stubNetwork10.register(J1, channel1); -// stubNetwork10.register(J2, channel2); -// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channel1); -// StubNetworkThread t2 = new StubNetworkThread(stubNetwork1, stubNetwork3, e1, channel1); -// StubNetworkThread t3 = new StubNetworkThread(stubNetwork1, stubNetwork4, e1, channel1); -// StubNetworkThread t4 = new StubNetworkThread(stubNetwork1, stubNetwork5, e1, channel1); -// StubNetworkThread t5 = new StubNetworkThread(stubNetwork1, stubNetwork6, e1, channel1); -// StubNetworkThread t6 = new StubNetworkThread(stubNetwork1, stubNetwork7, e1, channel1); -// StubNetworkThread t7 = new StubNetworkThread(stubNetwork1, stubNetwork8, e1, channel1); -// StubNetworkThread t8 = new StubNetworkThread(stubNetwork1, stubNetwork9, e1, channel1); -// StubNetworkThread t9 = new StubNetworkThread(stubNetwork1, stubNetwork10, e1, channel1); -// -// t1.start(); -// t2.start(); -// t3.start(); -// t4.start(); -// t5.start(); -// t6.start(); -// t7.start(); -// t8.start(); -// t9.start(); -// System.out.println(e1.id()); -// System.out.println(B1); -// System.out.println(B2); -// System.out.println(C1); -// System.out.println(C2); -// System.out.println(D1); -// System.out.println(D2); -// -// } + + } \ No newline at end of file From 6c438d95d9710acf6446b365802d1b899c8161dc Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Mon, 14 Mar 2022 07:48:57 -0700 Subject: [PATCH 08/51] adds lock --- src/test/java/networking/MockEngine.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 7ffde796..15444aee 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -2,27 +2,37 @@ import java.util.HashSet; import java.util.Set; +import java.util.concurrent.locks.ReentrantReadWriteLock; import model.Entity; import model.lightchain.Identifier; import protocol.Engine; public class MockEngine implements Engine { - private Set receivedEntityIds; + private final ReentrantReadWriteLock lock; + private final Set receivedEntityIds; public MockEngine() { this.receivedEntityIds = new HashSet<>(); + this.lock = new ReentrantReadWriteLock(); } @Override public void process(Entity e) throws IllegalArgumentException { - // TODO: put e.Id() in the set. + lock.writeLock(); + receivedEntityIds.add(e.id()); + + lock.writeLock(); } public boolean hasReceived(Entity e) { + lock.readLock(); + Identifier id = e.id(); boolean ok = this.receivedEntityIds.contains(id); + + lock.readLock(); return ok; } } \ No newline at end of file From 70b1da8b6fdaf42a2c708ac3430eed0af802d697 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Tue, 15 Mar 2022 13:53:43 +0300 Subject: [PATCH 09/51] Some tests Implemented --- src/test/java/networking/Hub.java | 6 +- src/test/java/networking/MockConduit.java | 26 +- src/test/java/networking/MockEngine.java | 47 ++-- src/test/java/networking/StubNetwork.java | 86 +++---- src/test/java/networking/StubNetworkTest.java | 226 +++++++++++------- 5 files changed, 222 insertions(+), 169 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 0fbfa358..8abd20a3 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -19,8 +19,12 @@ public void registerNetwork(Identifier key, Network network) { networks.put(key, network); } + public void transferEntity(Entity entity, Identifier identifier, String channel){ + StubNetwork net =this.getNetwork(identifier); + net.receiveUnicast(entity,channel); - public StubNetwork getNetwork(Identifier key) { + } + private StubNetwork getNetwork(Identifier key) { return (StubNetwork) networks.get(key); } diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 48d00450..0b468b9f 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,18 +5,14 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; -import network.Network; -import protocol.Engine; public class MockConduit implements Conduit { - private String channel; - private Engine engine; - private Hub hub; - - public MockConduit(String channel, Engine engine, Hub hub) { - this.channel = channel; - this.engine = engine; - this.hub = hub; + + private final String channel; + private final Hub hub; + public MockConduit(String channel,Hub hub) { + this.channel=channel; + this.hub=hub; } /** @@ -28,8 +24,9 @@ public MockConduit(String channel, Engine engine, Hub hub) { */ @Override public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { - StubNetwork net = hub.getNetwork(target); - deliverEntity(net,channel , e); + hub.transferEntity(e,target,channel); + + } /** @@ -55,8 +52,5 @@ public void put(Entity e) throws LightChainDistributedStorageException { public Entity get(Identifier identifier) throws LightChainDistributedStorageException { return null; } - public void deliverEntity(StubNetwork network,String channel, Entity en) { - Engine eng =network.getEngine(channel); - eng.process(en); - } + } \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 15444aee..ede91185 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -5,34 +5,43 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import model.Entity; +import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; +import network.Conduit; +import network.Network; import protocol.Engine; public class MockEngine implements Engine { - private final ReentrantReadWriteLock lock; - private final Set receivedEntityIds; + private final ReentrantReadWriteLock lock; + private final Set receivedEntityIds; - public MockEngine() { - this.receivedEntityIds = new HashSet<>(); - this.lock = new ReentrantReadWriteLock(); - } - @Override - public void process(Entity e) throws IllegalArgumentException { - lock.writeLock(); - receivedEntityIds.add(e.id()); + public MockEngine() { + this.receivedEntityIds = new HashSet<>(); + this.lock = new ReentrantReadWriteLock(); + } - lock.writeLock(); - } - public boolean hasReceived(Entity e) { - lock.readLock(); - Identifier id = e.id(); - boolean ok = this.receivedEntityIds.contains(id); - lock.readLock(); - return ok; - } + + @Override + public void process(Entity e) throws IllegalArgumentException { + lock.writeLock(); + + receivedEntityIds.add(e.id()); + + lock.writeLock(); + } + + public boolean hasReceived(Entity e) { + lock.readLock(); + + Identifier id = e.id(); + boolean ok = this.receivedEntityIds.contains(id); + + lock.readLock(); + return ok; + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 2002bfd5..893373ca 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -11,50 +11,50 @@ import java.util.concurrent.ConcurrentHashMap; public class StubNetwork implements Network { - private final ConcurrentHashMap engines; - private final ConcurrentHashMap conduits; - private final Hub hub; - private final Identifier identifier; - - - public StubNetwork(Hub hub) { - this.engines = new ConcurrentHashMap<>(); - this.conduits = new ConcurrentHashMap<>(); - this.hub = hub; - this.identifier = IdentifierFixture.NewIdentifier(); - this.hub.registerNetwork(identifier, this); - } - - public Identifier id() { - return this.identifier; - } - - private void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { - Conduit conduit = conduits.get(ch); - conduit.unicast(entity, stubNetworkR.id()); - } - - @Override - public MockConduit register(Engine en, String channel) throws IllegalStateException { - // TODO: this should be a separate class. - Conduit conduit = new MockConduit(channel,en, hub); - try { - if (engines.containsKey(channel)) { - throw new IllegalStateException(); - } - - engines.put(channel, en); - - conduits.put(channel, conduit); - } catch (Exception ex) { - ex.printStackTrace(); + private final ConcurrentHashMap engines; + + private final Hub hub; + private final Identifier identifier; + + + public StubNetwork(Hub hub) { + this.engines = new ConcurrentHashMap<>(); + + this.hub = hub; + this.identifier = IdentifierFixture.NewIdentifier(); + this.hub.registerNetwork(identifier, this); } - return (MockConduit) conduit; - } - public Engine getEngine(String ch){ - Engine engine = engines.get(ch); - return engine; - } + public Identifier id() { + return this.identifier; + } + + + public void receiveUnicast(Entity entity, String channel){ + Engine engine =getEngine(channel); + engine.process(entity); + } + @Override + public Conduit register(Engine en, String channel) throws IllegalStateException { + // + Conduit conduit = new MockConduit(channel,hub); + try { + if (engines.containsKey(channel)) { + throw new IllegalStateException(); + } + engines.put(channel, en); + + + + } catch (Exception ex) { + ex.printStackTrace(); + } + + return conduit; + } + + private Engine getEngine(String ch) { + return engines.get(ch); + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index d6ec679a..b1c86642 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Hashtable; +import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -18,115 +19,160 @@ import unittest.fixtures.EntityFixture; public class StubNetworkTest { - private ArrayList networkArrayList; - private ConcurrentHashMap> conduitConcurrentHashMap; - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. - @BeforeEach - void setup(){ - this.networkArrayList = new ArrayList(); - this.conduitConcurrentHashMap=new ConcurrentHashMap>(); - ArrayList conduitArrayList = new ArrayList(); - - String channel1 = "test-network-channel-1"; - String channel2 = "test-network-channel-2"; - Hub hub = new Hub(); - for (int i =0; i<10; i++){ - StubNetwork stubNetwork = new StubNetwork(hub); - Engine A1 =new MockEngine(); - Engine A2= new MockEngine(); - Conduit c1 =stubNetwork.register(A1,channel1); - Conduit c2 =stubNetwork.register(A2,channel2); - conduitArrayList.add(c1); - conduitArrayList.add(c2); - conduitConcurrentHashMap.put(stubNetwork,conduitArrayList); - networkArrayList.add(stubNetwork); - } - - } - @Test - void TestTwoStubNetworks_TwoEngines(){ - String channel1 = "test-network-channel-1"; - Hub hub = new Hub(); - - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); - - - Entity entity = new EntityFixture(); - - try { - c1.unicast(entity, network2.id()); - } catch (LightChainNetworkingException e) { - Assertions.fail(); - } - Assertions.assertTrue(A2.hasReceived(entity)); - } + private ConcurrentHashMap> conduitConcurrentHashMap; + + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + @BeforeEach + void setup() { + + this.conduitConcurrentHashMap = new ConcurrentHashMap<>(); + + String channel1 = "test-network-channel-1"; + String channel2 = "test-network-channel-2"; + Hub hub = new Hub(); + for (int i = 0; i < 10; i++) { + ArrayList conduitArrayList = new ArrayList<>(); + StubNetwork stubNetwork = new StubNetwork(hub); + Engine A1 = new MockEngine(); + Engine A2 = new MockEngine(); + Conduit c1 = stubNetwork.register(A1, channel1); + Conduit c2 = stubNetwork.register(A2, channel2); + conduitArrayList.add(c1); + conduitArrayList.add(c2); + conduitConcurrentHashMap.put(stubNetwork, conduitArrayList); - @Test - void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { - int concurrencyDegree = 100; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + } - String channel1 = "test-network-channel-1"; + } - Thread[] unicastThreads = new Thread[concurrencyDegree]; - Hub hub = new Hub(); + @Test + void TestTwoStubNetworks_TwoEngines() { + String channel1 = "test-network-channel-1"; + Hub hub = new Hub(); - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); - for(int i = 0; i < concurrencyDegree; i++){ - unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); try { - c1.unicast(entity, network2.id()); - if(!A2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - - countDownLatch.countDown(); + c1.unicast(entity, network2.id()); } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); + Assertions.fail(); } - }); + + Assertions.assertTrue(A2.hasReceived(entity)); } - for (Thread t: unicastThreads){ - t.start(); + @Test + void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + + String channel1 = "test-network-channel-1"; + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + Hub hub = new Hub(); + + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); + + + + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + + try { + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); } + void TestUnicastOneToAll_Concurrently() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + Iterator>> itr =conduitConcurrentHashMap.entrySet().iterator(); + + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + + + + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + + try { + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } - try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); + Assertions.assertEquals(0, threadError.get()); } - Assertions.assertEquals(0, threadError.get()); - } } \ No newline at end of file From 6caf0337c5b08978e1ea1c9510743e26c12b0e63 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:33:55 +0300 Subject: [PATCH 10/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/MockConduit.java | 4 +- src/test/java/networking/MockEngine.java | 9 ++- src/test/java/networking/StubNetwork.java | 2 +- src/test/java/networking/StubNetworkTest.java | 67 ++++++++++++------- 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 0b468b9f..f0dd2ad0 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,7 +5,9 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; - +/** + * MockConduit represents the Networking interface that is exposed to an Engine. + */ public class MockConduit implements Conduit { private final String channel; diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index ede91185..86bf8995 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -16,15 +16,18 @@ public class MockEngine implements Engine { private final Set receivedEntityIds; - public MockEngine() { this.receivedEntityIds = new HashSet<>(); this.lock = new ReentrantReadWriteLock(); } - - + /** + * Called by Network whenever an Entity is arrived for this engine. + * + * @param e the arrived Entity from the network. + * @throws IllegalArgumentException any unhappy path taken on processing the Entity. + */ @Override public void process(Entity e) throws IllegalArgumentException { diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 893373ca..4b417300 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -53,7 +53,7 @@ public Conduit register(Engine en, String channel) throws IllegalStateException return conduit; } - private Engine getEngine(String ch) { + public Engine getEngine(String ch) { return engines.get(ch); } diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index b1c86642..3bbd68c6 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -20,7 +21,11 @@ public class StubNetworkTest { - private ConcurrentHashMap> conduitConcurrentHashMap; + + private ArrayList networkArrayList; + private final String channel1 = "test-network-channel-1"; + private final String channel2 = "test-network-channel-2"; + private Hub hub; // TODO: add a test for each of the following scenarios: // Use mock engines. @@ -35,21 +40,18 @@ public class StubNetworkTest { @BeforeEach void setup() { - this.conduitConcurrentHashMap = new ConcurrentHashMap<>(); - String channel1 = "test-network-channel-1"; - String channel2 = "test-network-channel-2"; - Hub hub = new Hub(); - for (int i = 0; i < 10; i++) { - ArrayList conduitArrayList = new ArrayList<>(); + this.networkArrayList = new ArrayList<>(); + hub = new Hub(); + for (int i = 0; i < 9; i++) { + StubNetwork stubNetwork = new StubNetwork(hub); - Engine A1 = new MockEngine(); - Engine A2 = new MockEngine(); - Conduit c1 = stubNetwork.register(A1, channel1); - Conduit c2 = stubNetwork.register(A2, channel2); - conduitArrayList.add(c1); - conduitArrayList.add(c2); - conduitConcurrentHashMap.put(stubNetwork, conduitArrayList); + Engine E1 = new MockEngine(); + Engine E2 = new MockEngine(); + stubNetwork.register(E1, channel1); + stubNetwork.register(E2, channel2); + networkArrayList.add(stubNetwork); + } @@ -89,7 +91,7 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { String channel1 = "test-network-channel-1"; Thread[] unicastThreads = new Thread[concurrencyDegree]; - Hub hub = new Hub(); + StubNetwork network1 = new StubNetwork(hub); MockEngine A1 = new MockEngine(); @@ -100,7 +102,6 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { network2.register(A2, channel1); - for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); @@ -131,34 +132,51 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + + @Test void TestUnicastOneToAll_Concurrently() { - int concurrencyDegree = 100; + int concurrencyDegree = 9; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - Iterator>> itr =conduitConcurrentHashMap.entrySet().iterator(); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); Thread[] unicastThreads = new Thread[concurrencyDegree]; + int count = 0; + for (Network network : networkArrayList) { + unicastThreads[count] = new Thread(() -> { + try { - for (int i = 0; i < concurrencyDegree; i++) { - unicastThreads[i] = new Thread(() -> { - Entity entity = new EntityFixture(); - try { - c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + + if (!E1.hasReceived(entity)) { + System.out.println("error1"); + threadError.getAndIncrement(); + } + if (E2.hasReceived(entity)) { + System.out.println("error1"); threadError.getAndIncrement(); } countDownLatch.countDown(); } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } }); + count++; + } + for (Thread t : unicastThreads) { t.start(); } @@ -174,5 +192,4 @@ void TestUnicastOneToAll_Concurrently() { } - } \ No newline at end of file From 28a45a84904573fc2193708c250f6bffb8d0deb4 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:33:55 +0300 Subject: [PATCH 11/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/MockConduit.java | 4 +- src/test/java/networking/MockEngine.java | 9 ++- src/test/java/networking/StubNetwork.java | 2 +- src/test/java/networking/StubNetworkTest.java | 67 ++++++++++++------- .../java/networking/StubNetworkThread.java | 29 -------- 5 files changed, 52 insertions(+), 59 deletions(-) delete mode 100644 src/test/java/networking/StubNetworkThread.java diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 0b468b9f..f0dd2ad0 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,7 +5,9 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; - +/** + * MockConduit represents the Networking interface that is exposed to an Engine. + */ public class MockConduit implements Conduit { private final String channel; diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index ede91185..86bf8995 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -16,15 +16,18 @@ public class MockEngine implements Engine { private final Set receivedEntityIds; - public MockEngine() { this.receivedEntityIds = new HashSet<>(); this.lock = new ReentrantReadWriteLock(); } - - + /** + * Called by Network whenever an Entity is arrived for this engine. + * + * @param e the arrived Entity from the network. + * @throws IllegalArgumentException any unhappy path taken on processing the Entity. + */ @Override public void process(Entity e) throws IllegalArgumentException { diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 893373ca..4b417300 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -53,7 +53,7 @@ public Conduit register(Engine en, String channel) throws IllegalStateException return conduit; } - private Engine getEngine(String ch) { + public Engine getEngine(String ch) { return engines.get(ch); } diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index b1c86642..3bbd68c6 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -20,7 +21,11 @@ public class StubNetworkTest { - private ConcurrentHashMap> conduitConcurrentHashMap; + + private ArrayList networkArrayList; + private final String channel1 = "test-network-channel-1"; + private final String channel2 = "test-network-channel-2"; + private Hub hub; // TODO: add a test for each of the following scenarios: // Use mock engines. @@ -35,21 +40,18 @@ public class StubNetworkTest { @BeforeEach void setup() { - this.conduitConcurrentHashMap = new ConcurrentHashMap<>(); - String channel1 = "test-network-channel-1"; - String channel2 = "test-network-channel-2"; - Hub hub = new Hub(); - for (int i = 0; i < 10; i++) { - ArrayList conduitArrayList = new ArrayList<>(); + this.networkArrayList = new ArrayList<>(); + hub = new Hub(); + for (int i = 0; i < 9; i++) { + StubNetwork stubNetwork = new StubNetwork(hub); - Engine A1 = new MockEngine(); - Engine A2 = new MockEngine(); - Conduit c1 = stubNetwork.register(A1, channel1); - Conduit c2 = stubNetwork.register(A2, channel2); - conduitArrayList.add(c1); - conduitArrayList.add(c2); - conduitConcurrentHashMap.put(stubNetwork, conduitArrayList); + Engine E1 = new MockEngine(); + Engine E2 = new MockEngine(); + stubNetwork.register(E1, channel1); + stubNetwork.register(E2, channel2); + networkArrayList.add(stubNetwork); + } @@ -89,7 +91,7 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { String channel1 = "test-network-channel-1"; Thread[] unicastThreads = new Thread[concurrencyDegree]; - Hub hub = new Hub(); + StubNetwork network1 = new StubNetwork(hub); MockEngine A1 = new MockEngine(); @@ -100,7 +102,6 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { network2.register(A2, channel1); - for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); @@ -131,34 +132,51 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + + @Test void TestUnicastOneToAll_Concurrently() { - int concurrencyDegree = 100; + int concurrencyDegree = 9; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - Iterator>> itr =conduitConcurrentHashMap.entrySet().iterator(); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); Thread[] unicastThreads = new Thread[concurrencyDegree]; + int count = 0; + for (Network network : networkArrayList) { + unicastThreads[count] = new Thread(() -> { + try { - for (int i = 0; i < concurrencyDegree; i++) { - unicastThreads[i] = new Thread(() -> { - Entity entity = new EntityFixture(); - try { - c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + + if (!E1.hasReceived(entity)) { + System.out.println("error1"); + threadError.getAndIncrement(); + } + if (E2.hasReceived(entity)) { + System.out.println("error1"); threadError.getAndIncrement(); } countDownLatch.countDown(); } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } }); + count++; + } + for (Thread t : unicastThreads) { t.start(); } @@ -174,5 +192,4 @@ void TestUnicastOneToAll_Concurrently() { } - } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkThread.java b/src/test/java/networking/StubNetworkThread.java deleted file mode 100644 index 00e20117..00000000 --- a/src/test/java/networking/StubNetworkThread.java +++ /dev/null @@ -1,29 +0,0 @@ -package networking; - -import model.Entity; -import model.exceptions.LightChainNetworkingException; - -public class StubNetworkThread extends Thread{ - StubNetwork stubNetworkT; - StubNetwork stubNetworkR; - Entity entity; - String ch; - - public StubNetworkThread(StubNetwork stubNetworkT, StubNetwork stubNetworkR, Entity entity, String ch) { - this.stubNetworkT = stubNetworkT; - this.stubNetworkR = stubNetworkR; - this.entity = entity; - this.ch = ch; - } - -// @Override -// public void run() { -// try { -// -// // stubNetworkT.sendUnicast(ch,stubNetworkR,entity); -// } catch (LightChainNetworkingException e) { -// e.printStackTrace(); -// } -// -// } -} \ No newline at end of file From 02f3bc9db0bbe5e9a242534e8d15751f95424683 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:45:01 +0300 Subject: [PATCH 12/51] deletion of prints --- src/test/java/networking/StubNetworkTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 3bbd68c6..a5c3ae26 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -158,11 +158,9 @@ void TestUnicastOneToAll_Concurrently() { MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); if (!E1.hasReceived(entity)) { - System.out.println("error1"); threadError.getAndIncrement(); } if (E2.hasReceived(entity)) { - System.out.println("error1"); threadError.getAndIncrement(); } From e0e17506f48222be72d6e5bd80ea2d6dd9f6064d Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:47:57 +0300 Subject: [PATCH 13/51] deletion of prints --- src/main/java/model/lightchain/Identifier.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/model/lightchain/Identifier.java b/src/main/java/model/lightchain/Identifier.java index b104104b..a643b72c 100644 --- a/src/main/java/model/lightchain/Identifier.java +++ b/src/main/java/model/lightchain/Identifier.java @@ -52,8 +52,12 @@ public int comparedTo(Identifier other) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Identifier)) return false; + if (this == o) { + return true; + } + if (!(o instanceof Identifier)) { + return false; + } Identifier that = (Identifier) o; return Arrays.equals(value, that.value); } From 318326a8f10b6e63e965c657bc3d965014b9c3f5 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 14:18:44 +0300 Subject: [PATCH 14/51] deletion of prints --- src/test/java/networking/StubNetworkTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index a5c3ae26..c7df1817 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,14 +1,9 @@ package networking; import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; - import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; From 7972de01605187a48502b87f2800596364b0c6b1 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 11 Mar 2022 15:15:01 +0300 Subject: [PATCH 15/51] Continue to StubNetwork --- src/test/java/networking/Hub.java | 18 ++++- src/test/java/networking/MockEngine.java | 15 ++++- src/test/java/networking/StubNetwork.java | 67 +++++++++++++++++-- src/test/java/networking/StubNetworkTest.java | 53 ++++++++++++--- .../java/networking/StubNetworkThread.java | 29 ++++++++ 5 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 src/test/java/networking/StubNetworkThread.java diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index a6c0655a..fa573f8f 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -9,4 +9,20 @@ public class Hub { private ConcurrentHashMap networks; private ConcurrentHashMap entities; -} + + public Hub() { + this.networks=new ConcurrentHashMap<>(); + this.entities=new ConcurrentHashMap<>(); + } + + public void registerNetwork(Identifier key, Network network) { + networks.put( key, network); + + } + public StubNetwork getNetwork(Identifier key){ + + return (StubNetwork) networks.get(key); + + } + +} \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 78adb922..cf1d14dd 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -1,5 +1,6 @@ package networking; +import java.util.HashSet; import java.util.Set; import model.Entity; @@ -9,8 +10,20 @@ public class MockEngine implements Engine { private Set receivedEntityIds; + public MockEngine() { + this.receivedEntityIds = new HashSet<>(); + } + + @Override + public String toString() { + return "MockEngine{" + + "receivedEntityIds=" + receivedEntityIds + + '}'; + } + @Override public void process(Entity e) throws IllegalArgumentException { // TODO: put e.Id() in the set. + receivedEntityIds.add(e.id()); } -} +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index e64ac747..9f210755 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -2,21 +2,80 @@ import java.util.concurrent.ConcurrentHashMap; +import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.exceptions.LightChainNetworkingException; +import model.lightchain.Identifier; import network.Conduit; import network.Network; import protocol.Engine; +import unittest.fixtures.IdentifierFixture; public class StubNetwork implements Network { private ConcurrentHashMap engines; + private ConcurrentHashMap conduits; private Hub hub; + private Identifier identifier; - public StubNetwork(Hub hub){ + + + public StubNetwork(Hub hub) { this.engines = new ConcurrentHashMap<>(); + this.conduits= new ConcurrentHashMap<>(); this.hub = hub; + this.identifier = IdentifierFixture.NewIdentifier(); + this.hub.registerNetwork(identifier, this); + } + + public Identifier id() { + return this.identifier; + } + public void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { + Conduit conduit = conduits.get(ch); + conduit.unicast(entity,stubNetworkR.id()); + } @Override - public Conduit register(Engine e, String channel) throws IllegalStateException { - return null; + public Conduit register(Engine en, String channel) throws IllegalStateException { + Conduit conduit = new Conduit() { + @Override + public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { + StubNetwork net = hub.getNetwork(target); + net.deliverEntity(channel, e); + + } + + @Override + public void put(Entity e) throws LightChainDistributedStorageException { + + } + + @Override + public Entity get(Identifier identifier) throws LightChainDistributedStorageException { + return null; + } + }; + try { + if (engines.containsKey(channel)) { + throw new IllegalStateException(); + } + + engines.put(channel, en); + + conduits.put(channel, conduit); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return conduit; + } + + public void deliverEntity(String ch, Entity en) { + System.out.println(ch); + + Engine engine = engines.get(ch); + engine.process(en); + } -} +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index cab21f7e..595292b4 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,14 +1,45 @@ package networking; +import model.exceptions.LightChainNetworkingException; +import network.Conduit; +import network.Network; +import org.junit.jupiter.api.Test; +import protocol.Engine; +import unittest.fixtures.EntityFixture; + +import java.util.Iterator; + public class StubNetworkTest { - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. -} + + + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + + @Test + void test() throws LightChainNetworkingException { + Hub hub = new Hub(); + StubNetwork stubNetwork1 = new StubNetwork(hub); + StubNetwork stubNetwork2 = new StubNetwork(hub); + String channelA1="ChannelA1"; + + Engine A1 = new MockEngine(); + Engine B1 = new MockEngine(); + stubNetwork1.register(A1, channelA1); + stubNetwork2.register(B1,channelA1); + EntityFixture e1 = new EntityFixture(); + EntityFixture e2 = new EntityFixture(); + StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channelA1); + t1.start(); + //c1.unicast(e1, stubNetwork2.id()); + System.out.println(e1.id()); + System.out.println(B1); + } +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkThread.java b/src/test/java/networking/StubNetworkThread.java new file mode 100644 index 00000000..b88611b5 --- /dev/null +++ b/src/test/java/networking/StubNetworkThread.java @@ -0,0 +1,29 @@ +package networking; + +import model.Entity; +import model.exceptions.LightChainNetworkingException; + +public class StubNetworkThread extends Thread{ + StubNetwork stubNetworkT; + StubNetwork stubNetworkR; + Entity entity; + String ch; + + public StubNetworkThread(StubNetwork stubNetworkT, StubNetwork stubNetworkR, Entity entity, String ch) { + this.stubNetworkT = stubNetworkT; + this.stubNetworkR = stubNetworkR; + this.entity = entity; + this.ch = ch; + } + + @Override + public void run() { + try { + + stubNetworkT.sendUnicast(ch,stubNetworkR,entity); + } catch (LightChainNetworkingException e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file From 7c7479a9d8b89531ba5f0ef3d42c9674a015a463 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 11 Mar 2022 16:13:02 +0300 Subject: [PATCH 16/51] TestUnicastOneToAll_Sequentially implemented --- src/test/java/networking/StubNetwork.java | 2 +- src/test/java/networking/StubNetworkTest.java | 83 ++++++++++++++++++- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 9f210755..e9ec42c6 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -72,7 +72,7 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce } public void deliverEntity(String ch, Entity en) { - System.out.println(ch); + Engine engine = engines.get(ch); engine.process(en); diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 595292b4..0d6ac7e9 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -22,12 +22,45 @@ public class StubNetworkTest { // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B // and B must be on another same channel. // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + Hub hub = new Hub(); + StubNetwork stubNetwork1 = new StubNetwork(hub); + StubNetwork stubNetwork2 = new StubNetwork(hub); + StubNetwork stubNetwork3 = new StubNetwork(hub); + StubNetwork stubNetwork4 = new StubNetwork(hub); + StubNetwork stubNetwork5 = new StubNetwork(hub); + StubNetwork stubNetwork6 = new StubNetwork(hub); + StubNetwork stubNetwork7 = new StubNetwork(hub); + StubNetwork stubNetwork8 = new StubNetwork(hub); + StubNetwork stubNetwork9 = new StubNetwork(hub); + StubNetwork stubNetwork10 = new StubNetwork(hub); + String channel1="test-network-channel-1"; + String channel2="test-network-channel-2"; + Engine A1 = new MockEngine(); + Engine A2 = new MockEngine(); + Engine B1 = new MockEngine(); + Engine B2 = new MockEngine(); + Engine C1 = new MockEngine(); + Engine C2 = new MockEngine(); + Engine D1 = new MockEngine(); + Engine D2 = new MockEngine(); + Engine E1 = new MockEngine(); + Engine E2 = new MockEngine(); + Engine F1 = new MockEngine(); + Engine F2 = new MockEngine(); + Engine G1 = new MockEngine(); + Engine G2 = new MockEngine(); + Engine H1 = new MockEngine(); + Engine H2 = new MockEngine(); + Engine I1 = new MockEngine(); + Engine I2 = new MockEngine(); + Engine J1 = new MockEngine(); + Engine J2 = new MockEngine(); + @Test + void test() throws LightChainNetworkingException { - Hub hub = new Hub(); - StubNetwork stubNetwork1 = new StubNetwork(hub); - StubNetwork stubNetwork2 = new StubNetwork(hub); + String channelA1="ChannelA1"; Engine A1 = new MockEngine(); @@ -42,4 +75,48 @@ void test() throws LightChainNetworkingException { System.out.println(e1.id()); System.out.println(B1); } + + @Test + void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { + Conduit c1 =stubNetwork1.register(A1,channel1); + EntityFixture e1 =new EntityFixture(); + + stubNetwork1.register(A2,channel2); + stubNetwork2.register(B1,channel1); + stubNetwork2.register(B2,channel2); + stubNetwork3.register(C1,channel1); + stubNetwork3.register(C2,channel2); + stubNetwork4.register(D1,channel1); + stubNetwork4.register(D2,channel2); + stubNetwork5.register(E1,channel1); + stubNetwork5.register(E2,channel2); + stubNetwork6.register(F1,channel1); + stubNetwork6.register(F2,channel2); + stubNetwork7.register(G1,channel1); + stubNetwork7.register(G2,channel2); + stubNetwork8.register(H1,channel1); + stubNetwork8.register(H2,channel2); + stubNetwork9.register(I1,channel1); + stubNetwork9.register(I2,channel2); + stubNetwork10.register(J1,channel1); + stubNetwork10.register(J2,channel2); + c1.unicast(e1,stubNetwork2.id()); + c1.unicast(e1,stubNetwork3.id()); + c1.unicast(e1,stubNetwork4.id()); + c1.unicast(e1,stubNetwork5.id()); + c1.unicast(e1,stubNetwork6.id()); + c1.unicast(e1,stubNetwork7.id()); + c1.unicast(e1,stubNetwork8.id()); + c1.unicast(e1,stubNetwork9.id()); + c1.unicast(e1,stubNetwork10.id()); + System.out.println(e1.id()); + System.out.println(B1); + System.out.println(B2); + System.out.println(C1); + System.out.println(C2); + System.out.println(D1); + System.out.println(D2); + + + } } \ No newline at end of file From 2013671e453baa770feec653977340df56c5826a Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 11 Mar 2022 16:29:36 +0300 Subject: [PATCH 17/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/StubNetworkTest.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 0d6ac7e9..4702a9da 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -119,4 +119,57 @@ void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { } + + @Test + void TestUnicastOneToAll_Concurrently() { + + EntityFixture e1 =new EntityFixture(); + stubNetwork1.register(A1,channel1); + stubNetwork1.register(A2,channel2); + stubNetwork2.register(B1,channel1); + stubNetwork2.register(B2,channel2); + stubNetwork3.register(C1,channel1); + stubNetwork3.register(C2,channel2); + stubNetwork4.register(D1,channel1); + stubNetwork4.register(D2,channel2); + stubNetwork5.register(E1,channel1); + stubNetwork5.register(E2,channel2); + stubNetwork6.register(F1,channel1); + stubNetwork6.register(F2,channel2); + stubNetwork7.register(G1,channel1); + stubNetwork7.register(G2,channel2); + stubNetwork8.register(H1,channel1); + stubNetwork8.register(H2,channel2); + stubNetwork9.register(I1,channel1); + stubNetwork9.register(I2,channel2); + stubNetwork10.register(J1,channel1); + stubNetwork10.register(J2,channel2); + StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channel1); + StubNetworkThread t2 = new StubNetworkThread(stubNetwork1,stubNetwork3,e1,channel1); + StubNetworkThread t3 = new StubNetworkThread(stubNetwork1,stubNetwork4,e1,channel1); + StubNetworkThread t4 = new StubNetworkThread(stubNetwork1,stubNetwork5,e1,channel1); + StubNetworkThread t5 = new StubNetworkThread(stubNetwork1,stubNetwork6,e1,channel1); + StubNetworkThread t6 = new StubNetworkThread(stubNetwork1,stubNetwork7,e1,channel1); + StubNetworkThread t7 = new StubNetworkThread(stubNetwork1,stubNetwork8,e1,channel1); + StubNetworkThread t8 = new StubNetworkThread(stubNetwork1,stubNetwork9,e1,channel1); + StubNetworkThread t9 = new StubNetworkThread(stubNetwork1,stubNetwork10,e1,channel1); + + t1.start(); + t2.start(); + t3.start(); + t4.start(); + t5.start(); + t6.start(); + t7.start(); + t8.start(); + t9.start(); + System.out.println(e1.id()); + System.out.println(B1); + System.out.println(B2); + System.out.println(C1); + System.out.println(C2); + System.out.println(D1); + System.out.println(D2); + + } } \ No newline at end of file From b6f48da9ef67a057247b456c01473cb5d6035f8d Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Fri, 11 Mar 2022 07:23:11 -0800 Subject: [PATCH 18/51] wip --- src/test/java/networking/Hub.java | 13 +- src/test/java/networking/MockEngine.java | 6 +- src/test/java/networking/StubNetwork.java | 14 +- src/test/java/networking/StubNetworkTest.java | 307 +++++++++--------- 4 files changed, 162 insertions(+), 178 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index fa573f8f..5f7c4130 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -7,19 +7,20 @@ import network.Network; public class Hub { - private ConcurrentHashMap networks; - private ConcurrentHashMap entities; + private final ConcurrentHashMap networks; + private final ConcurrentHashMap entities; public Hub() { - this.networks=new ConcurrentHashMap<>(); - this.entities=new ConcurrentHashMap<>(); + this.networks = new ConcurrentHashMap<>(); + this.entities = new ConcurrentHashMap<>(); } public void registerNetwork(Identifier key, Network network) { - networks.put( key, network); + networks.put(key, network); } - public StubNetwork getNetwork(Identifier key){ + + public StubNetwork getNetwork(Identifier key) { return (StubNetwork) networks.get(key); diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index cf1d14dd..20b1f559 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -8,7 +8,7 @@ import protocol.Engine; public class MockEngine implements Engine { - private Set receivedEntityIds; + private final Set receivedEntityIds; public MockEngine() { this.receivedEntityIds = new HashSet<>(); @@ -17,8 +17,8 @@ public MockEngine() { @Override public String toString() { return "MockEngine{" + - "receivedEntityIds=" + receivedEntityIds + - '}'; + "receivedEntityIds=" + receivedEntityIds + + '}'; } @Override diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index e9ec42c6..1e134461 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -12,16 +12,15 @@ import unittest.fixtures.IdentifierFixture; public class StubNetwork implements Network { - private ConcurrentHashMap engines; - private ConcurrentHashMap conduits; - private Hub hub; - private Identifier identifier; - + private final ConcurrentHashMap engines; + private final ConcurrentHashMap conduits; + private final Hub hub; + private final Identifier identifier; public StubNetwork(Hub hub) { this.engines = new ConcurrentHashMap<>(); - this.conduits= new ConcurrentHashMap<>(); + this.conduits = new ConcurrentHashMap<>(); this.hub = hub; this.identifier = IdentifierFixture.NewIdentifier(); this.hub.registerNetwork(identifier, this); @@ -30,9 +29,10 @@ public StubNetwork(Hub hub) { public Identifier id() { return this.identifier; } + public void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { Conduit conduit = conduits.get(ch); - conduit.unicast(entity,stubNetworkR.id()); + conduit.unicast(entity, stubNetworkR.id()); } diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 4702a9da..887eac9b 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,175 +1,158 @@ package networking; +import java.util.ArrayList; +import java.util.Hashtable; + import model.exceptions.LightChainNetworkingException; import network.Conduit; -import network.Network; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import protocol.Engine; import unittest.fixtures.EntityFixture; -import java.util.Iterator; - public class StubNetworkTest { - - - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + + @Test + void TestTwoStubNetworks_TwoEngines(){ + String channel1 = "test-network-channel-1"; Hub hub = new Hub(); - StubNetwork stubNetwork1 = new StubNetwork(hub); - StubNetwork stubNetwork2 = new StubNetwork(hub); - StubNetwork stubNetwork3 = new StubNetwork(hub); - StubNetwork stubNetwork4 = new StubNetwork(hub); - StubNetwork stubNetwork5 = new StubNetwork(hub); - StubNetwork stubNetwork6 = new StubNetwork(hub); - StubNetwork stubNetwork7 = new StubNetwork(hub); - StubNetwork stubNetwork8 = new StubNetwork(hub); - StubNetwork stubNetwork9 = new StubNetwork(hub); - StubNetwork stubNetwork10 = new StubNetwork(hub); - String channel1="test-network-channel-1"; - String channel2="test-network-channel-2"; - Engine A1 = new MockEngine(); - Engine A2 = new MockEngine(); - Engine B1 = new MockEngine(); - Engine B2 = new MockEngine(); - Engine C1 = new MockEngine(); - Engine C2 = new MockEngine(); - Engine D1 = new MockEngine(); - Engine D2 = new MockEngine(); - Engine E1 = new MockEngine(); - Engine E2 = new MockEngine(); - Engine F1 = new MockEngine(); - Engine F2 = new MockEngine(); - Engine G1 = new MockEngine(); - Engine G2 = new MockEngine(); - Engine H1 = new MockEngine(); - Engine H2 = new MockEngine(); - Engine I1 = new MockEngine(); - Engine I2 = new MockEngine(); - Engine J1 = new MockEngine(); - Engine J2 = new MockEngine(); - - - @Test - - void test() throws LightChainNetworkingException { - - String channelA1="ChannelA1"; - Engine A1 = new MockEngine(); - Engine B1 = new MockEngine(); - stubNetwork1.register(A1, channelA1); - stubNetwork2.register(B1,channelA1); - EntityFixture e1 = new EntityFixture(); - EntityFixture e2 = new EntityFixture(); - StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channelA1); - t1.start(); - //c1.unicast(e1, stubNetwork2.id()); - System.out.println(e1.id()); - System.out.println(B1); - } - - @Test - void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { - Conduit c1 =stubNetwork1.register(A1,channel1); - EntityFixture e1 =new EntityFixture(); - - stubNetwork1.register(A2,channel2); - stubNetwork2.register(B1,channel1); - stubNetwork2.register(B2,channel2); - stubNetwork3.register(C1,channel1); - stubNetwork3.register(C2,channel2); - stubNetwork4.register(D1,channel1); - stubNetwork4.register(D2,channel2); - stubNetwork5.register(E1,channel1); - stubNetwork5.register(E2,channel2); - stubNetwork6.register(F1,channel1); - stubNetwork6.register(F2,channel2); - stubNetwork7.register(G1,channel1); - stubNetwork7.register(G2,channel2); - stubNetwork8.register(H1,channel1); - stubNetwork8.register(H2,channel2); - stubNetwork9.register(I1,channel1); - stubNetwork9.register(I2,channel2); - stubNetwork10.register(J1,channel1); - stubNetwork10.register(J2,channel2); - c1.unicast(e1,stubNetwork2.id()); - c1.unicast(e1,stubNetwork3.id()); - c1.unicast(e1,stubNetwork4.id()); - c1.unicast(e1,stubNetwork5.id()); - c1.unicast(e1,stubNetwork6.id()); - c1.unicast(e1,stubNetwork7.id()); - c1.unicast(e1,stubNetwork8.id()); - c1.unicast(e1,stubNetwork9.id()); - c1.unicast(e1,stubNetwork10.id()); - System.out.println(e1.id()); - System.out.println(B1); - System.out.println(B2); - System.out.println(C1); - System.out.println(C2); - System.out.println(D1); - System.out.println(D2); - - - } - - @Test - void TestUnicastOneToAll_Concurrently() { - - EntityFixture e1 =new EntityFixture(); - stubNetwork1.register(A1,channel1); - stubNetwork1.register(A2,channel2); - stubNetwork2.register(B1,channel1); - stubNetwork2.register(B2,channel2); - stubNetwork3.register(C1,channel1); - stubNetwork3.register(C2,channel2); - stubNetwork4.register(D1,channel1); - stubNetwork4.register(D2,channel2); - stubNetwork5.register(E1,channel1); - stubNetwork5.register(E2,channel2); - stubNetwork6.register(F1,channel1); - stubNetwork6.register(F2,channel2); - stubNetwork7.register(G1,channel1); - stubNetwork7.register(G2,channel2); - stubNetwork8.register(H1,channel1); - stubNetwork8.register(H2,channel2); - stubNetwork9.register(I1,channel1); - stubNetwork9.register(I2,channel2); - stubNetwork10.register(J1,channel1); - stubNetwork10.register(J2,channel2); - StubNetworkThread t1 = new StubNetworkThread(stubNetwork1,stubNetwork2,e1,channel1); - StubNetworkThread t2 = new StubNetworkThread(stubNetwork1,stubNetwork3,e1,channel1); - StubNetworkThread t3 = new StubNetworkThread(stubNetwork1,stubNetwork4,e1,channel1); - StubNetworkThread t4 = new StubNetworkThread(stubNetwork1,stubNetwork5,e1,channel1); - StubNetworkThread t5 = new StubNetworkThread(stubNetwork1,stubNetwork6,e1,channel1); - StubNetworkThread t6 = new StubNetworkThread(stubNetwork1,stubNetwork7,e1,channel1); - StubNetworkThread t7 = new StubNetworkThread(stubNetwork1,stubNetwork8,e1,channel1); - StubNetworkThread t8 = new StubNetworkThread(stubNetwork1,stubNetwork9,e1,channel1); - StubNetworkThread t9 = new StubNetworkThread(stubNetwork1,stubNetwork10,e1,channel1); - - t1.start(); - t2.start(); - t3.start(); - t4.start(); - t5.start(); - t6.start(); - t7.start(); - t8.start(); - t9.start(); - System.out.println(e1.id()); - System.out.println(B1); - System.out.println(B2); - System.out.println(C1); - System.out.println(C2); - System.out.println(D1); - System.out.println(D2); + StubNetwork network1 = new StubNetwork(hub); + Engine A1 = new MockEngine(); + network1.register(A1, channel1); - } + StubNetwork network2 = new StubNetwork(hub); + Engine A2 = new MockEngine(); + network2.register(A2, channel1); + + + network1.sendUnicast(); + + } + + +// @Test +// void test() throws LightChainNetworkingException { +// +// String channelA1 = "ChannelA1"; +// +// Engine A1 = new MockEngine(); +// Engine B1 = new MockEngine(); +// stubNetwork1.register(A1, channelA1); +// stubNetwork2.register(B1, channelA1); +// EntityFixture e1 = new EntityFixture(); +// EntityFixture e2 = new EntityFixture(); +// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channelA1); +// t1.start(); +// //c1.unicast(e1, stubNetwork2.id()); +// System.out.println(e1.id()); +// System.out.println(B1); +// } +// +// @Test +// void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { +// Conduit c1 = stubNetwork1.register(A1, channel1); +// EntityFixture e1 = new EntityFixture(); +// +// stubNetwork1.register(A2, channel2); +// stubNetwork2.register(B1, channel1); +// stubNetwork2.register(B2, channel2); +// stubNetwork3.register(C1, channel1); +// stubNetwork3.register(C2, channel2); +// stubNetwork4.register(D1, channel1); +// stubNetwork4.register(D2, channel2); +// stubNetwork5.register(E1, channel1); +// stubNetwork5.register(E2, channel2); +// stubNetwork6.register(F1, channel1); +// stubNetwork6.register(F2, channel2); +// stubNetwork7.register(G1, channel1); +// stubNetwork7.register(G2, channel2); +// stubNetwork8.register(H1, channel1); +// stubNetwork8.register(H2, channel2); +// stubNetwork9.register(I1, channel1); +// stubNetwork9.register(I2, channel2); +// stubNetwork10.register(J1, channel1); +// stubNetwork10.register(J2, channel2); +// c1.unicast(e1, stubNetwork2.id()); +// c1.unicast(e1, stubNetwork3.id()); +// c1.unicast(e1, stubNetwork4.id()); +// c1.unicast(e1, stubNetwork5.id()); +// c1.unicast(e1, stubNetwork6.id()); +// c1.unicast(e1, stubNetwork7.id()); +// c1.unicast(e1, stubNetwork8.id()); +// c1.unicast(e1, stubNetwork9.id()); +// c1.unicast(e1, stubNetwork10.id()); +// System.out.println(e1.id()); +// System.out.println(B1); +// System.out.println(B2); +// System.out.println(C1); +// System.out.println(C2); +// System.out.println(D1); +// System.out.println(D2); +// +// +// } +// +// @Test +// void TestUnicastOneToAll_Concurrently() { +// +// EntityFixture e1 = new EntityFixture(); +// stubNetwork1.register(A1, channel1); +// stubNetwork1.register(A2, channel2); +// stubNetwork2.register(B1, channel1); +// stubNetwork2.register(B2, channel2); +// stubNetwork3.register(C1, channel1); +// stubNetwork3.register(C2, channel2); +// stubNetwork4.register(D1, channel1); +// stubNetwork4.register(D2, channel2); +// stubNetwork5.register(E1, channel1); +// stubNetwork5.register(E2, channel2); +// stubNetwork6.register(F1, channel1); +// stubNetwork6.register(F2, channel2); +// stubNetwork7.register(G1, channel1); +// stubNetwork7.register(G2, channel2); +// stubNetwork8.register(H1, channel1); +// stubNetwork8.register(H2, channel2); +// stubNetwork9.register(I1, channel1); +// stubNetwork9.register(I2, channel2); +// stubNetwork10.register(J1, channel1); +// stubNetwork10.register(J2, channel2); +// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channel1); +// StubNetworkThread t2 = new StubNetworkThread(stubNetwork1, stubNetwork3, e1, channel1); +// StubNetworkThread t3 = new StubNetworkThread(stubNetwork1, stubNetwork4, e1, channel1); +// StubNetworkThread t4 = new StubNetworkThread(stubNetwork1, stubNetwork5, e1, channel1); +// StubNetworkThread t5 = new StubNetworkThread(stubNetwork1, stubNetwork6, e1, channel1); +// StubNetworkThread t6 = new StubNetworkThread(stubNetwork1, stubNetwork7, e1, channel1); +// StubNetworkThread t7 = new StubNetworkThread(stubNetwork1, stubNetwork8, e1, channel1); +// StubNetworkThread t8 = new StubNetworkThread(stubNetwork1, stubNetwork9, e1, channel1); +// StubNetworkThread t9 = new StubNetworkThread(stubNetwork1, stubNetwork10, e1, channel1); +// +// t1.start(); +// t2.start(); +// t3.start(); +// t4.start(); +// t5.start(); +// t6.start(); +// t7.start(); +// t8.start(); +// t9.start(); +// System.out.println(e1.id()); +// System.out.println(B1); +// System.out.println(B2); +// System.out.println(C1); +// System.out.println(C2); +// System.out.println(D1); +// System.out.println(D2); +// +// } } \ No newline at end of file From 18c4e782246238bbe570d825801f24e5b615a046 Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Fri, 11 Mar 2022 07:41:59 -0800 Subject: [PATCH 19/51] implements test for two engines --- .../java/model/lightchain/Identifier.java | 13 ++++++++++++ src/test/java/networking/Hub.java | 2 -- src/test/java/networking/MockEngine.java | 15 +++++++------- src/test/java/networking/StubNetwork.java | 10 ++++------ src/test/java/networking/StubNetworkTest.java | 17 ++++++++++++---- .../java/networking/StubNetworkThread.java | 20 +++++++++---------- 6 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/main/java/model/lightchain/Identifier.java b/src/main/java/model/lightchain/Identifier.java index 2b83703c..b104104b 100644 --- a/src/main/java/model/lightchain/Identifier.java +++ b/src/main/java/model/lightchain/Identifier.java @@ -49,4 +49,17 @@ public int comparedTo(Identifier other) { int result = Arrays.compare(this.value, other.value); return Integer.compare(result, 0); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Identifier)) return false; + Identifier that = (Identifier) o; + return Arrays.equals(value, that.value); + } + + @Override + public int hashCode() { + return Arrays.hashCode(value); + } } diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 5f7c4130..0fbfa358 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -21,9 +21,7 @@ public void registerNetwork(Identifier key, Network network) { } public StubNetwork getNetwork(Identifier key) { - return (StubNetwork) networks.get(key); - } } \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 20b1f559..7ffde796 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -8,22 +8,21 @@ import protocol.Engine; public class MockEngine implements Engine { - private final Set receivedEntityIds; + private Set receivedEntityIds; public MockEngine() { this.receivedEntityIds = new HashSet<>(); } - @Override - public String toString() { - return "MockEngine{" + - "receivedEntityIds=" + receivedEntityIds + - '}'; - } - @Override public void process(Entity e) throws IllegalArgumentException { // TODO: put e.Id() in the set. receivedEntityIds.add(e.id()); } + + public boolean hasReceived(Entity e) { + Identifier id = e.id(); + boolean ok = this.receivedEntityIds.contains(id); + return ok; + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 1e134461..0dbd69ce 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -30,20 +30,19 @@ public Identifier id() { return this.identifier; } - public void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { + private void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { Conduit conduit = conduits.get(ch); conduit.unicast(entity, stubNetworkR.id()); - } @Override public Conduit register(Engine en, String channel) throws IllegalStateException { + // TODO: this should be a separate class. Conduit conduit = new Conduit() { @Override public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { StubNetwork net = hub.getNetwork(target); net.deliverEntity(channel, e); - } @Override @@ -56,6 +55,8 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce return null; } }; + + try { if (engines.containsKey(channel)) { throw new IllegalStateException(); @@ -72,10 +73,7 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce } public void deliverEntity(String ch, Entity en) { - - Engine engine = engines.get(ch); engine.process(en); - } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 887eac9b..4dd2fd0c 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,8 +3,10 @@ import java.util.ArrayList; import java.util.Hashtable; +import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import protocol.Engine; @@ -28,16 +30,23 @@ void TestTwoStubNetworks_TwoEngines(){ Hub hub = new Hub(); StubNetwork network1 = new StubNetwork(hub); - Engine A1 = new MockEngine(); - network1.register(A1, channel1); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); StubNetwork network2 = new StubNetwork(hub); - Engine A2 = new MockEngine(); + MockEngine A2 = new MockEngine(); network2.register(A2, channel1); - network1.sendUnicast(); + Entity entity = new EntityFixture(); + try { + c1.unicast(entity, network2.id()); + } catch (LightChainNetworkingException e) { + Assertions.fail(); + } + + Assertions.assertTrue(A2.hasReceived(entity)); } diff --git a/src/test/java/networking/StubNetworkThread.java b/src/test/java/networking/StubNetworkThread.java index b88611b5..00e20117 100644 --- a/src/test/java/networking/StubNetworkThread.java +++ b/src/test/java/networking/StubNetworkThread.java @@ -16,14 +16,14 @@ public StubNetworkThread(StubNetwork stubNetworkT, StubNetwork stubNetworkR, Ent this.ch = ch; } - @Override - public void run() { - try { - - stubNetworkT.sendUnicast(ch,stubNetworkR,entity); - } catch (LightChainNetworkingException e) { - e.printStackTrace(); - } - - } +// @Override +// public void run() { +// try { +// +// // stubNetworkT.sendUnicast(ch,stubNetworkR,entity); +// } catch (LightChainNetworkingException e) { +// e.printStackTrace(); +// } +// +// } } \ No newline at end of file From b63acf9ecff1b29b7e6022d479f44e6bebd22729 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Mon, 14 Mar 2022 17:10:35 +0300 Subject: [PATCH 20/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/MockConduit.java | 62 +++++++++++++++++++ src/test/java/networking/StubNetwork.java | 35 +++-------- src/test/java/networking/StubNetworkTest.java | 57 ++++++++++------- 3 files changed, 104 insertions(+), 50 deletions(-) create mode 100644 src/test/java/networking/MockConduit.java diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java new file mode 100644 index 00000000..48d00450 --- /dev/null +++ b/src/test/java/networking/MockConduit.java @@ -0,0 +1,62 @@ +package networking; + +import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.exceptions.LightChainNetworkingException; +import model.lightchain.Identifier; +import network.Conduit; +import network.Network; +import protocol.Engine; + +public class MockConduit implements Conduit { + private String channel; + private Engine engine; + private Hub hub; + + public MockConduit(String channel, Engine engine, Hub hub) { + this.channel = channel; + this.engine = engine; + this.hub = hub; + } + + /** + * Sends the Entity through the Network to the remote target. + * + * @param e the Entity to be sent over the network. + * @param target Identifier of the receiver. + * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. + */ + @Override + public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { + StubNetwork net = hub.getNetwork(target); + deliverEntity(net,channel , e); + } + + /** + * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. + * + * @param e the Entity to be stored over the network. + * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. + */ + @Override + public void put(Entity e) throws LightChainDistributedStorageException { + + } + + /** + * Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table + * (DHT) of nodes. + * + * @param identifier identifier of the entity to be retrieved. + * @return the retrieved entity or null if it does not exist. + * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. + */ + @Override + public Entity get(Identifier identifier) throws LightChainDistributedStorageException { + return null; + } + public void deliverEntity(StubNetwork network,String channel, Entity en) { + Engine eng =network.getEngine(channel); + eng.process(en); + } +} \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 0dbd69ce..2002bfd5 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -1,9 +1,6 @@ package networking; -import java.util.concurrent.ConcurrentHashMap; - import model.Entity; -import model.exceptions.LightChainDistributedStorageException; import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; @@ -11,6 +8,8 @@ import protocol.Engine; import unittest.fixtures.IdentifierFixture; +import java.util.concurrent.ConcurrentHashMap; + public class StubNetwork implements Network { private final ConcurrentHashMap engines; private final ConcurrentHashMap conduits; @@ -36,27 +35,9 @@ private void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) thr } @Override - public Conduit register(Engine en, String channel) throws IllegalStateException { + public MockConduit register(Engine en, String channel) throws IllegalStateException { // TODO: this should be a separate class. - Conduit conduit = new Conduit() { - @Override - public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { - StubNetwork net = hub.getNetwork(target); - net.deliverEntity(channel, e); - } - - @Override - public void put(Entity e) throws LightChainDistributedStorageException { - - } - - @Override - public Entity get(Identifier identifier) throws LightChainDistributedStorageException { - return null; - } - }; - - + Conduit conduit = new MockConduit(channel,en, hub); try { if (engines.containsKey(channel)) { throw new IllegalStateException(); @@ -69,11 +50,11 @@ public Entity get(Identifier identifier) throws LightChainDistributedStorageExce ex.printStackTrace(); } - return conduit; + return (MockConduit) conduit; } - - public void deliverEntity(String ch, Entity en) { + public Engine getEngine(String ch){ Engine engine = engines.get(ch); - engine.process(en); + return engine; } + } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 4dd2fd0c..0aaacefe 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -2,10 +2,12 @@ import java.util.ArrayList; import java.util.Hashtable; +import java.util.concurrent.ConcurrentHashMap; import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; +import network.Network; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -13,6 +15,8 @@ import unittest.fixtures.EntityFixture; public class StubNetworkTest { + private ArrayList networkArrayList; + private ConcurrentHashMap> conduitConcurrentHashMap; // TODO: add a test for each of the following scenarios: // Use mock engines. // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. @@ -23,7 +27,28 @@ public class StubNetworkTest { // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B // and B must be on another same channel. // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + @BeforeEach + void setup(){ + this.networkArrayList = new ArrayList(); + this.conduitConcurrentHashMap=new ConcurrentHashMap>(); + ArrayList conduitArrayList = new ArrayList(); + String channel1 = "test-network-channel-1"; + String channel2 = "test-network-channel-2"; + Hub hub = new Hub(); + for (int i =0; i<10; i++){ + StubNetwork stubNetwork = new StubNetwork(hub); + Engine A1 =new MockEngine(); + Engine A2= new MockEngine(); + Conduit c1 =stubNetwork.register(A1,channel1); + Conduit c2 =stubNetwork.register(A2,channel2); + conduitArrayList.add(c1); + conduitArrayList.add(c2); + conduitConcurrentHashMap.put(stubNetwork,conduitArrayList); + networkArrayList.add(stubNetwork); + } + + } @Test void TestTwoStubNetworks_TwoEngines(){ String channel1 = "test-network-channel-1"; @@ -50,26 +75,13 @@ void TestTwoStubNetworks_TwoEngines(){ } -// @Test -// void test() throws LightChainNetworkingException { -// -// String channelA1 = "ChannelA1"; -// -// Engine A1 = new MockEngine(); -// Engine B1 = new MockEngine(); -// stubNetwork1.register(A1, channelA1); -// stubNetwork2.register(B1, channelA1); -// EntityFixture e1 = new EntityFixture(); -// EntityFixture e2 = new EntityFixture(); -// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channelA1); -// t1.start(); -// //c1.unicast(e1, stubNetwork2.id()); -// System.out.println(e1.id()); -// System.out.println(B1); -// } -// -// @Test -// void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { + + + @Test + void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { + + + // Conduit c1 = stubNetwork1.register(A1, channel1); // EntityFixture e1 = new EntityFixture(); // @@ -108,9 +120,8 @@ void TestTwoStubNetworks_TwoEngines(){ // System.out.println(C2); // System.out.println(D1); // System.out.println(D2); -// -// -// } + + } // // @Test // void TestUnicastOneToAll_Concurrently() { From 0878ecdfdc69de20dc0bdb1a323ad8b46d3f52df Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Mon, 14 Mar 2022 07:35:04 -0700 Subject: [PATCH 21/51] adds concurrent test --- src/test/java/networking/StubNetworkTest.java | 146 ++++++------------ 1 file changed, 50 insertions(+), 96 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 0aaacefe..d6ec679a 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,6 +3,9 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import model.Entity; import model.exceptions.LightChainNetworkingException; @@ -74,105 +77,56 @@ void TestTwoStubNetworks_TwoEngines(){ Assertions.assertTrue(A2.hasReceived(entity)); } + @Test + void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + String channel1 = "test-network-channel-1"; + Thread[] unicastThreads = new Thread[concurrencyDegree]; + Hub hub = new Hub(); + + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); - @Test - void TestUnicastOneToAll_Sequentially() throws LightChainNetworkingException { - - - -// Conduit c1 = stubNetwork1.register(A1, channel1); -// EntityFixture e1 = new EntityFixture(); -// -// stubNetwork1.register(A2, channel2); -// stubNetwork2.register(B1, channel1); -// stubNetwork2.register(B2, channel2); -// stubNetwork3.register(C1, channel1); -// stubNetwork3.register(C2, channel2); -// stubNetwork4.register(D1, channel1); -// stubNetwork4.register(D2, channel2); -// stubNetwork5.register(E1, channel1); -// stubNetwork5.register(E2, channel2); -// stubNetwork6.register(F1, channel1); -// stubNetwork6.register(F2, channel2); -// stubNetwork7.register(G1, channel1); -// stubNetwork7.register(G2, channel2); -// stubNetwork8.register(H1, channel1); -// stubNetwork8.register(H2, channel2); -// stubNetwork9.register(I1, channel1); -// stubNetwork9.register(I2, channel2); -// stubNetwork10.register(J1, channel1); -// stubNetwork10.register(J2, channel2); -// c1.unicast(e1, stubNetwork2.id()); -// c1.unicast(e1, stubNetwork3.id()); -// c1.unicast(e1, stubNetwork4.id()); -// c1.unicast(e1, stubNetwork5.id()); -// c1.unicast(e1, stubNetwork6.id()); -// c1.unicast(e1, stubNetwork7.id()); -// c1.unicast(e1, stubNetwork8.id()); -// c1.unicast(e1, stubNetwork9.id()); -// c1.unicast(e1, stubNetwork10.id()); -// System.out.println(e1.id()); -// System.out.println(B1); -// System.out.println(B2); -// System.out.println(C1); -// System.out.println(C2); -// System.out.println(D1); -// System.out.println(D2); + for(int i = 0; i < concurrencyDegree; i++){ + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + + try { + c1.unicast(entity, network2.id()); + if(!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t: unicastThreads){ + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); } -// -// @Test -// void TestUnicastOneToAll_Concurrently() { -// -// EntityFixture e1 = new EntityFixture(); -// stubNetwork1.register(A1, channel1); -// stubNetwork1.register(A2, channel2); -// stubNetwork2.register(B1, channel1); -// stubNetwork2.register(B2, channel2); -// stubNetwork3.register(C1, channel1); -// stubNetwork3.register(C2, channel2); -// stubNetwork4.register(D1, channel1); -// stubNetwork4.register(D2, channel2); -// stubNetwork5.register(E1, channel1); -// stubNetwork5.register(E2, channel2); -// stubNetwork6.register(F1, channel1); -// stubNetwork6.register(F2, channel2); -// stubNetwork7.register(G1, channel1); -// stubNetwork7.register(G2, channel2); -// stubNetwork8.register(H1, channel1); -// stubNetwork8.register(H2, channel2); -// stubNetwork9.register(I1, channel1); -// stubNetwork9.register(I2, channel2); -// stubNetwork10.register(J1, channel1); -// stubNetwork10.register(J2, channel2); -// StubNetworkThread t1 = new StubNetworkThread(stubNetwork1, stubNetwork2, e1, channel1); -// StubNetworkThread t2 = new StubNetworkThread(stubNetwork1, stubNetwork3, e1, channel1); -// StubNetworkThread t3 = new StubNetworkThread(stubNetwork1, stubNetwork4, e1, channel1); -// StubNetworkThread t4 = new StubNetworkThread(stubNetwork1, stubNetwork5, e1, channel1); -// StubNetworkThread t5 = new StubNetworkThread(stubNetwork1, stubNetwork6, e1, channel1); -// StubNetworkThread t6 = new StubNetworkThread(stubNetwork1, stubNetwork7, e1, channel1); -// StubNetworkThread t7 = new StubNetworkThread(stubNetwork1, stubNetwork8, e1, channel1); -// StubNetworkThread t8 = new StubNetworkThread(stubNetwork1, stubNetwork9, e1, channel1); -// StubNetworkThread t9 = new StubNetworkThread(stubNetwork1, stubNetwork10, e1, channel1); -// -// t1.start(); -// t2.start(); -// t3.start(); -// t4.start(); -// t5.start(); -// t6.start(); -// t7.start(); -// t8.start(); -// t9.start(); -// System.out.println(e1.id()); -// System.out.println(B1); -// System.out.println(B2); -// System.out.println(C1); -// System.out.println(C2); -// System.out.println(D1); -// System.out.println(D2); -// -// } + + } \ No newline at end of file From 7b098d62ffae22483232634a3efc80a28880d89f Mon Sep 17 00:00:00 2001 From: yhassanzadeh13 Date: Mon, 14 Mar 2022 07:48:57 -0700 Subject: [PATCH 22/51] adds lock --- src/test/java/networking/MockEngine.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 7ffde796..15444aee 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -2,27 +2,37 @@ import java.util.HashSet; import java.util.Set; +import java.util.concurrent.locks.ReentrantReadWriteLock; import model.Entity; import model.lightchain.Identifier; import protocol.Engine; public class MockEngine implements Engine { - private Set receivedEntityIds; + private final ReentrantReadWriteLock lock; + private final Set receivedEntityIds; public MockEngine() { this.receivedEntityIds = new HashSet<>(); + this.lock = new ReentrantReadWriteLock(); } @Override public void process(Entity e) throws IllegalArgumentException { - // TODO: put e.Id() in the set. + lock.writeLock(); + receivedEntityIds.add(e.id()); + + lock.writeLock(); } public boolean hasReceived(Entity e) { + lock.readLock(); + Identifier id = e.id(); boolean ok = this.receivedEntityIds.contains(id); + + lock.readLock(); return ok; } } \ No newline at end of file From 3485624009b667e58a3cb1e0d694e920edb47c51 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Tue, 15 Mar 2022 13:53:43 +0300 Subject: [PATCH 23/51] Some tests Implemented --- src/test/java/networking/Hub.java | 6 +- src/test/java/networking/MockConduit.java | 26 +- src/test/java/networking/MockEngine.java | 47 ++-- src/test/java/networking/StubNetwork.java | 86 +++---- src/test/java/networking/StubNetworkTest.java | 226 +++++++++++------- 5 files changed, 222 insertions(+), 169 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 0fbfa358..8abd20a3 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -19,8 +19,12 @@ public void registerNetwork(Identifier key, Network network) { networks.put(key, network); } + public void transferEntity(Entity entity, Identifier identifier, String channel){ + StubNetwork net =this.getNetwork(identifier); + net.receiveUnicast(entity,channel); - public StubNetwork getNetwork(Identifier key) { + } + private StubNetwork getNetwork(Identifier key) { return (StubNetwork) networks.get(key); } diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 48d00450..0b468b9f 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,18 +5,14 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; -import network.Network; -import protocol.Engine; public class MockConduit implements Conduit { - private String channel; - private Engine engine; - private Hub hub; - - public MockConduit(String channel, Engine engine, Hub hub) { - this.channel = channel; - this.engine = engine; - this.hub = hub; + + private final String channel; + private final Hub hub; + public MockConduit(String channel,Hub hub) { + this.channel=channel; + this.hub=hub; } /** @@ -28,8 +24,9 @@ public MockConduit(String channel, Engine engine, Hub hub) { */ @Override public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { - StubNetwork net = hub.getNetwork(target); - deliverEntity(net,channel , e); + hub.transferEntity(e,target,channel); + + } /** @@ -55,8 +52,5 @@ public void put(Entity e) throws LightChainDistributedStorageException { public Entity get(Identifier identifier) throws LightChainDistributedStorageException { return null; } - public void deliverEntity(StubNetwork network,String channel, Entity en) { - Engine eng =network.getEngine(channel); - eng.process(en); - } + } \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 15444aee..ede91185 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -5,34 +5,43 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import model.Entity; +import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; +import network.Conduit; +import network.Network; import protocol.Engine; public class MockEngine implements Engine { - private final ReentrantReadWriteLock lock; - private final Set receivedEntityIds; + private final ReentrantReadWriteLock lock; + private final Set receivedEntityIds; - public MockEngine() { - this.receivedEntityIds = new HashSet<>(); - this.lock = new ReentrantReadWriteLock(); - } - @Override - public void process(Entity e) throws IllegalArgumentException { - lock.writeLock(); - receivedEntityIds.add(e.id()); + public MockEngine() { + this.receivedEntityIds = new HashSet<>(); + this.lock = new ReentrantReadWriteLock(); + } - lock.writeLock(); - } - public boolean hasReceived(Entity e) { - lock.readLock(); - Identifier id = e.id(); - boolean ok = this.receivedEntityIds.contains(id); - lock.readLock(); - return ok; - } + + @Override + public void process(Entity e) throws IllegalArgumentException { + lock.writeLock(); + + receivedEntityIds.add(e.id()); + + lock.writeLock(); + } + + public boolean hasReceived(Entity e) { + lock.readLock(); + + Identifier id = e.id(); + boolean ok = this.receivedEntityIds.contains(id); + + lock.readLock(); + return ok; + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 2002bfd5..893373ca 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -11,50 +11,50 @@ import java.util.concurrent.ConcurrentHashMap; public class StubNetwork implements Network { - private final ConcurrentHashMap engines; - private final ConcurrentHashMap conduits; - private final Hub hub; - private final Identifier identifier; - - - public StubNetwork(Hub hub) { - this.engines = new ConcurrentHashMap<>(); - this.conduits = new ConcurrentHashMap<>(); - this.hub = hub; - this.identifier = IdentifierFixture.NewIdentifier(); - this.hub.registerNetwork(identifier, this); - } - - public Identifier id() { - return this.identifier; - } - - private void sendUnicast(String ch, StubNetwork stubNetworkR, Entity entity) throws LightChainNetworkingException { - Conduit conduit = conduits.get(ch); - conduit.unicast(entity, stubNetworkR.id()); - } - - @Override - public MockConduit register(Engine en, String channel) throws IllegalStateException { - // TODO: this should be a separate class. - Conduit conduit = new MockConduit(channel,en, hub); - try { - if (engines.containsKey(channel)) { - throw new IllegalStateException(); - } - - engines.put(channel, en); - - conduits.put(channel, conduit); - } catch (Exception ex) { - ex.printStackTrace(); + private final ConcurrentHashMap engines; + + private final Hub hub; + private final Identifier identifier; + + + public StubNetwork(Hub hub) { + this.engines = new ConcurrentHashMap<>(); + + this.hub = hub; + this.identifier = IdentifierFixture.NewIdentifier(); + this.hub.registerNetwork(identifier, this); } - return (MockConduit) conduit; - } - public Engine getEngine(String ch){ - Engine engine = engines.get(ch); - return engine; - } + public Identifier id() { + return this.identifier; + } + + + public void receiveUnicast(Entity entity, String channel){ + Engine engine =getEngine(channel); + engine.process(entity); + } + @Override + public Conduit register(Engine en, String channel) throws IllegalStateException { + // + Conduit conduit = new MockConduit(channel,hub); + try { + if (engines.containsKey(channel)) { + throw new IllegalStateException(); + } + engines.put(channel, en); + + + + } catch (Exception ex) { + ex.printStackTrace(); + } + + return conduit; + } + + private Engine getEngine(String ch) { + return engines.get(ch); + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index d6ec679a..b1c86642 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Hashtable; +import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -18,115 +19,160 @@ import unittest.fixtures.EntityFixture; public class StubNetworkTest { - private ArrayList networkArrayList; - private ConcurrentHashMap> conduitConcurrentHashMap; - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. - @BeforeEach - void setup(){ - this.networkArrayList = new ArrayList(); - this.conduitConcurrentHashMap=new ConcurrentHashMap>(); - ArrayList conduitArrayList = new ArrayList(); - - String channel1 = "test-network-channel-1"; - String channel2 = "test-network-channel-2"; - Hub hub = new Hub(); - for (int i =0; i<10; i++){ - StubNetwork stubNetwork = new StubNetwork(hub); - Engine A1 =new MockEngine(); - Engine A2= new MockEngine(); - Conduit c1 =stubNetwork.register(A1,channel1); - Conduit c2 =stubNetwork.register(A2,channel2); - conduitArrayList.add(c1); - conduitArrayList.add(c2); - conduitConcurrentHashMap.put(stubNetwork,conduitArrayList); - networkArrayList.add(stubNetwork); - } - - } - @Test - void TestTwoStubNetworks_TwoEngines(){ - String channel1 = "test-network-channel-1"; - Hub hub = new Hub(); - - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); - - - Entity entity = new EntityFixture(); - - try { - c1.unicast(entity, network2.id()); - } catch (LightChainNetworkingException e) { - Assertions.fail(); - } - Assertions.assertTrue(A2.hasReceived(entity)); - } + private ConcurrentHashMap> conduitConcurrentHashMap; + + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + @BeforeEach + void setup() { + + this.conduitConcurrentHashMap = new ConcurrentHashMap<>(); + + String channel1 = "test-network-channel-1"; + String channel2 = "test-network-channel-2"; + Hub hub = new Hub(); + for (int i = 0; i < 10; i++) { + ArrayList conduitArrayList = new ArrayList<>(); + StubNetwork stubNetwork = new StubNetwork(hub); + Engine A1 = new MockEngine(); + Engine A2 = new MockEngine(); + Conduit c1 = stubNetwork.register(A1, channel1); + Conduit c2 = stubNetwork.register(A2, channel2); + conduitArrayList.add(c1); + conduitArrayList.add(c2); + conduitConcurrentHashMap.put(stubNetwork, conduitArrayList); - @Test - void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { - int concurrencyDegree = 100; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + } - String channel1 = "test-network-channel-1"; + } - Thread[] unicastThreads = new Thread[concurrencyDegree]; - Hub hub = new Hub(); + @Test + void TestTwoStubNetworks_TwoEngines() { + String channel1 = "test-network-channel-1"; + Hub hub = new Hub(); - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); - for(int i = 0; i < concurrencyDegree; i++){ - unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); try { - c1.unicast(entity, network2.id()); - if(!A2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - - countDownLatch.countDown(); + c1.unicast(entity, network2.id()); } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); + Assertions.fail(); } - }); + + Assertions.assertTrue(A2.hasReceived(entity)); } - for (Thread t: unicastThreads){ - t.start(); + @Test + void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + + String channel1 = "test-network-channel-1"; + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + Hub hub = new Hub(); + + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); + + + + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + + try { + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); } + void TestUnicastOneToAll_Concurrently() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + Iterator>> itr =conduitConcurrentHashMap.entrySet().iterator(); + + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + + + + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + + try { + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } - try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); + Assertions.assertEquals(0, threadError.get()); } - Assertions.assertEquals(0, threadError.get()); - } } \ No newline at end of file From 7f2bc73e2ce029ceff4da8bcbb5143a40dfc290f Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:33:55 +0300 Subject: [PATCH 24/51] TestUnicastOneToAll_Concurrently Implemented --- src/test/java/networking/MockConduit.java | 4 +- src/test/java/networking/MockEngine.java | 9 ++- src/test/java/networking/StubNetwork.java | 2 +- src/test/java/networking/StubNetworkTest.java | 67 ++++++++++++------- .../java/networking/StubNetworkThread.java | 29 -------- 5 files changed, 52 insertions(+), 59 deletions(-) delete mode 100644 src/test/java/networking/StubNetworkThread.java diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 0b468b9f..f0dd2ad0 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,7 +5,9 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; - +/** + * MockConduit represents the Networking interface that is exposed to an Engine. + */ public class MockConduit implements Conduit { private final String channel; diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index ede91185..86bf8995 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -16,15 +16,18 @@ public class MockEngine implements Engine { private final Set receivedEntityIds; - public MockEngine() { this.receivedEntityIds = new HashSet<>(); this.lock = new ReentrantReadWriteLock(); } - - + /** + * Called by Network whenever an Entity is arrived for this engine. + * + * @param e the arrived Entity from the network. + * @throws IllegalArgumentException any unhappy path taken on processing the Entity. + */ @Override public void process(Entity e) throws IllegalArgumentException { diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 893373ca..4b417300 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -53,7 +53,7 @@ public Conduit register(Engine en, String channel) throws IllegalStateException return conduit; } - private Engine getEngine(String ch) { + public Engine getEngine(String ch) { return engines.get(ch); } diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index b1c86642..3bbd68c6 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -20,7 +21,11 @@ public class StubNetworkTest { - private ConcurrentHashMap> conduitConcurrentHashMap; + + private ArrayList networkArrayList; + private final String channel1 = "test-network-channel-1"; + private final String channel2 = "test-network-channel-2"; + private Hub hub; // TODO: add a test for each of the following scenarios: // Use mock engines. @@ -35,21 +40,18 @@ public class StubNetworkTest { @BeforeEach void setup() { - this.conduitConcurrentHashMap = new ConcurrentHashMap<>(); - String channel1 = "test-network-channel-1"; - String channel2 = "test-network-channel-2"; - Hub hub = new Hub(); - for (int i = 0; i < 10; i++) { - ArrayList conduitArrayList = new ArrayList<>(); + this.networkArrayList = new ArrayList<>(); + hub = new Hub(); + for (int i = 0; i < 9; i++) { + StubNetwork stubNetwork = new StubNetwork(hub); - Engine A1 = new MockEngine(); - Engine A2 = new MockEngine(); - Conduit c1 = stubNetwork.register(A1, channel1); - Conduit c2 = stubNetwork.register(A2, channel2); - conduitArrayList.add(c1); - conduitArrayList.add(c2); - conduitConcurrentHashMap.put(stubNetwork, conduitArrayList); + Engine E1 = new MockEngine(); + Engine E2 = new MockEngine(); + stubNetwork.register(E1, channel1); + stubNetwork.register(E2, channel2); + networkArrayList.add(stubNetwork); + } @@ -89,7 +91,7 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { String channel1 = "test-network-channel-1"; Thread[] unicastThreads = new Thread[concurrencyDegree]; - Hub hub = new Hub(); + StubNetwork network1 = new StubNetwork(hub); MockEngine A1 = new MockEngine(); @@ -100,7 +102,6 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { network2.register(A2, channel1); - for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); @@ -131,34 +132,51 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + + @Test void TestUnicastOneToAll_Concurrently() { - int concurrencyDegree = 100; + int concurrencyDegree = 9; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - Iterator>> itr =conduitConcurrentHashMap.entrySet().iterator(); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); Thread[] unicastThreads = new Thread[concurrencyDegree]; + int count = 0; + for (Network network : networkArrayList) { + unicastThreads[count] = new Thread(() -> { + try { - for (int i = 0; i < concurrencyDegree; i++) { - unicastThreads[i] = new Thread(() -> { - Entity entity = new EntityFixture(); - try { - c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + + if (!E1.hasReceived(entity)) { + System.out.println("error1"); + threadError.getAndIncrement(); + } + if (E2.hasReceived(entity)) { + System.out.println("error1"); threadError.getAndIncrement(); } countDownLatch.countDown(); } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } }); + count++; + } + for (Thread t : unicastThreads) { t.start(); } @@ -174,5 +192,4 @@ void TestUnicastOneToAll_Concurrently() { } - } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkThread.java b/src/test/java/networking/StubNetworkThread.java deleted file mode 100644 index 00e20117..00000000 --- a/src/test/java/networking/StubNetworkThread.java +++ /dev/null @@ -1,29 +0,0 @@ -package networking; - -import model.Entity; -import model.exceptions.LightChainNetworkingException; - -public class StubNetworkThread extends Thread{ - StubNetwork stubNetworkT; - StubNetwork stubNetworkR; - Entity entity; - String ch; - - public StubNetworkThread(StubNetwork stubNetworkT, StubNetwork stubNetworkR, Entity entity, String ch) { - this.stubNetworkT = stubNetworkT; - this.stubNetworkR = stubNetworkR; - this.entity = entity; - this.ch = ch; - } - -// @Override -// public void run() { -// try { -// -// // stubNetworkT.sendUnicast(ch,stubNetworkR,entity); -// } catch (LightChainNetworkingException e) { -// e.printStackTrace(); -// } -// -// } -} \ No newline at end of file From c9287a7c34f680bb458595bc31b54a688de9b226 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:45:01 +0300 Subject: [PATCH 25/51] deletion of prints --- src/test/java/networking/StubNetworkTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 3bbd68c6..a5c3ae26 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -158,11 +158,9 @@ void TestUnicastOneToAll_Concurrently() { MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); if (!E1.hasReceived(entity)) { - System.out.println("error1"); threadError.getAndIncrement(); } if (E2.hasReceived(entity)) { - System.out.println("error1"); threadError.getAndIncrement(); } From 47fa11306522b83621b95ab925f5880d2c5a9fd2 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 13:47:57 +0300 Subject: [PATCH 26/51] deletion of prints --- src/main/java/model/lightchain/Identifier.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/model/lightchain/Identifier.java b/src/main/java/model/lightchain/Identifier.java index b104104b..a643b72c 100644 --- a/src/main/java/model/lightchain/Identifier.java +++ b/src/main/java/model/lightchain/Identifier.java @@ -52,8 +52,12 @@ public int comparedTo(Identifier other) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof Identifier)) return false; + if (this == o) { + return true; + } + if (!(o instanceof Identifier)) { + return false; + } Identifier that = (Identifier) o; return Arrays.equals(value, that.value); } From d369b9d1f18c786827149bb24be430777e11ff3e Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 14:18:44 +0300 Subject: [PATCH 27/51] deletion of prints --- src/test/java/networking/StubNetworkTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index a5c3ae26..c7df1817 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,14 +1,9 @@ package networking; import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; - import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; From c4d0a2aeb75090b881c40e6ac3ce99ae187e9d8e Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 14:31:05 +0300 Subject: [PATCH 28/51] rebase --- src/test/java/networking/StubNetwork.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 4b417300..5b817474 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -21,7 +21,7 @@ public StubNetwork(Hub hub) { this.engines = new ConcurrentHashMap<>(); this.hub = hub; - this.identifier = IdentifierFixture.NewIdentifier(); + this.identifier = IdentifierFixture.newIdentifier(); this.hub.registerNetwork(identifier, this); } From 6fdbb248f06e06cb5ffdda7bebc2ef68acd6fc5a Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 21:04:15 +0300 Subject: [PATCH 29/51] TestUnicastOneToAll_Sequentially implementations --- src/test/java/networking/Hub.java | 42 ++++++++++--------- src/test/java/networking/MockConduit.java | 1 - src/test/java/networking/StubNetwork.java | 18 ++++---- src/test/java/networking/StubNetworkTest.java | 31 ++++++++++++++ 4 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 8abd20a3..34caaf05 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -7,25 +7,27 @@ import network.Network; public class Hub { - private final ConcurrentHashMap networks; - private final ConcurrentHashMap entities; - - public Hub() { - this.networks = new ConcurrentHashMap<>(); - this.entities = new ConcurrentHashMap<>(); - } - - public void registerNetwork(Identifier key, Network network) { - networks.put(key, network); - - } - public void transferEntity(Entity entity, Identifier identifier, String channel){ - StubNetwork net =this.getNetwork(identifier); - net.receiveUnicast(entity,channel); - - } - private StubNetwork getNetwork(Identifier key) { - return (StubNetwork) networks.get(key); - } + private final ConcurrentHashMap networks; + private final ConcurrentHashMap entities; + + public Hub() { + this.networks = new ConcurrentHashMap<>(); + this.entities = new ConcurrentHashMap<>(); + } + + public void registerNetwork(Identifier key, Network network) { + networks.put(key, network); + + } + + public void transferEntity(Entity entity, Identifier identifier, String channel) { + StubNetwork net = this.getNetwork(identifier); + net.receiveUnicast(entity, channel); + + } + + private StubNetwork getNetwork(Identifier key) { + return (StubNetwork) networks.get(key); + } } \ No newline at end of file diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index f0dd2ad0..7a28d298 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -28,7 +28,6 @@ public MockConduit(String channel,Hub hub) { public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { hub.transferEntity(e,target,channel); - } /** diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 5b817474..6e1a8d06 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -30,14 +30,19 @@ public Identifier id() { } - public void receiveUnicast(Entity entity, String channel){ - Engine engine =getEngine(channel); - engine.process(entity); + public void receiveUnicast(Entity entity, String channel) { + Engine engine = getEngine(channel); + try { + engine.process(entity); + } catch (IllegalArgumentException e) { + throw new IllegalStateException("could not process the entity" + e); + } } + @Override public Conduit register(Engine en, String channel) throws IllegalStateException { // - Conduit conduit = new MockConduit(channel,hub); + Conduit conduit = new MockConduit(channel, hub); try { if (engines.containsKey(channel)) { throw new IllegalStateException(); @@ -45,9 +50,8 @@ public Conduit register(Engine en, String channel) throws IllegalStateException engines.put(channel, en); - - } catch (Exception ex) { - ex.printStackTrace(); + } catch (IllegalArgumentException ex) { + throw new IllegalStateException("could not register the engine" + ex); } return conduit; diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index c7df1817..5a76fee6 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -4,6 +4,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; + import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; @@ -71,6 +72,7 @@ void TestTwoStubNetworks_TwoEngines() { try { c1.unicast(entity, network2.id()); } catch (LightChainNetworkingException e) { + Assertions.fail(); } @@ -128,6 +130,34 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + @Test + void TestUnicastOneToAll_Sequentially() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + int count = 0; + for (Network network : networkArrayList) { + try { + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + + if (!E1.hasReceived(entity)) { + count++; + } + if (E2.hasReceived(entity)) { + count++; + } + } catch (LightChainNetworkingException e) { + + count++; + } + + } + Assertions.assertEquals(0, count); + } + @Test void TestUnicastOneToAll_Concurrently() { int concurrencyDegree = 9; @@ -185,4 +215,5 @@ void TestUnicastOneToAll_Concurrently() { } + } \ No newline at end of file From be56fa88043fc770d94c3f919ad71a00f2280632 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 23:27:23 +0300 Subject: [PATCH 30/51] TestUnicastOneToSome_Sequentially implementation --- src/test/java/networking/StubNetworkTest.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 5a76fee6..3ada4193 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,6 +1,8 @@ package networking; import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -38,7 +40,7 @@ void setup() { this.networkArrayList = new ArrayList<>(); - hub = new Hub(); + this.hub = new Hub(); for (int i = 0; i < 9; i++) { StubNetwork stubNetwork = new StubNetwork(hub); @@ -213,6 +215,41 @@ void TestUnicastOneToAll_Concurrently() { Assertions.assertEquals(0, threadError.get()); } + @Test + void TestUnicastOneToSome_Sequentially(){ + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + int size= networkArrayList.size(); + int count = 0; + List first=new ArrayList<>(networkArrayList.subList(0,size/2)); + List second=new ArrayList<>(networkArrayList.subList(size/2,size)); + Iterator first_it= first.iterator(); + Iterator second_it= second.iterator(); + while (first_it.hasNext() && second_it.hasNext()) { + Network network_fh= first_it.next(); + Network network_sh= second_it.next(); + try { + c1.unicast(entity, ((StubNetwork) network_fh).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); + MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); + MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + if (!E1.hasReceived(entity)) { + count++; + } + if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { + count++; + } + } catch (LightChainNetworkingException e) { + + count++; + } + + } + + } From 6fdb87cd4f7b48742e92ebdcf8a5d68f54b4d897 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Thu, 17 Mar 2022 23:29:01 +0300 Subject: [PATCH 31/51] TestUnicastOneToSome_Sequentially implementation --- src/test/java/networking/StubNetworkTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 3ada4193..32eeca88 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -248,6 +248,7 @@ void TestUnicastOneToSome_Sequentially(){ } } + Assertions.assertEquals(0, count); } From 0b9d3ffd5bec2df585f3b9d75ed87c88638e0967 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 18 Mar 2022 11:07:06 +0300 Subject: [PATCH 32/51] TestUnicastOneToSome_Concurrently implementation --- src/test/java/networking/StubNetworkTest.java | 86 +++++++++++++++++-- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 32eeca88..61f70e76 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -215,21 +215,22 @@ void TestUnicastOneToAll_Concurrently() { Assertions.assertEquals(0, threadError.get()); } + @Test - void TestUnicastOneToSome_Sequentially(){ + void TestUnicastOneToSome_Sequentially() { StubNetwork network1 = new StubNetwork(hub); MockEngine A1 = new MockEngine(); Conduit c1 = network1.register(A1, channel1); Entity entity = new EntityFixture(); - int size= networkArrayList.size(); + int size = networkArrayList.size(); int count = 0; - List first=new ArrayList<>(networkArrayList.subList(0,size/2)); - List second=new ArrayList<>(networkArrayList.subList(size/2,size)); - Iterator first_it= first.iterator(); - Iterator second_it= second.iterator(); + List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); + List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); + Iterator first_it = first.iterator(); + Iterator second_it = second.iterator(); while (first_it.hasNext() && second_it.hasNext()) { - Network network_fh= first_it.next(); - Network network_sh= second_it.next(); + Network network_fh = first_it.next(); + Network network_sh = second_it.next(); try { c1.unicast(entity, ((StubNetwork) network_fh).id()); MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); @@ -252,6 +253,75 @@ void TestUnicastOneToSome_Sequentially(){ } + @Test + void TestUnicastOneToSome_Concurrently() { + + + int concurrencyDegree = 4; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + int size = networkArrayList.size(); + List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); + List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); + Iterator first_it = first.iterator(); + Iterator second_it = second.iterator(); + + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + int count = 0; + while (first_it.hasNext() && second_it.hasNext()) { + Network network_fh = first_it.next(); + Network network_sh = second_it.next(); + + unicastThreads[count] = new Thread(() -> { + + + try { + + + c1.unicast(entity, ((StubNetwork) network_fh).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); + MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); + MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + + if (!E1.hasReceived(entity)) { + threadError.getAndIncrement(); + } + if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + + threadError.getAndIncrement(); + } + }); + count++; + + } + + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); + + + } } \ No newline at end of file From 8216a90d7aa8e4a59e9247669cf01ff2fe6c7b8b Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 18 Mar 2022 11:19:50 +0300 Subject: [PATCH 33/51] TestUnicastOneToAll_Sequentially_TwoEngines implementation --- src/test/java/networking/StubNetworkTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 61f70e76..e0162260 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -322,6 +322,38 @@ void TestUnicastOneToSome_Concurrently() { } + @Test + void TestUnicastOneToAll_Sequentially_TwoEngines(){ + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + MockEngine A2 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Conduit c2 = network1.register(A2, channel2); + Entity entity1 = new EntityFixture(); + Entity entity2 = new EntityFixture(); + int count = 0; + for (Network network : networkArrayList) { + try { + c1.unicast(entity1, ((StubNetwork) network).id()); + c2.unicast(entity2, ((StubNetwork) network).id()); + + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + + if (!E1.hasReceived(entity1) ||!E2.hasReceived(entity2)) { + count++; + } + if (E2.hasReceived(entity1)|| E1.hasReceived(entity2)) { + count++; + } + } catch (LightChainNetworkingException e) { + + count++; + } + + } + Assertions.assertEquals(0, count); + } } \ No newline at end of file From 552d0a9839a9c39f7dd1edf2c51864e28937ec37 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 18 Mar 2022 12:23:03 +0300 Subject: [PATCH 34/51] TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages implementation --- src/test/java/networking/StubNetworkTest.java | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index e0162260..e9078569 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -87,7 +87,6 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - String channel1 = "test-network-channel-1"; Thread[] unicastThreads = new Thread[concurrencyDegree]; @@ -132,6 +131,63 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + @Test + + void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + + + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + Conduit c2=network2.register(A2, channel1); + + + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + Entity entity2= new EntityFixture(); + try { + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + + c2.unicast(entity2,network1.id()); + if (!A1.hasReceived(entity2)) { + threadError.getAndIncrement(); + + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(360, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); + } + @Test void TestUnicastOneToAll_Sequentially() { StubNetwork network1 = new StubNetwork(hub); From 08222a71c81bb8564a1f5874ec784a705132d23a Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 18 Mar 2022 12:33:37 +0300 Subject: [PATCH 35/51] TestTwoStubNetworks_FourEngines_ConcurrentMessages implementation --- src/test/java/networking/StubNetworkTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index e9078569..b7a354f0 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -188,6 +188,61 @@ void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + @Test + void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + + + Thread[] unicastThreads = new Thread[concurrencyDegree]; + + + StubNetwork network1 = new StubNetwork(hub); + MockEngine A = new MockEngine(); + Conduit c1A = network1.register(A, channel1); + MockEngine B = new MockEngine(); + Conduit c1B = network1.register(B, channel2); + + StubNetwork network2 = new StubNetwork(hub); + MockEngine C = new MockEngine(); + MockEngine D = new MockEngine(); + network2.register(C,channel1); + network2.register(D,channel2); + + + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity1 = new EntityFixture(); + Entity entity2 = new EntityFixture(); + try { + c1A.unicast(entity1, network2.id()); + c1B.unicast(entity2, network2.id()); + if (!C.hasReceived(entity1)|| C.hasReceived(entity2) || !D.hasReceived(entity2) || D.hasReceived(entity1)) { + threadError.getAndIncrement(); + } + + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + + try { + boolean doneOneTime = countDownLatch.await(240, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); + } + @Test void TestUnicastOneToAll_Sequentially() { StubNetwork network1 = new StubNetwork(hub); From bd63e34147499fa06a1e67927bb60ceb06795f61 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 18 Mar 2022 13:17:03 +0300 Subject: [PATCH 36/51] TestRegisterToOccupiedChannel implementation --- src/test/java/networking/StubNetworkTest.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index b7a354f0..6bda68be 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -179,7 +179,7 @@ void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { } try { - boolean doneOneTime = countDownLatch.await(360, TimeUnit.SECONDS); + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); Assertions.assertTrue(doneOneTime); } catch (InterruptedException e) { Assertions.fail(); @@ -234,7 +234,7 @@ void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { } try { - boolean doneOneTime = countDownLatch.await(240, TimeUnit.SECONDS); + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); Assertions.assertTrue(doneOneTime); } catch (InterruptedException e) { Assertions.fail(); @@ -242,7 +242,20 @@ void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { Assertions.assertEquals(0, threadError.get()); } + @Test + void TestRegisterToOccupiedChannel(){ + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + network1.register(A1, channel1); + MockEngine B1 = new MockEngine(); + try { + network1.register(B1, channel1); + Assertions.fail("fail! method was expected to throw an exception"); + } catch (IllegalStateException e) { + //throw new IllegalStateException("could not register to channel since its already occupied"); + } + } @Test void TestUnicastOneToAll_Sequentially() { StubNetwork network1 = new StubNetwork(hub); From fd616fb1d65eabb357f1a67de1998be0d102ce96 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 18 Mar 2022 13:29:13 +0300 Subject: [PATCH 37/51] Javadoc modification --- src/test/java/networking/StubNetwork.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 6e1a8d06..66638537 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -9,7 +9,9 @@ import unittest.fixtures.IdentifierFixture; import java.util.concurrent.ConcurrentHashMap; - +/** + * Network represents the networking layer of the LightChain node. + */ public class StubNetwork implements Network { private final ConcurrentHashMap engines; @@ -38,7 +40,14 @@ public void receiveUnicast(Entity entity, String channel) { throw new IllegalStateException("could not process the entity" + e); } } - + /** + * Registers an Engine to the Network by providing it with a Conduit. + * + * @param en the Engine to be registered. + * @param channel the unique channel corresponding to the Engine. + * @return unique Conduit object created to connect the Network to the Engine. + * @throws IllegalStateException if the channel is already taken by another Engine. + */ @Override public Conduit register(Engine en, String channel) throws IllegalStateException { // From 90f7985dc1832d2a7134bb3d6dc2eb9ac6fab221 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Fri, 25 Mar 2022 17:56:00 +0300 Subject: [PATCH 38/51] Import reorganize --- src/test/java/networking/StubNetwork.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index c838e1f9..9b1bba8d 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -8,6 +8,8 @@ import protocol.Engine; import unittest.fixtures.IdentifierFixture; +import java.util.concurrent.ConcurrentHashMap; + /** * A mock implementation of networking layer as a test util. */ From 244b55b0937d30e29b9d38851640e7257088987a Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Sat, 26 Mar 2022 23:20:36 +0300 Subject: [PATCH 39/51] Cl changes --- src/test/java/networking/Hub.java | 59 +- src/test/java/networking/MockConduit.java | 88 +- src/test/java/networking/MockEngine.java | 50 +- src/test/java/networking/StubNetwork.java | 101 ++- src/test/java/networking/StubNetworkTest.java | 808 ++++++++---------- 5 files changed, 539 insertions(+), 567 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 710eaa95..1bdd58fb 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -10,27 +10,44 @@ * Models the core communication part of the networking layer that allows stub network instances to talk to each other. */ public class Hub { - private final ConcurrentHashMap networks; - private final ConcurrentHashMap entities; - public Hub() { - this.networks = new ConcurrentHashMap<>(); - this.entities = new ConcurrentHashMap<>(); - } - - public void registerNetwork(Identifier key, Network network) { - networks.put(key, network); - - } - - public void transferEntity(Entity entity, Identifier identifier, String channel) { - StubNetwork net = this.getNetwork(identifier); - net.receiveUnicast(entity, channel); - - } - - private StubNetwork getNetwork(Identifier key) { - return (StubNetwork) networks.get(key); - } + private final ConcurrentHashMap networks; + private final ConcurrentHashMap entities; + + /** + * Create a hub + */ + public Hub() { + this.networks = new ConcurrentHashMap<>(); + this.entities = new ConcurrentHashMap<>(); + } + + /** + * @param key identifier of network. + * @param network to be registered. + */ + public void registerNetwork(Identifier key, Network network) { + networks.put(key, network); + + } + + /** + * @param entity entity to be transferred. + * @param identifier identifier of target. + * @param channel channel of the transmitter-target engine. + */ + public void transferEntity(Entity entity, Identifier identifier, String channel) { + StubNetwork net = this.getNetwork(identifier); + net.receiveUnicast(entity, channel); + + } + + /** + * @param key identity of. + * @return network. + */ + private StubNetwork getNetwork(Identifier key) { + return (StubNetwork) networks.get(key); + } } \ No newline at end of file diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 7a28d298..57ceaa5c 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,53 +5,55 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; + /** * MockConduit represents the Networking interface that is exposed to an Engine. */ public class MockConduit implements Conduit { - private final String channel; - private final Hub hub; - public MockConduit(String channel,Hub hub) { - this.channel=channel; - this.hub=hub; - } - - /** - * Sends the Entity through the Network to the remote target. - * - * @param e the Entity to be sent over the network. - * @param target Identifier of the receiver. - * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. - */ - @Override - public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { - hub.transferEntity(e,target,channel); - - } - - /** - * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. - * - * @param e the Entity to be stored over the network. - * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. - */ - @Override - public void put(Entity e) throws LightChainDistributedStorageException { - - } - - /** - * Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table - * (DHT) of nodes. - * - * @param identifier identifier of the entity to be retrieved. - * @return the retrieved entity or null if it does not exist. - * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. - */ - @Override - public Entity get(Identifier identifier) throws LightChainDistributedStorageException { - return null; - } + private final String channel; + private final Hub hub; + + public MockConduit(String channel, Hub hub) { + this.channel = channel; + this.hub = hub; + } + + /** + * Sends the Entity through the Network to the remote target. + * + * @param e the Entity to be sent over the network. + * @param target Identifier of the receiver. + * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. + */ + @Override + public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { + hub.transferEntity(e, target, channel); + + } + + /** + * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. + * + * @param e the Entity to be stored over the network. + * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. + */ + @Override + public void put(Entity e) throws LightChainDistributedStorageException { + + } + + /** + * Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table + * (DHT) of nodes. + * + * @param identifier identifier of the entity to be retrieved. + * @return the retrieved entity or null if it does not exist. + * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. + */ + @Override + public Entity get(Identifier identifier) throws LightChainDistributedStorageException { + return null; + } } \ No newline at end of file diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 361e7428..6bcb1b79 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -15,39 +15,39 @@ * Represents a mock implementation of Engine interface for testing. */ public class MockEngine implements Engine { - private final ReentrantReadWriteLock lock; - private final Set receivedEntityIds; + private final ReentrantReadWriteLock lock; + private final Set receivedEntityIds; - public MockEngine() { - this.receivedEntityIds = new HashSet<>(); - this.lock = new ReentrantReadWriteLock(); - } + public MockEngine() { + this.receivedEntityIds = new HashSet<>(); + this.lock = new ReentrantReadWriteLock(); + } - /** - * Called by Network whenever an Entity is arrived for this engine. - * - * @param e the arrived Entity from the network. - * @throws IllegalArgumentException any unhappy path taken on processing the Entity. - */ + /** + * Called by Network whenever an Entity is arrived for this engine. + * + * @param e the arrived Entity from the network. + * @throws IllegalArgumentException any unhappy path taken on processing the Entity. + */ - @Override - public void process(Entity e) throws IllegalArgumentException { - lock.writeLock(); + @Override + public void process(Entity e) throws IllegalArgumentException { + lock.writeLock(); - receivedEntityIds.add(e.id()); + receivedEntityIds.add(e.id()); - lock.writeLock(); - } + lock.writeLock(); + } - public boolean hasReceived(Entity e) { - lock.readLock(); + public boolean hasReceived(Entity e) { + lock.readLock(); - Identifier id = e.id(); - boolean ok = this.receivedEntityIds.contains(id); + Identifier id = e.id(); + boolean ok = this.receivedEntityIds.contains(id); - lock.readLock(); - return ok; - } + lock.readLock(); + return ok; + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 9b1bba8d..85923e09 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -14,61 +14,72 @@ * A mock implementation of networking layer as a test util. */ public class StubNetwork implements Network { - private final ConcurrentHashMap engines; + private final ConcurrentHashMap engines; + private final Hub hub; + private final Identifier identifier; - private final Hub hub; - private final Identifier identifier; + /** + * + * @param hub the hub which stubnetwork registered is. + */ + public StubNetwork(Hub hub) { + this.engines = new ConcurrentHashMap<>(); + this.hub = hub; + this.identifier = IdentifierFixture.newIdentifier(); + this.hub.registerNetwork(identifier, this); + } - public StubNetwork(Hub hub) { - this.engines = new ConcurrentHashMap<>(); + /** + * + * @return identifier + */ + public Identifier id() { + return this.identifier; + } - this.hub = hub; - this.identifier = IdentifierFixture.newIdentifier(); - this.hub.registerNetwork(identifier, this); + /** + * + * @param entity received entity + * @param channel the channel through which the received entity is sent + */ + public void receiveUnicast(Entity entity, String channel) { + Engine engine = getEngine(channel); + try { + engine.process(entity); + } catch (IllegalArgumentException e) { + throw new IllegalStateException("could not process the entity" + e); } + } - public Identifier id() { - return this.identifier; - } + /** + * Registers an Engine to the Network by providing it with a Conduit. + * + * @param en the Engine to be registered. + * @param channel the unique channel corresponding to the Engine. + * @return unique Conduit object created to connect the Network to the Engine. + * @throws IllegalStateException if the channel is already taken by another Engine. + */ + @Override + public Conduit register(Engine en, String channel) throws IllegalStateException { + // + Conduit conduit = new MockConduit(channel, hub); + try { + if (engines.containsKey(channel)) { + throw new IllegalStateException(); + } + engines.put(channel, en); - public void receiveUnicast(Entity entity, String channel) { - Engine engine = getEngine(channel); - try { - engine.process(entity); - } catch (IllegalArgumentException e) { - throw new IllegalStateException("could not process the entity" + e); - } + } catch (IllegalArgumentException ex) { + throw new IllegalStateException("could not register the engine" + ex); } - /** - * Registers an Engine to the Network by providing it with a Conduit. - * - * @param en the Engine to be registered. - * @param channel the unique channel corresponding to the Engine. - * @return unique Conduit object created to connect the Network to the Engine. - * @throws IllegalStateException if the channel is already taken by another Engine. - */ - @Override - public Conduit register(Engine en, String channel) throws IllegalStateException { - // - Conduit conduit = new MockConduit(channel, hub); - try { - if (engines.containsKey(channel)) { - throw new IllegalStateException(); - } - engines.put(channel, en); - - } catch (IllegalArgumentException ex) { - throw new IllegalStateException("could not register the engine" + ex); - } + return conduit; + } - return conduit; - } - - public Engine getEngine(String ch) { - return engines.get(ch); - } + public Engine getEngine(String ch) { + return engines.get(ch); + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 6bda68be..6b442f8c 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -17,467 +17,409 @@ import protocol.Engine; import unittest.fixtures.EntityFixture; +/** + * Encapculates tests for Stubnetwork + */ public class StubNetworkTest { - - private ArrayList networkArrayList; - private final String channel1 = "test-network-channel-1"; - private final String channel2 = "test-network-channel-2"; - private Hub hub; - - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. - @BeforeEach - void setup() { - - - this.networkArrayList = new ArrayList<>(); - this.hub = new Hub(); - for (int i = 0; i < 9; i++) { - - StubNetwork stubNetwork = new StubNetwork(hub); - Engine E1 = new MockEngine(); - Engine E2 = new MockEngine(); - stubNetwork.register(E1, channel1); - stubNetwork.register(E2, channel2); - networkArrayList.add(stubNetwork); - - - } - + private ArrayList networkArrayList; + private final String channel1 = "test-network-channel-1"; + private final String channel2 = "test-network-channel-2"; + private Hub hub; + // TODO: add a test for each of the following scenarios: + // Use mock engines. + // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. + // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. + // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies + // are received by Engine A. + // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only + // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B + // and B must be on another same channel. + // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. + + /** + * implement before each test + */ + @BeforeEach + void setup() { + this.networkArrayList = new ArrayList<>(); + this.hub = new Hub(); + for (int i = 0; i < 9; i++) { + StubNetwork stubNetwork = new StubNetwork(hub); + Engine E1 = new MockEngine(); + Engine E2 = new MockEngine(); + stubNetwork.register(E1, channel1); + stubNetwork.register(E2, channel2); + networkArrayList.add(stubNetwork); } - - @Test - void TestTwoStubNetworks_TwoEngines() { - String channel1 = "test-network-channel-1"; - Hub hub = new Hub(); - - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); - - + } + + /** + * Test two stub networks with two engines + */ + @Test + void TestTwoStubNetworks_TwoEngines() { + String channel1 = "test-network-channel-1"; + Hub hub = new Hub(); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); + Entity entity = new EntityFixture(); + try { + c1.unicast(entity, network2.id()); + } catch (LightChainNetworkingException e) { + Assertions.fail(); + } + Assertions.assertTrue(A2.hasReceived(entity)); + } + + /** + * test two stub networks with two engines concurrently + */ + @Test + void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + network2.register(A2, channel1); + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); - try { - c1.unicast(entity, network2.id()); + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + countDownLatch.countDown(); } catch (LightChainNetworkingException e) { - - Assertions.fail(); + threadError.getAndIncrement(); } - - Assertions.assertTrue(A2.hasReceived(entity)); + }); } - - @Test - void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { - int concurrencyDegree = 100; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - - - Thread[] unicastThreads = new Thread[concurrencyDegree]; - - - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); - - - for (int i = 0; i < concurrencyDegree; i++) { - unicastThreads[i] = new Thread(() -> { - Entity entity = new EntityFixture(); - - try { - c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); - } - }); - } - - for (Thread t : unicastThreads) { - t.start(); - } - - try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); - } - - Assertions.assertEquals(0, threadError.get()); + for (Thread t : unicastThreads) { + t.start(); } - - @Test - - void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { - int concurrencyDegree = 100; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - - - Thread[] unicastThreads = new Thread[concurrencyDegree]; - - - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - - StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - Conduit c2=network2.register(A2, channel1); - - - for (int i = 0; i < concurrencyDegree; i++) { - unicastThreads[i] = new Thread(() -> { - Entity entity = new EntityFixture(); - Entity entity2= new EntityFixture(); - try { - c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - - c2.unicast(entity2,network1.id()); - if (!A1.hasReceived(entity2)) { - threadError.getAndIncrement(); - - } - - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); - } - }); - } - - for (Thread t : unicastThreads) { - t.start(); - } - - try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); - } - - Assertions.assertEquals(0, threadError.get()); + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); } - - @Test - void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { - int concurrencyDegree = 100; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - - - Thread[] unicastThreads = new Thread[concurrencyDegree]; - - - StubNetwork network1 = new StubNetwork(hub); - MockEngine A = new MockEngine(); - Conduit c1A = network1.register(A, channel1); - MockEngine B = new MockEngine(); - Conduit c1B = network1.register(B, channel2); - - StubNetwork network2 = new StubNetwork(hub); - MockEngine C = new MockEngine(); - MockEngine D = new MockEngine(); - network2.register(C,channel1); - network2.register(D,channel2); - - - for (int i = 0; i < concurrencyDegree; i++) { - unicastThreads[i] = new Thread(() -> { - Entity entity1 = new EntityFixture(); - Entity entity2 = new EntityFixture(); - try { - c1A.unicast(entity1, network2.id()); - c1B.unicast(entity2, network2.id()); - if (!C.hasReceived(entity1)|| C.hasReceived(entity2) || !D.hasReceived(entity2) || D.hasReceived(entity1)) { - threadError.getAndIncrement(); - } - - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); - } - }); - } - - for (Thread t : unicastThreads) { - t.start(); - } - + Assertions.assertEquals(0, threadError.get()); + } + + /** + * Test for two stub networks with reply + */ + @Test + void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + StubNetwork network2 = new StubNetwork(hub); + MockEngine A2 = new MockEngine(); + Conduit c2 = network2.register(A2, channel1); + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity = new EntityFixture(); + Entity entity2 = new EntityFixture(); try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); + c1.unicast(entity, network2.id()); + if (!A2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + c2.unicast(entity2, network1.id()); + if (!A1.hasReceived(entity2)) { + threadError.getAndIncrement(); + } + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } - - Assertions.assertEquals(0, threadError.get()); + }); + } + for (Thread t : unicastThreads) { + t.start(); } - @Test - void TestRegisterToOccupiedChannel(){ - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - network1.register(A1, channel1); - MockEngine B1 = new MockEngine(); + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + Assertions.assertEquals(0, threadError.get()); + } + + /** + * Test two stub netwokrs for engines, concurrently messages. + */ + @Test + void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { + int concurrencyDegree = 100; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + StubNetwork network1 = new StubNetwork(hub); + MockEngine A = new MockEngine(); + Conduit c1A = network1.register(A, channel1); + MockEngine B = new MockEngine(); + Conduit c1B = network1.register(B, channel2); + StubNetwork network2 = new StubNetwork(hub); + MockEngine C = new MockEngine(); + MockEngine D = new MockEngine(); + network2.register(C, channel1); + network2.register(D, channel2); + for (int i = 0; i < concurrencyDegree; i++) { + unicastThreads[i] = new Thread(() -> { + Entity entity1 = new EntityFixture(); + Entity entity2 = new EntityFixture(); try { - network1.register(B1, channel1); - Assertions.fail("fail! method was expected to throw an exception"); - } catch (IllegalStateException e) { - - //throw new IllegalStateException("could not register to channel since its already occupied"); + c1A.unicast(entity1, network2.id()); + c1B.unicast(entity2, network2.id()); + if (!C.hasReceived(entity1) || C.hasReceived(entity2) || !D.hasReceived(entity2) || D.hasReceived(entity1)) { + threadError.getAndIncrement(); + } + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } + }); } - @Test - void TestUnicastOneToAll_Sequentially() { - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - Entity entity = new EntityFixture(); - int count = 0; - for (Network network : networkArrayList) { - try { - c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - - if (!E1.hasReceived(entity)) { - count++; - } - if (E2.hasReceived(entity)) { - count++; - } - } catch (LightChainNetworkingException e) { - - count++; - } - - } - Assertions.assertEquals(0, count); + for (Thread t : unicastThreads) { + t.start(); } - - @Test - void TestUnicastOneToAll_Concurrently() { - int concurrencyDegree = 9; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - Entity entity = new EntityFixture(); - - - Thread[] unicastThreads = new Thread[concurrencyDegree]; - int count = 0; - for (Network network : networkArrayList) { - unicastThreads[count] = new Thread(() -> { - - - try { - - - c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - - if (!E1.hasReceived(entity)) { - threadError.getAndIncrement(); - } - if (E2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - - threadError.getAndIncrement(); - } - }); - count++; - + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + Assertions.assertEquals(0, threadError.get()); + } + + /** + * Test for Registeration to Occupied Channel + */ + @Test + void TestRegisterToOccupiedChannel() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + network1.register(A1, channel1); + MockEngine B1 = new MockEngine(); + try { + network1.register(B1, channel1); + Assertions.fail("fail! method was expected to throw an exception"); + } catch (IllegalStateException e) { + //throw new IllegalStateException("could not register to channel since its already occupied"); + } + } + + /** + * Test for Unicast one engine to all other stub networks. + */ + @Test + void TestUnicastOneToAll_Sequentially() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + int count = 0; + for (Network network : networkArrayList) { + try { + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + if (!E1.hasReceived(entity)) { + count++; } - - - for (Thread t : unicastThreads) { - t.start(); + if (E2.hasReceived(entity)) { + count++; } - + } catch (LightChainNetworkingException e) { + count++; + } + } + Assertions.assertEquals(0, count); + } + + /** + * Test one engine unicasts to all others concurrently. + */ + @Test + void TestUnicastOneToAll_Concurrently() { + int concurrencyDegree = 9; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + int count = 0; + for (Network network : networkArrayList) { + unicastThreads[count] = new Thread(() -> { try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + if (!E1.hasReceived(entity)) { + threadError.getAndIncrement(); + } + if (E2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } - - Assertions.assertEquals(0, threadError.get()); + }); + count++; } - - @Test - void TestUnicastOneToSome_Sequentially() { - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - Entity entity = new EntityFixture(); - int size = networkArrayList.size(); - int count = 0; - List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); - List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); - Iterator first_it = first.iterator(); - Iterator second_it = second.iterator(); - while (first_it.hasNext() && second_it.hasNext()) { - Network network_fh = first_it.next(); - Network network_sh = second_it.next(); - try { - c1.unicast(entity, ((StubNetwork) network_fh).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); - MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); - MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); - if (!E1.hasReceived(entity)) { - count++; - } - if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { - count++; - } - } catch (LightChainNetworkingException e) { - - count++; - } - - } - Assertions.assertEquals(0, count); - + for (Thread t : unicastThreads) { + t.start(); } - - @Test - void TestUnicastOneToSome_Concurrently() { - - - int concurrencyDegree = 4; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - Entity entity = new EntityFixture(); - int size = networkArrayList.size(); - List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); - List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); - Iterator first_it = first.iterator(); - Iterator second_it = second.iterator(); - - - Thread[] unicastThreads = new Thread[concurrencyDegree]; - int count = 0; - while (first_it.hasNext() && second_it.hasNext()) { - Network network_fh = first_it.next(); - Network network_sh = second_it.next(); - - unicastThreads[count] = new Thread(() -> { - - - try { - - - c1.unicast(entity, ((StubNetwork) network_fh).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); - MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); - MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); - - if (!E1.hasReceived(entity)) { - threadError.getAndIncrement(); - } - if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - - threadError.getAndIncrement(); - } - }); - count++; - + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + Assertions.assertEquals(0, threadError.get()); + } + + /** + * Test one engine sends unicast to some sequentially + */ + @Test + void TestUnicastOneToSome_Sequentially() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + int size = networkArrayList.size(); + int count = 0; + List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); + List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); + Iterator first_it = first.iterator(); + Iterator second_it = second.iterator(); + while (first_it.hasNext() && second_it.hasNext()) { + Network network_fh = first_it.next(); + Network network_sh = second_it.next(); + try { + c1.unicast(entity, ((StubNetwork) network_fh).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); + MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); + MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + if (!E1.hasReceived(entity)) { + count++; } - - - for (Thread t : unicastThreads) { - t.start(); + if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { + count++; } - + } catch (LightChainNetworkingException e) { + count++; + } + } + Assertions.assertEquals(0, count); + } + + /** + * Test one engine send unicast to some concurrently + */ + @Test + void TestUnicastOneToSome_Concurrently() { + int concurrencyDegree = 4; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Entity entity = new EntityFixture(); + int size = networkArrayList.size(); + List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); + List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); + Iterator first_it = first.iterator(); + Iterator second_it = second.iterator(); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + int count = 0; + while (first_it.hasNext() && second_it.hasNext()) { + Network network_fh = first_it.next(); + Network network_sh = second_it.next(); + unicastThreads[count] = new Thread(() -> { try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); + c1.unicast(entity, ((StubNetwork) network_fh).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); + MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); + MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + if (!E1.hasReceived(entity)) { + threadError.getAndIncrement(); + } + if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + countDownLatch.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); } - - Assertions.assertEquals(0, threadError.get()); - - + }); + count++; } - @Test - void TestUnicastOneToAll_Sequentially_TwoEngines(){ - StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - MockEngine A2 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - Conduit c2 = network1.register(A2, channel2); - Entity entity1 = new EntityFixture(); - Entity entity2 = new EntityFixture(); - int count = 0; - for (Network network : networkArrayList) { - try { - c1.unicast(entity1, ((StubNetwork) network).id()); - c2.unicast(entity2, ((StubNetwork) network).id()); - - MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - - if (!E1.hasReceived(entity1) ||!E2.hasReceived(entity2)) { - count++; - } - if (E2.hasReceived(entity1)|| E1.hasReceived(entity2)) { - count++; - } - } catch (LightChainNetworkingException e) { - - count++; - } - + for (Thread t : unicastThreads) { + t.start(); + } + try { + boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + Assertions.assertEquals(0, threadError.get()); + } + + /** + * test two engines sends other engines sequentially + */ + @Test + void TestUnicastOneToAll_Sequentially_TwoEngines() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine A1 = new MockEngine(); + MockEngine A2 = new MockEngine(); + Conduit c1 = network1.register(A1, channel1); + Conduit c2 = network1.register(A2, channel2); + Entity entity1 = new EntityFixture(); + Entity entity2 = new EntityFixture(); + int count = 0; + for (Network network : networkArrayList) { + try { + c1.unicast(entity1, ((StubNetwork) network).id()); + c2.unicast(entity2, ((StubNetwork) network).id()); + MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + if (!E1.hasReceived(entity1) || !E2.hasReceived(entity2)) { + count++; + } + if (E2.hasReceived(entity1) || E1.hasReceived(entity2)) { + count++; } - Assertions.assertEquals(0, count); + } catch (LightChainNetworkingException e) { + count++; + } } - - + Assertions.assertEquals(0, count); + } } \ No newline at end of file From 8bd906910797c469522675f5c7596ee173e28404 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Sat, 26 Mar 2022 23:58:55 +0300 Subject: [PATCH 40/51] Cl changes --- src/test/java/networking/Hub.java | 8 +- src/test/java/networking/MockEngine.java | 15 +- src/test/java/networking/StubNetwork.java | 23 +-- src/test/java/networking/StubNetworkTest.java | 185 ++++++++---------- 4 files changed, 112 insertions(+), 119 deletions(-) diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 1bdd58fb..0f577c59 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -15,7 +15,7 @@ public class Hub { private final ConcurrentHashMap entities; /** - * Create a hub + * Create a hub. */ public Hub() { this.networks = new ConcurrentHashMap<>(); @@ -23,6 +23,8 @@ public Hub() { } /** + * Registeration of a network to the Hub. + * * @param key identifier of network. * @param network to be registered. */ @@ -32,6 +34,8 @@ public void registerNetwork(Identifier key, Network network) { } /** + * Transfer entity from a stubnetwork to another through hub. + * * @param entity entity to be transferred. * @param identifier identifier of target. * @param channel channel of the transmitter-target engine. @@ -43,6 +47,8 @@ public void transferEntity(Entity entity, Identifier identifier, String channel) } /** + * Get the network with identifier. + * * @param key identity of. * @return network. */ diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 6bcb1b79..182f2429 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -5,10 +5,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import model.Entity; -import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; -import network.Conduit; -import network.Network; import protocol.Engine; /** @@ -18,13 +15,11 @@ public class MockEngine implements Engine { private final ReentrantReadWriteLock lock; private final Set receivedEntityIds; - public MockEngine() { this.receivedEntityIds = new HashSet<>(); this.lock = new ReentrantReadWriteLock(); } - /** * Called by Network whenever an Entity is arrived for this engine. * @@ -35,18 +30,20 @@ public MockEngine() { @Override public void process(Entity e) throws IllegalArgumentException { lock.writeLock(); - receivedEntityIds.add(e.id()); - lock.writeLock(); } + /** + * Check whether an entity is received. + * + * @param e the entitiy. + * @return true if the entity received, otherwise false. + */ public boolean hasReceived(Entity e) { lock.readLock(); - Identifier id = e.id(); boolean ok = this.receivedEntityIds.contains(id); - lock.readLock(); return ok; } diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 85923e09..9a535962 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -1,17 +1,18 @@ package networking; +import java.util.concurrent.ConcurrentHashMap; + import model.Entity; -import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; import network.Network; import protocol.Engine; import unittest.fixtures.IdentifierFixture; -import java.util.concurrent.ConcurrentHashMap; - /** + * * A mock implementation of networking layer as a test util. + * */ public class StubNetwork implements Network { private final ConcurrentHashMap engines; @@ -19,20 +20,22 @@ public class StubNetwork implements Network { private final Identifier identifier; /** + * + * Create stubNetwork. * * @param hub the hub which stubnetwork registered is. */ public StubNetwork(Hub hub) { this.engines = new ConcurrentHashMap<>(); - this.hub = hub; this.identifier = IdentifierFixture.newIdentifier(); this.hub.registerNetwork(identifier, this); } /** + * Get the identifier of the stubnet. * - * @return identifier + * @return identifier. */ public Identifier id() { return this.identifier; @@ -40,7 +43,9 @@ public Identifier id() { /** * - * @param entity received entity + * Forward the incoming entity to the engine whose channel is given. + * + * @param entity received entity * @param channel the channel through which the received entity is sent */ public void receiveUnicast(Entity entity, String channel) { @@ -53,6 +58,7 @@ public void receiveUnicast(Entity entity, String channel) { } /** + * * Registers an Engine to the Network by providing it with a Conduit. * * @param en the Engine to be registered. @@ -62,24 +68,19 @@ public void receiveUnicast(Entity entity, String channel) { */ @Override public Conduit register(Engine en, String channel) throws IllegalStateException { - // Conduit conduit = new MockConduit(channel, hub); try { if (engines.containsKey(channel)) { throw new IllegalStateException(); } engines.put(channel, en); - - } catch (IllegalArgumentException ex) { throw new IllegalStateException("could not register the engine" + ex); } - return conduit; } public Engine getEngine(String ch) { return engines.get(ch); } - } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 6b442f8c..633fed2e 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -26,19 +26,8 @@ public class StubNetworkTest { private final String channel1 = "test-network-channel-1"; private final String channel2 = "test-network-channel-2"; private Hub hub; - // TODO: add a test for each of the following scenarios: - // Use mock engines. - // 1. Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, and the message is received by Engine B. - // 2. Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, and ALL messages received by Engine B. - // 3. Extend case 2 with Engine B also sending a reply message to Engine A for each received messages and all replies - // are received by Engine A. - // 4. Engines A and B on one StubNetwork can CONCURRENTLY send 100 messages to Engines C and D on another StubNetwork (A -> C) and (B -> D), and each Engine only - // receives messages destinated for it (C receives all messages from A) and (D receives all messages from B). Note that A and C must be on the same channel, and B - // and B must be on another same channel. - // 5. Stub network throws an exception if an engine is registering itself on an already taken channel. - /** - * implement before each test + * Implement before each test. */ @BeforeEach void setup() { @@ -46,57 +35,57 @@ void setup() { this.hub = new Hub(); for (int i = 0; i < 9; i++) { StubNetwork stubNetwork = new StubNetwork(hub); - Engine E1 = new MockEngine(); - Engine E2 = new MockEngine(); - stubNetwork.register(E1, channel1); - stubNetwork.register(E2, channel2); + Engine e1 = new MockEngine(); + Engine e2 = new MockEngine(); + stubNetwork.register(e1, channel1); + stubNetwork.register(e2, channel2); networkArrayList.add(stubNetwork); } } /** - * Test two stub networks with two engines + * Test two stub networks with two engines. */ @Test - void TestTwoStubNetworks_TwoEngines() { + void testTwoStubNetworksTwoEngines() { String channel1 = "test-network-channel-1"; Hub hub = new Hub(); StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); + MockEngine a2 = new MockEngine(); + network2.register(a2, channel1); Entity entity = new EntityFixture(); try { c1.unicast(entity, network2.id()); } catch (LightChainNetworkingException e) { Assertions.fail(); } - Assertions.assertTrue(A2.hasReceived(entity)); + Assertions.assertTrue(a2.hasReceived(entity)); } /** - * test two stub networks with two engines concurrently + * test two stub networks with two engines concurrently. */ @Test - void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { + void testTwoStubNetworksTwoEnginesConcurrentMessages() { int concurrencyDegree = 100; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); Thread[] unicastThreads = new Thread[concurrencyDegree]; StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - network2.register(A2, channel1); + MockEngine a2 = new MockEngine(); + network2.register(a2, channel1); for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); try { c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { + if (!a2.hasReceived(entity)) { threadError.getAndIncrement(); } countDownLatch.countDown(); @@ -118,31 +107,31 @@ void TestTwoStubNetworks_TwoEngines_ConcurrentMessages() { } /** - * Test for two stub networks with reply + * Test for two stub networks with reply. */ @Test - void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { + void testTwoStubNetworksTwoEnginesReplyConcurrentMessages() { int concurrencyDegree = 100; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); Thread[] unicastThreads = new Thread[concurrencyDegree]; StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); StubNetwork network2 = new StubNetwork(hub); - MockEngine A2 = new MockEngine(); - Conduit c2 = network2.register(A2, channel1); + MockEngine a2 = new MockEngine(); + Conduit c2 = network2.register(a2, channel1); for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); Entity entity2 = new EntityFixture(); try { c1.unicast(entity, network2.id()); - if (!A2.hasReceived(entity)) { + if (!a2.hasReceived(entity)) { threadError.getAndIncrement(); } c2.unicast(entity2, network1.id()); - if (!A1.hasReceived(entity2)) { + if (!a1.hasReceived(entity2)) { threadError.getAndIncrement(); } countDownLatch.countDown(); @@ -167,29 +156,29 @@ void TestTwoStubNetworks_TwoEngines_Reply_ConcurrentMessages() { * Test two stub netwokrs for engines, concurrently messages. */ @Test - void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { + void testTwoStubNetworksFourEnginesConcurrentMessages() { int concurrencyDegree = 100; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); Thread[] unicastThreads = new Thread[concurrencyDegree]; StubNetwork network1 = new StubNetwork(hub); - MockEngine A = new MockEngine(); - Conduit c1A = network1.register(A, channel1); - MockEngine B = new MockEngine(); - Conduit c1B = network1.register(B, channel2); + MockEngine a = new MockEngine(); + Conduit c1 = network1.register(a, channel1); + MockEngine b = new MockEngine(); + Conduit c2 = network1.register(b, channel2); StubNetwork network2 = new StubNetwork(hub); - MockEngine C = new MockEngine(); - MockEngine D = new MockEngine(); - network2.register(C, channel1); - network2.register(D, channel2); + MockEngine c = new MockEngine(); + MockEngine d = new MockEngine(); + network2.register(c, channel1); + network2.register(d, channel2); for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity1 = new EntityFixture(); Entity entity2 = new EntityFixture(); try { - c1A.unicast(entity1, network2.id()); - c1B.unicast(entity2, network2.id()); - if (!C.hasReceived(entity1) || C.hasReceived(entity2) || !D.hasReceived(entity2) || D.hasReceived(entity1)) { + c1.unicast(entity1, network2.id()); + c2.unicast(entity2, network2.id()); + if (!c.hasReceived(entity1) || c.hasReceived(entity2) || !d.hasReceived(entity2) || d.hasReceived(entity1)) { threadError.getAndIncrement(); } countDownLatch.countDown(); @@ -214,13 +203,13 @@ void TestTwoStubNetworks_FourEngines_ConcurrentMessages() { * Test for Registeration to Occupied Channel */ @Test - void TestRegisterToOccupiedChannel() { + void testRegisterToOccupiedChannel() { StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - network1.register(A1, channel1); - MockEngine B1 = new MockEngine(); + MockEngine a1 = new MockEngine(); + network1.register(a1, channel1); + MockEngine b1 = new MockEngine(); try { - network1.register(B1, channel1); + network1.register(b1, channel1); Assertions.fail("fail! method was expected to throw an exception"); } catch (IllegalStateException e) { //throw new IllegalStateException("could not register to channel since its already occupied"); @@ -231,21 +220,21 @@ void TestRegisterToOccupiedChannel() { * Test for Unicast one engine to all other stub networks. */ @Test - void TestUnicastOneToAll_Sequentially() { + void testUnicastOneToAllSequentially() { StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); int count = 0; for (Network network : networkArrayList) { try { c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - if (!E1.hasReceived(entity)) { + MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + if (!e1.hasReceived(entity)) { count++; } - if (E2.hasReceived(entity)) { + if (e2.hasReceived(entity)) { count++; } } catch (LightChainNetworkingException e) { @@ -259,13 +248,13 @@ void TestUnicastOneToAll_Sequentially() { * Test one engine unicasts to all others concurrently. */ @Test - void TestUnicastOneToAll_Concurrently() { + void testUnicastOneToAllConcurrently() { int concurrencyDegree = 9; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); Thread[] unicastThreads = new Thread[concurrencyDegree]; int count = 0; @@ -273,12 +262,12 @@ void TestUnicastOneToAll_Concurrently() { unicastThreads[count] = new Thread(() -> { try { c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - if (!E1.hasReceived(entity)) { + MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + if (!e1.hasReceived(entity)) { threadError.getAndIncrement(); } - if (E2.hasReceived(entity)) { + if (e2.hasReceived(entity)) { threadError.getAndIncrement(); } countDownLatch.countDown(); @@ -301,13 +290,13 @@ void TestUnicastOneToAll_Concurrently() { } /** - * Test one engine sends unicast to some sequentially + * Test one engine sends unicast to some sequentially. */ @Test - void TestUnicastOneToSome_Sequentially() { + void testUnicastOneToSomeSequentially() { StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); int size = networkArrayList.size(); int count = 0; @@ -320,14 +309,14 @@ void TestUnicastOneToSome_Sequentially() { Network network_sh = second_it.next(); try { c1.unicast(entity, ((StubNetwork) network_fh).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); - MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); - MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); - if (!E1.hasReceived(entity)) { + MockEngine e1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); + MockEngine m1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); + MockEngine m2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + if (!e1.hasReceived(entity)) { count++; } - if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { + if (e2.hasReceived(entity) || m1.hasReceived(entity) || m2.hasReceived(entity)) { count++; } } catch (LightChainNetworkingException e) { @@ -341,13 +330,13 @@ void TestUnicastOneToSome_Sequentially() { * Test one engine send unicast to some concurrently */ @Test - void TestUnicastOneToSome_Concurrently() { + void testUnicastOneToSomeConcurrently() { int concurrencyDegree = 4; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); int size = networkArrayList.size(); List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); @@ -362,14 +351,14 @@ void TestUnicastOneToSome_Concurrently() { unicastThreads[count] = new Thread(() -> { try { c1.unicast(entity, ((StubNetwork) network_fh).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); - MockEngine M1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); - MockEngine M2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); - if (!E1.hasReceived(entity)) { + MockEngine e1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); + MockEngine m1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); + MockEngine m2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + if (!e1.hasReceived(entity)) { threadError.getAndIncrement(); } - if (E2.hasReceived(entity) || M1.hasReceived(entity) || M2.hasReceived(entity)) { + if (e2.hasReceived(entity) || m1.hasReceived(entity) || m2.hasReceived(entity)) { threadError.getAndIncrement(); } countDownLatch.countDown(); @@ -392,15 +381,15 @@ void TestUnicastOneToSome_Concurrently() { } /** - * test two engines sends other engines sequentially + * Test two engines sends other engines sequentially. */ @Test - void TestUnicastOneToAll_Sequentially_TwoEngines() { + void testUnicastOneToAll_SequentiallyTwoEngines() { StubNetwork network1 = new StubNetwork(hub); - MockEngine A1 = new MockEngine(); - MockEngine A2 = new MockEngine(); - Conduit c1 = network1.register(A1, channel1); - Conduit c2 = network1.register(A2, channel2); + MockEngine a1 = new MockEngine(); + MockEngine a2 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); + Conduit c2 = network1.register(a2, channel2); Entity entity1 = new EntityFixture(); Entity entity2 = new EntityFixture(); int count = 0; @@ -408,12 +397,12 @@ void TestUnicastOneToAll_Sequentially_TwoEngines() { try { c1.unicast(entity1, ((StubNetwork) network).id()); c2.unicast(entity2, ((StubNetwork) network).id()); - MockEngine E1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine E2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - if (!E1.hasReceived(entity1) || !E2.hasReceived(entity2)) { + MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + if (!e1.hasReceived(entity1) || !e2.hasReceived(entity2)) { count++; } - if (E2.hasReceived(entity1) || E1.hasReceived(entity2)) { + if (e2.hasReceived(entity1) || e1.hasReceived(entity2)) { count++; } } catch (LightChainNetworkingException e) { From e952cff948d896db5c2cf39c075f4caf988b2672 Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Sun, 27 Mar 2022 00:07:44 +0300 Subject: [PATCH 41/51] Cl changes --- src/test/java/networking/StubNetwork.java | 5 -- src/test/java/networking/StubNetworkTest.java | 47 ++++++++++--------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 9a535962..e15981df 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -10,9 +10,7 @@ import unittest.fixtures.IdentifierFixture; /** - * * A mock implementation of networking layer as a test util. - * */ public class StubNetwork implements Network { private final ConcurrentHashMap engines; @@ -20,7 +18,6 @@ public class StubNetwork implements Network { private final Identifier identifier; /** - * * Create stubNetwork. * * @param hub the hub which stubnetwork registered is. @@ -42,7 +39,6 @@ public Identifier id() { } /** - * * Forward the incoming entity to the engine whose channel is given. * * @param entity received entity @@ -58,7 +54,6 @@ public void receiveUnicast(Entity entity, String channel) { } /** - * * Registers an Engine to the Network by providing it with a Conduit. * * @param en the Engine to be registered. diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index 633fed2e..c079c70d 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -18,7 +18,7 @@ import unittest.fixtures.EntityFixture; /** - * Encapculates tests for Stubnetwork + * Encapculates tests for Stubnetwork. */ public class StubNetworkTest { @@ -26,6 +26,7 @@ public class StubNetworkTest { private final String channel1 = "test-network-channel-1"; private final String channel2 = "test-network-channel-2"; private Hub hub; + /** * Implement before each test. */ @@ -200,7 +201,7 @@ void testTwoStubNetworksFourEnginesConcurrentMessages() { } /** - * Test for Registeration to Occupied Channel + * Test for Registeration to Occupied Channel. */ @Test void testRegisterToOccupiedChannel() { @@ -302,17 +303,17 @@ void testUnicastOneToSomeSequentially() { int count = 0; List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); - Iterator first_it = first.iterator(); - Iterator second_it = second.iterator(); - while (first_it.hasNext() && second_it.hasNext()) { - Network network_fh = first_it.next(); - Network network_sh = second_it.next(); + Iterator firstit = first.iterator(); + Iterator secondit = second.iterator(); + while (firstit.hasNext() && secondit.hasNext()) { + Network networkfh = firstit.next(); + Network networksh = secondit.next(); try { - c1.unicast(entity, ((StubNetwork) network_fh).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); - MockEngine m1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); - MockEngine m2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + c1.unicast(entity, ((StubNetwork) networkfh).id()); + MockEngine e1 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel2); + MockEngine m1 = (MockEngine) ((StubNetwork) networksh).getEngine(channel1); + MockEngine m2 = (MockEngine) ((StubNetwork) networksh).getEngine(channel2); if (!e1.hasReceived(entity)) { count++; } @@ -327,7 +328,7 @@ void testUnicastOneToSomeSequentially() { } /** - * Test one engine send unicast to some concurrently + * Test one engine send unicast to some concurrently. */ @Test void testUnicastOneToSomeConcurrently() { @@ -341,20 +342,20 @@ void testUnicastOneToSomeConcurrently() { int size = networkArrayList.size(); List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); - Iterator first_it = first.iterator(); - Iterator second_it = second.iterator(); + Iterator firstit = first.iterator(); + Iterator secondit = second.iterator(); Thread[] unicastThreads = new Thread[concurrencyDegree]; int count = 0; - while (first_it.hasNext() && second_it.hasNext()) { - Network network_fh = first_it.next(); - Network network_sh = second_it.next(); + while (firstit.hasNext() && secondit.hasNext()) { + Network networkfh = firstit.next(); + Network networksh = secondit.next(); unicastThreads[count] = new Thread(() -> { try { - c1.unicast(entity, ((StubNetwork) network_fh).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) network_fh).getEngine(channel2); - MockEngine m1 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel1); - MockEngine m2 = (MockEngine) ((StubNetwork) network_sh).getEngine(channel2); + c1.unicast(entity, ((StubNetwork) networkfh).id()); + MockEngine e1 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel2); + MockEngine m1 = (MockEngine) ((StubNetwork) networksh).getEngine(channel1); + MockEngine m2 = (MockEngine) ((StubNetwork) networksh).getEngine(channel2); if (!e1.hasReceived(entity)) { threadError.getAndIncrement(); } From dd0b643ae7eacb7dcd2ca02d1d6ad5870bf6cc66 Mon Sep 17 00:00:00 2001 From: Yahya Date: Mon, 4 Apr 2022 08:11:44 -0700 Subject: [PATCH 42/51] applies revisions --- src/main/java/network/NetworkAdapter.java | 41 ++++++++++++++++++++ src/test/java/networking/Hub.java | 26 ++++++------- src/test/java/networking/MockConduit.java | 10 ++--- src/test/java/networking/StubNetwork.java | 46 ++++++++++++++++++++++- 4 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 src/main/java/network/NetworkAdapter.java diff --git a/src/main/java/network/NetworkAdapter.java b/src/main/java/network/NetworkAdapter.java new file mode 100644 index 00000000..e5151104 --- /dev/null +++ b/src/main/java/network/NetworkAdapter.java @@ -0,0 +1,41 @@ +package network; + +import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.exceptions.LightChainNetworkingException; +import model.lightchain.Identifier; + +/** + * NetworkAdapter models the interface that is exposed to the conduits from the networking layer. + */ +public interface NetworkAdapter { + /** + * Sends the Entity through the Network to the remote target. + * + * @param e the Entity to be sent over the network. + * @param target Identifier of the receiver. + * @param channel channel on which this entity is sent. + * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. + */ + void unicast(Entity e, Identifier target, String channel) throws LightChainNetworkingException; + + /** + * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. + * + * @param e the Entity to be stored over the network. + * @param namespace namespace on which this entity is stored. + * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. + */ + void put(Entity e, String namespace) throws LightChainDistributedStorageException; + + /** + * Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table + * (DHT) of nodes. + * + * @param identifier identifier of the entity to be retrieved. + * @param namespace the namespace on which this query is resolved. + * @return the retrieved entity or null if it does not exist. + * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. + */ + Entity get(Identifier identifier, String namespace) throws LightChainDistributedStorageException; +} diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 0f577c59..760e6ba9 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -10,7 +10,6 @@ * Models the core communication part of the networking layer that allows stub network instances to talk to each other. */ public class Hub { - private final ConcurrentHashMap networks; private final ConcurrentHashMap entities; @@ -25,23 +24,23 @@ public Hub() { /** * Registeration of a network to the Hub. * - * @param key identifier of network. + * @param identifier identifier of network. * @param network to be registered. */ - public void registerNetwork(Identifier key, Network network) { - networks.put(key, network); + public void registerNetwork(Identifier identifier, Network network) { + networks.put(identifier, network); } /** - * Transfer entity from a stubnetwork to another through hub. + * Transfer entity from to another network on the same channel. * * @param entity entity to be transferred. - * @param identifier identifier of target. - * @param channel channel of the transmitter-target engine. + * @param target identifier of target. + * @param channel channel on which the entity is delivered to target. */ - public void transferEntity(Entity entity, Identifier identifier, String channel) { - StubNetwork net = this.getNetwork(identifier); + public void transferEntity(Entity entity, Identifier target, String channel) { + StubNetwork net = this.getNetwork(target); net.receiveUnicast(entity, channel); } @@ -49,11 +48,10 @@ public void transferEntity(Entity entity, Identifier identifier, String channel) /** * Get the network with identifier. * - * @param key identity of. - * @return network. + * @param identifier identity of network. + * @return network corresponding to identifier. */ - private StubNetwork getNetwork(Identifier key) { - return (StubNetwork) networks.get(key); + private StubNetwork getNetwork(Identifier identifier) { + return (StubNetwork) networks.get(identifier); } - } \ No newline at end of file diff --git a/src/test/java/networking/MockConduit.java b/src/test/java/networking/MockConduit.java index 57ceaa5c..7ff40438 100644 --- a/src/test/java/networking/MockConduit.java +++ b/src/test/java/networking/MockConduit.java @@ -5,6 +5,7 @@ import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; +import network.NetworkAdapter; /** * MockConduit represents the Networking interface that is exposed to an Engine. @@ -12,11 +13,11 @@ public class MockConduit implements Conduit { private final String channel; - private final Hub hub; + private final NetworkAdapter networkAdapter; - public MockConduit(String channel, Hub hub) { + public MockConduit(String channel, NetworkAdapter adapter) { this.channel = channel; - this.hub = hub; + this.networkAdapter = adapter; } /** @@ -28,8 +29,7 @@ public MockConduit(String channel, Hub hub) { */ @Override public void unicast(Entity e, Identifier target) throws LightChainNetworkingException { - hub.transferEntity(e, target, channel); - + this.networkAdapter.unicast(e, target, channel); } /** diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index e15981df..833da652 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -3,16 +3,19 @@ import java.util.concurrent.ConcurrentHashMap; import model.Entity; +import model.exceptions.LightChainDistributedStorageException; +import model.exceptions.LightChainNetworkingException; import model.lightchain.Identifier; import network.Conduit; import network.Network; +import network.NetworkAdapter; import protocol.Engine; import unittest.fixtures.IdentifierFixture; /** * A mock implementation of networking layer as a test util. */ -public class StubNetwork implements Network { +public class StubNetwork implements Network, NetworkAdapter { private final ConcurrentHashMap engines; private final Hub hub; private final Identifier identifier; @@ -63,7 +66,7 @@ public void receiveUnicast(Entity entity, String channel) { */ @Override public Conduit register(Engine en, String channel) throws IllegalStateException { - Conduit conduit = new MockConduit(channel, hub); + Conduit conduit = new MockConduit(channel, this); try { if (engines.containsKey(channel)) { throw new IllegalStateException(); @@ -78,4 +81,43 @@ public Conduit register(Engine en, String channel) throws IllegalStateException public Engine getEngine(String ch) { return engines.get(ch); } + + /** + * Sends the Entity through the Network to the remote target. + * + * @param e the Entity to be sent over the network. + * @param target Identifier of the receiver. + * @param channel channel on which this entity is sent. + * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. + */ + @Override + public void unicast(Entity e, Identifier target, String channel) throws LightChainNetworkingException { + this.hub.transferEntity(e, target, channel); + } + + /** + * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. + * + * @param e the Entity to be stored over the network. + * @param namespace namespace on which this entity is stored. + * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. + */ + @Override + public void put(Entity e, String namespace) throws LightChainDistributedStorageException { + + } + + /** + * Retrieves the entity corresponding to the given identifier form the underlying Distributed Hash Table + * (DHT) of nodes. + * + * @param identifier identifier of the entity to be retrieved. + * @param namespace the namespace on which this query is resolved. + * @return the retrieved entity or null if it does not exist. + * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. + */ + @Override + public Entity get(Identifier identifier, String namespace) throws LightChainDistributedStorageException { + return null; + } } \ No newline at end of file From 3fc5beded2843f5a5be6fe068b18e0cea6633b8e Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Wed, 6 Apr 2022 02:39:19 +0300 Subject: [PATCH 43/51] StubNetworkTest is fixed --- src/test/java/networking/StubNetworkTest.java | 83 +++++++------------ 1 file changed, 32 insertions(+), 51 deletions(-) diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index c079c70d..a2be1d29 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -49,8 +49,6 @@ void setup() { */ @Test void testTwoStubNetworksTwoEngines() { - String channel1 = "test-network-channel-1"; - Hub hub = new Hub(); StubNetwork network1 = new StubNetwork(hub); MockEngine a1 = new MockEngine(); Conduit c1 = network1.register(a1, channel1); @@ -154,7 +152,7 @@ void testTwoStubNetworksTwoEnginesReplyConcurrentMessages() { } /** - * Test two stub netwokrs for engines, concurrently messages. + * Test two stub netwokrs with four engines, concurrently messages. */ @Test void testTwoStubNetworksFourEnginesConcurrentMessages() { @@ -226,23 +224,17 @@ void testUnicastOneToAllSequentially() { MockEngine a1 = new MockEngine(); Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); - int count = 0; for (Network network : networkArrayList) { try { c1.unicast(entity, ((StubNetwork) network).id()); MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - if (!e1.hasReceived(entity)) { - count++; - } - if (e2.hasReceived(entity)) { - count++; - } + Assertions.assertTrue(e1.hasReceived(entity)); + Assertions.assertFalse(e2.hasReceived(entity)); } catch (LightChainNetworkingException e) { - count++; + Assertions.fail(); } } - Assertions.assertEquals(0, count); } /** @@ -300,31 +292,25 @@ void testUnicastOneToSomeSequentially() { Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); int size = networkArrayList.size(); - int count = 0; List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); - Iterator firstit = first.iterator(); - Iterator secondit = second.iterator(); - while (firstit.hasNext() && secondit.hasNext()) { - Network networkfh = firstit.next(); - Network networksh = secondit.next(); + Iterator firstIt = first.iterator(); + Iterator secondIt = second.iterator(); + while (firstIt.hasNext() && secondIt.hasNext()) { + Network networkFirst = firstIt.next(); + Network networkSecond = secondIt.next(); try { - c1.unicast(entity, ((StubNetwork) networkfh).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel2); - MockEngine m1 = (MockEngine) ((StubNetwork) networksh).getEngine(channel1); - MockEngine m2 = (MockEngine) ((StubNetwork) networksh).getEngine(channel2); - if (!e1.hasReceived(entity)) { - count++; - } - if (e2.hasReceived(entity) || m1.hasReceived(entity) || m2.hasReceived(entity)) { - count++; - } + c1.unicast(entity, ((StubNetwork) networkFirst).id()); + MockEngine e1 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel2); + MockEngine m1 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel1); + MockEngine m2 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel2); + Assertions.assertTrue(e1.hasReceived(entity)); + Assertions.assertFalse(e2.hasReceived(entity) || m1.hasReceived(entity) || m2.hasReceived(entity)); } catch (LightChainNetworkingException e) { - count++; + Assertions.fail(); } } - Assertions.assertEquals(0, count); } /** @@ -342,20 +328,20 @@ void testUnicastOneToSomeConcurrently() { int size = networkArrayList.size(); List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); - Iterator firstit = first.iterator(); - Iterator secondit = second.iterator(); + Iterator firstIt = first.iterator(); + Iterator secondIt = second.iterator(); Thread[] unicastThreads = new Thread[concurrencyDegree]; int count = 0; - while (firstit.hasNext() && secondit.hasNext()) { - Network networkfh = firstit.next(); - Network networksh = secondit.next(); + while (firstIt.hasNext() && secondIt.hasNext()) { + Network networkFirst = firstIt.next(); + Network networkSecond = secondIt.next(); unicastThreads[count] = new Thread(() -> { try { - c1.unicast(entity, ((StubNetwork) networkfh).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) networkfh).getEngine(channel2); - MockEngine m1 = (MockEngine) ((StubNetwork) networksh).getEngine(channel1); - MockEngine m2 = (MockEngine) ((StubNetwork) networksh).getEngine(channel2); + c1.unicast(entity, ((StubNetwork) networkFirst).id()); + MockEngine e1 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel2); + MockEngine m1 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel1); + MockEngine m2 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel2); if (!e1.hasReceived(entity)) { threadError.getAndIncrement(); } @@ -382,7 +368,7 @@ void testUnicastOneToSomeConcurrently() { } /** - * Test two engines sends other engines sequentially. + * Test two engines sends different entities other engines sequentially. */ @Test void testUnicastOneToAll_SequentiallyTwoEngines() { @@ -393,23 +379,18 @@ void testUnicastOneToAll_SequentiallyTwoEngines() { Conduit c2 = network1.register(a2, channel2); Entity entity1 = new EntityFixture(); Entity entity2 = new EntityFixture(); - int count = 0; for (Network network : networkArrayList) { try { c1.unicast(entity1, ((StubNetwork) network).id()); c2.unicast(entity2, ((StubNetwork) network).id()); MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - if (!e1.hasReceived(entity1) || !e2.hasReceived(entity2)) { - count++; - } - if (e2.hasReceived(entity1) || e1.hasReceived(entity2)) { - count++; - } + Assertions.assertTrue(e1.hasReceived(entity1) && e2.hasReceived(entity2)); + Assertions.assertFalse(e2.hasReceived(entity1) || e1.hasReceived(entity2)); + } catch (LightChainNetworkingException e) { - count++; + Assertions.fail(); } } - Assertions.assertEquals(0, count); } } \ No newline at end of file From f7f1c97d20172eecf2c946e20382832013f4f86a Mon Sep 17 00:00:00 2001 From: akucukoduk16 Date: Sun, 10 Apr 2022 16:45:46 +0300 Subject: [PATCH 44/51] StubNetworkTest Identifier fixed. --- src/main/java/model/lightchain/Identifier.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/main/java/model/lightchain/Identifier.java b/src/main/java/model/lightchain/Identifier.java index 5d9dfb62..26c546ff 100644 --- a/src/main/java/model/lightchain/Identifier.java +++ b/src/main/java/model/lightchain/Identifier.java @@ -77,21 +77,4 @@ public int comparedTo(Identifier other) { int result = Arrays.compare(this.value, other.value); return Integer.compare(result, 0); } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof Identifier)) { - return false; - } - Identifier that = (Identifier) o; - return Arrays.equals(value, that.value); - } - - @Override - public int hashCode() { - return Arrays.hashCode(value); - } } From 809ab188420585fab776dd3772434a8169c21d9c Mon Sep 17 00:00:00 2001 From: Yahya Date: Sun, 10 Apr 2022 07:03:27 -0700 Subject: [PATCH 45/51] applies revision --- src/test/java/networking/MockEngine.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 182f2429..9e4094c4 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -29,22 +29,25 @@ public MockEngine() { @Override public void process(Entity e) throws IllegalArgumentException { - lock.writeLock(); + lock.writeLock().lock(); + receivedEntityIds.add(e.id()); - lock.writeLock(); + + lock.writeLock().unlock(); } /** * Check whether an entity is received. * - * @param e the entitiy. + * @param e the entity. * @return true if the entity received, otherwise false. */ public boolean hasReceived(Entity e) { - lock.readLock(); - Identifier id = e.id(); - boolean ok = this.receivedEntityIds.contains(id); - lock.readLock(); + lock.readLock().lock(); + + boolean ok = this.receivedEntityIds.contains(e.id()); + + lock.readLock().unlock(); return ok; } } \ No newline at end of file From 0d7b720b37dae0cca791ea7ccb595a94955c85e9 Mon Sep 17 00:00:00 2001 From: Yahya Date: Mon, 11 Apr 2022 06:31:14 -0700 Subject: [PATCH 46/51] applies revisions --- src/test/java/networking/MockEngine.java | 14 ++++ src/test/java/networking/StubNetworkTest.java | 80 ++++++++++--------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/test/java/networking/MockEngine.java b/src/test/java/networking/MockEngine.java index 9e4094c4..6b613b23 100644 --- a/src/test/java/networking/MockEngine.java +++ b/src/test/java/networking/MockEngine.java @@ -50,4 +50,18 @@ public boolean hasReceived(Entity e) { lock.readLock().unlock(); return ok; } + + /** + * Total distinct entities this engine received. + * + * @return total messages it received. + */ + public int totalReceived() { + lock.readLock().lock(); + + int size = receivedEntityIds.size(); + + lock.readLock().unlock(); + return size; + } } \ No newline at end of file diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index a2be1d29..ef84f4af 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -18,54 +18,56 @@ import unittest.fixtures.EntityFixture; /** - * Encapculates tests for Stubnetwork. + * Encapsulates tests for the stubnetwork. */ public class StubNetworkTest { - private ArrayList networkArrayList; + private ArrayList networks; private final String channel1 = "test-network-channel-1"; private final String channel2 = "test-network-channel-2"; private Hub hub; /** - * Implement before each test. + * Creates a hub with 10 connected networks, each network has two mock engines on different channels. */ @BeforeEach void setup() { - this.networkArrayList = new ArrayList<>(); + this.networks = new ArrayList<>(); this.hub = new Hub(); for (int i = 0; i < 9; i++) { StubNetwork stubNetwork = new StubNetwork(hub); - Engine e1 = new MockEngine(); - Engine e2 = new MockEngine(); - stubNetwork.register(e1, channel1); - stubNetwork.register(e2, channel2); - networkArrayList.add(stubNetwork); + stubNetwork.register(new MockEngine(), channel1); + stubNetwork.register(new MockEngine(), channel2); + networks.add(stubNetwork); } } /** - * Test two stub networks with two engines. + * Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, + * and the message is received by Engine B. */ @Test void testTwoStubNetworksTwoEngines() { - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - StubNetwork network2 = new StubNetwork(hub); - MockEngine a2 = new MockEngine(); - network2.register(a2, channel1); + StubNetwork networkA = new StubNetwork(hub); + MockEngine engineA = new MockEngine(); + Conduit conduitA = networkA.register(engineA, channel1); + + StubNetwork networkB = new StubNetwork(hub); + MockEngine engineB = new MockEngine(); + networkB.register(engineB, channel1); + Entity entity = new EntityFixture(); try { - c1.unicast(entity, network2.id()); + conduitA.unicast(entity, networkB.id()); } catch (LightChainNetworkingException e) { Assertions.fail(); } - Assertions.assertTrue(a2.hasReceived(entity)); + Assertions.assertTrue(engineB.hasReceived(entity)); } /** - * test two stub networks with two engines concurrently. + * Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, + * and ALL messages received by Engine B. */ @Test void testTwoStubNetworksTwoEnginesConcurrentMessages() { @@ -73,18 +75,21 @@ void testTwoStubNetworksTwoEnginesConcurrentMessages() { AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); Thread[] unicastThreads = new Thread[concurrencyDegree]; - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - StubNetwork network2 = new StubNetwork(hub); - MockEngine a2 = new MockEngine(); - network2.register(a2, channel1); + + StubNetwork networkA = new StubNetwork(hub); + MockEngine engineA = new MockEngine(); + Conduit conduitA = networkA.register(engineA, channel1); + + StubNetwork networkB = new StubNetwork(hub); + MockEngine engineB = new MockEngine(); + networkB.register(engineB, channel1); + for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { Entity entity = new EntityFixture(); try { - c1.unicast(entity, network2.id()); - if (!a2.hasReceived(entity)) { + conduitA.unicast(entity, networkB.id()); + if (!engineB.hasReceived(entity)) { threadError.getAndIncrement(); } countDownLatch.countDown(); @@ -93,9 +98,11 @@ void testTwoStubNetworksTwoEnginesConcurrentMessages() { } }); } + for (Thread t : unicastThreads) { t.start(); } + try { boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); Assertions.assertTrue(doneOneTime); @@ -103,6 +110,7 @@ void testTwoStubNetworksTwoEnginesConcurrentMessages() { Assertions.fail(); } Assertions.assertEquals(0, threadError.get()); + Assertions.assertEquals(concurrencyDegree, engineB.totalReceived()); } /** @@ -224,7 +232,7 @@ void testUnicastOneToAllSequentially() { MockEngine a1 = new MockEngine(); Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); - for (Network network : networkArrayList) { + for (Network network : networks) { try { c1.unicast(entity, ((StubNetwork) network).id()); MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); @@ -251,7 +259,7 @@ void testUnicastOneToAllConcurrently() { Entity entity = new EntityFixture(); Thread[] unicastThreads = new Thread[concurrencyDegree]; int count = 0; - for (Network network : networkArrayList) { + for (Network network : networks) { unicastThreads[count] = new Thread(() -> { try { c1.unicast(entity, ((StubNetwork) network).id()); @@ -291,9 +299,9 @@ void testUnicastOneToSomeSequentially() { MockEngine a1 = new MockEngine(); Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); - int size = networkArrayList.size(); - List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); - List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); + int size = networks.size(); + List first = new ArrayList<>(networks.subList(0, size / 2)); + List second = new ArrayList<>(networks.subList(size / 2, size)); Iterator firstIt = first.iterator(); Iterator secondIt = second.iterator(); while (firstIt.hasNext() && secondIt.hasNext()) { @@ -325,9 +333,9 @@ void testUnicastOneToSomeConcurrently() { MockEngine a1 = new MockEngine(); Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); - int size = networkArrayList.size(); - List first = new ArrayList<>(networkArrayList.subList(0, size / 2)); - List second = new ArrayList<>(networkArrayList.subList(size / 2, size)); + int size = networks.size(); + List first = new ArrayList<>(networks.subList(0, size / 2)); + List second = new ArrayList<>(networks.subList(size / 2, size)); Iterator firstIt = first.iterator(); Iterator secondIt = second.iterator(); Thread[] unicastThreads = new Thread[concurrencyDegree]; @@ -379,7 +387,7 @@ void testUnicastOneToAll_SequentiallyTwoEngines() { Conduit c2 = network1.register(a2, channel2); Entity entity1 = new EntityFixture(); Entity entity2 = new EntityFixture(); - for (Network network : networkArrayList) { + for (Network network : networks) { try { c1.unicast(entity1, ((StubNetwork) network).id()); c2.unicast(entity2, ((StubNetwork) network).id()); From 465d3ad09c5b6671dd8eb10d23d45a9a51212a61 Mon Sep 17 00:00:00 2001 From: Yahya Date: Mon, 11 Apr 2022 07:30:30 -0700 Subject: [PATCH 47/51] applies revisions --- src/test/java/networking/StubNetwork.java | 2 +- .../networking/StubNetworkEpidemicTest.java | 241 ++++++++++++++ src/test/java/networking/StubNetworkTest.java | 312 ++++-------------- 3 files changed, 314 insertions(+), 241 deletions(-) create mode 100644 src/test/java/networking/StubNetworkEpidemicTest.java diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 833da652..d69c963d 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -73,7 +73,7 @@ public Conduit register(Engine en, String channel) throws IllegalStateException } engines.put(channel, en); } catch (IllegalArgumentException ex) { - throw new IllegalStateException("could not register the engine" + ex); + throw new IllegalStateException("could not register the engine"); } return conduit; } diff --git a/src/test/java/networking/StubNetworkEpidemicTest.java b/src/test/java/networking/StubNetworkEpidemicTest.java new file mode 100644 index 00000000..299a41ec --- /dev/null +++ b/src/test/java/networking/StubNetworkEpidemicTest.java @@ -0,0 +1,241 @@ +package networking; + +import java.util.ArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import model.Entity; +import model.exceptions.LightChainNetworkingException; +import network.Conduit; +import network.Network; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import unittest.fixtures.EntityFixture; + +/** + * Encapsulates one-to-all test cases of stub network. + */ +public class StubNetworkEpidemicTest { + private final String channel1 = "test-network-channel-1"; + private final String channel2 = "test-network-channel-2"; + private ArrayList networks; + private Hub hub; + + + /** + * Creates a hub with 10 connected networks, each network has two mock engines on different channels. + */ + @BeforeEach + void setup() { + this.networks = new ArrayList<>(); + this.hub = new Hub(); + for (int i = 0; i < 9; i++) { + StubNetwork stubNetwork = new StubNetwork(hub); + stubNetwork.register(new MockEngine(), channel1); + stubNetwork.register(new MockEngine(), channel2); + networks.add(stubNetwork); + } + } + + /** + * Test for Unicast one engine to all other stub networks. + */ + @Test + void testUnicastOneToAllSequentially() { + Hub hub = new Hub(); + + StubNetwork network1 = new StubNetwork(hub); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); + Entity entity = new EntityFixture(); + + for (Network network : networks) { + try { + c1.unicast(entity, ((StubNetwork) network).id()); + MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); + MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + + // only engine on channel-1 should receive the entity. + Assertions.assertTrue(e1.hasReceived(entity)); + Assertions.assertFalse(e2.hasReceived(entity)); + } catch (LightChainNetworkingException e) { + Assertions.fail(); + } + } + } + + /** + * Test one engine unicasts to all others concurrently. + */ + @Test + void testUnicastOneToAllConcurrently() { + int concurrencyDegree = 9; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch sendDone = new CountDownLatch(concurrencyDegree); + + StubNetwork network1 = new StubNetwork(hub); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); + + Entity entity = new EntityFixture(); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + + for (int i = 0; i < networks.size(); i++) { + int finalI = i; + unicastThreads[i] = new Thread(() -> { + try { + c1.unicast(entity, (this.networks.get(finalI).id())); + MockEngine e1 = (MockEngine) this.networks.get(finalI).getEngine(channel1); + MockEngine e2 = (MockEngine) this.networks.get(finalI).getEngine(channel2); + if (!e1.hasReceived(entity)) { + threadError.getAndIncrement(); + } + if (e2.hasReceived(entity)) { + threadError.getAndIncrement(); + } + sendDone.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + try { + boolean doneOneTime = sendDone.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); + } + + /** + * Test one engine sends unicast to some sequentially. + */ + @Test + void testUnicastOneToSomeSequentially() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); + + Entity entity = new EntityFixture(); + + // unicast only to the first half + for (int i = 0; i < networks.size() / 2; i++) { + try { + c1.unicast(entity, this.networks.get(i).id()); + } catch (LightChainNetworkingException e) { + Assertions.fail(); + } + } + + + // checks only first half of network should receive it. + for (int i = 0; i < networks.size(); i++) { + // first half of networks should receive unicast + MockEngine e1 = (MockEngine) this.networks.get(i).getEngine(channel1); + MockEngine e2 = (MockEngine) this.networks.get(i).getEngine(channel2); + if (i < networks.size() / 2) { + + Assertions.assertTrue(e1.hasReceived(entity) // only engine on channel-1 should receive it. + && !e2.hasReceived(entity)); + } else { + Assertions.assertFalse(e1.hasReceived(entity) || e2.hasReceived(entity)); + } + } + + } + + /** + * Test one engine send unicast to some concurrently. + */ + @Test + void testUnicastOneToSomeConcurrently() { + int concurrencyDegree = networks.size() / 2; + AtomicInteger threadError = new AtomicInteger(); + CountDownLatch sentDone = new CountDownLatch(concurrencyDegree); + StubNetwork network1 = new StubNetwork(hub); + + MockEngine a1 = new MockEngine(); + Conduit c1 = network1.register(a1, channel1); + Entity entity = new EntityFixture(); + Thread[] unicastThreads = new Thread[concurrencyDegree]; + + + // concurrently unicasts to the first half of network + for (int i = 0; i < concurrencyDegree; i++) { + int finalI = i; + unicastThreads[i] = new Thread(() -> { + try { + c1.unicast(entity, this.networks.get(finalI).id()); + sentDone.countDown(); + } catch (LightChainNetworkingException e) { + threadError.getAndIncrement(); + } + }); + } + + for (Thread t : unicastThreads) { + t.start(); + } + try { + boolean doneOneTime = sentDone.await(60, TimeUnit.SECONDS); + Assertions.assertTrue(doneOneTime); + } catch (InterruptedException e) { + Assertions.fail(); + } + + Assertions.assertEquals(0, threadError.get()); + + // checks only first half of network should receive it. + for (int i = 0; i < networks.size(); i++) { + // first half of networks should receive unicast + MockEngine e1 = (MockEngine) this.networks.get(i).getEngine(channel1); + MockEngine e2 = (MockEngine) this.networks.get(i).getEngine(channel2); + if (i < networks.size() / 2) { + + Assertions.assertTrue(e1.hasReceived(entity) // only engine on channel-1 should receive it. + && !e2.hasReceived(entity)); + } else { + Assertions.assertFalse(e1.hasReceived(entity) || e2.hasReceived(entity)); + } + } + + } + + /** + * Test two engines sends different distinct entities over distinct channels other engines sequentially. + */ + @Test + void testUnicastOneToAll_SequentiallyTwoEngines() { + StubNetwork network1 = new StubNetwork(hub); + MockEngine a1 = new MockEngine(); + MockEngine a2 = new MockEngine(); + + Conduit c1 = network1.register(a1, channel1); + Conduit c2 = network1.register(a2, channel2); + + Entity entity1 = new EntityFixture(); + Entity entity2 = new EntityFixture(); + + for (StubNetwork network : networks) { + try { + c1.unicast(entity1, network.id()); + c2.unicast(entity2, network.id()); + MockEngine e1 = (MockEngine) network.getEngine(channel1); + MockEngine e2 = (MockEngine) network.getEngine(channel2); + Assertions.assertTrue(e1.hasReceived(entity1) && e2.hasReceived(entity2)); + Assertions.assertFalse(e2.hasReceived(entity1) || e1.hasReceived(entity2)); + + } catch (LightChainNetworkingException e) { + Assertions.fail(); + } + } + } +} diff --git a/src/test/java/networking/StubNetworkTest.java b/src/test/java/networking/StubNetworkTest.java index ef84f4af..95ce2e86 100644 --- a/src/test/java/networking/StubNetworkTest.java +++ b/src/test/java/networking/StubNetworkTest.java @@ -1,8 +1,5 @@ package networking; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -10,11 +7,8 @@ import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; -import network.Network; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import protocol.Engine; import unittest.fixtures.EntityFixture; /** @@ -22,25 +16,8 @@ */ public class StubNetworkTest { - private ArrayList networks; private final String channel1 = "test-network-channel-1"; private final String channel2 = "test-network-channel-2"; - private Hub hub; - - /** - * Creates a hub with 10 connected networks, each network has two mock engines on different channels. - */ - @BeforeEach - void setup() { - this.networks = new ArrayList<>(); - this.hub = new Hub(); - for (int i = 0; i < 9; i++) { - StubNetwork stubNetwork = new StubNetwork(hub); - stubNetwork.register(new MockEngine(), channel1); - stubNetwork.register(new MockEngine(), channel2); - networks.add(stubNetwork); - } - } /** * Engine A (on one stub network) can send message to Engine B (on another stub network) through its StubNetwork, @@ -48,6 +25,7 @@ void setup() { */ @Test void testTwoStubNetworksTwoEngines() { + Hub hub = new Hub(); StubNetwork networkA = new StubNetwork(hub); MockEngine engineA = new MockEngine(); Conduit conduitA = networkA.register(engineA, channel1); @@ -71,6 +49,8 @@ void testTwoStubNetworksTwoEngines() { */ @Test void testTwoStubNetworksTwoEnginesConcurrentMessages() { + Hub hub = new Hub(); + int concurrencyDegree = 100; AtomicInteger threadError = new AtomicInteger(); CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); @@ -114,44 +94,57 @@ void testTwoStubNetworksTwoEnginesConcurrentMessages() { } /** - * Test for two stub networks with reply. + * Engine A can CONCURRENTLY send 100 messages to Engine B through its StubNetwork, + * and ALL messages received by Engine B. + * Engine B also sending a reply message to Engine A for each received messages and all replies + * are received by Engine A. */ @Test void testTwoStubNetworksTwoEnginesReplyConcurrentMessages() { + Hub hub = new Hub(); + int concurrencyDegree = 100; AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + CountDownLatch sendDone = new CountDownLatch(concurrencyDegree); Thread[] unicastThreads = new Thread[concurrencyDegree]; - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - StubNetwork network2 = new StubNetwork(hub); - MockEngine a2 = new MockEngine(); - Conduit c2 = network2.register(a2, channel1); + + StubNetwork networkA = new StubNetwork(hub); + MockEngine engineA = new MockEngine(); + Conduit conduitA = networkA.register(engineA, channel1); + + StubNetwork networkB = new StubNetwork(hub); + MockEngine engineB = new MockEngine(); + Conduit conduitB = networkB.register(engineB, channel1); + for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { - Entity entity = new EntityFixture(); - Entity entity2 = new EntityFixture(); + Entity message = new EntityFixture(); + Entity reply = new EntityFixture(); try { - c1.unicast(entity, network2.id()); - if (!a2.hasReceived(entity)) { + // A -> B + conduitA.unicast(message, networkB.id()); + if (!engineB.hasReceived(message)) { threadError.getAndIncrement(); } - c2.unicast(entity2, network1.id()); - if (!a1.hasReceived(entity2)) { + + // B -> A + conduitB.unicast(reply, networkA.id()); + if (!engineA.hasReceived(reply)) { threadError.getAndIncrement(); } - countDownLatch.countDown(); + sendDone.countDown(); } catch (LightChainNetworkingException e) { threadError.getAndIncrement(); } }); } + for (Thread t : unicastThreads) { t.start(); } + try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + boolean doneOneTime = sendDone.await(60, TimeUnit.SECONDS); Assertions.assertTrue(doneOneTime); } catch (InterruptedException e) { Assertions.fail(); @@ -160,35 +153,51 @@ void testTwoStubNetworksTwoEnginesReplyConcurrentMessages() { } /** - * Test two stub netwokrs with four engines, concurrently messages. + * Engines A1 and A2 on one StubNetwork can CONCURRENTLY send 100 messages to Engines B1 and B2 on another StubNetwork + * (A1 -> B1) and (A2 -> B2), and each Engine only + * receives messages destinated for it (B1 receives all messages from A1) and (B2 receives all messages from A2). */ @Test void testTwoStubNetworksFourEnginesConcurrentMessages() { + Hub hub = new Hub(); + int concurrencyDegree = 100; AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); + CountDownLatch sendDone = new CountDownLatch(concurrencyDegree); Thread[] unicastThreads = new Thread[concurrencyDegree]; - StubNetwork network1 = new StubNetwork(hub); - MockEngine a = new MockEngine(); - Conduit c1 = network1.register(a, channel1); - MockEngine b = new MockEngine(); - Conduit c2 = network1.register(b, channel2); - StubNetwork network2 = new StubNetwork(hub); - MockEngine c = new MockEngine(); - MockEngine d = new MockEngine(); - network2.register(c, channel1); - network2.register(d, channel2); + + // network A + StubNetwork networkA = new StubNetwork(hub); + MockEngine engineA1 = new MockEngine(); + Conduit conduitA1 = networkA.register(engineA1, channel1); + + MockEngine engineA2 = new MockEngine(); + Conduit conduitA2 = networkA.register(engineA2, channel2); + + // network B + StubNetwork networkB = new StubNetwork(hub); + MockEngine engineB1 = new MockEngine(); + MockEngine engineB2 = new MockEngine(); + networkB.register(engineB1, channel1); + networkB.register(engineB2, channel2); + for (int i = 0; i < concurrencyDegree; i++) { unicastThreads[i] = new Thread(() -> { - Entity entity1 = new EntityFixture(); - Entity entity2 = new EntityFixture(); + Entity messageA1toB1 = new EntityFixture(); + Entity messageA2toB2 = new EntityFixture(); try { - c1.unicast(entity1, network2.id()); - c2.unicast(entity2, network2.id()); - if (!c.hasReceived(entity1) || c.hasReceived(entity2) || !d.hasReceived(entity2) || d.hasReceived(entity1)) { + // A1 -> B1 + // A2 -> B2 + conduitA1.unicast(messageA1toB1, networkB.id()); + conduitA2.unicast(messageA2toB2, networkB.id()); + + if (!engineB1.hasReceived(messageA1toB1) + || engineB1.hasReceived(messageA2toB2) + || !engineB2.hasReceived(messageA2toB2) + || engineB2.hasReceived(messageA1toB1)) { threadError.getAndIncrement(); } - countDownLatch.countDown(); + sendDone.countDown(); } catch (LightChainNetworkingException e) { threadError.getAndIncrement(); } @@ -198,7 +207,7 @@ void testTwoStubNetworksFourEnginesConcurrentMessages() { t.start(); } try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); + boolean doneOneTime = sendDone.await(60, TimeUnit.SECONDS); Assertions.assertTrue(doneOneTime); } catch (InterruptedException e) { Assertions.fail(); @@ -207,10 +216,12 @@ void testTwoStubNetworksFourEnginesConcurrentMessages() { } /** - * Test for Registeration to Occupied Channel. + * Stub network throws an exception if an engine is registering itself on an already taken channel. */ @Test void testRegisterToOccupiedChannel() { + Hub hub = new Hub(); + StubNetwork network1 = new StubNetwork(hub); MockEngine a1 = new MockEngine(); network1.register(a1, channel1); @@ -218,187 +229,8 @@ void testRegisterToOccupiedChannel() { try { network1.register(b1, channel1); Assertions.fail("fail! method was expected to throw an exception"); - } catch (IllegalStateException e) { - //throw new IllegalStateException("could not register to channel since its already occupied"); - } - } - - /** - * Test for Unicast one engine to all other stub networks. - */ - @Test - void testUnicastOneToAllSequentially() { - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - Entity entity = new EntityFixture(); - for (Network network : networks) { - try { - c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - Assertions.assertTrue(e1.hasReceived(entity)); - Assertions.assertFalse(e2.hasReceived(entity)); - } catch (LightChainNetworkingException e) { - Assertions.fail(); - } - } - } - - /** - * Test one engine unicasts to all others concurrently. - */ - @Test - void testUnicastOneToAllConcurrently() { - int concurrencyDegree = 9; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - Entity entity = new EntityFixture(); - Thread[] unicastThreads = new Thread[concurrencyDegree]; - int count = 0; - for (Network network : networks) { - unicastThreads[count] = new Thread(() -> { - try { - c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - if (!e1.hasReceived(entity)) { - threadError.getAndIncrement(); - } - if (e2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); - } - }); - count++; - } - for (Thread t : unicastThreads) { - t.start(); - } - try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); - } - Assertions.assertEquals(0, threadError.get()); - } - - /** - * Test one engine sends unicast to some sequentially. - */ - @Test - void testUnicastOneToSomeSequentially() { - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - Entity entity = new EntityFixture(); - int size = networks.size(); - List first = new ArrayList<>(networks.subList(0, size / 2)); - List second = new ArrayList<>(networks.subList(size / 2, size)); - Iterator firstIt = first.iterator(); - Iterator secondIt = second.iterator(); - while (firstIt.hasNext() && secondIt.hasNext()) { - Network networkFirst = firstIt.next(); - Network networkSecond = secondIt.next(); - try { - c1.unicast(entity, ((StubNetwork) networkFirst).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel2); - MockEngine m1 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel1); - MockEngine m2 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel2); - Assertions.assertTrue(e1.hasReceived(entity)); - Assertions.assertFalse(e2.hasReceived(entity) || m1.hasReceived(entity) || m2.hasReceived(entity)); - } catch (LightChainNetworkingException e) { - Assertions.fail(); - } - } - } - - /** - * Test one engine send unicast to some concurrently. - */ - @Test - void testUnicastOneToSomeConcurrently() { - int concurrencyDegree = 4; - AtomicInteger threadError = new AtomicInteger(); - CountDownLatch countDownLatch = new CountDownLatch(concurrencyDegree); - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - Entity entity = new EntityFixture(); - int size = networks.size(); - List first = new ArrayList<>(networks.subList(0, size / 2)); - List second = new ArrayList<>(networks.subList(size / 2, size)); - Iterator firstIt = first.iterator(); - Iterator secondIt = second.iterator(); - Thread[] unicastThreads = new Thread[concurrencyDegree]; - int count = 0; - while (firstIt.hasNext() && secondIt.hasNext()) { - Network networkFirst = firstIt.next(); - Network networkSecond = secondIt.next(); - unicastThreads[count] = new Thread(() -> { - try { - c1.unicast(entity, ((StubNetwork) networkFirst).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) networkFirst).getEngine(channel2); - MockEngine m1 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel1); - MockEngine m2 = (MockEngine) ((StubNetwork) networkSecond).getEngine(channel2); - if (!e1.hasReceived(entity)) { - threadError.getAndIncrement(); - } - if (e2.hasReceived(entity) || m1.hasReceived(entity) || m2.hasReceived(entity)) { - threadError.getAndIncrement(); - } - countDownLatch.countDown(); - } catch (LightChainNetworkingException e) { - threadError.getAndIncrement(); - } - }); - count++; - } - for (Thread t : unicastThreads) { - t.start(); - } - try { - boolean doneOneTime = countDownLatch.await(60, TimeUnit.SECONDS); - Assertions.assertTrue(doneOneTime); - } catch (InterruptedException e) { - Assertions.fail(); - } - Assertions.assertEquals(0, threadError.get()); - } - - /** - * Test two engines sends different entities other engines sequentially. - */ - @Test - void testUnicastOneToAll_SequentiallyTwoEngines() { - StubNetwork network1 = new StubNetwork(hub); - MockEngine a1 = new MockEngine(); - MockEngine a2 = new MockEngine(); - Conduit c1 = network1.register(a1, channel1); - Conduit c2 = network1.register(a2, channel2); - Entity entity1 = new EntityFixture(); - Entity entity2 = new EntityFixture(); - for (Network network : networks) { - try { - c1.unicast(entity1, ((StubNetwork) network).id()); - c2.unicast(entity2, ((StubNetwork) network).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); - Assertions.assertTrue(e1.hasReceived(entity1) && e2.hasReceived(entity2)); - Assertions.assertFalse(e2.hasReceived(entity1) || e1.hasReceived(entity2)); - - } catch (LightChainNetworkingException e) { - Assertions.fail(); - } + } catch (IllegalStateException ignored) { + // ignored } } } \ No newline at end of file From bd1c97116a9e819b9120cc02aa1a5a0b36176ddd Mon Sep 17 00:00:00 2001 From: Yahya Date: Tue, 12 Apr 2022 06:17:35 -0700 Subject: [PATCH 48/51] applies revisions --- .../exceptions/LightChainNetworkingException.java | 6 +++++- src/test/java/networking/Hub.java | 9 ++++++--- src/test/java/networking/StubNetwork.java | 11 ++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/model/exceptions/LightChainNetworkingException.java b/src/main/java/model/exceptions/LightChainNetworkingException.java index cff8c42a..59ab4174 100644 --- a/src/main/java/model/exceptions/LightChainNetworkingException.java +++ b/src/main/java/model/exceptions/LightChainNetworkingException.java @@ -3,4 +3,8 @@ /** * Represents a runtime exception happens on the Networking layer of LightChain. */ -public class LightChainNetworkingException extends Exception{ } +public class LightChainNetworkingException extends Exception{ + public LightChainNetworkingException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/test/java/networking/Hub.java b/src/test/java/networking/Hub.java index 760e6ba9..77a83555 100644 --- a/src/test/java/networking/Hub.java +++ b/src/test/java/networking/Hub.java @@ -29,7 +29,6 @@ public Hub() { */ public void registerNetwork(Identifier identifier, Network network) { networks.put(identifier, network); - } /** @@ -39,9 +38,13 @@ public void registerNetwork(Identifier identifier, Network network) { * @param target identifier of target. * @param channel channel on which the entity is delivered to target. */ - public void transferEntity(Entity entity, Identifier target, String channel) { + public void transferEntity(Entity entity, Identifier target, String channel) throws IllegalStateException { StubNetwork net = this.getNetwork(target); - net.receiveUnicast(entity, channel); + try { + net.receiveUnicast(entity, channel); + } catch (IllegalArgumentException ex) { + throw new IllegalStateException("target network failed on receiving unicast: " + ex.getMessage()); + } } diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index d69c963d..3a93e44f 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -47,12 +47,12 @@ public Identifier id() { * @param entity received entity * @param channel the channel through which the received entity is sent */ - public void receiveUnicast(Entity entity, String channel) { + public void receiveUnicast(Entity entity, String channel) throws IllegalArgumentException { Engine engine = getEngine(channel); try { engine.process(entity); } catch (IllegalArgumentException e) { - throw new IllegalStateException("could not process the entity" + e); + throw new IllegalStateException("could not process the entity", e); } } @@ -92,7 +92,12 @@ public Engine getEngine(String ch) { */ @Override public void unicast(Entity e, Identifier target, String channel) throws LightChainNetworkingException { - this.hub.transferEntity(e, target, channel); + try { + this.hub.transferEntity(e, target, channel); + } catch (IllegalStateException ex){ + throw new LightChainNetworkingException("stub network could not transfer entity", ex); + } + } /** From ef7a7da1cf09cd949df9d79f83ea68ff813ab76c Mon Sep 17 00:00:00 2001 From: Yahya Date: Tue, 12 Apr 2022 06:20:52 -0700 Subject: [PATCH 49/51] fixes lint --- .../exceptions/LightChainNetworkingException.java | 2 +- src/test/java/networking/StubNetwork.java | 10 +++++----- src/test/java/networking/StubNetworkEpidemicTest.java | 2 -- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/model/exceptions/LightChainNetworkingException.java b/src/main/java/model/exceptions/LightChainNetworkingException.java index 59ab4174..bef1f521 100644 --- a/src/main/java/model/exceptions/LightChainNetworkingException.java +++ b/src/main/java/model/exceptions/LightChainNetworkingException.java @@ -3,7 +3,7 @@ /** * Represents a runtime exception happens on the Networking layer of LightChain. */ -public class LightChainNetworkingException extends Exception{ +public class LightChainNetworkingException extends Exception { public LightChainNetworkingException(String message, Throwable cause) { super(message, cause); } diff --git a/src/test/java/networking/StubNetwork.java b/src/test/java/networking/StubNetwork.java index 3a93e44f..11b13e4e 100644 --- a/src/test/java/networking/StubNetwork.java +++ b/src/test/java/networking/StubNetwork.java @@ -85,8 +85,8 @@ public Engine getEngine(String ch) { /** * Sends the Entity through the Network to the remote target. * - * @param e the Entity to be sent over the network. - * @param target Identifier of the receiver. + * @param e the Entity to be sent over the network. + * @param target Identifier of the receiver. * @param channel channel on which this entity is sent. * @throws LightChainNetworkingException any unhappy path taken on sending the Entity. */ @@ -94,7 +94,7 @@ public Engine getEngine(String ch) { public void unicast(Entity e, Identifier target, String channel) throws LightChainNetworkingException { try { this.hub.transferEntity(e, target, channel); - } catch (IllegalStateException ex){ + } catch (IllegalStateException ex) { throw new LightChainNetworkingException("stub network could not transfer entity", ex); } @@ -103,7 +103,7 @@ public void unicast(Entity e, Identifier target, String channel) throws LightCha /** * Stores given Entity on the underlying Distributed Hash Table (DHT) of nodes. * - * @param e the Entity to be stored over the network. + * @param e the Entity to be stored over the network. * @param namespace namespace on which this entity is stored. * @throws LightChainDistributedStorageException any unhappy path taken on storing the Entity. */ @@ -117,7 +117,7 @@ public void put(Entity e, String namespace) throws LightChainDistributedStorageE * (DHT) of nodes. * * @param identifier identifier of the entity to be retrieved. - * @param namespace the namespace on which this query is resolved. + * @param namespace the namespace on which this query is resolved. * @return the retrieved entity or null if it does not exist. * @throws LightChainDistributedStorageException any unhappy path taken on retrieving the Entity. */ diff --git a/src/test/java/networking/StubNetworkEpidemicTest.java b/src/test/java/networking/StubNetworkEpidemicTest.java index 299a41ec..7b3e52aa 100644 --- a/src/test/java/networking/StubNetworkEpidemicTest.java +++ b/src/test/java/networking/StubNetworkEpidemicTest.java @@ -135,7 +135,6 @@ void testUnicastOneToSomeSequentially() { } } - // checks only first half of network should receive it. for (int i = 0; i < networks.size(); i++) { // first half of networks should receive unicast @@ -167,7 +166,6 @@ void testUnicastOneToSomeConcurrently() { Entity entity = new EntityFixture(); Thread[] unicastThreads = new Thread[concurrencyDegree]; - // concurrently unicasts to the first half of network for (int i = 0; i < concurrencyDegree; i++) { int finalI = i; From 196ebbddc4161ae8481d57eb89ab2cea740adef9 Mon Sep 17 00:00:00 2001 From: Yahya Date: Tue, 12 Apr 2022 06:27:07 -0700 Subject: [PATCH 50/51] applies revision --- .../java/networking/StubNetworkEpidemicTest.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/test/java/networking/StubNetworkEpidemicTest.java b/src/test/java/networking/StubNetworkEpidemicTest.java index 7b3e52aa..601ab61b 100644 --- a/src/test/java/networking/StubNetworkEpidemicTest.java +++ b/src/test/java/networking/StubNetworkEpidemicTest.java @@ -44,18 +44,16 @@ void setup() { */ @Test void testUnicastOneToAllSequentially() { - Hub hub = new Hub(); - - StubNetwork network1 = new StubNetwork(hub); + StubNetwork network1 = new StubNetwork(this.hub); MockEngine a1 = new MockEngine(); Conduit c1 = network1.register(a1, channel1); Entity entity = new EntityFixture(); - for (Network network : networks) { + for (int i = 0; i < networks.size(); i++) { try { - c1.unicast(entity, ((StubNetwork) network).id()); - MockEngine e1 = (MockEngine) ((StubNetwork) network).getEngine(channel1); - MockEngine e2 = (MockEngine) ((StubNetwork) network).getEngine(channel2); + c1.unicast(entity, networks.get(i).id()); + MockEngine e1 = (MockEngine) networks.get(i).getEngine(channel1); + MockEngine e2 = (MockEngine) networks.get(i).getEngine(channel2); // only engine on channel-1 should receive the entity. Assertions.assertTrue(e1.hasReceived(entity)); From a9b9f5a95ac9089dbe317aca32873ecaf0982614 Mon Sep 17 00:00:00 2001 From: Yahya Date: Tue, 12 Apr 2022 06:28:47 -0700 Subject: [PATCH 51/51] fixes lint --- src/test/java/networking/StubNetworkEpidemicTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/networking/StubNetworkEpidemicTest.java b/src/test/java/networking/StubNetworkEpidemicTest.java index 601ab61b..9bd0766a 100644 --- a/src/test/java/networking/StubNetworkEpidemicTest.java +++ b/src/test/java/networking/StubNetworkEpidemicTest.java @@ -8,7 +8,6 @@ import model.Entity; import model.exceptions.LightChainNetworkingException; import network.Conduit; -import network.Network; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;