Merge branch 'master' into 4823-python-sdk-writable-collection-api
[arvados.git] / sdk / python / tests / arvados_testutil.py
index aa0da982be2ed831982a6dccd22899f185eddb11..2926b20bfa9608d649cac04cfa0ae35268a5ca10 100644 (file)
@@ -8,6 +8,7 @@ import httplib2
 import io
 import mock
 import os
+import Queue
 import requests
 import shutil
 import tempfile
@@ -19,6 +20,18 @@ TEST_HOST = '100::'
 
 skip_sleep = mock.patch('time.sleep', lambda n: None)  # clown'll eat me
 
+def queue_with(items):
+    """Return a thread-safe iterator that yields the given items.
+
+    +items+ can be given as an array or an iterator. If an iterator is
+    given, it will be consumed to fill the queue before queue_with()
+    returns.
+    """
+    queue = Queue.Queue()
+    for val in items:
+        queue.put(val)
+    return lambda *args, **kwargs: queue.get(block=False)
+
 # fake_httplib2_response and mock_responses
 # mock calls to httplib2.Http.request()
 def fake_httplib2_response(code, **headers):
@@ -27,8 +40,8 @@ def fake_httplib2_response(code, **headers):
     return httplib2.Response(headers)
 
 def mock_responses(body, *codes, **headers):
-    return mock.patch('httplib2.Http.request', side_effect=(
-            (fake_httplib2_response(code, **headers), body) for code in codes))
+    return mock.patch('httplib2.Http.request', side_effect=queue_with((
+        (fake_httplib2_response(code, **headers), body) for code in codes)))
 
 # fake_requests_response, mock_get_responses and mock_put_responses
 # mock calls to requests.get() and requests.put()
@@ -45,14 +58,14 @@ def mock_put_responses(body, *codes, **headers):
     if isinstance(body, tuple):
         codes = list(codes)
         codes.insert(0, body)
-        m.return_value.put.side_effect = (fake_requests_response(code, b, **headers) for b, code in codes)
+        m.return_value.put.side_effect = queue_with((fake_requests_response(code, b, **headers) for b, code in codes))
     else:
-        m.return_value.put.side_effect = (fake_requests_response(code, body, **headers) for code in codes)
+        m.return_value.put.side_effect = queue_with((fake_requests_response(code, body, **headers) for code in codes))
     return mock.patch('requests.Session', m)
 
 def mock_get_responses(body, *codes, **headers):
     m = mock.MagicMock()
-    m.return_value.get.side_effect = (fake_requests_response(code, body, **headers) for code in codes)
+    m.return_value.get.side_effect = queue_with((fake_requests_response(code, body, **headers) for code in codes))
     return mock.patch('requests.Session', m)
 
 def mock_get(side_effect):