summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-09-08 23:09:42 +0000
committerGerrit Code Review <review@openstack.org>2015-09-08 23:09:42 +0000
commitf630894f66e6048daf299fb2bab2ffeff6666693 (patch)
treedfb8f428bcc7dfaf79ab5d5e511aacbbdd6446ff
parent6231459156c7ffcea574efb2e01ce0b5ec78d9ac (diff)
parent33b24a6984c8de2f26af7900202bb85b6b5db125 (diff)
downloadpython-keystoneclient-f630894f66e6048daf299fb2bab2ffeff6666693.tar.gz
Merge "Fixes missing socket attribute error during init_poolmanager"
-rw-r--r--keystoneclient/session.py21
-rw-r--r--keystoneclient/tests/unit/test_session.py37
2 files changed, 54 insertions, 4 deletions
diff --git a/keystoneclient/session.py b/keystoneclient/session.py
index 6f2e930..e3db3b9 100644
--- a/keystoneclient/session.py
+++ b/keystoneclient/session.py
@@ -938,10 +938,6 @@ class TCPKeepAliveAdapter(requests.adapters.HTTPAdapter):
(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),
# Turn on TCP Keep-Alive
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
- # Set the maximum number of keep-alive probes
- (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 4),
- # Send keep-alive probes every 15 seconds
- (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 15),
]
# Some operating systems (e.g., OSX) do not support setting
@@ -952,6 +948,23 @@ class TCPKeepAliveAdapter(requests.adapters.HTTPAdapter):
(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 60)
]
+ # TODO(claudiub): Windows does not contain the TCP_KEEPCNT and
+ # TCP_KEEPINTVL socket attributes. Instead, it contains
+ # SIO_KEEPALIVE_VALS, which can be set via ioctl, which should be
+ # set once it is available in requests.
+ # https://msdn.microsoft.com/en-us/library/dd877220%28VS.85%29.aspx
+ if hasattr(socket, 'TCP_KEEPCNT'):
+ socket_options += [
+ # Set the maximum number of keep-alive probes
+ (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 4)
+ ]
+
+ if hasattr(socket, 'TCP_KEEPINTVL'):
+ socket_options += [
+ # Send keep-alive probes every 15 seconds
+ (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 15)
+ ]
+
# After waiting 60 seconds, and then sending a probe once every 15
# seconds 4 times, these options should ensure that a connection
# hands for no longer than 2 minutes before a ConnectionError is
diff --git a/keystoneclient/tests/unit/test_session.py b/keystoneclient/tests/unit/test_session.py
index f7384cd..a5c4474 100644
--- a/keystoneclient/tests/unit/test_session.py
+++ b/keystoneclient/tests/unit/test_session.py
@@ -280,6 +280,43 @@ class SessionTests(utils.TestCase):
self.assertEqual(1, logger.debug.call_count)
+class TCPKeepAliveAdapter(utils.TestCase):
+
+ @mock.patch.object(client_session, 'socket')
+ @mock.patch('requests.adapters.HTTPAdapter.init_poolmanager')
+ def test_init_poolmanager_all_options(self, mock_parent_init_poolmanager,
+ mock_socket):
+ # properties expected to be in socket.
+ mock_socket.TCP_KEEPIDLE = mock.sentinel.TCP_KEEPIDLE
+ mock_socket.TCP_KEEPCNT = mock.sentinel.TCP_KEEPCNT
+ mock_socket.TCP_KEEPINTVL = mock.sentinel.TCP_KEEPINTVL
+ desired_opts = [mock_socket.TCP_KEEPIDLE, mock_socket.TCP_KEEPCNT,
+ mock_socket.TCP_KEEPINTVL]
+
+ adapter = client_session.TCPKeepAliveAdapter()
+ adapter.init_poolmanager()
+
+ call_args, call_kwargs = mock_parent_init_poolmanager.call_args
+ called_socket_opts = call_kwargs['socket_options']
+ call_options = [opt for (protocol, opt, value) in called_socket_opts]
+ for opt in desired_opts:
+ self.assertIn(opt, call_options)
+
+ @mock.patch.object(client_session, 'socket')
+ @mock.patch('requests.adapters.HTTPAdapter.init_poolmanager')
+ def test_init_poolmanager(self, mock_parent_init_poolmanager, mock_socket):
+ spec = ['IPPROTO_TCP', 'TCP_NODELAY', 'SOL_SOCKET', 'SO_KEEPALIVE']
+ mock_socket.mock_add_spec(spec)
+ adapter = client_session.TCPKeepAliveAdapter()
+ adapter.init_poolmanager()
+
+ call_args, call_kwargs = mock_parent_init_poolmanager.call_args
+ called_socket_opts = call_kwargs['socket_options']
+ call_options = [opt for (protocol, opt, value) in called_socket_opts]
+ self.assertEqual([mock_socket.TCP_NODELAY, mock_socket.SO_KEEPALIVE],
+ call_options)
+
+
class RedirectTests(utils.TestCase):
REDIRECT_CHAIN = ['http://myhost:3445/',