-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHubUsageTests.java
More file actions
250 lines (206 loc) · 9.01 KB
/
HubUsageTests.java
File metadata and controls
250 lines (206 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package net.sharksystem.hub;
import net.sharksystem.SharkException;
import net.sharksystem.asap.ASAPEncounterManagerImpl;
import net.sharksystem.asap.ASAPException;
import net.sharksystem.asap.apps.testsupport.ASAPTestPeerFS;
import net.sharksystem.hub.peerside.*;
import net.sharksystem.hub.hubside.ASAPTCPHub;
import net.sharksystem.utils.fs.FSUtils;
import net.sharksystem.utils.testsupport.TestHelper;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import static net.sharksystem.hub.TestConstants.*;
public class HubUsageTests {
String ROOT_FOLDER = TestConstants.ROOT_DIRECTORY + HubUsageTests.class.getSimpleName() + "/";
static int portNumber = 6907;
static int getPort() {
return portNumber++;
}
@Test
public void usageSharedConnection() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
true,
false,
"NON_CAN_TCP",
"YZ",
false);
}
@Test
public void usageNewConnection() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
true,
true,
"BOTH_CAN_TCP",
"YZ",
false);
}
@Test
public void usageNewConnection2_0() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
true,
false,
"ALICE_CAN_TCP",
"YZ",
false);
}
@Test
public void usageNewConnection2_1() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
true,
false,
"A",
"Y",
false);
}
@Test
public void usageNewConnection2_2() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
true,
false,
"A",
"XYZ",
false);
}
@Test
public void usageNewConnection3_0() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
false,
true,
"BOB_CAN_TCP",
"YY",
false);
/*
Die Länge der Nachrichten ist richtig. Da kommen zu viele Nullen an. Aber die Anzahl ist richtig.
Ist das ein Problem des Lesens oder schreibens?
*/
}
@Test
public void usageNewConnection3_1() throws IOException, InterruptedException, ASAPException {
this.runUsageTest(
false,
true,
"A",
"Y",
false);
}
public void runUsageTest(
boolean aliceCanCreateTCPConnections,
boolean bobCanCreateTCPConnections,
String messageA, String messageB,
boolean pureBytes) throws IOException, InterruptedException, ASAPException {
int maxTimeInSeconds = Connector.DEFAULT_TIMEOUT_IN_MILLIS / 1000;
maxTimeInSeconds = maxTimeInSeconds > 0 ? maxTimeInSeconds : 1;
int specificPort = getPort();
CharSequence host = "localhost";
ASAPTCPHub hub = new ASAPTCPHub(specificPort, true);
hub.setPortRange(7000, 9000); // optional - required to configure a firewall
hub.setMaxIdleConnectionInSeconds(maxTimeInSeconds);
new Thread(hub).start();
HubConnector aliceHubConnector = SharedTCPChannelConnectorPeerSide.createTCPHubConnector(host, specificPort);
HubConnectorTester aliceListener = new HubConnectorTester(ALICE_ID, messageA, messageB, pureBytes);
aliceHubConnector.addListener(aliceListener);
aliceHubConnector.connectHub(ALICE_ID, aliceCanCreateTCPConnections);
Thread.sleep(100);
Collection<CharSequence> peerNames = aliceHubConnector.getPeerIDs();
Assert.assertEquals(0, peerNames.size());
HubConnectorTester bobListener = new HubConnectorTester(BOB_ID, messageA, messageB, pureBytes);
HubConnector bobHubConnector = SharedTCPChannelConnectorPeerSide.createTCPHubConnector(host, specificPort);
bobHubConnector.addListener(bobListener);
bobHubConnector.connectHub(BOB_ID, bobCanCreateTCPConnections);
Thread.sleep(100);
peerNames = bobHubConnector.getPeerIDs();
Assert.assertEquals(1, peerNames.size());
aliceHubConnector.syncHubInformation();
Thread.sleep(100);
peerNames = aliceHubConnector.getPeerIDs();
Assert.assertEquals(1, peerNames.size());
// bobHubConnector.syncHubInformation();
/// Alice meets Bob
aliceHubConnector.connectPeer(BOB_ID);
//Thread.sleep(Long.MAX_VALUE);
Thread.sleep(maxTimeInSeconds * 1000);
System.out.println("************************ back from sleep ****************************");
//Thread.sleep(Long.MAX_VALUE);
Assert.assertEquals(1, aliceListener.numberNotifications());
Assert.assertEquals(1, bobListener.numberNotifications());
Assert.assertFalse(aliceListener.error);
Assert.assertFalse(bobListener.error);
System.out.println("************************ alice connector disconnect ****************************");
aliceHubConnector.disconnectHub();
Thread.sleep(maxTimeInSeconds * 1000);
Thread.sleep(maxTimeInSeconds * 1000);
System.out.println("************************ bob connector sync ****************************");
bobHubConnector.syncHubInformation();
Thread.sleep(maxTimeInSeconds * 1000);
peerNames = bobHubConnector.getPeerIDs();
for(CharSequence name : peerNames) {
System.out.println(name);
}
Assert.assertEquals(0, peerNames.size());
}
@Test
public void usageHubManagerAndTwoPeers_MultiChannel_True() throws IOException, InterruptedException, SharkException {
this.usageHubManagerAndTwoPeers(true);
}
@Test
public void usageHubManagerAndTwoPeers_MultiChannel_False() throws IOException, InterruptedException, SharkException {
this.usageHubManagerAndTwoPeers(false);
}
public void usageHubManagerAndTwoPeers(boolean multichannel) throws IOException, InterruptedException, SharkException {
int hubPort = TestHelper.getPortNumber();
HubConnectorDescription localHostHubDescription =
new TCPHubConnectorDescriptionImpl("localhost", hubPort, multichannel);
Collection<HubConnectorDescription> hubDescriptions = new ArrayList<>();
hubDescriptions.add(localHostHubDescription);
// launch asap hub
ASAPTCPHub hub = ASAPTCPHub.startTCPHubThread(hubPort, multichannel, MAX_IDLE_IN_SECONDS);
// give it moment to settle in
Thread.sleep(100);
Collection<CharSequence> formats = new ArrayList<>();
formats.add(FORMAT);
//////////////////////////////// setup Alice
FSUtils.removeFolder(ALICE_ROOTFOLDER);
ASAPTestPeerFS aliceASAPPeer = new ASAPTestPeerFS(ALICE_ID, formats);
// send a message
aliceASAPPeer.sendASAPMessage(FORMAT, URI, "Hi there".getBytes());
////////////// setup Bob
FSUtils.removeFolder(BOB_ROOTFOLDER);
ASAPTestPeerFS bobASAPPeer = new ASAPTestPeerFS(BOB_ID, formats);
DummyMessageReceivedListener bobListener = new DummyMessageReceivedListener(BOB_ID);
bobASAPPeer.addASAPMessageReceivedListener(FORMAT, bobListener);
///////////////////// connect to hub - Alice
// setup encounter manager with a connection handler
ASAPEncounterManagerImpl aliceEncounterManager =
new ASAPEncounterManagerImpl(aliceASAPPeer);
// setup hub manager
ASAPHubManager aliceHubManager = ASAPHubManagerImpl.createASAPHubManager(aliceEncounterManager);
// connect with bulk import
aliceHubManager.connectASAPHubs(hubDescriptions, aliceASAPPeer, true);
Thread.sleep(100);
aliceHubManager.forceSyncWithHubs();
///////////////////// connect to hub - Bob
// setup encounter manager with a connection handler
ASAPEncounterManagerImpl bobEncounterManager =
new ASAPEncounterManagerImpl(bobASAPPeer);
// setup hub manager
// ASAPHubManager bobHubManager = ASAPHubManagerImpl.startASAPHubManager(bobEncounterManager);
ASAPHubManager bobHubManager = ASAPHubManagerImpl.createASAPHubManager(bobEncounterManager);
// connect to hub - Bob
bobHubManager.connectASAPHubs(hubDescriptions, bobASAPPeer, true);
Thread.sleep(100);
bobHubManager.forceSyncWithHubs();
// give them moment to exchange data
Thread.sleep(500);
//Thread.sleep(Long.MAX_VALUE);
System.out.println("slept a moment");
// Bob received a message?
Assert.assertEquals(1, bobListener.counter);
// shut down
hub.kill();
aliceHubManager.kill();
bobHubManager.kill();
}
}