Explorar el Código

Move `run_concurrently` from `tests/utils` to `src/oscar/test/utils`

This change will allow to import `run_concurrently` helper for test
purposes in other libraries.
master
Basil Dubyk hace 6 años
padre
commit
be7bff35ae
Se han modificado 3 ficheros con 37 adiciones y 37 borrados
  1. 36
    0
      src/oscar/test/utils.py
  2. 1
    1
      tests/integration/order/test_creator.py
  3. 0
    36
      tests/utils.py

+ 36
- 0
src/oscar/test/utils.py Ver fichero

@@ -1,4 +1,8 @@
1
+import queue
2
+import threading
3
+
1 4
 from django.contrib.auth.models import AnonymousUser
5
+from django.db import connection
2 6
 from django.contrib.messages.storage.fallback import FallbackStorage
3 7
 from django.contrib.sessions.backends.db import SessionStore
4 8
 from django.core.signing import Signer
@@ -26,3 +30,35 @@ class RequestFactory(BaseRequestFactory):
26 30
         request.cookies_to_delete = []
27 31
 
28 32
         return request
33
+
34
+
35
+def run_concurrently(fn, kwargs=None, num_threads=5):
36
+    exceptions = queue.Queue()
37
+
38
+    def worker(**kwargs):
39
+        try:
40
+            fn(**kwargs)
41
+        except Exception as exc:
42
+            exceptions.put(exc)
43
+        else:
44
+            exceptions.put(None)
45
+        finally:
46
+            connection.close()
47
+
48
+    kwargs = kwargs if kwargs is not None else {}
49
+
50
+    # Run them
51
+    threads = [
52
+        threading.Thread(target=worker, name='thread-%d' % i, kwargs=kwargs)
53
+        for i in range(num_threads)
54
+    ]
55
+    try:
56
+        for thread in threads:
57
+            thread.start()
58
+    finally:
59
+        for thread in threads:
60
+            thread.join()
61
+
62
+    # Retrieve exceptions
63
+    exceptions = [exceptions.get(block=False) for i in range(num_threads)]
64
+    return [exc for exc in exceptions if exc is not None]

+ 1
- 1
tests/integration/order/test_creator.py Ver fichero

@@ -19,7 +19,7 @@ from oscar.apps.voucher.models import Voucher
19 19
 from oscar.core.loading import get_class
20 20
 from oscar.test import factories
21 21
 from oscar.test.basket import add_product
22
-from tests.utils import run_concurrently
22
+from oscar.test.utils import run_concurrently
23 23
 
24 24
 Range = get_class('offer.models', 'Range')
25 25
 Benefit = get_class('offer.models', 'Benefit')

+ 0
- 36
tests/utils.py Ver fichero

@@ -1,36 +0,0 @@
1
-import queue
2
-import threading
3
-
4
-from django.db import connection
5
-
6
-
7
-def run_concurrently(fn, kwargs=None, num_threads=5):
8
-    exceptions = queue.Queue()
9
-
10
-    def worker(**kwargs):
11
-        try:
12
-            fn(**kwargs)
13
-        except Exception as exc:
14
-            exceptions.put(exc)
15
-        else:
16
-            exceptions.put(None)
17
-        finally:
18
-            connection.close()
19
-
20
-    kwargs = kwargs if kwargs is not None else {}
21
-
22
-    # Run them
23
-    threads = [
24
-        threading.Thread(target=worker, name='thread-%d' % i, kwargs=kwargs)
25
-        for i in range(num_threads)
26
-    ]
27
-    try:
28
-        for thread in threads:
29
-            thread.start()
30
-    finally:
31
-        for thread in threads:
32
-            thread.join()
33
-
34
-    # Retrieve exceptions
35
-    exceptions = [exceptions.get(block=False) for i in range(num_threads)]
36
-    return [exc for exc in exceptions if exc is not None]

Loading…
Cancelar
Guardar