Skip to content

Commit 18c791a

Browse files
committed
we have got a much better bug reporting methode now.
1 parent 9394387 commit 18c791a

File tree

3 files changed

+80
-22
lines changed

3 files changed

+80
-22
lines changed

src/net/sharksystem/asap/util/Batchprocessor.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

13-
public class Batchprocessor extends Thread {
13+
public class Batchprocessor implements Runnable {
1414
List<String> cmdList = new ArrayList<>();
1515
private CmdLineUI cmdLineUI;
1616
private PrintStream printStream;
1717
private ByteArrayInputStream inputStream;
18+
private Thread runningThread;
1819

1920
public Batchprocessor() {
2021
this(true);
@@ -31,8 +32,24 @@ public void addCommand(String cmd) {
3132
this.cmdList.add(cmd);
3233
}
3334

34-
@Override
35-
public void run() {
35+
public void execute() {
36+
this.prepareExecution();
37+
this.doExecution();
38+
}
39+
40+
public void executeAsThread() {
41+
this.prepareExecution();
42+
this.runningThread = new Thread(this);
43+
this.runningThread.start();
44+
}
45+
46+
public void join() throws InterruptedException {
47+
if(this.runningThread != null && this.runningThread.isAlive()) {
48+
this.runningThread.join();
49+
}
50+
}
51+
52+
private void prepareExecution() {
3653
// prepare output
3754
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3855
PrintStream ps = new PrintStream(baos);
@@ -41,11 +58,16 @@ public void run() {
4158
ps.println(cmd);
4259
}
4360

61+
// clean cmd list
62+
this.cmdList = new ArrayList<>();
63+
4464
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
4565

4666
this.printStream = System.out;
4767
this.inputStream = bais;
68+
}
4869

70+
private void doExecution() {
4971
this.cmdLineUI.runCommandLoop(this.printStream, this.inputStream);
5072

5173
// in any case - give it some time to tidy up
@@ -54,5 +76,12 @@ public void run() {
5476
} catch (InterruptedException e) {
5577
// ignore
5678
}
79+
80+
this.runningThread = null;
81+
}
82+
83+
@Override
84+
public void run() {
85+
this.doExecution();
5786
}
5887
}

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ public void runCommandLoop() {
301301
this.doKill("all");
302302
again = false; break; // end loop
303303

304-
default: this.consoleOutput.println("unknown command:" +
305-
cmdLineString);
304+
default: System.err.println("unknown command:" + cmdLineString);
306305
this.printUsage();
307306
rememberCommand = false;
308307
break;

test/net/sharksystem/asap/BatchprocessorTest.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,65 @@ public class BatchprocessorTest {
1010
public void secondMessageNotSendTest() throws IOException, ASAPException, InterruptedException {
1111
Batchprocessor aliceCommands = new Batchprocessor();
1212
Batchprocessor bobCommands = new Batchprocessor();
13+
14+
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
15+
System.out.println("+ STEP 1 +");
16+
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
17+
18+
// prepare
1319
aliceCommands.addCommand(
1420
"newpeer Alice\n" +
1521
"newapp Alice chat\n" +
1622
"newmessage Alice chat sn2://abChat HiBob\n" +
17-
"connect 7070 Alice\n" +
18-
"sleep 2000\n" +
19-
"newmessage Alice chat sn2://abChat HiBob2\n" +
20-
"sleep 1000\n" +
21-
"kill all\n" +
22-
"open 7071 Alice\n" +
23-
"sleep 5000"
24-
);
25-
23+
"open 7070 Alice");
2624

2725
bobCommands.addCommand(
2826
"newpeer Bob\n" +
2927
"newapp Bob chat\n" +
3028
"newmessage Bob chat sn2://abChat HiAlice\n" +
31-
"open 7070 Bob\n" +
32-
"sleep 1000\n" +
33-
"connect 7071 Bob\n" +
34-
"sleep 1000"
35-
);
29+
"connect 7070 Bob");
3630

37-
// lets go
38-
aliceCommands.start();
39-
bobCommands.start();
31+
aliceCommands.executeAsThread();
32+
bobCommands.executeAsThread();
4033

34+
// give it some time
35+
Thread.sleep(1000);
36+
// wait until ends
4137
aliceCommands.join();
4238
bobCommands.join();
39+
40+
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
41+
System.out.println("+ STEP 2 +");
42+
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
43+
44+
// add message while online - terminate connection
45+
aliceCommands.addCommand(
46+
"newmessage Alice chat sn2://abChat HiBob2\n" +
47+
"kill all");
48+
49+
aliceCommands.execute();
50+
51+
// connection closed - re-establish - Bob as TCP server now
52+
aliceCommands.addCommand(
53+
"sleep 500\n" +
54+
"connect 7071 Alice");
55+
56+
bobCommands.addCommand("open 7071 Bob");
57+
58+
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
59+
System.out.println("+ STEP 3 +");
60+
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
61+
62+
aliceCommands.executeAsThread();
63+
bobCommands.executeAsThread();
64+
65+
// give it some time
66+
Thread.sleep(1000);
67+
// wait until ends
68+
aliceCommands.join();
69+
bobCommands.join();
70+
71+
// problem Alice' second message is not transmitted
72+
4373
}
4474
}

0 commit comments

Comments
 (0)