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;
|
||||
|
||||
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.data.Base64;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.util.SimpleTimer2;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class MessageOutputStreamTest {
|
||||
public class MessageOutputStreamTest extends TestCase {
|
||||
private I2PAppContext _context;
|
||||
private Log _log;
|
||||
private SimpleTimer2 _st2;
|
||||
|
||||
public MessageOutputStreamTest() {
|
||||
@Before
|
||||
public void setUp() {
|
||||
_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();
|
||||
MessageOutputStream out = new MessageOutputStream(_context, receiver);
|
||||
MessageOutputStream out = new MessageOutputStream(_context, _st2, receiver, 100);
|
||||
byte buf[] = new byte[128*1024];
|
||||
_context.random().nextBytes(buf);
|
||||
try {
|
||||
out.write(buf);
|
||||
out.flush();
|
||||
} catch (IOException ioe) { ioe.printStackTrace(); }
|
||||
out.write(buf);
|
||||
out.flush();
|
||||
byte read[] = receiver.getData();
|
||||
int firstOff = -1;
|
||||
for (int k = 0; k < buf.length; k++) {
|
||||
@@ -36,14 +37,12 @@ public class MessageOutputStreamTest {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (firstOff < 0) {
|
||||
System.out.println("** Read match");
|
||||
} else {
|
||||
System.out.println("** Read does not match: first off = " + firstOff);
|
||||
_log.error("read does not match (first off = " + firstOff + "): \n"
|
||||
+ Base64.encode(buf) + "\n"
|
||||
+ Base64.encode(read));
|
||||
}
|
||||
assertTrue(
|
||||
"read does not match (first off = " + firstOff + "): \n"
|
||||
+ Base64.encode(buf) + "\n"
|
||||
+ Base64.encode(read)
|
||||
,
|
||||
firstOff < 0);
|
||||
}
|
||||
|
||||
private class Receiver implements MessageOutputStream.DataReceiver {
|
||||
@@ -66,9 +65,4 @@ public class MessageOutputStreamTest {
|
||||
public boolean writeFailed() { return false; }
|
||||
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.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.I2PClient;
|
||||
import net.i2p.client.I2PClientFactory;
|
||||
import net.i2p.client.I2PSession;
|
||||
import net.i2p.data.Destination;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PingTest {
|
||||
public void test() {
|
||||
try {
|
||||
I2PAppContext context = I2PAppContext.getGlobalContext();
|
||||
I2PSession session = createSession();
|
||||
ConnectionManager mgr = new ConnectionManager(context, session, -1, null);
|
||||
Log log = context.logManager().getLog(PingTest.class);
|
||||
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 class PingTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
I2PAppContext context = I2PAppContext.getGlobalContext();
|
||||
I2PSession session = createSession();
|
||||
ConnectionManager mgr = new ConnectionManager(context, session, new ConnectionOptions());
|
||||
for (int i = 0; i < 10; i++) {
|
||||
boolean ponged = mgr.ping(session.getMyDestination(), 2*1000);
|
||||
assertTrue(ponged);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
PingTest pt = new PingTest();
|
||||
pt.test();
|
||||
private I2PSession createSession() throws Exception {
|
||||
I2PClient client = I2PClientFactory.createClient();
|
||||
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