|
| 1 | +package bugreports; |
| 2 | + |
| 3 | +import net.sharksystem.asap.ASAPException; |
| 4 | +import net.sharksystem.asap.ASAPPeerFS; |
| 5 | +import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS; |
| 6 | +import net.sharksystem.asap.mockAndTemplates.ASAPMessageReceivedListenerExample; |
| 7 | +import net.sharksystem.asap.mockAndTemplates.TestUtils; |
| 8 | +import org.apache.commons.io.FileUtils; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collection; |
| 17 | + |
| 18 | +public class BugReportASAPPeerFS { |
| 19 | + |
| 20 | + ASAPPeerFSTestHelper testHelper; |
| 21 | + |
| 22 | + private ASAPTestPeerFS aliceTestPeer, bobTestPeer; |
| 23 | + |
| 24 | + private String TEST_URI = "sn://test"; |
| 25 | + |
| 26 | + private Collection<CharSequence> formats; |
| 27 | + |
| 28 | + private CharSequence format = "ASAPTest"; |
| 29 | + |
| 30 | + private static final int PORT = 7777; |
| 31 | + |
| 32 | + private static int port = 0; |
| 33 | + |
| 34 | + static int getPortNumber() { |
| 35 | + if (BugReportASAPPeerFS.port == 0) { |
| 36 | + BugReportASAPPeerFS.port = PORT; |
| 37 | + } else { |
| 38 | + BugReportASAPPeerFS.port++; |
| 39 | + } |
| 40 | + |
| 41 | + return BugReportASAPPeerFS.port; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void setUp() throws IOException, ASAPException { |
| 47 | + // delete directory „testPeerFS” to prevent errors when running twice |
| 48 | + FileUtils.deleteDirectory(new File("testPeerFS")); |
| 49 | + |
| 50 | + // root folder of all ASAPPeers |
| 51 | + String rootfolder = "./testPeerFS"; |
| 52 | + |
| 53 | + formats = new ArrayList<>(); |
| 54 | + formats.add(format); |
| 55 | + |
| 56 | + // helper class with usefull testing functions |
| 57 | + testHelper = new ASAPPeerFSTestHelper(rootfolder, format); |
| 58 | + |
| 59 | + this.aliceTestPeer = new ASAPTestPeerFS("ALICE", rootfolder + "/ALICE", formats); |
| 60 | + this.bobTestPeer = new ASAPTestPeerFS("BOB", rootfolder + "/BOB", formats); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void singleEncounter_differentURIs() throws InterruptedException, IOException, ASAPException { |
| 65 | + String uriAlice = "Tier"; |
| 66 | + String uriBob = "Wasser"; |
| 67 | + |
| 68 | + simpleEncounterWithMessageExchange(uriAlice, uriBob); |
| 69 | + |
| 70 | + // chunks should be created for each send message |
| 71 | + // this will only be checked on single encounter tests, as in further tests this gets more and more tedious to do |
| 72 | + Assertions.assertTrue(aliceTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriAlice, 0)); |
| 73 | + Assertions.assertTrue(bobTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriBob, 0)); |
| 74 | + Assertions.assertTrue(aliceTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriBob, 1)); |
| 75 | + Assertions.assertTrue(bobTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriAlice, 1)); |
| 76 | + |
| 77 | + // each message should have created a new era, so there should be a meta and content file in each subfolder |
| 78 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uriBob, 0)); |
| 79 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uriAlice, 0)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void singleEncounter_sameURIs() throws InterruptedException, IOException, ASAPException { |
| 84 | + String uriAlice = "Tier"; |
| 85 | + String uriBob = "Tier"; |
| 86 | + |
| 87 | + simpleEncounterWithMessageExchange(uriAlice, uriBob); |
| 88 | + |
| 89 | + // chunks should be created for each send message |
| 90 | + // this will only be checked on single encounter tests, as in further tests this gets more and more tedious to do |
| 91 | + Assertions.assertTrue(aliceTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriAlice, 0)); |
| 92 | + Assertions.assertTrue(bobTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriBob, 0)); |
| 93 | + Assertions.assertTrue(aliceTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriBob, 1)); |
| 94 | + Assertions.assertTrue(bobTestPeer.getASAPStorage(format).getChunkStorage().existsChunk(uriAlice, 1)); |
| 95 | + |
| 96 | + // each message should have created a new era, so there should be a meta and content file in each subfolder |
| 97 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uriBob, 0)); |
| 98 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uriAlice, 0)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void multiEncounter_sameURIs() throws InterruptedException, IOException, ASAPException { |
| 103 | + |
| 104 | + String uri = "Eis"; |
| 105 | + |
| 106 | + simpleEncounterWithMessageExchange(uri, uri); |
| 107 | + |
| 108 | + simpleEncounterWithMessageExchange(uri, uri); |
| 109 | + |
| 110 | + simpleEncounterWithMessageExchange(uri, uri); |
| 111 | + |
| 112 | + simpleEncounterWithMessageExchange(uri, uri); |
| 113 | + |
| 114 | + // each message should have created a new era, so there should be a meta and content file in each subfolder |
| 115 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uri, 0)); |
| 116 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uri, 1)); |
| 117 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uri, 2)); |
| 118 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uri, 3)); |
| 119 | + |
| 120 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uri, 0)); |
| 121 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uri, 1)); |
| 122 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uri, 2)); |
| 123 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uri, 3)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void multiEncounter_partiallyDifferentURIs() throws InterruptedException, IOException, ASAPException { |
| 128 | + |
| 129 | + String uri = "Eis"; |
| 130 | + |
| 131 | + // sending different uris back and forth now breaks it |
| 132 | + simpleEncounterWithMessageExchange("Tiger", uri); |
| 133 | + |
| 134 | + simpleEncounterWithMessageExchange(uri, "Elefant"); |
| 135 | + |
| 136 | + simpleEncounterWithMessageExchange("Hallo", uri); |
| 137 | + |
| 138 | + simpleEncounterWithMessageExchange(uri, "Hallo"); |
| 139 | + |
| 140 | + // all eras SHOULD exists, but some are missing! |
| 141 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uri, 0)); |
| 142 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", "Elefant", 1)); |
| 143 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", uri, 2)); |
| 144 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", "Hallo", 3)); |
| 145 | + |
| 146 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", "Tiger", 0)); |
| 147 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uri, 1)); |
| 148 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", "Hallo", 2)); |
| 149 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", uri, 3)); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void multiEncounter_completelyDifferentURIs() throws InterruptedException, IOException, ASAPException { |
| 154 | + |
| 155 | + String uriAlice = "aliceUri1"; |
| 156 | + String uriBob = "bobUri1"; |
| 157 | + |
| 158 | + simpleEncounterWithMessageExchange(uriAlice, uriBob); |
| 159 | + |
| 160 | + uriAlice = "aliceUri2"; |
| 161 | + uriBob = "bobUri2"; |
| 162 | + simpleEncounterWithMessageExchange(uriAlice, uriBob); |
| 163 | + |
| 164 | + uriAlice = "aliceUri3"; |
| 165 | + uriBob = "bobUri3"; |
| 166 | + simpleEncounterWithMessageExchange(uriAlice, uriBob); |
| 167 | + |
| 168 | + uriAlice = "aliceUri4"; |
| 169 | + uriBob = "bobUri4"; |
| 170 | + simpleEncounterWithMessageExchange(uriAlice, uriBob); |
| 171 | + |
| 172 | + // we expect ALL of the uris to exist, but they don't. |
| 173 | + // only two messages of bob and one message of alice gets transmitted, then it stops transmitting for the rest.. |
| 174 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", "bobUri1", 0)); |
| 175 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", "bobUri2", 1)); |
| 176 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", "bobUri3", 2)); |
| 177 | + Assertions.assertTrue(testHelper.senderEraShouldExist("ALICE", "BOB", "bobUri4", 3)); |
| 178 | + |
| 179 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", "aliceUri1", 0)); |
| 180 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", "aliceUri2", 1)); |
| 181 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", "aliceUri3", 2)); |
| 182 | + Assertions.assertTrue(testHelper.senderEraShouldExist("BOB", "ALICE", "aliceUri4", 3)); |
| 183 | + } |
| 184 | + |
| 185 | + // sends messages with given uri, starts and then stops the encounter |
| 186 | + // message content is irrelevant, we don't test for it |
| 187 | + public void simpleEncounterWithMessageExchange(String uriAlice, String uriBob) |
| 188 | + throws IOException, ASAPException, InterruptedException { |
| 189 | + |
| 190 | + // simulate ASAP first encounter with full ASAP protocol stack and engines |
| 191 | + System.out.println("+++++++++++++++++++ 1st encounter starts soon ++++++++++++++++++++"); |
| 192 | + Thread.sleep(50); |
| 193 | + |
| 194 | + // setup message received listener - this should be replaced with your code - you implement a listener. |
| 195 | + ASAPMessageReceivedListenerExample aliceMessageReceivedListenerExample = |
| 196 | + new ASAPMessageReceivedListenerExample(); |
| 197 | + |
| 198 | + aliceTestPeer.addASAPMessageReceivedListener(format, aliceMessageReceivedListenerExample); |
| 199 | + |
| 200 | + // example - this should be produced by your application |
| 201 | + byte[] serializedData = TestUtils.serializeExample(42, "from alice", true); |
| 202 | + |
| 203 | + aliceTestPeer.sendASAPMessage(format, uriAlice, serializedData); |
| 204 | + |
| 205 | + ///////////////// BOB ////////////////////////////////////////////////////////////// |
| 206 | + |
| 207 | + // this should be replaced with your code - you must implement a listener. |
| 208 | + ASAPMessageReceivedListenerExample asapMessageReceivedListenerExample = |
| 209 | + new ASAPMessageReceivedListenerExample(); |
| 210 | + |
| 211 | + // register your listener (or that mock) with asap connection mock |
| 212 | + bobTestPeer.addASAPMessageReceivedListener(format, asapMessageReceivedListenerExample); |
| 213 | + |
| 214 | + // bob writes something |
| 215 | + bobTestPeer.sendASAPMessage(format, uriBob, |
| 216 | + TestUtils.serializeExample(43, "from bob", false)); |
| 217 | + bobTestPeer.sendASAPMessage(format, uriBob, |
| 218 | + TestUtils.serializeExample(44, "from bob again", false)); |
| 219 | + |
| 220 | + // give your app a moment to process |
| 221 | + Thread.sleep(500); |
| 222 | + // start actual encounter |
| 223 | + aliceTestPeer.startEncounter(getPortNumber(), bobTestPeer); |
| 224 | + |
| 225 | + // give your app a moment to process |
| 226 | + Thread.sleep(1000); |
| 227 | + // stop encounter |
| 228 | + bobTestPeer.stopEncounter(aliceTestPeer); |
| 229 | + // give your app a moment to process |
| 230 | + Thread.sleep(1000); |
| 231 | + } |
| 232 | + |
| 233 | +} |
0 commit comments