summaryrefslogtreecommitdiff
blob: a2a8929b08d4eedaa5f0c3b8ee6dacbe24d7815d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
From c4f918572751151eb3bfc7dfa94580b3e2867a9e Mon Sep 17 00:00:00 2001
From: Jon Dufresne <jon.dufresne@gmail.com>
Date: Sun, 3 Feb 2019 09:02:24 -0800
Subject: [PATCH] Fix unhandled exceptions from threads during tests

A queue.Queue() object was not always passed to SessionThread. In this
case, SessionThread._make_request() would raise an exception trying to
call methods on the expected object. Now, always pass a usable object to
SessionThread.

Previously appeared as:

    Traceback (most recent call last):
      File "/usr/lib64/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib64/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "toolbelt/requests_toolbelt/threaded/thread.py", line 41, in _make_request
        kwargs = self._jobs.get_nowait()
    AttributeError: 'NoneType' object has no attribute 'get_nowait'

    Exception in thread cd08fad6-d21d-41b0-921e-737a149b12be:
    Traceback (most recent call last):
      File "/usr/lib64/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib64/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "toolbelt/requests_toolbelt/threaded/thread.py", line 41, in _make_request
        kwargs = self._jobs.get_nowait()
    AttributeError: 'NoneType' object has no attribute 'get_nowait'

    Exception in thread 4fb72f0d-ba1c-4a78-97a2-4a7283ea01fe:
    Traceback (most recent call last):
      File "/usr/lib64/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib64/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "toolbelt/requests_toolbelt/threaded/thread.py", line 41, in _make_request
        kwargs = self._jobs.get_nowait()
    AttributeError: 'NoneType' object has no attribute 'get_nowait'

    Exception in thread 5f3711af-0c01-4821-9e25-8074bbbf769b:
    Traceback (most recent call last):
      File "/usr/lib64/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib64/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "toolbelt/requests_toolbelt/threaded/thread.py", line 41, in _make_request
        kwargs = self._jobs.get_nowait()
    AttributeError: 'NoneType' object has no attribute 'get_nowait'
---
 tests/threaded/test_pool.py   | 15 ++++++++++-----
 tests/threaded/test_thread.py |  5 ++++-
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/tests/threaded/test_pool.py b/tests/threaded/test_pool.py
index b0653bb..b949dd8 100644
--- a/tests/threaded/test_pool.py
+++ b/tests/threaded/test_pool.py
@@ -26,32 +26,37 @@ def test_requires_positive_number_of_processes(self):
 
     def test_number_of_processes_can_be_arbitrary(self):
         """Show that the number of processes can be set."""
-        p = pool.Pool(None, num_processes=100)
+        job_queue = queue.Queue()
+        p = pool.Pool(job_queue, num_processes=100)
         assert p._processes == 100
         assert len(p._pool) == 100
 
-        p = pool.Pool(None, num_processes=1)
+        job_queue = queue.Queue()
+        p = pool.Pool(job_queue, num_processes=1)
         assert p._processes == 1
         assert len(p._pool) == 1
 
     def test_initializer_is_called(self):
         """Ensure that the initializer function is called."""
+        job_queue = queue.Queue()
         initializer = mock.MagicMock()
-        pool.Pool(None, num_processes=1, initializer=initializer)
+        pool.Pool(job_queue, num_processes=1, initializer=initializer)
         assert initializer.called is True
         initializer.assert_called_once_with(mock.ANY)
 
     def test_auth_generator_is_called(self):
         """Ensure that the auth_generator function is called."""
+        job_queue = queue.Queue()
         auth_generator = mock.MagicMock()
-        pool.Pool(None, num_processes=1, auth_generator=auth_generator)
+        pool.Pool(job_queue, num_processes=1, auth_generator=auth_generator)
         assert auth_generator.called is True
         auth_generator.assert_called_once_with(mock.ANY)
 
     def test_session_is_called(self):
         """Ensure that the session function is called."""
+        job_queue = queue.Queue()
         session = mock.MagicMock()
-        pool.Pool(None, num_processes=1, session=session)
+        pool.Pool(job_queue, num_processes=1, session=session)
         assert session.called is True
         session.assert_called_once_with()
 
diff --git a/tests/threaded/test_thread.py b/tests/threaded/test_thread.py
index bb92f7f..fd7e96b 100644
--- a/tests/threaded/test_thread.py
+++ b/tests/threaded/test_thread.py
@@ -19,6 +19,8 @@ def _make_mocks():
 
 def _initialize_a_session_thread(session=None, job_queue=None,
                                  response_queue=None, exception_queue=None):
+    if job_queue is None:
+        job_queue = queue.Queue()
     with mock.patch.object(threading, 'Thread') as Thread:
         thread_instance = mock.MagicMock()
         Thread.return_value = thread_instance
@@ -52,10 +54,11 @@ def test_thread_initialization(self):
 
     def test_is_alive_proxies_to_worker(self):
         """Test that we proxy the is_alive method to the Thread."""
+        job_queue = queue.Queue()
         with mock.patch.object(threading, 'Thread') as Thread:
             thread_instance = mock.MagicMock()
             Thread.return_value = thread_instance
-            st = thread.SessionThread(None, None, None, None)
+            st = thread.SessionThread(None, job_queue, None, None)
 
         st.is_alive()
         thread_instance.is_alive.assert_called_once_with()