Created
January 10, 2010 21:39
-
-
Save notnoop/273786 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.util.concurrent.Semaphore; | |
/** | |
* Unit test for detecting connection drops while transmitting | |
* packets. | |
* | |
* In this test, the server closes a connection while the client | |
* is transmitting data. The expected behavior is for the client | |
* to throw an IOException. | |
* | |
* Unfortunately, the execution terminates without any exceptions | |
* | |
* This is written using the built-in concurrency functionality. | |
* Checkout SocketDropTestTC, to see it better written in | |
* the ThreadedTC concurrency testing library! | |
* | |
*/ | |
public class SocketDropTestSemaphored { | |
public static void main(String[] args) throws Throwable { | |
final int PORT = 8005; | |
final int FIRST_BUF_SIZE = 5; | |
final Throwable[] errors = new Throwable[1]; | |
final Semaphore serverClosing = new Semaphore(0); | |
final Semaphore messageFlushed = new Semaphore(0); | |
class ServerThread extends Thread { | |
public void run() { | |
try { | |
ServerSocket ssocket = new ServerSocket(PORT); | |
Socket socket = ssocket.accept(); | |
InputStream s = socket.getInputStream(); | |
s.read(new byte[FIRST_BUF_SIZE]); | |
messageFlushed.acquire(); | |
socket.close(); | |
ssocket.close(); | |
System.out.println("Closed socket"); | |
serverClosing.release(); | |
} catch (Throwable e) { | |
errors[0] = e; | |
} | |
} | |
} | |
class ClientThread extends Thread { | |
public void run() { | |
try { | |
Socket socket = new Socket("localhost", PORT); | |
OutputStream st = socket.getOutputStream(); | |
st.write(new byte[FIRST_BUF_SIZE]); | |
st.flush(); | |
messageFlushed.release(); | |
serverClosing.acquire(1); | |
System.out.println("writing new packets"); | |
// sending more packets while server already | |
// closed connection | |
st.write(32); | |
st.flush(); | |
st.close(); | |
System.out.println("Sent"); | |
} catch (Throwable e) { | |
errors[0] = e; | |
} | |
} | |
} | |
Thread thread1 = new ServerThread(); | |
Thread thread2 = new ClientThread(); | |
thread1.start(); | |
thread2.start(); | |
thread1.join(); | |
thread2.join(); | |
if (errors[0] != null) | |
throw errors[0]; | |
throw new AssertionError("Should have gotten an error by now"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import com.notnoop.threadedtc.*; | |
/** | |
* "Unit test" for detection of connection dropped. | |
* | |
* In this test, the server closes a connection while the client | |
* is transmitting data. The expected behavior is for the client | |
* to throw an IOException. | |
* | |
* Unfortunately, the execution terminates without any exceptions | |
* | |
* This test is written to use the threadedTC library | |
* available at http://github.com/notnoop/threadedtc | |
*/ | |
public class SocketDropTestTC { | |
public static void main(String[] args) { | |
final int PORT = 8005; | |
final int FIRST_BUF_SIZE = 5; | |
final Conductor c = new Conductor(); | |
c.thread("Server", new TCRunnable() { | |
public void run() throws Exception { | |
ServerSocket ssocket = new ServerSocket(PORT); | |
Socket socket = ssocket.accept(); | |
InputStream s = socket.getInputStream(); | |
s.read(new byte[FIRST_BUF_SIZE]); | |
c.waitForBeat(1); | |
socket.close(); | |
ssocket.close(); | |
System.out.println("Closed at beat: " + c.beat()); | |
} | |
}); | |
c.thread("Client", new TCRunnable() { | |
public void run() throws Exception { | |
Socket socket = new Socket("localhost", PORT); | |
OutputStream st = socket.getOutputStream(); | |
st.write(new byte[FIRST_BUF_SIZE]); | |
st.flush(); | |
// Ensure that the following section runs after | |
// the socket closes | |
c.waitForBeat(2); | |
System.out.println("writing new packets"); | |
st.write(32); | |
st.flush(); | |
st.close(); | |
System.out.println("Sent at beat: " + c.beat()); | |
throw new AssertionError("Flush should have thrown an exception!"); | |
} | |
}); | |
c.waitTillFinished(); | |
throw new AssertionError("Should have gotten an error by now"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment