Changeset a308179


Ignore:
Timestamp:
Jun 28, 2013 3:21:02 PM (7 years ago)
Author:
zab2 <zab2@…>
Branches:
master
Children:
bddfe3ed
Parents:
f8648ff
Message:

Fix and junit-ify tests

Location:
apps/streaming/java/test/junit/net/i2p/client/streaming
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • apps/streaming/java/test/junit/net/i2p/client/streaming/MessageOutputStreamTest.java

    rf8648ff ra308179  
    22
    33import java.io.ByteArrayOutputStream;
    4 import java.io.IOException;
     4
     5import junit.framework.TestCase;
     6
     7import org.junit.Before;
     8import org.junit.Test;
    59
    610import net.i2p.I2PAppContext;
    711import net.i2p.data.Base64;
    8 import net.i2p.util.Log;
     12import net.i2p.util.SimpleTimer2;
    913
    10 /**
    11  *
    12  */
    13 public class MessageOutputStreamTest {
     14public class MessageOutputStreamTest extends TestCase {
    1415    private I2PAppContext _context;
    15     private Log _log;
     16    private SimpleTimer2 _st2;
    1617   
    17     public MessageOutputStreamTest() {
     18    @Before
     19    public void setUp() {
    1820        _context = I2PAppContext.getGlobalContext();
    19         _log = _context.logManager().getLog(MessageOutputStreamTest.class);
     21        _st2 = _context.simpleTimer2();
    2022    }
    2123   
    22     public void test() {
     24    @Test
     25    public void test() throws Exception {
    2326        Receiver receiver = new Receiver();
    24         MessageOutputStream out = new MessageOutputStream(_context, receiver);
     27        MessageOutputStream out = new MessageOutputStream(_context, _st2, receiver, 100);
    2528        byte buf[] = new byte[128*1024];
    2629        _context.random().nextBytes(buf);
    27         try {
    28             out.write(buf);
    29             out.flush();
    30         } catch (IOException ioe) { ioe.printStackTrace(); }
     30        out.write(buf);
     31        out.flush();
    3132        byte read[] = receiver.getData();
    3233        int firstOff = -1;
     
    3738            }
    3839        }
    39         if (firstOff < 0) {
    40             System.out.println("** Read match");
    41         } else {
    42             System.out.println("** Read does not match: first off = " + firstOff);
    43             _log.error("read does not match (first off = " + firstOff + "): \n"
    44                         + Base64.encode(buf) + "\n"
    45                         + Base64.encode(read));
    46         }
     40        assertTrue(
     41                "read does not match (first off = " + firstOff + "): \n"
     42                    + Base64.encode(buf) + "\n"
     43                    + Base64.encode(read)
     44                ,
     45                firstOff < 0);
    4746    }
    4847   
     
    6766        public boolean writeSuccessful() { return true; }
    6867    }
    69    
    70     public static void main(String args[]) {
    71         MessageOutputStreamTest t = new MessageOutputStreamTest();
    72         t.test();
    73     }
    7468}
  • apps/streaming/java/test/junit/net/i2p/client/streaming/PingTest.java

    rf8648ff ra308179  
    55import java.util.Properties;
    66
     7import org.junit.Test;
     8
     9import junit.framework.TestCase;
     10
    711import net.i2p.I2PAppContext;
    812import net.i2p.client.I2PClient;
    913import net.i2p.client.I2PClientFactory;
    1014import net.i2p.client.I2PSession;
    11 import net.i2p.data.Destination;
    12 import net.i2p.util.Log;
    1315
    14 /**
    15  *
    16  */
    17 public class PingTest {
    18     public void test() {
    19         try {
    20             I2PAppContext context = I2PAppContext.getGlobalContext();
    21             I2PSession session = createSession();
    22             ConnectionManager mgr = new ConnectionManager(context, session, -1, null);
    23             Log log = context.logManager().getLog(PingTest.class);
    24             for (int i = 0; i < 10; i++) {
    25                 log.debug("ping " + i);
    26                 long before = context.clock().now();
    27                 boolean ponged = mgr.ping(session.getMyDestination(), 2*1000);
    28                 long after = context.clock().now();
    29                 log.debug("ponged? " + ponged + " after " + (after-before) + "ms");
    30             }
    31         } catch (Exception e) {
    32             e.printStackTrace();
    33         }
    34         try { Thread.sleep(30*1000); } catch (Exception e) {}
    35        
    36     }
    37    
    38     private I2PSession createSession() {
    39         try {
    40             I2PClient client = I2PClientFactory.createClient();
    41             ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
    42             Destination dest = client.createDestination(baos);
    43             I2PSession sess = client.createSession(new ByteArrayInputStream(baos.toByteArray()), new Properties());
    44             sess.connect();
    45             return sess;
    46         } catch (Exception e) {
    47             e.printStackTrace();
    48             throw new RuntimeException("b0rk b0rk b0rk");
     16public class PingTest extends TestCase {
     17
     18    @Test
     19    public void test() throws Exception {
     20        I2PAppContext context = I2PAppContext.getGlobalContext();
     21        I2PSession session = createSession();
     22        ConnectionManager mgr = new ConnectionManager(context, session, new ConnectionOptions());
     23        for (int i = 0; i < 10; i++) {
     24            boolean ponged = mgr.ping(session.getMyDestination(), 2*1000);
     25            assertTrue(ponged);
    4926        }
    5027    }
    5128   
    52     public static void main(String args[]) {
    53         PingTest pt = new PingTest();
    54         pt.test();
     29    private I2PSession createSession() throws Exception {
     30        I2PClient client = I2PClientFactory.createClient();
     31        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
     32        client.createDestination(baos);
     33        I2PSession sess = client.createSession(new ByteArrayInputStream(baos.toByteArray()), new Properties());
     34        sess.connect();
     35        return sess;
    5536    }
    5637}
Note: See TracChangeset for help on using the changeset viewer.