From cb50c1bd8b3719e37941324e343625ed0c5a0f43 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Tue, 28 Apr 2015 12:44:53 +0000
Subject: [PATCH] improve UDP test

---
 .../udp/UDPEndpointTestStandalone.java        | 38 +++++++++++++------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/router/java/test/junit/net/i2p/router/transport/udp/UDPEndpointTestStandalone.java b/router/java/test/junit/net/i2p/router/transport/udp/UDPEndpointTestStandalone.java
index e9816f6e50..a57da24f59 100644
--- a/router/java/test/junit/net/i2p/router/transport/udp/UDPEndpointTestStandalone.java
+++ b/router/java/test/junit/net/i2p/router/transport/udp/UDPEndpointTestStandalone.java
@@ -3,13 +3,13 @@ package net.i2p.router.transport.udp;
 import java.net.InetAddress;
 import java.net.SocketException;
 import java.net.UnknownHostException;
-import java.util.ArrayList;
 import java.util.Collections;
-import java.util.List;
 import java.util.Properties;
+import java.util.Set;
 
 import net.i2p.data.ByteArray;
 import net.i2p.router.RouterContext;
+import net.i2p.util.ConcurrentHashSet;
 import net.i2p.util.I2PThread;
 import net.i2p.util.Log;
 
@@ -24,12 +24,12 @@ public class UDPEndpointTestStandalone {
     private final Log _log;
     private UDPEndpoint _endpoints[];
     private volatile boolean _beginTest;
-    private final List<ByteArray> _sentNotReceived;
+    private final Set<ByteArray> _sentNotReceived;
     
     public UDPEndpointTestStandalone(RouterContext ctx) {
         _context = ctx;
         _log = ctx.logManager().getLog(UDPEndpointTestStandalone.class);
-        _sentNotReceived = Collections.synchronizedList(new ArrayList<ByteArray>(1000));
+        _sentNotReceived = new ConcurrentHashSet<ByteArray>(128);
     }
     
     public void runTest(int numPeers) {
@@ -107,9 +107,12 @@ public class UDPEndpointTestStandalone {
                 _log.error("die", uhe);
                 System.exit(0);
             }
+            int MIN = 1 + UDPPacket.MAC_SIZE + UDPPacket.IV_SIZE;
+            int MAX = PeerState.LARGE_MTU - PacketBuilder.IP_HEADER_SIZE;
             _log.debug("Beginning to write");
             for (int curPacket = 0; curPacket < 2000; curPacket++) {
-                byte data[] = new byte[1024];
+                int sz = MIN + _context.random().nextInt(MAX - MIN - 1);
+                byte data[] = new byte[sz];
                 _context.random().nextBytes(data);
                 int curPeer = (curPacket % _endpoints.length);
                 if (_endpoints[curPeer] == _endpoint)
@@ -117,22 +120,35 @@ public class UDPEndpointTestStandalone {
                 if (curPeer >= _endpoints.length)
                     curPeer = 0;
                 UDPPacket packet = builder.buildPacket(data, localhost, _endpoints[curPeer].getListenPort());
-                    int outstanding = _sentNotReceived.size() + 1;
-                    _sentNotReceived.add(new ByteArray(data, 0, 1024));
-                    _log.debug("Sending packet " + curPacket + " with outstanding " + outstanding);
+                int outstanding = _sentNotReceived.size() + 1;
+                _sentNotReceived.add(new ByteArray(data));
+                _log.debug("Sending packet " + curPacket + " with " + sz + " byte payload, outstanding " + outstanding);
                 try {
                     _endpoint.send(packet);
                 } catch (Exception e) {
                     _log.error("die", e);
                     break;
                 }
-                try { Thread.sleep(3); } catch (InterruptedException ie) {}
+                int batch = 32 + _context.random().nextInt(32);
+                if (curPacket % batch == 0 || _sentNotReceived.size() > 100) {
+                    try { Thread.sleep(3); } catch (InterruptedException ie) {}
+                }
                 //if (_log.shouldLog(Log.DEBUG))
                 //    _log.debug("Sent to " + _endpoints[curPeer].getListenPort() + " from " + _endpoint.getListenPort());
             }
             _log.debug("Done sending packets");
-            try { Thread.sleep(10*1000); } catch (InterruptedException e) {}
-            System.exit(0);
+            for (int i = 0; i < 20; i++) {
+                if (_sentNotReceived.isEmpty())
+                    break;
+                try { Thread.sleep(500); } catch (InterruptedException e) {}
+            }
+            if (_sentNotReceived.isEmpty()) {
+                _log.info("Test passed");
+                System.exit(0);
+            } else {
+                _log.error("Test failed, " + _sentNotReceived.size() + " not received");
+                System.exit(1);
+            }
         }
     }
     
-- 
GitLab