forked from I2P_Developers/i2p.i2p
Fix and junit-ify tests
This commit is contained in:
@@ -1,33 +1,34 @@
|
|||||||
package net.i2p.client.streaming;
|
package net.i2p.client.streaming;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.Base64;
|
import net.i2p.data.Base64;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.SimpleTimer2;
|
||||||
|
|
||||||
/**
|
public class MessageOutputStreamTest extends TestCase {
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class MessageOutputStreamTest {
|
|
||||||
private I2PAppContext _context;
|
private I2PAppContext _context;
|
||||||
private Log _log;
|
private SimpleTimer2 _st2;
|
||||||
|
|
||||||
public MessageOutputStreamTest() {
|
@Before
|
||||||
|
public void setUp() {
|
||||||
_context = I2PAppContext.getGlobalContext();
|
_context = I2PAppContext.getGlobalContext();
|
||||||
_log = _context.logManager().getLog(MessageOutputStreamTest.class);
|
_st2 = _context.simpleTimer2();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() {
|
@Test
|
||||||
|
public void test() throws Exception {
|
||||||
Receiver receiver = new Receiver();
|
Receiver receiver = new Receiver();
|
||||||
MessageOutputStream out = new MessageOutputStream(_context, receiver);
|
MessageOutputStream out = new MessageOutputStream(_context, _st2, receiver, 100);
|
||||||
byte buf[] = new byte[128*1024];
|
byte buf[] = new byte[128*1024];
|
||||||
_context.random().nextBytes(buf);
|
_context.random().nextBytes(buf);
|
||||||
try {
|
out.write(buf);
|
||||||
out.write(buf);
|
out.flush();
|
||||||
out.flush();
|
|
||||||
} catch (IOException ioe) { ioe.printStackTrace(); }
|
|
||||||
byte read[] = receiver.getData();
|
byte read[] = receiver.getData();
|
||||||
int firstOff = -1;
|
int firstOff = -1;
|
||||||
for (int k = 0; k < buf.length; k++) {
|
for (int k = 0; k < buf.length; k++) {
|
||||||
@@ -36,14 +37,12 @@ public class MessageOutputStreamTest {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (firstOff < 0) {
|
assertTrue(
|
||||||
System.out.println("** Read match");
|
"read does not match (first off = " + firstOff + "): \n"
|
||||||
} else {
|
+ Base64.encode(buf) + "\n"
|
||||||
System.out.println("** Read does not match: first off = " + firstOff);
|
+ Base64.encode(read)
|
||||||
_log.error("read does not match (first off = " + firstOff + "): \n"
|
,
|
||||||
+ Base64.encode(buf) + "\n"
|
firstOff < 0);
|
||||||
+ Base64.encode(read));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Receiver implements MessageOutputStream.DataReceiver {
|
private class Receiver implements MessageOutputStream.DataReceiver {
|
||||||
@@ -66,9 +65,4 @@ public class MessageOutputStreamTest {
|
|||||||
public boolean writeFailed() { return false; }
|
public boolean writeFailed() { return false; }
|
||||||
public boolean writeSuccessful() { return true; }
|
public boolean writeSuccessful() { return true; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
|
||||||
MessageOutputStreamTest t = new MessageOutputStreamTest();
|
|
||||||
t.test();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -4,53 +4,34 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.client.I2PClient;
|
import net.i2p.client.I2PClient;
|
||||||
import net.i2p.client.I2PClientFactory;
|
import net.i2p.client.I2PClientFactory;
|
||||||
import net.i2p.client.I2PSession;
|
import net.i2p.client.I2PSession;
|
||||||
import net.i2p.data.Destination;
|
|
||||||
import net.i2p.util.Log;
|
|
||||||
|
|
||||||
/**
|
public class PingTest extends TestCase {
|
||||||
*
|
|
||||||
*/
|
@Test
|
||||||
public class PingTest {
|
public void test() throws Exception {
|
||||||
public void test() {
|
I2PAppContext context = I2PAppContext.getGlobalContext();
|
||||||
try {
|
I2PSession session = createSession();
|
||||||
I2PAppContext context = I2PAppContext.getGlobalContext();
|
ConnectionManager mgr = new ConnectionManager(context, session, new ConnectionOptions());
|
||||||
I2PSession session = createSession();
|
for (int i = 0; i < 10; i++) {
|
||||||
ConnectionManager mgr = new ConnectionManager(context, session, -1, null);
|
boolean ponged = mgr.ping(session.getMyDestination(), 2*1000);
|
||||||
Log log = context.logManager().getLog(PingTest.class);
|
assertTrue(ponged);
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
log.debug("ping " + i);
|
|
||||||
long before = context.clock().now();
|
|
||||||
boolean ponged = mgr.ping(session.getMyDestination(), 2*1000);
|
|
||||||
long after = context.clock().now();
|
|
||||||
log.debug("ponged? " + ponged + " after " + (after-before) + "ms");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
try { Thread.sleep(30*1000); } catch (Exception e) {}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private I2PSession createSession() {
|
|
||||||
try {
|
|
||||||
I2PClient client = I2PClientFactory.createClient();
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
|
|
||||||
Destination dest = client.createDestination(baos);
|
|
||||||
I2PSession sess = client.createSession(new ByteArrayInputStream(baos.toByteArray()), new Properties());
|
|
||||||
sess.connect();
|
|
||||||
return sess;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException("b0rk b0rk b0rk");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
private I2PSession createSession() throws Exception {
|
||||||
PingTest pt = new PingTest();
|
I2PClient client = I2PClientFactory.createClient();
|
||||||
pt.test();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
|
||||||
|
client.createDestination(baos);
|
||||||
|
I2PSession sess = client.createSession(new ByteArrayInputStream(baos.toByteArray()), new Properties());
|
||||||
|
sess.connect();
|
||||||
|
return sess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user