From 18ab9b80d207c3c6e21c1f60d7eaec2f3f7fdac6 Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Mon, 8 Nov 2004 05:48:36 +0000
Subject: [PATCH] testStaggered - read what we can while writing randomly

---
 .../streaming/MessageInputStreamTest.java     | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/apps/streaming/java/test/net/i2p/client/streaming/MessageInputStreamTest.java b/apps/streaming/java/test/net/i2p/client/streaming/MessageInputStreamTest.java
index c1d0b2145b..24b1167bfe 100644
--- a/apps/streaming/java/test/net/i2p/client/streaming/MessageInputStreamTest.java
+++ b/apps/streaming/java/test/net/i2p/client/streaming/MessageInputStreamTest.java
@@ -110,12 +110,52 @@ public class MessageInputStreamTest {
         }
     }
     
+    public void testStaggered() {
+        byte orig[] = new byte[256*1024];
+        byte read[] = new byte[orig.length];
+        _context.random().nextBytes(orig);
+        
+        MessageInputStream in = new MessageInputStream(_context);
+        ArrayList order = new ArrayList(32);
+        for (int i = 0; i < orig.length / 1024; i++)
+            order.add(new Integer(i));
+        Collections.shuffle(order);
+        
+        int offset = 0;
+        for (int i = 0; i < orig.length / 1024; i++) {
+            byte msg[] = new byte[1024];
+            Integer cur = (Integer)order.get(i);
+            System.arraycopy(orig, cur.intValue()*1024, msg, 0, 1024);
+            in.messageReceived(cur.intValue(), msg);
+            _log.debug("Injecting " + cur);
+            
+            try {
+                if (in.available() > 0) {
+                    int curRead = in.read(read, offset, read.length-offset);
+                    _log.debug("read " + curRead);
+                    if (curRead == -1)
+                        throw new RuntimeException("EOF with offset " + offset);
+                    else
+                        offset += curRead;
+                }
+            } catch (IOException ioe) {
+                throw new RuntimeException("IOE: " + ioe.getMessage());
+            }
+        }
+        
+        if (!DataHelper.eq(orig, read))
+            throw new RuntimeException("Failed test: data read is not equal");
+
+        _log.info("Passed test: staggered");
+    }
+    
     public static void main(String args[]) {
         MessageInputStreamTest t = new MessageInputStreamTest();
         try {
             t.testInOrder();
             t.testRandomOrder();
             t.testRandomDups();
+            t.testStaggered();
         } catch (Exception e) {
             e.printStackTrace();
         }
-- 
GitLab