88import net .sharksystem .asap .ASAPException ;
99import net .sharksystem .ui .messenger .cli .commands .testing .*;
1010import net .sharksystem .ui .messenger .cli .testlanguage .TestLanguageCompiler ;
11+ import net .sharksystem .utils .Log ;
1112import net .sharksystem .utils .SerializationHelper ;
1213
1314import java .io .IOException ;
@@ -136,9 +137,11 @@ public void becomeTestPeer() throws IOException, SharkException {
136137 false // no encryption
137138 );
138139 this .tellUI ("script request sent: " + scriptRQMessage );
140+ Log .writeLog (this , "script request sent: " + scriptRQMessage );
139141 }
140142
141143 private int lastReceivedScriptIndex = -1 ;
144+ private List <TestScriptDescription > handledScripts = new ArrayList <>();
142145 // peers willing to execute test script sent its environment description
143146 public void receivedTestScript (CharSequence testScriptChannel ) {
144147 if (!this .beTestPeer ) {
@@ -149,7 +152,7 @@ public void receivedTestScript(CharSequence testScriptChannel) {
149152 try {
150153 SharkNetMessageList scriptMessages =
151154 this .getSharkMessengerComponent ().getChannel (testScriptChannel ).getMessages ();
152- for (int scriptIndex = this .lastScriptRQIndex + 1 ; scriptIndex < scriptMessages .size (); scriptIndex ++) {
155+ for (int scriptIndex = this .lastReceivedScriptIndex + 1 ; scriptIndex < scriptMessages .size (); scriptIndex ++) {
153156 this .lastReceivedScriptIndex = scriptIndex ; // update each round - before possible exceptions
154157
155158 SharkNetMessage testSharkMessage = scriptMessages .getSharkMessage (scriptIndex , false );
@@ -159,6 +162,23 @@ public void receivedTestScript(CharSequence testScriptChannel) {
159162 // for me?
160163 if (testScriptDescription .peerID .equalsIgnoreCase (this .getSharkPeer ().getPeerID ().toString ())) {
161164 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 ;
174+ }
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+ }
162182
163183 /*
164184 // produce test running thread
@@ -173,12 +193,17 @@ public void receivedTestScript(CharSequence testScriptChannel) {
173193 Integer .toString (testScriptDescription .testNumber ),
174194 testScriptDescription .script ).start ();
175195 this .tellUI ("running script: " + testScriptDescription .script );
196+ Log .writeLog (this , "script running, remember to avoid redo it: " + testScriptDescription );
197+ this .handledScripts .add (testScriptDescription );
176198 } else {
177- this .tellUI ("test script received, not for me though. Index: " + scriptIndex );
199+ this .tellUI ("test script received, not for me though. Index: " + testScriptDescription );
200+ Log .writeLog (this , "test script received, not for me: " + testScriptDescription );
178201 }
179202 }
180203 } catch (IOException | SharkException e ) {
181- this .tellUIError ("problems handling test script received channel: " + e .getLocalizedMessage ());
204+ String log = "problems handling test script received channel: " + e .getLocalizedMessage ();
205+ this .tellUIError (log );
206+ Log .writeLogErr (this , log );
182207 }
183208 }
184209
@@ -234,6 +259,7 @@ public void receivedScriptRQ(CharSequence scriptRQChannel) {
234259 this .tellUI ("script request received - ignore - not a test orchestrator" );
235260 return ;
236261 } else {
262+ Log .writeLog (this , "script request received - try to stage a test" );
237263 this .tellUI ("script request received - try to stage a test" );
238264 }
239265 // add information of this new volunteer and see if we have enough participant for one new test
@@ -250,12 +276,16 @@ public void receivedScriptRQ(CharSequence scriptRQChannel) {
250276
251277 // add or replace information
252278 this .availablePeers .put (peerHostDescription .peerID , peerHostDescription );
279+ String log = "added volunteering peer: " + peerHostDescription ;
280+ this .tellUI (log );
281+ Log .writeLog (this , log );
253282
254283 // try to set up a test(s).
255284 this .stageTests ();
256285 }
257286 } catch (SharkNetMessengerException | IOException | ASAPException e ) {
258287 this .tellUIError ("problems handling script RQ channel: " + e .getLocalizedMessage ());
288+ Log .writeLogErr (this , "problems handling script RQ channel: " + e .getLocalizedMessage ());
259289 }
260290 }
261291 }
@@ -266,6 +296,9 @@ private void stageTests() throws UnknownHostException {
266296 do {
267297 testEnsemble = this .findFittingPeers ();
268298 if (testEnsemble != null ) {
299+ String log = "found enough peers for a new test run: test#" + testEnsemble .waitingTestIndex ;
300+ this .tellUI (log );
301+ Log .writeLog (this , log );
269302 // we found an ensemble to run a test
270303 OrchestratedTest waitingTest = this .orchestratedTestsWaiting .get (testEnsemble .waitingTestIndex );
271304 // add actual test ensemble
@@ -292,9 +325,11 @@ private void stageTests() throws UnknownHostException {
292325 if (!this .orchestratedTestsReady .isEmpty ()) {
293326 // launch tests
294327 this .tellUI ("test staged .. going to launch" );
328+ Log .writeLog (this , "test(s) staged .. going to launch" );
295329 this .launchTests ();
296330 } else {
297331 this .tellUI ("couldn't stage a test - not enough peers" );
332+ Log .writeLog (this , "couldn't stage a test - not enough peers" );
298333 }
299334 }
300335
@@ -313,10 +348,14 @@ private class OrchestratedTestLauncher extends Thread {
313348 public static final String ORCHESTRATOR_PEER_NAME = "orchest" ;
314349 public static final String LAUNCH_TEST_TAG_PREAMBLE = "launchTest_" ;
315350 public final static int FINAL_WAIT_PERIODE_BEFORE_LAUNCH = 1000 ;
351+ public final static int MAX_TEST_DURATION_IN_MILLIS = 10000 ;
316352
317353 private static String scriptStartOrchestrator_SyncWithPeers = null ;
318354 private static String scriptStartPeer_SyncWithOrchestator = null ;
319355 private static String scriptEnd_Exit = null ;
356+ private static String scriptSetTimeBomb =
357+ TestLanguageCompiler .CLI_TIME_BOMB + TestLanguageCompiler .CLI_SPACE
358+ + MAX_TEST_DURATION_IN_MILLIS + TestLanguageCompiler .LANGUAGE_SEPARATOR ;
320359
321360 OrchestratedTestLauncher (OrchestratedTest test2run ) throws UnknownHostException {
322361 this .test2run = test2run ;
@@ -390,6 +429,10 @@ public void run() {
390429 String [] effectiveScripts = new String [this .test2run .scripts .size ()];
391430 for (int peerIndex = 0 ; peerIndex < this .test2run .scripts .size (); peerIndex ++) {
392431 sb = new StringBuilder ();
432+ // set time bomb to avoid orphan processes
433+ SharkNetMessengerAppSupportingDistributedTesting .
434+ this .tellUI ("WARNING: set timeBock in each peer script. Adopt this code for longer lasting test scenarios. <<<< WARNING" );
435+ sb .append (scriptSetTimeBomb );
393436 // add open connection to orchestrator
394437 sb .append (scriptStartPeer_SyncWithOrchestator );
395438
@@ -420,15 +463,16 @@ public void run() {
420463 }
421464
422465 // run orchestrator script first - wait to collect logs
466+ Log .writeLog (this , "launching orchestrator: " );
423467 try {
424468 ScriptRunnerProcess scriptRunnerProcess =
425469 scriptRunnerProcess = new ScriptRunnerProcess (ORCHESTRATOR_PEER_NAME ,
426470 Integer .toString (this .testNumber ), orchestratorScript );
427471 scriptRunnerProcess .start ();
428472 } catch (IOException e ) {
429- SharkNetMessengerAppSupportingDistributedTesting .
430- this .tellUIError ("could not start orchestrator process / don't send scripts to peers: "
431- + e . getStackTrace () );
473+ String log = "could not start orchestrator process / don't send scripts to peers: " + e . getStackTrace ();
474+ SharkNetMessengerAppSupportingDistributedTesting . this .tellUIError (log );
475+ Log . writeLogErr ( this , log );
432476 return ;
433477 }
434478
@@ -447,6 +491,7 @@ public void run() {
447491 try { Thread .sleep (1000 ); } catch (InterruptedException e ) {}
448492
449493 SharkNetMessengerAppSupportingDistributedTesting .this .tellUI ("sending test scripts to peers" );
494+ Log .writeLog (this , "sending test scripts to peers" );
450495
451496 try {
452497 for (int i = 0 ; i < this .test2run .peerEnvironment .size (); i ++) {
@@ -468,9 +513,13 @@ public void run() {
468513 false , // no signing
469514 false // no encryption
470515 );
516+ Log .writeLog (this , "sent script: ip | test# | script"
517+ + peerEnvironment .peerID + " | "
518+ + this .testNumber + " | "
519+ + effectiveScripts [i ]);
471520
472521 SharkNetMessengerAppSupportingDistributedTesting .this .
473- tellUI ("test scripts sent to " + peerEnvironment .ipAddress );
522+ tellUI ("test scripts sent to " + peerEnvironment .peerID + "@" + peerEnvironment . ipAddress );
474523 }
475524 }
476525 catch (IOException | SharkNetMessengerException ioe ) {
@@ -487,7 +536,6 @@ private void launchTests() throws UnknownHostException {
487536 while (!this .orchestratedTestsReady .isEmpty ()) {
488537 OrchestratedTest readyTest = this .orchestratedTestsReady .remove (0 );
489538 OrchestratedTestLauncher readyTestLauncher = new OrchestratedTestLauncher (readyTest );
490-
491539 readyTestLauncher .start ();
492540 }
493541 }
0 commit comments