forked from I2P_Developers/i2p.i2p
Refactor tests
This commit is contained in:
@ -21,48 +21,44 @@ public class MessageInputStreamTest extends TestCase {
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
private ConnectionOptions _options;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_log = _context.logManager().getLog(MessageInputStreamTest.class);
|
||||
_options = new ConnectionOptions();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInOrder() {
|
||||
public void testInOrder() throws IOException {
|
||||
byte orig[] = new byte[256*1024];
|
||||
_context.random().nextBytes(orig);
|
||||
|
||||
|
||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
for (int i = 0; i < orig.length / 1024; i++) {
|
||||
byte msg[] = new byte[1024];
|
||||
System.arraycopy(orig, i*1024, msg, 0, 1024);
|
||||
in.messageReceived(i, new ByteArray(msg));
|
||||
}
|
||||
|
||||
|
||||
byte read[] = new byte[orig.length];
|
||||
try {
|
||||
int howMany = DataHelper.read(in, read);
|
||||
if (howMany != orig.length)
|
||||
throw new RuntimeException("Failed test: not enough bytes read [" + howMany + "]");
|
||||
if (!DataHelper.eq(orig, read))
|
||||
throw new RuntimeException("Failed test: data read is not equal");
|
||||
|
||||
_log.info("Passed test: in order");
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("IOError reading: " + ioe.getMessage());
|
||||
}
|
||||
int howMany = DataHelper.read(in, read);
|
||||
if (howMany != orig.length)
|
||||
fail("not enough bytes read [" + howMany + "]");
|
||||
if (!DataHelper.eq(orig, read))
|
||||
fail("data read is not equal");
|
||||
|
||||
_log.info("Passed test: in order");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRandomOrder() {
|
||||
public void testRandomOrder() throws IOException {
|
||||
byte orig[] = new byte[256*1024];
|
||||
_context.random().nextBytes(orig);
|
||||
|
||||
|
||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
||||
for (int i = 0; i < orig.length / 1024; i++)
|
||||
order.add(new Integer(i));
|
||||
@ -74,28 +70,24 @@ public class MessageInputStreamTest extends TestCase {
|
||||
in.messageReceived(cur.intValue(), new ByteArray(msg));
|
||||
_log.debug("Injecting " + cur);
|
||||
}
|
||||
|
||||
|
||||
byte read[] = new byte[orig.length];
|
||||
try {
|
||||
int howMany = DataHelper.read(in, read);
|
||||
if (howMany != orig.length)
|
||||
throw new RuntimeException("Failed test: not enough bytes read [" + howMany + "]");
|
||||
if (!DataHelper.eq(orig, read))
|
||||
throw new RuntimeException("Failed test: data read is not equal");
|
||||
|
||||
_log.info("Passed test: random order");
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("IOError reading: " + ioe.getMessage());
|
||||
}
|
||||
int howMany = DataHelper.read(in, read);
|
||||
if (howMany != orig.length)
|
||||
fail("not enough bytes read [" + howMany + "]");
|
||||
if (!DataHelper.eq(orig, read))
|
||||
fail("data read is not equal");
|
||||
|
||||
_log.info("Passed test: random order");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRandomDups() {
|
||||
public void testRandomDups() throws IOException {
|
||||
byte orig[] = new byte[256*1024];
|
||||
_context.random().nextBytes(orig);
|
||||
|
||||
|
||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
for (int n = 0; n < 3; n++) {
|
||||
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
||||
for (int i = 0; i < orig.length / 1024; i++)
|
||||
@ -109,58 +101,54 @@ public class MessageInputStreamTest extends TestCase {
|
||||
_log.debug("Injecting " + cur);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
byte read[] = new byte[orig.length];
|
||||
try {
|
||||
int howMany = DataHelper.read(in, read);
|
||||
if (howMany != orig.length)
|
||||
throw new RuntimeException("Failed test: not enough bytes read [" + howMany + "]");
|
||||
if (!DataHelper.eq(orig, read))
|
||||
throw new RuntimeException("Failed test: data read is not equal");
|
||||
|
||||
_log.info("Passed test: random dups");
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("IOError reading: " + ioe.getMessage());
|
||||
}
|
||||
int howMany = DataHelper.read(in, read);
|
||||
if (howMany != orig.length)
|
||||
fail("not enough bytes read [" + howMany + "]");
|
||||
if (!DataHelper.eq(orig, read))
|
||||
fail("data read is not equal");
|
||||
|
||||
_log.info("Passed test: random dups");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStaggered() {
|
||||
public void testStaggered() throws IOException {
|
||||
byte orig[] = new byte[256*1024];
|
||||
byte read[] = new byte[orig.length];
|
||||
_context.random().nextBytes(orig);
|
||||
|
||||
|
||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
||||
for (int i = 0; i < orig.length / 1024; i++)
|
||||
order.add(new Integer(i));
|
||||
Collections.shuffle(order);
|
||||
|
||||
|
||||
int offset = 0;
|
||||
for (int i = 0; i < orig.length / 1024; i++) {
|
||||
byte msg[] = new byte[1024];
|
||||
Integer cur = (Integer)order.get(i);
|
||||
System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024);
|
||||
in.messageReceived(cur.intValue(), new ByteArray(msg));
|
||||
_log.debug("Injecting " + cur);
|
||||
|
||||
try {
|
||||
try {
|
||||
for (int i = 0; i < orig.length / 1024; i++) {
|
||||
byte msg[] = new byte[1024];
|
||||
Integer cur = (Integer)order.get(i);
|
||||
System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024);
|
||||
in.messageReceived(cur.intValue(), new ByteArray(msg));
|
||||
_log.debug("Injecting " + cur);
|
||||
|
||||
if (in.available() > 0) {
|
||||
int curRead = in.read(read, offset, read.length-offset);
|
||||
_log.debug("read " + curRead);
|
||||
if (curRead == -1)
|
||||
throw new RuntimeException("EOF with offset " + offset);
|
||||
fail("EOF with offset " + offset);
|
||||
else
|
||||
offset += curRead;
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("IOE: " + ioe.getMessage());
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
|
||||
|
||||
if (!DataHelper.eq(orig, read))
|
||||
throw new RuntimeException("Failed test: data read is not equal");
|
||||
fail("data read is not equal");
|
||||
|
||||
_log.info("Passed test: staggered");
|
||||
}
|
||||
|
@ -14,13 +14,13 @@ import net.i2p.util.SimpleTimer2;
|
||||
public class MessageOutputStreamTest extends TestCase {
|
||||
private I2PAppContext _context;
|
||||
private SimpleTimer2 _st2;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
_context = I2PAppContext.getGlobalContext();
|
||||
_st2 = _context.simpleTimer2();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
Receiver receiver = new Receiver();
|
||||
@ -39,12 +39,12 @@ public class MessageOutputStreamTest extends TestCase {
|
||||
}
|
||||
assertTrue(
|
||||
"read does not match (first off = " + firstOff + "): \n"
|
||||
+ Base64.encode(buf) + "\n"
|
||||
+ Base64.encode(read)
|
||||
,
|
||||
firstOff < 0);
|
||||
+ Base64.encode(buf) + "\n"
|
||||
+ Base64.encode(read)
|
||||
,
|
||||
firstOff < 0);
|
||||
}
|
||||
|
||||
|
||||
private class Receiver implements MessageOutputStream.DataReceiver {
|
||||
private ByteArrayOutputStream _data;
|
||||
public Receiver() {
|
||||
@ -52,7 +52,7 @@ public class MessageOutputStreamTest extends TestCase {
|
||||
}
|
||||
public MessageOutputStream.WriteStatus writeData(byte[] buf, int off, int size) {
|
||||
_data.write(buf, off, size);
|
||||
return new DummyWriteStatus();
|
||||
return new DummyWriteStatus();
|
||||
}
|
||||
public boolean writeInProcess() { return false; }
|
||||
public byte[] getData() { return _data.toByteArray(); }
|
||||
|
Reference in New Issue
Block a user