Skip to content

Commit c157c0e

Browse files
committed
network testing OK
1 parent 7a61254 commit c157c0e

19 files changed

Lines changed: 138 additions & 98 deletions

concurrency/AtomicIntegerTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.concurrent.*;
44
import java.util.concurrent.atomic.*;
55
import java.util.*;
6+
import net.mindview.util.*;
67

78
public class AtomicIntegerTest implements Runnable {
89
private AtomicInteger i = new AtomicInteger(0);
@@ -14,13 +15,7 @@ public void run() {
1415
evenIncrement();
1516
}
1617
public static void main(String[] args) {
17-
new Timer().schedule(new TimerTask() {
18-
@Override
19-
public void run() {
20-
System.out.println("Aborting");
21-
System.exit(0);
22-
}
23-
}, 5000); // Terminate after 5 seconds
18+
new TimedAbort(5); // Terminate after 5 seconds
2419
ExecutorService exec =
2520
Executors.newCachedThreadPool();
2621
AtomicIntegerTest ait = new AtomicIntegerTest();

concurrency/CloseResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class CloseResource {
1212
public static void main(String[] args) throws Exception {
1313
ExecutorService exec = Executors.newCachedThreadPool();
1414
ServerSocket server = new ServerSocket(8080);
15-
try (InputStream socketInput = new Socket("localhost", 8080).getInputStream()) {
15+
try (InputStream socketInput =
16+
new Socket("localhost", 8080).getInputStream()) {
1617
exec.execute(new IOBlocked(socketInput));
1718
exec.execute(new IOBlocked(System.in));
1819
TimeUnit.MILLISECONDS.sleep(100);

concurrency/NaiveExceptionHandling.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//: concurrency/NaiveExceptionHandling.java
22
// ©2015 MindView LLC: see Copyright.txt
3-
// {RunByHand}
3+
// {ValidateByHand}
44
// {ThrowsException}
55
import java.util.concurrent.*;
66

concurrency/SemaphoreDemo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public String toString() {
3333

3434
public class SemaphoreDemo {
3535
final static int SIZE = 25;
36-
public static void main(String[] args) throws Exception {
36+
public static void
37+
main(String[] args) throws Exception {
3738
final Pool<Fat> pool = new Pool<>(Fat.class, SIZE);
3839
ExecutorService exec = Executors.newCachedThreadPool();
3940
for(int i = 0; i < SIZE; i++)

concurrency/build.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
<jrun cls="MoreBasicThreads" />
4444
<jrun cls="MultiLock" />
4545
<jrun cls="MutexEvenGenerator" failOnError='false' timeOut='4000' msg='* Timeout for Testing *' />
46-
<jrun cls="NaiveExceptionHandling" failOnError='false' msg='* Exception was Expected *' />
4746
<jrun cls="NIOInterruption" />
4847
<jrun cls="NotifyVsNotifyAll" />
4948
<jrun cls="OrnamentalGarden" />

logging/LoggingLevels.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
public class LoggingLevels {
77
private static Logger
88
lgr = Logger.getLogger("com"),
9-
lgr2 = Logger.getLogger("com.bruceeckel"),
10-
util= Logger.getLogger("com.bruceeckel.util"),
11-
test= Logger.getLogger("com.bruceeckel.test"),
9+
lgr2 = Logger.getLogger("net.mindview"),
10+
util= Logger.getLogger("net.mindview.util"),
11+
test= Logger.getLogger("net.mindview.test"),
1212
rand = Logger.getLogger("random");
1313
private static void logMessages() {
1414
lgr.info("com : info");
15-
lgr2.info("com.bruceeckel : info");
15+
lgr2.info("net.mindview : info");
1616
util.info("util : info");
1717
test.severe("test : severe");
1818
rand.info("random : info");

net/mindview/util/TimedAbort.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//: net/mindview/util/TimedAbort.java
2+
// ©2015 MindView LLC: see Copyright.txt
3+
// Terminate a program after n seconds.
4+
package net.mindview.util;
5+
import java.util.*;
6+
7+
public class TimedAbort {
8+
public TimedAbort(int n) {
9+
new Timer().schedule(new TimerTask() {
10+
@Override
11+
public void run() {
12+
System.out.println("TimedAbort " + n);
13+
System.exit(0);
14+
}
15+
}, n * 1000);
16+
}
17+
} ///:~

network/ChatterClient.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
//: network/ChatterClient.java
22
// ©2015 MindView LLC: see Copyright.txt
3-
// {TimeOutDuringTesting}
4-
// Tests the ChatterServer by starting multiple
3+
// {ValidateByHand}
4+
// Tests the ChatterServer by starting multiple
55
// clients, each of which sends datagrams.
66
import java.net.*;
77
import java.io.*;
8+
import net.mindview.util.*;
89

910
public class ChatterClient extends Thread {
1011
// Can listen & send on the same socket:
1112
private DatagramSocket s;
1213
private InetAddress hostAddress;
1314
private byte[] buf = new byte[1000];
14-
private DatagramPacket dp =
15+
private DatagramPacket dp =
1516
new DatagramPacket(buf, buf.length);
1617
private int id;
1718

@@ -20,7 +21,7 @@ public ChatterClient(int identifier) {
2021
try {
2122
// Auto-assign port number:
2223
s = new DatagramSocket();
23-
hostAddress =
24+
hostAddress =
2425
InetAddress.getByName("localhost");
2526
} catch(UnknownHostException e) {
2627
System.err.println("Cannot find host");
@@ -29,36 +30,37 @@ public ChatterClient(int identifier) {
2930
System.err.println("Can't open socket");
3031
e.printStackTrace();
3132
System.exit(1);
32-
}
33+
}
3334
System.out.println("ChatterClient starting");
3435
}
35-
@Override
36-
public void run() {
36+
public void sendAndEcho(String msg) {
3737
try {
38-
for(int i = 0; i < 25; i++) {
39-
String outMessage = "Client #" +
40-
id + ", message #" + i;
41-
// Make and send a datagram:
42-
s.send(Dgram.toDatagram(outMessage,
43-
hostAddress,
44-
ChatterServer.INPORT));
45-
// Block until it echoes back:
46-
s.receive(dp);
47-
// Print out the echoed contents:
48-
String rcvd = "Client #" + id +
49-
", rcvd from " +
50-
dp.getAddress() + ", " +
51-
dp.getPort() + ": " +
52-
Dgram.toString(dp);
53-
System.out.println(rcvd);
54-
}
38+
// Make and send a datagram:
39+
s.send(Dgram.toDatagram(msg,
40+
hostAddress,
41+
ChatterServer.INPORT));
42+
// Block until it echoes back:
43+
s.receive(dp);
44+
// Print out the echoed contents:
45+
String rcvd = "Client #" + id +
46+
", rcvd from " +
47+
dp.getAddress() + ", " +
48+
dp.getPort() + ": " +
49+
Dgram.toString(dp);
50+
System.out.println(rcvd);
5551
} catch(IOException e) {
5652
e.printStackTrace();
5753
System.exit(1);
5854
}
5955
}
56+
@Override
57+
public void run() {
58+
for(int i = 0; i <= 25; i++)
59+
sendAndEcho("Client #" + id + ", message #" + i);
60+
}
6061
public static void main(String[] args) {
61-
for(int i = 0; i < 10; i++)
62+
new TimedAbort(5); // Terminate after 5 seconds
63+
for(int i = 0; i <= 10; i++)
6264
new ChatterClient(i).start();
6365
}
6466
} ///:~

network/ChatterServer.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
start java ChatterServer
2+
timeout /t 1
3+
java ChatterClient

network/ChatterServer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//: network/ChatterServer.java
22
// ©2015 MindView LLC: see Copyright.txt
3-
// {TimeOutDuringTesting}
3+
// {ValidateByHand}
44
// A server that echoes datagrams
55
import java.net.*;
66
import java.io.*;
7+
import net.mindview.util.*;
78

89
public class ChatterServer {
910
static final int INPORT = 1711;
1011
private byte[] buf = new byte[1000];
11-
private DatagramPacket dp =
12+
private DatagramPacket dp =
1213
new DatagramPacket(buf, buf.length);
1314
// Can listen & send on the same socket:
1415
private DatagramSocket socket;
@@ -24,12 +25,12 @@ public ChatterServer() {
2425
", from address: " + dp.getAddress() +
2526
", port: " + dp.getPort();
2627
System.out.println(rcvd);
27-
String echoString =
28+
String echoString =
2829
"Echoed: " + rcvd;
2930
// Extract the address and port from the
3031
// received datagram to find out where to
3132
// send it back:
32-
DatagramPacket echo =
33+
DatagramPacket echo =
3334
Dgram.toDatagram(echoString,
3435
dp.getAddress(), dp.getPort());
3536
socket.send(echo);
@@ -43,6 +44,7 @@ public ChatterServer() {
4344
}
4445
}
4546
public static void main(String[] args) {
47+
new TimedAbort(5); // Terminate after 5 seconds
4648
new ChatterServer();
4749
}
4850
} ///:~

0 commit comments

Comments
 (0)