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