Skip to content

Commit 1f7c7f9

Browse files
committed
working on orchestrated tests
1 parent 6351f24 commit 1f7c7f9

6 files changed

Lines changed: 124 additions & 53 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/net/sharksystem/ui/messenger/cli/commands/messenger/UICommandSendMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public void execute() throws Exception {
117117
// send message
118118
messenger.sendSharkMessage(effectiveFormat,
119119
contentBytes, channelURI, receiverID, this.sign, this.encrypt);
120+
this.getSharkMessengerApp().tellUI("message sent");
120121
} catch (SharkException | IOException e) {
121122
this.getSharkMessengerApp().tellUIError(e.getLocalizedMessage());
122123
}
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,36 @@
11
package net.sharksystem.ui.messenger.cli.commands.testing;
22

33
import net.sharksystem.hub.peerside.ASAPHubManager;
4-
import net.sharksystem.ui.messenger.cli.ProductionUI;
54
import net.sharksystem.utils.Log;
65

76
import java.io.File;
87
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
910

1011
public class ScriptRunnerProcess {
12+
private final ProcessBuilder pb;
1113
private String command;
1214

1315
public ScriptRunnerProcess(String peerName, String testName, String script) throws IOException {
1416
// produce command line to launch a new SNM CLI
1517
peerName = Helper.getFriendlyPeerName(peerName);
1618

17-
StringBuilder sb = new StringBuilder();
18-
sb.append("java -jar ");
19-
sb.append(Helper.SNM_CLI_JAR_FILENAME);
20-
sb.append(" ");
21-
sb.append(peerName + "_" + testName);
22-
sb.append(" ");
23-
sb.append(String.valueOf(ASAPHubManager.DEFAULT_WAIT_INTERVAL_IN_SECONDS));
24-
sb.append(" \"");
25-
sb.append(script);
26-
sb.append("\"");
27-
28-
// a specified system command.
29-
this.command = sb.toString();
19+
List<String> args = new ArrayList<>();
20+
args.add("java");
21+
args.add("-jar");
22+
args.add(Helper.SNM_CLI_JAR_FILENAME);
23+
args.add(peerName + "_" + testName);
24+
args.add(String.valueOf(ASAPHubManager.DEFAULT_WAIT_INTERVAL_IN_SECONDS));
25+
args.add("sendMessage Hi;exit");
26+
this.pb = new ProcessBuilder(args);
27+
28+
File log = new File(peerName + "_uiOutErr.txt");
29+
this.pb.redirectErrorStream(true);
30+
this.pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
3031
}
31-
public void start() throws IOException {
32-
/*
33-
array of strings, each element of which has environment variable settings in the format name=value,
34-
or null if the subprocess should inherit the environment of the current process.
35-
*/
36-
String[] env = null;
3732

38-
/*
39-
the working directory of the subprocess,
40-
or null if the subprocess should inherit the working directory of the current process.
41-
*/
42-
File workingDir = null;
43-
44-
Log.writeLog(this, "launch new process: " + this.command);
45-
System.out.println("launch new process: " + this.command);
46-
Runtime.getRuntime().exec(this.command, env, workingDir);
33+
public void start() throws IOException {
34+
this.pb.start();
4735
}
4836
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.sharksystem.ui.messenger.cli.commands.testing;
2+
3+
import net.sharksystem.hub.peerside.ASAPHubManager;
4+
import net.sharksystem.utils.Log;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
9+
public class ScriptRunnerProcess_RuntimeExec {
10+
private String command;
11+
12+
public ScriptRunnerProcess_RuntimeExec(String peerName, String testName, String script) throws IOException {
13+
// produce command line to launch a new SNM CLI
14+
peerName = Helper.getFriendlyPeerName(peerName);
15+
16+
StringBuilder sb = new StringBuilder();
17+
sb.append("java -jar ");
18+
sb.append(Helper.SNM_CLI_JAR_FILENAME);
19+
sb.append(" ");
20+
sb.append(peerName + "_" + testName);
21+
sb.append(" ");
22+
sb.append(String.valueOf(ASAPHubManager.DEFAULT_WAIT_INTERVAL_IN_SECONDS));
23+
sb.append(" \"");
24+
sb.append(script);
25+
sb.append("\"");
26+
27+
// a specified system command.
28+
this.command = sb.toString();
29+
}
30+
public void start() throws IOException {
31+
/*
32+
array of strings, each element of which has environment variable settings in the format name=value,
33+
or null if the subprocess should inherit the environment of the current process.
34+
*/
35+
String[] env = null;
36+
37+
/*
38+
the working directory of the subprocess,
39+
or null if the subprocess should inherit the working directory of the current process.
40+
*/
41+
File workingDir = null;
42+
43+
Log.writeLog(this, "launch new process: " + this.command);
44+
System.out.println("launch new process: " + this.command);
45+
Process exec = Runtime.getRuntime().exec(this.command, env, workingDir);
46+
// OutputStream outputStream = exec.getOutputStream();
47+
}
48+
}

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,28 @@ protected void execute() throws Exception {
2424

2525
// what works - independent tests - no interaction between test peers
2626
// 2025-12-03 with thread variant
27-
//
27+
// 2025-12-05 works with process builder
2828
String script0_A = "sendMessage HiFromTest;wait 1000;lsMessages;";
2929
String script0_B = "sendMessage HiFromTest;wait 1000;lsMessages;";
3030

31-
// Orchestrator: orchestrateTest dummy; openTCP 6907
32-
// dann Peers: connectTCP localhost 6907; scriptRQ
33-
34-
// testing - seems orchestrator does not receive peerSettled release messages - it does sometimes, though.
35-
// Race condition - no doubt. S***.
36-
// check locks - getting even weirder - to test peer names are generated twice?
37-
// There is nothing but a good mystery :)
38-
// what the difference to scenario 0? no additional TCP connections(?)
39-
4031
// going to launch new processes rather new threads: orchestrator (not yet); test peers (not yet)
4132
String script1_A = "wait 5000;connectTCP localhost 9999;release A1;wait 5000;lsMessages;";
4233
String script1_B = "openTCP 9999;block A1;sendMessage HiFromBob;wait 5000;";
4334

44-
/* effective scripts
45-
// message sent but not received but at least scenario runs - it is a race condition causing the problem above.
46-
o: openTCP 1984;block peerSettled_0_0;block peerSettled_0_1;release launchTest_0;;exit;
47-
a: connectTCP 192.168.0.116 1984;release peerSettled_0_0;block launchTest_0;wait 1000;wait 5000;connectTCP localhost 9999;release A1;wait 5000;lsMessages;;exit;
48-
b: connectTCP 192.168.0.116 1984;release peerSettled_0_1;block launchTest_0;wait 1000;openTCP 9999;block A1;sendMessage HiFromBob;wait 5000;;exit;
49-
*/
50-
5135
//List<PeerHostingEnvironmentDescription> requiredPeerEnvironment = new ArrayList<>();
5236
List<String> scripts = new ArrayList<>();
5337

38+
// Orchestrator: orchestrateTest dummy; openTCP 6907
39+
// dann Peers: connectTCP localhost 6907; scriptRQ
40+
5441
// fill with example data
5542
this.getSharkMessengerApp().tellUI("use sample data - todo: fill with real data");
5643
// anything will do
44+
/*
5745
scripts.add(script0_A);
46+
scripts.add(script0_B);
47+
*/
48+
scripts.add(script1_A);
5849
scripts.add(script1_B);
5950

6051
this.snmTestSupport.orchestrateTest(scripts);

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import net.sharksystem.SharkException;
44
import net.sharksystem.hub.peerside.ASAPHubManager;
55
import net.sharksystem.ui.messenger.cli.ProductionUI;
6-
import net.sharksystem.ui.messenger.cli.commands.testing.ScriptRunnerProcess;
6+
import net.sharksystem.ui.messenger.cli.commands.testing.ScriptRunnerProcess_RuntimeExec;
77
import org.junit.jupiter.api.Test;
88

9-
import java.io.File;
10-
import java.io.IOException;
9+
import java.io.*;
10+
import java.util.ArrayList;
11+
import java.util.List;
1112

1213
public class ScriptRunnerTests {
1314
@Test
@@ -30,12 +31,50 @@ public void testTestScriptAsParameter() {
3031

3132
@Test
3233
public void testTestRunnerRuntimeExec() throws IOException {
33-
ScriptRunnerProcess srp =
34-
new ScriptRunnerProcess("Alice", "test1", "sendMessage msgInTest");
34+
ScriptRunnerProcess_RuntimeExec srp =
35+
new ScriptRunnerProcess_RuntimeExec("Alice", "test1", "sendMessage msgInTest");
3536

3637
srp.start();
3738
}
3839

40+
@Test
41+
public void pbTest() throws IOException {
42+
ProcessBuilder pb =
43+
new ProcessBuilder("java", "-version");
44+
File log = new File("log");
45+
pb.redirectErrorStream(true);
46+
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
47+
pb.start();
48+
}
49+
50+
@Test
51+
public void pbTestRealWorks1() throws IOException {
52+
ProcessBuilder pb =
53+
new ProcessBuilder("java", "-jar", "SharkNetMessengerCLI.jar", "Bob", "6000", "sendMessage Hi;exit");
54+
File log = new File("logBob");
55+
pb.redirectErrorStream(true);
56+
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
57+
pb.start();
58+
}
59+
60+
@Test
61+
public void pbTestReal() throws IOException {
62+
List<String> args = new ArrayList<>();
63+
args.add("java");
64+
args.add("-jar");
65+
args.add("SharkNetMessengerCLI.jar");
66+
args.add("Bob");
67+
args.add("6000");
68+
args.add("sendMessage Hi;exit");
69+
ProcessBuilder pb = new ProcessBuilder(args);
70+
71+
File log = new File("logBob");
72+
73+
pb.redirectErrorStream(true);
74+
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
75+
pb.start();
76+
}
77+
3978
@Test
4079
public void whatWorks() throws IOException {
4180
// a specified system command.
@@ -54,6 +93,10 @@ public void whatWorks() throws IOException {
5493
*/
5594
File workingDir = null; // works with Windows
5695

57-
Runtime.getRuntime().exec(command, env, workingDir);
96+
Process subProcess = Runtime.getRuntime().exec(command, env, workingDir);
97+
InputStream inputStream = subProcess.getInputStream();// reader connected to stdout of subprocess
98+
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
99+
String temp = br.readLine();
100+
while( temp != null ) System.out.println(temp);
58101
}
59102
}

0 commit comments

Comments
 (0)