Skip to content

Commit 1dfaf0b

Browse files
committed
snapshot - working on orchestrated tests; looks much better, going to make some real tests.
1 parent 4c22dd7 commit 1dfaf0b

3 files changed

Lines changed: 172 additions & 92 deletions

File tree

src/net/sharksystem/ui/messenger/cli/SharkNetMessengerAppSupportingDistributedTesting.java

Lines changed: 136 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515
import java.io.PrintStream;
16+
import java.net.ServerSocket;
1617
import java.net.UnknownHostException;
1718
import java.util.*;
1819

@@ -144,66 +145,72 @@ public void becomeTestPeer() throws IOException, SharkException {
144145
private List<TestScriptDescription> handledScripts = new ArrayList<>();
145146
// peers willing to execute test script sent its environment description
146147
public void receivedTestScript(CharSequence testScriptChannel) {
147-
if(!this.beTestPeer) {
148-
this.tellUI("test script received but no tester peer - ignored");
149-
return;
150-
}
151-
// add information of this new volunteer and see if we have enough participant for one new test
152-
try {
153-
SharkNetMessageList scriptMessages =
154-
this.getSharkMessengerComponent().getChannel(testScriptChannel).getMessages();
155-
for (int scriptIndex = this.lastReceivedScriptIndex + 1; scriptIndex < scriptMessages.size(); scriptIndex++) {
156-
this.lastReceivedScriptIndex = scriptIndex; // update each round - before possible exceptions
157-
158-
SharkNetMessage testSharkMessage = scriptMessages.getSharkMessage(scriptIndex, false);
159-
TestScriptDescription testScriptDescription =
160-
new TestScriptDescription(testSharkMessage.getContent());
161-
162-
// for me?
163-
if(testScriptDescription.peerID.equalsIgnoreCase(this.getSharkPeer().getPeerID().toString())) {
164-
this.tellUI("test script received - prepare script runner" + testScriptDescription);
165-
Log.writeLog(this, "test script addressed to me received: " + testScriptDescription);
166-
167-
// this is script is for - alright. Is it a copy?
168-
boolean copy = false;
169-
for(TestScriptDescription oldScript : this.handledScripts) {
170-
Log.writeLog(this, "copy? " + testScriptDescription + " | " + oldScript);
171-
if(TestScriptDescription.same(oldScript, testScriptDescription)) {
172-
copy = true;
173-
break;
148+
// less likely but more than one script could be received
149+
synchronized (SharkNetMessengerAppSupportingDistributedTesting.this) {
150+
if (!this.beTestPeer) {
151+
this.tellUI("test script received but no tester peer - ignored");
152+
return;
153+
}
154+
// add information of this new volunteer and see if we have enough participant for one new test
155+
try {
156+
SharkNetMessageList scriptMessages =
157+
this.getSharkMessengerComponent().getChannel(testScriptChannel).getMessages();
158+
for (int scriptIndex = this.lastReceivedScriptIndex + 1; scriptIndex < scriptMessages.size(); scriptIndex++) {
159+
this.lastReceivedScriptIndex = scriptIndex; // update each round - before possible exceptions
160+
161+
SharkNetMessage testSharkMessage = scriptMessages.getSharkMessage(scriptIndex, false);
162+
TestScriptDescription testScriptDescription =
163+
new TestScriptDescription(testSharkMessage.getContent());
164+
165+
// for me?
166+
if (testScriptDescription.peerID.equalsIgnoreCase(this.getSharkPeer().getPeerID().toString())) {
167+
this.tellUI("test script received - prepare script runner" + testScriptDescription);
168+
Log.writeLog(this, "test script addressed to me received: " + testScriptDescription);
169+
170+
// this is script is for - alright. Is it a copy?
171+
boolean copy = false;
172+
for (TestScriptDescription oldScript : this.handledScripts) {
173+
Log.writeLog(this, "copy? " + testScriptDescription + " | " + oldScript);
174+
if (TestScriptDescription.same(oldScript, testScriptDescription)) {
175+
copy = true;
176+
break;
177+
}
178+
}
179+
if (copy) {
180+
String log = "received copy of an already handled test case, ignore: " + testScriptDescription;
181+
Log.writeLog(this, log);
182+
this.tellUI(log);
183+
continue;
174184
}
175-
}
176-
if(copy) {
177-
String log = "received copy of an already handled test case, ignore: " + testScriptDescription;
178-
Log.writeLog(this, log);
179-
this.tellUI(log);
180-
continue;
181-
}
182185

183-
/*
184-
// produce test running thread
185-
new ScriptRunnerThread(
186-
Integer.toString(testScriptDescription.peerIndex),
187-
Integer.toString(testScriptDescription.testNumber),
188-
testScriptDescription.script).start();
189-
*/
190-
// produce test running process
191-
new ScriptRunnerProcess(
186+
/*
187+
// produce test running thread
188+
new ScriptRunnerThread(
192189
Integer.toString(testScriptDescription.peerIndex),
193190
Integer.toString(testScriptDescription.testNumber),
194191
testScriptDescription.script).start();
195-
this.tellUI("running script: " + testScriptDescription.script);
196-
Log.writeLog(this, "script running, remember to avoid redo it: " + testScriptDescription);
197-
this.handledScripts.add(testScriptDescription);
198-
} else {
199-
this.tellUI("test script received, not for me though. Index: " + testScriptDescription);
200-
Log.writeLog(this, "test script received, not for me: " + testScriptDescription);
192+
*/
193+
// produce test running process
194+
new ScriptRunnerProcess(
195+
Integer.toString(testScriptDescription.peerIndex),
196+
Integer.toString(testScriptDescription.testNumber),
197+
testScriptDescription.script).start();
198+
this.tellUI("running script: " + testScriptDescription.script);
199+
Log.writeLog(this, "script running, remember to avoid redo it: " + testScriptDescription);
200+
this.handledScripts.add(testScriptDescription);
201+
}
202+
/*
203+
else {
204+
this.tellUI("test script received, not for me though.");
205+
Log.writeLog(this, "test script received, not for me: " + testScriptDescription);
206+
}
207+
*/
201208
}
209+
} catch (IOException | SharkException e) {
210+
String log = "problems handling test script received channel: " + e.getLocalizedMessage();
211+
this.tellUIError(log);
212+
Log.writeLogErr(this, log);
202213
}
203-
} catch (IOException | SharkException e) {
204-
String log = "problems handling test script received channel: " + e.getLocalizedMessage();
205-
this.tellUIError(log);
206-
Log.writeLogErr(this, log);
207214
}
208215
}
209216

@@ -255,37 +262,39 @@ public void orchestrateTest(List<PeerHostingEnvironmentDescription> requiredPeer
255262
private int lastScriptRQIndex = -1;
256263
// peers willing to execute test script sent its environment description
257264
public void receivedScriptRQ(CharSequence scriptRQChannel) {
258-
if(!this.beTestOrchestrator) {
259-
this.tellUI("script request received - ignore - not a test orchestrator");
260-
return;
261-
} else {
262-
Log.writeLog(this, "script request received - try to stage a test");
263-
this.tellUI("script request received - try to stage a test");
264-
}
265-
// add information of this new volunteer and see if we have enough participant for one new test
266-
synchronized (this) {
267-
try {
268-
SharkNetMessageList rqMessages =
269-
this.getSharkMessengerComponent().getChannel(scriptRQChannel).getMessages();
270-
for (int rqIndex = this.lastScriptRQIndex + 1; rqIndex < rqMessages.size(); rqIndex++) {
271-
this.lastScriptRQIndex = rqIndex; // update each round - before possible exceptions
272-
273-
SharkNetMessage rqSharkMessage = rqMessages.getSharkMessage(rqIndex, false);
274-
PeerHostingEnvironmentDescription peerHostDescription =
275-
new PeerHostingEnvironmentDescription(rqSharkMessage.getContent());
276-
277-
// add or replace information
278-
this.availablePeers.put(peerHostDescription.peerID, peerHostDescription);
279-
String log = "added volunteering peer: " + peerHostDescription;
280-
this.tellUI(log);
281-
Log.writeLog(this, log);
282-
283-
// try to set up a test(s).
284-
this.stageTests();
265+
synchronized (SharkNetMessengerAppSupportingDistributedTesting.this) {
266+
if (!this.beTestOrchestrator) {
267+
this.tellUI("script request received - ignore - not a test orchestrator");
268+
return;
269+
} else {
270+
Log.writeLog(this, "script request received - try to stage a test");
271+
this.tellUI("script request received - try to stage a test");
272+
}
273+
// add information of this new volunteer and see if we have enough participant for one new test
274+
synchronized (this) {
275+
try {
276+
SharkNetMessageList rqMessages =
277+
this.getSharkMessengerComponent().getChannel(scriptRQChannel).getMessages();
278+
for (int rqIndex = this.lastScriptRQIndex + 1; rqIndex < rqMessages.size(); rqIndex++) {
279+
this.lastScriptRQIndex = rqIndex; // update each round - before possible exceptions
280+
281+
SharkNetMessage rqSharkMessage = rqMessages.getSharkMessage(rqIndex, false);
282+
PeerHostingEnvironmentDescription peerHostDescription =
283+
new PeerHostingEnvironmentDescription(rqSharkMessage.getContent());
284+
285+
// add or replace information
286+
this.availablePeers.put(peerHostDescription.peerID, peerHostDescription);
287+
String log = "added volunteering peer: " + peerHostDescription;
288+
this.tellUI(log);
289+
Log.writeLog(this, log);
290+
291+
// try to set up a test(s).
292+
this.stageTests();
293+
}
294+
} catch (SharkNetMessengerException | IOException | ASAPException e) {
295+
this.tellUIError("problems handling script RQ channel: " + e.getLocalizedMessage());
296+
Log.writeLogErr(this, "problems handling script RQ channel: " + e.getLocalizedMessage());
285297
}
286-
} catch (SharkNetMessengerException | IOException | ASAPException e) {
287-
this.tellUIError("problems handling script RQ channel: " + e.getLocalizedMessage());
288-
Log.writeLogErr(this, "problems handling script RQ channel: " + e.getLocalizedMessage());
289298
}
290299
}
291300
}
@@ -340,15 +349,13 @@ private void stageTests() throws UnknownHostException {
340349
* Test performed - results are received by test orchestrator
341350
* */
342351
private class OrchestratedTestLauncher extends Thread {
343-
public static int nextTestNumber = 0;
344-
public int testNumber = 0;
345352
final OrchestratedTest test2run;
346-
public static final int ORCHESTRATOR_PORT = 1984;
353+
public static final int FIRST_ORCHESTRATOR_PORT = 1000;
347354
public static final String SETTLED_TAG_PREAMBLE = "peerSettled_";
348355
public static final String ORCHESTRATOR_PEER_NAME = "orchest";
349356
public static final String LAUNCH_TEST_TAG_PREAMBLE = "launchTest_";
350357
public final static int FINAL_WAIT_PERIODE_BEFORE_LAUNCH = 1000;
351-
public final static int MAX_TEST_DURATION_IN_MILLIS = 10000;
358+
public final static int MAX_TEST_DURATION_IN_MILLIS = 1000*60*2; // 2minutes
352359

353360
private static String scriptStartOrchestrator_SyncWithPeers = null;
354361
private static String scriptStartPeer_SyncWithOrchestator = null;
@@ -357,17 +364,51 @@ private class OrchestratedTestLauncher extends Thread {
357364
TestLanguageCompiler.CLI_TIME_BOMB + TestLanguageCompiler.CLI_SPACE
358365
+ MAX_TEST_DURATION_IN_MILLIS + TestLanguageCompiler.LANGUAGE_SEPARATOR;
359366

367+
public static int nextTestNumber = 0;
368+
public int testNumber = 0;
369+
370+
// sync with other orchestrated tests
371+
private static synchronized int getAvailablePortNumber() {
372+
int port = FIRST_ORCHESTRATOR_PORT;
373+
374+
ServerSocket srvSocket = null;
375+
while(srvSocket == null && port < 65535) {
376+
try {
377+
srvSocket = new ServerSocket(port);
378+
// got a port - close port - will be opened again in a few millis
379+
srvSocket.close();
380+
return port;
381+
} catch (IOException e) {
382+
// taken
383+
}
384+
port++;
385+
}
386+
387+
if(srvSocket == null) {
388+
System.err.println("your system uses any thinkable orchestratorPort - amazing - give up");
389+
Log.writeLogErr(SharkNetMessengerAppSupportingDistributedTesting.OrchestratedTestLauncher.class,
390+
"not a single orchestratorPort available on this system - hard to imagine - give up");
391+
System.exit(1);
392+
}
393+
// never reach this point
394+
return -1;
395+
}
396+
360397
OrchestratedTestLauncher(OrchestratedTest test2run) throws UnknownHostException {
361398
this.test2run = test2run;
362399
synchronized (OrchestratedTestLauncher.class) {
363400
this.testNumber = nextTestNumber++;
364401
}
402+
403+
// find available orchestratorPort
404+
int portNumber4ThisTest = getAvailablePortNumber();
405+
365406
// init class member
366407
if(OrchestratedTestLauncher.scriptStartOrchestrator_SyncWithPeers == null) {
367408
StringBuilder sb = new StringBuilder();
368409
sb.append(TestLanguageCompiler.CLI_OPEN_TCP);
369410
sb.append(TestLanguageCompiler.CLI_SPACE);
370-
sb.append(ORCHESTRATOR_PORT);
411+
sb.append(portNumber4ThisTest);
371412
sb.append(TestLanguageCompiler.LANGUAGE_SEPARATOR);
372413
OrchestratedTestLauncher.scriptStartOrchestrator_SyncWithPeers = sb.toString();
373414
}
@@ -378,7 +419,7 @@ private class OrchestratedTestLauncher extends Thread {
378419
sb.append(TestLanguageCompiler.CLI_SPACE);
379420
sb.append(SharkNetMessengerAppSupportingDistributedTesting.this.getLocalIPAddress());
380421
sb.append(TestLanguageCompiler.CLI_SPACE);
381-
sb.append(ORCHESTRATOR_PORT);
422+
sb.append(portNumber4ThisTest);
382423
sb.append(TestLanguageCompiler.LANGUAGE_SEPARATOR);
383424
OrchestratedTestLauncher.scriptStartPeer_SyncWithOrchestator = sb.toString();
384425
}
@@ -401,6 +442,11 @@ public void run() {
401442

402443
// produce orchestrator script - sync and collect data
403444
StringBuilder sb = new StringBuilder();
445+
SharkNetMessengerAppSupportingDistributedTesting.
446+
this.tellUI("WARNING: set timeBomb for orchestrator peer. Adopt this code for longer lasting test scenarios. <<<< WARNING");
447+
sb.append(TestLanguageCompiler.CLI_TIME_BOMB + TestLanguageCompiler.CLI_SPACE
448+
+ MAX_TEST_DURATION_IN_MILLIS*10 + TestLanguageCompiler.LANGUAGE_SEPARATOR);
449+
404450
sb.append(OrchestratedTestLauncher.scriptStartOrchestrator_SyncWithPeers);
405451
// wait for each peer to settle
406452
for(int peerIndex = 0; peerIndex < this.test2run.scripts.size(); peerIndex++) {
@@ -431,7 +477,7 @@ public void run() {
431477
sb = new StringBuilder();
432478
// set time bomb to avoid orphan processes
433479
SharkNetMessengerAppSupportingDistributedTesting.
434-
this.tellUI("WARNING: set timeBock in each peer script. Adopt this code for longer lasting test scenarios. <<<< WARNING");
480+
this.tellUI("WARNING: set timeBomb in each peer script. Adopt this code for longer lasting test scenarios. <<<< WARNING");
435481
sb.append(scriptSetTimeBomb);
436482
// add open connection to orchestrator
437483
sb.append(scriptStartPeer_SyncWithOrchestator);

src/net/sharksystem/ui/messenger/cli/commands/testing/UICommandOrchestrateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ protected void execute() throws Exception {
2929
String script0_B = "sendMessage HiFromTest;wait 1000;lsMessages;";
3030

3131
// going to launch new processes rather new threads: orchestrator (not yet); test peers (not yet)
32-
String script1_A = "wait 5000;connectTCP localhost 9999;release A1;wait 5000;lsMessages;";
33-
String script1_B = "openTCP 9999;block A1;sendMessage HiFromBob;wait 5000;";
32+
String script1_A = "wait 2000;connectTCP localhost 9999;release A1;wait 1000;lsMessages;";
33+
String script1_B = "openTCP 9999;block A1;sendMessage HiFromBob;";
3434

3535
//List<PeerHostingEnvironmentDescription> requiredPeerEnvironment = new ArrayList<>();
3636
List<String> scripts = new ArrayList<>();

tests/net/sharksystem/messenger/testScripts/ScriptRunnerTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.jupiter.api.Test;
88

99
import java.io.*;
10+
import java.net.ServerSocket;
1011
import java.util.ArrayList;
1112
import java.util.List;
1213

@@ -29,6 +30,39 @@ public void testTestScriptAsParameter() {
2930
}
3031
}
3132

33+
@Test
34+
public void testGetAvailablePort() throws IOException {
35+
int port = 1000;
36+
ServerSocket srvSocket = null;
37+
ServerSocket srvSocket1 = new ServerSocket(1000);
38+
ServerSocket srvSocket2 = new ServerSocket(1001);
39+
40+
while(srvSocket == null && port < 65535) {
41+
try {
42+
srvSocket = new ServerSocket(port);
43+
} catch (IOException e) {
44+
System.out.println("taken: " + port);
45+
}
46+
port++;
47+
}
48+
srvSocket.close();
49+
System.out.println(srvSocket);
50+
}
51+
52+
53+
private ServerSocket getServerSocket(int port) {
54+
try {
55+
ServerSocket srv = new ServerSocket(port);
56+
srv.close();
57+
// srv.accept();
58+
return srv;
59+
} catch (IOException e) {
60+
System.out.println(e.getLocalizedMessage());
61+
}
62+
return null;
63+
}
64+
65+
3266
@Test
3367
public void testTestRunnerRuntimeExec() throws IOException {
3468
ScriptRunnerProcess_RuntimeExec srp =

0 commit comments

Comments
 (0)