2013-06-26 22:47:49 -07:00
|
|
|
# Copyright (c) 2010-2013 OpenStack, LLC.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import sys
|
2015-12-04 11:28:05 -08:00
|
|
|
import unittest
|
2013-06-26 22:47:49 -07:00
|
|
|
import threading
|
2014-03-24 18:20:22 +01:00
|
|
|
import six
|
2014-04-04 21:13:01 +02:00
|
|
|
|
|
|
|
from concurrent.futures import as_completed
|
2014-03-24 18:20:22 +01:00
|
|
|
from six.moves.queue import Queue, Empty
|
2014-04-04 21:13:01 +02:00
|
|
|
from time import sleep
|
2013-06-26 22:47:49 -07:00
|
|
|
|
|
|
|
from swiftclient import multithreading as mt
|
2015-01-04 21:14:02 +00:00
|
|
|
from .utils import CaptureStream
|
2013-06-26 22:47:49 -07:00
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class ThreadTestCase(unittest.TestCase):
|
2013-06-26 22:47:49 -07:00
|
|
|
def setUp(self):
|
|
|
|
super(ThreadTestCase, self).setUp()
|
2014-04-04 21:13:01 +02:00
|
|
|
self.got_items = Queue()
|
2013-06-26 22:47:49 -07:00
|
|
|
self.got_args_kwargs = Queue()
|
|
|
|
self.starting_thread_count = threading.active_count()
|
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
def _func(self, conn, item, *args, **kwargs):
|
|
|
|
self.got_items.put((conn, item))
|
2013-06-26 22:47:49 -07:00
|
|
|
self.got_args_kwargs.put((args, kwargs))
|
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
if item == 'sleep':
|
|
|
|
sleep(1)
|
|
|
|
if item == 'go boom':
|
2013-06-26 22:47:49 -07:00
|
|
|
raise Exception('I went boom!')
|
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
return 'success'
|
|
|
|
|
|
|
|
def _create_conn(self):
|
|
|
|
return "This is a connection"
|
|
|
|
|
|
|
|
def _create_conn_fail(self):
|
|
|
|
raise Exception("This is a failed connection")
|
2013-06-26 22:47:49 -07:00
|
|
|
|
|
|
|
def assertQueueContains(self, queue, expected_contents):
|
|
|
|
got_contents = []
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
got_contents.append(queue.get(timeout=0.1))
|
|
|
|
except Empty:
|
|
|
|
pass
|
|
|
|
if isinstance(expected_contents, set):
|
|
|
|
got_contents = set(got_contents)
|
|
|
|
self.assertEqual(expected_contents, got_contents)
|
|
|
|
|
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
class TestConnectionThreadPoolExecutor(ThreadTestCase):
|
2013-06-26 22:47:49 -07:00
|
|
|
def setUp(self):
|
2014-04-04 21:13:01 +02:00
|
|
|
super(TestConnectionThreadPoolExecutor, self).setUp()
|
2013-06-26 22:47:49 -07:00
|
|
|
self.input_queue = Queue()
|
|
|
|
self.stored_results = []
|
|
|
|
|
|
|
|
def tearDown(self):
|
2014-04-04 21:13:01 +02:00
|
|
|
super(TestConnectionThreadPoolExecutor, self).tearDown()
|
|
|
|
|
|
|
|
def test_submit_good_connection(self):
|
|
|
|
ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 1)
|
|
|
|
with ctpe as pool:
|
|
|
|
# Try submitting a job that should succeed
|
|
|
|
f = pool.submit(self._func, "succeed")
|
|
|
|
f.result()
|
|
|
|
self.assertQueueContains(
|
|
|
|
self.got_items,
|
|
|
|
[("This is a connection", "succeed")]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now a job that fails
|
|
|
|
try:
|
|
|
|
f = pool.submit(self._func, "go boom")
|
|
|
|
f.result()
|
|
|
|
except Exception as e:
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual('I went boom!', str(e))
|
2015-09-09 17:41:21 -07:00
|
|
|
else:
|
|
|
|
self.fail('I never went boom!')
|
2014-04-04 21:13:01 +02:00
|
|
|
|
|
|
|
# Has the connection been returned to the pool?
|
|
|
|
f = pool.submit(self._func, "succeed")
|
|
|
|
f.result()
|
|
|
|
self.assertQueueContains(
|
|
|
|
self.got_items,
|
|
|
|
[
|
|
|
|
("This is a connection", "go boom"),
|
|
|
|
("This is a connection", "succeed")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_submit_bad_connection(self):
|
|
|
|
ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn_fail, 1)
|
|
|
|
with ctpe as pool:
|
|
|
|
# Now a connection that fails
|
|
|
|
try:
|
|
|
|
f = pool.submit(self._func, "succeed")
|
|
|
|
f.result()
|
|
|
|
except Exception as e:
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual('This is a failed connection', str(e))
|
2015-09-09 17:41:21 -07:00
|
|
|
else:
|
|
|
|
self.fail('The connection did not fail')
|
2014-04-04 21:13:01 +02:00
|
|
|
|
|
|
|
# Make sure we don't lock up on failed connections
|
|
|
|
try:
|
|
|
|
f = pool.submit(self._func, "go boom")
|
|
|
|
f.result()
|
|
|
|
except Exception as e:
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual('This is a failed connection', str(e))
|
2015-09-09 17:41:21 -07:00
|
|
|
else:
|
|
|
|
self.fail('The connection did not fail')
|
2014-04-04 21:13:01 +02:00
|
|
|
|
|
|
|
def test_lazy_connections(self):
|
|
|
|
ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 10)
|
|
|
|
with ctpe as pool:
|
|
|
|
# Submit multiple jobs sequentially - should only use 1 conn
|
|
|
|
f = pool.submit(self._func, "succeed")
|
|
|
|
f.result()
|
|
|
|
f = pool.submit(self._func, "succeed")
|
|
|
|
f.result()
|
|
|
|
f = pool.submit(self._func, "succeed")
|
|
|
|
f.result()
|
|
|
|
|
|
|
|
expected_connections = [(0, "This is a connection")]
|
|
|
|
expected_connections.extend([(x, None) for x in range(1, 10)])
|
|
|
|
|
|
|
|
self.assertQueueContains(
|
|
|
|
pool._connections, expected_connections
|
|
|
|
)
|
|
|
|
|
|
|
|
ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 10)
|
|
|
|
with ctpe as pool:
|
|
|
|
fs = []
|
|
|
|
f1 = pool.submit(self._func, "sleep")
|
|
|
|
f2 = pool.submit(self._func, "sleep")
|
|
|
|
f3 = pool.submit(self._func, "sleep")
|
|
|
|
fs.extend([f1, f2, f3])
|
|
|
|
|
|
|
|
expected_connections = [
|
|
|
|
(0, "This is a connection"),
|
|
|
|
(1, "This is a connection"),
|
|
|
|
(2, "This is a connection")
|
|
|
|
]
|
|
|
|
expected_connections.extend([(x, None) for x in range(3, 10)])
|
|
|
|
|
|
|
|
for f in as_completed(fs):
|
|
|
|
f.result()
|
|
|
|
|
|
|
|
self.assertQueueContains(
|
|
|
|
pool._connections, expected_connections
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestOutputManager(unittest.TestCase):
|
2014-04-04 21:13:01 +02:00
|
|
|
|
|
|
|
def test_instantiation(self):
|
|
|
|
output_manager = mt.OutputManager()
|
|
|
|
|
|
|
|
self.assertEqual(sys.stdout, output_manager.print_stream)
|
|
|
|
self.assertEqual(sys.stderr, output_manager.error_stream)
|
2013-06-26 22:47:49 -07:00
|
|
|
|
|
|
|
def test_printers(self):
|
2015-01-04 21:14:02 +00:00
|
|
|
out_stream = CaptureStream(sys.stdout)
|
|
|
|
err_stream = CaptureStream(sys.stderr)
|
2014-04-04 21:13:01 +02:00
|
|
|
starting_thread_count = threading.active_count()
|
2013-06-26 22:47:49 -07:00
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
with mt.OutputManager(
|
2013-06-26 22:47:49 -07:00
|
|
|
print_stream=out_stream,
|
|
|
|
error_stream=err_stream) as thread_manager:
|
|
|
|
|
|
|
|
# Sanity-checking these gives power to the previous test which
|
|
|
|
# looked at the default values of thread_manager.print/error_stream
|
|
|
|
self.assertEqual(out_stream, thread_manager.print_stream)
|
|
|
|
self.assertEqual(err_stream, thread_manager.error_stream)
|
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
# No printing has happened yet, so no new threads
|
|
|
|
self.assertEqual(starting_thread_count,
|
2013-06-26 22:47:49 -07:00
|
|
|
threading.active_count())
|
|
|
|
|
|
|
|
thread_manager.print_msg('one-argument')
|
|
|
|
thread_manager.print_msg('one %s, %d fish', 'fish', 88)
|
|
|
|
thread_manager.error('I have %d problems, but a %s is not one',
|
|
|
|
99, u'\u062A\u062A')
|
|
|
|
thread_manager.print_msg('some\n%s\nover the %r', 'where',
|
|
|
|
u'\u062A\u062A')
|
|
|
|
thread_manager.error('one-error-argument')
|
|
|
|
thread_manager.error('Sometimes\n%.1f%% just\ndoes not\nwork!',
|
|
|
|
3.14159)
|
2015-01-04 21:14:02 +00:00
|
|
|
thread_manager.print_raw(
|
|
|
|
u'some raw bytes: \u062A\u062A'.encode('utf-8'))
|
2013-06-26 22:47:49 -07:00
|
|
|
|
2015-01-14 11:10:48 +00:00
|
|
|
thread_manager.print_items([
|
2015-05-17 23:51:37 -07:00
|
|
|
('key', 'value'),
|
|
|
|
('object', u'O\u0308bject'),
|
2015-01-14 11:10:48 +00:00
|
|
|
])
|
|
|
|
|
2015-05-17 23:51:37 -07:00
|
|
|
thread_manager.print_raw(b'\xffugly\xffraw')
|
|
|
|
|
2014-04-04 21:13:01 +02:00
|
|
|
# Now we have a thread for error printing and a thread for
|
|
|
|
# normal print messages
|
|
|
|
self.assertEqual(starting_thread_count + 2,
|
|
|
|
threading.active_count())
|
|
|
|
|
|
|
|
# The threads should have been cleaned up
|
|
|
|
self.assertEqual(starting_thread_count, threading.active_count())
|
2013-06-26 22:47:49 -07:00
|
|
|
|
2014-03-31 12:42:50 +02:00
|
|
|
if six.PY3:
|
|
|
|
over_the = "over the '\u062a\u062a'\n"
|
|
|
|
else:
|
|
|
|
over_the = "over the u'\\u062a\\u062a'\n"
|
2015-01-04 21:14:02 +00:00
|
|
|
# We write to the CaptureStream so no decoding is performed
|
|
|
|
self.assertEqual(''.join([
|
2013-06-26 22:47:49 -07:00
|
|
|
'one-argument\n',
|
|
|
|
'one fish, 88 fish\n',
|
2015-01-14 11:10:48 +00:00
|
|
|
'some\n', 'where\n',
|
2015-05-17 23:51:37 -07:00
|
|
|
over_the,
|
|
|
|
u'some raw bytes: \u062a\u062a',
|
2015-01-14 11:10:48 +00:00
|
|
|
' key: value\n',
|
2015-05-17 23:51:37 -07:00
|
|
|
u' object: O\u0308bject\n'
|
|
|
|
]).encode('utf8') + b'\xffugly\xffraw', out_stream.getvalue())
|
2013-06-26 22:47:49 -07:00
|
|
|
|
2015-01-04 21:14:02 +00:00
|
|
|
self.assertEqual(''.join([
|
2015-05-17 23:51:37 -07:00
|
|
|
u'I have 99 problems, but a \u062A\u062A is not one\n',
|
2013-06-26 22:47:49 -07:00
|
|
|
'one-error-argument\n',
|
2015-01-04 21:14:02 +00:00
|
|
|
'Sometimes\n', '3.1% just\n', 'does not\n', 'work!\n'
|
2015-05-17 23:51:37 -07:00
|
|
|
]), err_stream.getvalue().decode('utf8'))
|
2013-06-26 22:47:49 -07:00
|
|
|
|
|
|
|
self.assertEqual(3, thread_manager.error_count)
|