forked from I2P_Developers/i2p.i2p
Refactor part 2
This commit is contained in:
@ -4,11 +4,11 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.ByteArray;
|
import net.i2p.data.ByteArray;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
@ -21,12 +21,21 @@ public class MessageInputStreamTest extends TestCase {
|
|||||||
private I2PAppContext _context;
|
private I2PAppContext _context;
|
||||||
private Log _log;
|
private Log _log;
|
||||||
private ConnectionOptions _options;
|
private ConnectionOptions _options;
|
||||||
|
private MessageInputStream in;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
_context = I2PAppContext.getGlobalContext();
|
_context = I2PAppContext.getGlobalContext();
|
||||||
_log = _context.logManager().getLog(MessageInputStreamTest.class);
|
_log = _context.logManager().getLog(MessageInputStreamTest.class);
|
||||||
_options = new ConnectionOptions();
|
_options = new ConnectionOptions();
|
||||||
|
|
||||||
|
in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
||||||
|
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -34,8 +43,6 @@ public class MessageInputStreamTest extends TestCase {
|
|||||||
byte orig[] = new byte[256*1024];
|
byte orig[] = new byte[256*1024];
|
||||||
_context.random().nextBytes(orig);
|
_context.random().nextBytes(orig);
|
||||||
|
|
||||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
|
||||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
|
||||||
for (int i = 0; i < orig.length / 1024; i++) {
|
for (int i = 0; i < orig.length / 1024; i++) {
|
||||||
byte msg[] = new byte[1024];
|
byte msg[] = new byte[1024];
|
||||||
System.arraycopy(orig, i*1024, msg, 0, 1024);
|
System.arraycopy(orig, i*1024, msg, 0, 1024);
|
||||||
@ -57,8 +64,6 @@ public class MessageInputStreamTest extends TestCase {
|
|||||||
byte orig[] = new byte[256*1024];
|
byte orig[] = new byte[256*1024];
|
||||||
_context.random().nextBytes(orig);
|
_context.random().nextBytes(orig);
|
||||||
|
|
||||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
|
||||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
|
||||||
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
||||||
for (int i = 0; i < orig.length / 1024; i++)
|
for (int i = 0; i < orig.length / 1024; i++)
|
||||||
order.add(new Integer(i));
|
order.add(new Integer(i));
|
||||||
@ -86,8 +91,6 @@ public class MessageInputStreamTest extends TestCase {
|
|||||||
byte orig[] = new byte[256*1024];
|
byte orig[] = new byte[256*1024];
|
||||||
_context.random().nextBytes(orig);
|
_context.random().nextBytes(orig);
|
||||||
|
|
||||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
|
||||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
|
||||||
for (int n = 0; n < 3; n++) {
|
for (int n = 0; n < 3; n++) {
|
||||||
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
||||||
for (int i = 0; i < orig.length / 1024; i++)
|
for (int i = 0; i < orig.length / 1024; i++)
|
||||||
@ -118,33 +121,27 @@ public class MessageInputStreamTest extends TestCase {
|
|||||||
byte read[] = new byte[orig.length];
|
byte read[] = new byte[orig.length];
|
||||||
_context.random().nextBytes(orig);
|
_context.random().nextBytes(orig);
|
||||||
|
|
||||||
MessageInputStream in = new MessageInputStream(_context, _options.getMaxMessageSize(),
|
|
||||||
_options.getMaxWindowSize(), _options.getInboundBufferSize());
|
|
||||||
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
ArrayList<Integer> order = new ArrayList<Integer>(32);
|
||||||
for (int i = 0; i < orig.length / 1024; i++)
|
for (int i = 0; i < orig.length / 1024; i++)
|
||||||
order.add(new Integer(i));
|
order.add(new Integer(i));
|
||||||
Collections.shuffle(order);
|
Collections.shuffle(order);
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
try {
|
for (int i = 0; i < orig.length / 1024; i++) {
|
||||||
for (int i = 0; i < orig.length / 1024; i++) {
|
byte msg[] = new byte[1024];
|
||||||
byte msg[] = new byte[1024];
|
Integer cur = (Integer)order.get(i);
|
||||||
Integer cur = (Integer)order.get(i);
|
System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024);
|
||||||
System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024);
|
in.messageReceived(cur.intValue(), new ByteArray(msg));
|
||||||
in.messageReceived(cur.intValue(), new ByteArray(msg));
|
_log.debug("Injecting " + cur);
|
||||||
_log.debug("Injecting " + cur);
|
|
||||||
|
|
||||||
if (in.available() > 0) {
|
if (in.available() > 0) {
|
||||||
int curRead = in.read(read, offset, read.length-offset);
|
int curRead = in.read(read, offset, read.length-offset);
|
||||||
_log.debug("read " + curRead);
|
_log.debug("read " + curRead);
|
||||||
if (curRead == -1)
|
if (curRead == -1)
|
||||||
fail("EOF with offset " + offset);
|
fail("EOF with offset " + offset);
|
||||||
else
|
else
|
||||||
offset += curRead;
|
offset += curRead;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!DataHelper.eq(orig, read))
|
if (!DataHelper.eq(orig, read))
|
||||||
|
Reference in New Issue
Block a user