Made the test case more robust.

The call to EndPoint.blockReadable() must be enclosed in a while loop, since the EndPoint
may be woken up spuriously or by write readyness, and no further bytes may be read.
This commit is contained in:
Simone Bordet 2011-11-10 15:31:56 +01:00
parent 7cc526b9dd
commit 34fbbd5e28
1 changed files with 76 additions and 70 deletions

View File

@ -1,11 +1,9 @@
package org.eclipse.jetty.io.nio; package org.eclipse.jetty.io.nio;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.nio.channels.SelectionKey; import java.nio.channels.SelectionKey;
@ -24,10 +22,13 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SelectChannelEndPointTest public class SelectChannelEndPointTest
{ {
protected ServerSocketChannel _connector; protected ServerSocketChannel _connector;
protected ServerSocketChannel __serverSocket;
protected QueuedThreadPool _threadPool = new QueuedThreadPool(); protected QueuedThreadPool _threadPool = new QueuedThreadPool();
protected SelectorManager _manager = new SelectorManager() protected SelectorManager _manager = new SelectorManager()
{ {
@ -67,8 +68,8 @@ public class SelectChannelEndPointTest
} }
}; };
boolean _echo=true; // Must be volatile or the test may fail spuriously
int _blockAt=0; private volatile int _blockAt=0;
@Before @Before
public void startManager() throws Exception public void startManager() throws Exception
@ -120,14 +121,14 @@ public class SelectChannelEndPointTest
((AsyncEndPoint)_endp).cancelIdle(); ((AsyncEndPoint)_endp).cancelIdle();
} }
if (_blockAt>0 && _in.length()>0 && _in.length()<_blockAt) while (_blockAt>0 && _in.length()>0 && _in.length()<_blockAt)
{ {
_endp.blockReadable(10000); _endp.blockReadable(10000);
if (_in.space()>0 && _endp.fill(_in)>0) if (_in.space()>0 && _endp.fill(_in)>0)
progress=true; progress=true;
} }
if (_echo && _in.hasContent() && _in.skip(_out.put(_in))>0) if (_in.hasContent() && _in.skip(_out.put(_in))>0)
progress=true; progress=true;
if (_out.hasContent() && _endp.flush(_out)>0) if (_out.hasContent() && _endp.flush(_out)>0)
@ -274,41 +275,46 @@ public class SelectChannelEndPointTest
{ {
Socket client = newClient(); Socket client = newClient();
client.setSoTimeout(200);
SocketChannel server = _connector.accept(); SocketChannel server = _connector.accept();
server.configureBlocking(false); server.configureBlocking(false);
_manager.register(server); _manager.register(server);
OutputStream clientOutputStream = client.getOutputStream();
InputStream clientInputStream = client.getInputStream();
int specifiedTimeout = 200;
client.setSoTimeout(specifiedTimeout);
// Write 8 and cause block for 10 // Write 8 and cause block for 10
_blockAt=10; _blockAt=10;
client.getOutputStream().write("12345678".getBytes("UTF-8")); clientOutputStream.write("12345678".getBytes("UTF-8"));
clientOutputStream.flush();
long specifiedTimeout = 200L; Thread.sleep(2 * specifiedTimeout);
Thread.sleep(specifiedTimeout);
// No echo as blocking for 10 // No echo as blocking for 10
long start=System.currentTimeMillis(); long start=System.currentTimeMillis();
try try
{ {
int b= client.getInputStream().read(); int b = clientInputStream.read();
Assert.fail("Should have timed out waiting for a response, but read "+b); Assert.fail("Should have timed out waiting for a response, but read "+b);
} }
catch(SocketTimeoutException e) catch(SocketTimeoutException e)
{ {
System.err.println("blocked "+(System.currentTimeMillis()-start)); int elapsed = Long.valueOf(System.currentTimeMillis() - start).intValue();
Assert.assertThat("Expected timeout", System.currentTimeMillis()-start, greaterThanOrEqualTo(specifiedTimeout)); System.err.println("blocked " + elapsed);
Assert.assertThat("Expected timeout", elapsed, greaterThanOrEqualTo(specifiedTimeout));
} }
// write remaining characters // write remaining characters
client.getOutputStream().write("90ABCDEF".getBytes("UTF-8")); clientOutputStream.write("90ABCDEF".getBytes("UTF-8"));
clientOutputStream.flush();
// Verify echo server to client // Verify echo server to client
for (char c : "1234567890ABCDEF".toCharArray()) for (char c : "1234567890ABCDEF".toCharArray())
{ {
int b = client.getInputStream().read(); int b = clientInputStream.read();
assertTrue(b>0); assertTrue(b>0);
assertEquals(c,(char)b); assertEquals(c,(char)b);
} }