From 1cdfecb446e2e6369fd07b059d6759eccd52e344 Mon Sep 17 00:00:00 2001
From: robk-tahoe <robk-tahoe@allmydata.com>
Date: Mon, 20 Oct 2008 16:24:27 -0700
Subject: [PATCH] fuse/runtests: added 'read_in_random_order' test

this test uploads a test file to tahoe, and then reads the file from fuse,
but reads the blocks of the file in a random order; this is designed to
exercise the asynchronous download feature of blackmatch - where the file
is downloaded from tahoe asynchronously, and rather than blocking open()
for the entirety of the download, instead individual read() calls are
blocked until enough of the file has been downloaded to satisfy them
---
 contrib/fuse/runtests.py | 43 ++++++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/contrib/fuse/runtests.py b/contrib/fuse/runtests.py
index 45d157c7..67f68091 100644
--- a/contrib/fuse/runtests.py
+++ b/contrib/fuse/runtests.py
@@ -159,6 +159,12 @@ def run_unit_tests(argv):
 def run_system_test(config):
     return SystemTest(config).run()
 
+def drepr(obj):
+    r = repr(obj)
+    if len(r) > 200:
+        return r[:100] + ' ... ' + r[-100:]
+    else:
+        return r
 
 ### System Testing:
 class SystemTest (object):
@@ -449,6 +455,37 @@ class SystemTest (object):
             tmpl = 'Expected file contents %r but found %r'
             raise TestFailure(tmpl, body, found)
 
+    def test_read_in_random_order(self, testcap, testdir):
+        sz = 2**20
+        bs = 2**10
+        assert(sz % bs == 0)
+        name = 'random_read_order'
+        body = os.urandom(sz)
+
+        cap = self.webapi_call('PUT', '/uri', body)
+        self.attach_node(testcap, cap, name)
+
+        # XXX this should also do a test where sz%bs != 0, so that it correctly tests
+        # the edge case where the last read is a 'short' block
+        path = os.path.join(testdir, name)
+        fsize = os.path.getsize(path)
+        if fsize != len(body):
+            tmpl = 'Expected file size %s but found %s'
+            raise TestFailure(tmpl, len(body), fsize)
+
+        f = open(path, 'r')
+        posns = range(0,sz,bs)
+        random.shuffle(posns)
+        data = [None] * (sz/bs)
+        for p in posns:
+            f.seek(p)
+            data[p/bs] = f.read(bs)
+        found = ''.join(data)
+
+        if found != body:
+            tmpl = 'Expected file contents %s but found %s'
+            raise TestFailure(tmpl, drepr(body), drepr(found))
+
     def get_file(self, dircap, path):
         body = self.webapi_call('GET', '/uri/%s/%s' % (dircap, path))
         return body
@@ -483,12 +520,6 @@ class SystemTest (object):
 
     def _check_write(self, testcap, name, expected_body):
         uploaded_body = self.get_file(testcap, name)
-        def drepr(obj):
-            r = repr(obj)
-            if len(r) > 200:
-                return r[:100] + ' ... ' + r[-100:]
-            else:
-                return r
         if uploaded_body != expected_body:
             tmpl = 'Expected file contents %s but found %s'
             raise TestFailure(tmpl, drepr(expected_body), drepr(uploaded_body))
-- 
2.45.2