Methods Summary |
---|
void | cleanUp()Clean up after the test.
try {
client.close();
server.close();
} catch (IOException e) {
System.out.println("TestSocket cleanUp failed with:");
e.printStackTrace();
}
receivedDatagram = null;
sendDatagram = null;
|
private Datagram | createDatagramToReceive()Creates a datagram packet to be received at server
receivedDatagram = null;
numBytesToSend = strTestMsg.length();
try {
receivedDatagram = server.newDatagram(numBytesToSend);
} catch (IOException ioe) {
cleanUp();
System.out.println("Cannot create Datagram. IOException: " + ioe);
}
return receivedDatagram;
|
private Datagram | createDatagramToSend(java.lang.String address)Creates a datagram packet to be sent by client
sendDatagram = null;
numBytesToSend = strTestMsg.length();
byte[] buf = new byte[numBytesToSend];
buf = strTestMsg.getBytes();
try {
sendDatagram = client.newDatagram(buf, numBytesToSend, address);
} catch (IOException ioe) {
cleanUp();
System.out.println("Cannot create Datagram. IOException: " + ioe);
}
return sendDatagram;
|
public void | run()Run method to open the server datagram and block on a read.
try {
// Create the server listening datagram socket
server = (UDPDatagramConnection)
Connector.open("datagram://:"+PORT);
// Notify indicating that server is available
synchronized (this) {
notifyAll();
}
receivedDatagram = createDatagramToReceive();
server.receive(receivedDatagram);
} catch (IOException ioe) {
System.out.println("TestDatagram reader thread failed with:");
ioe.printStackTrace();
} finally {
synchronized (this) {
readData = true;
notifyAll();
}
}
|
public void | runTests()Run the test by setting up datagram client in main thread and datagram
server in a separate thread. The server thread blocks for read
operation. The client writes a data packet to server. The test passes
successfully when server receives the data back from client properly.
setUp();
declare("TestDatagramWouldBlock");
testWouldBlock();
cleanUp();
|
void | setUp()Open the client connection and start the thread to call the run
method. It waits until the server is available.
try {
// Create client datagram socket
client = (UDPDatagramConnection)
Connector.open("datagram://localhost:"+PORT);
// Launch a thread which would be blocked for read operation
// from above datagram connection
t1 = new Thread(this);
t1.start();
// Wait for the server to become available
synchronized (this) {
while (server == null) {
try {
// Wait for sufficiently long time so that server
// is blocked for read operation
wait(1200);
} catch (InterruptedException e) {
// Ignore interrupt
}
}
}
} catch (IOException ioe) {
System.out.println("TestDatagram setUp failed with:");
ioe.printStackTrace();
}
|
void | testWouldBlock()In this test, client datagram socket sends a packet to server.
Upon receiving the packet at server, message is printed back.
assertNotNull("Verify datagram socket open", client);
sendDatagram = createDatagramToSend("datagram://localhost:"+PORT);
try {
client.send(sendDatagram);
} catch (IOException ioe) {
cleanUp();
System.out.println("Cannot send datagram, send thrown " + ioe);
return;
}
synchronized (this) {
if (!readData) {
try {
wait(2000);
} catch (InterruptedException e) {
System.out.println("Catch interrupt");
}
}
}
byte[] buf = receivedDatagram.getData();
String rcvdMsg = new String(buf);
// IMPL_NOTE: System.out.println("Received msg : " + rcvdMsg);
assertTrue("Verify data received at server", readData);
|