图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.6 KiB

  1. import unittest
  2. from unittest.mock import patch
  3. import pytest
  4. try:
  5. from kazoo.handlers.eventlet import green_socket as socket
  6. EVENTLET_HANDLER_AVAILABLE = True
  7. except ImportError:
  8. EVENTLET_HANDLER_AVAILABLE = False
  9. class TestCreateTCPConnection(unittest.TestCase):
  10. def test_timeout_arg(self):
  11. from kazoo.handlers import utils
  12. from kazoo.handlers.utils import create_tcp_connection, socket, time
  13. with patch.object(socket, "create_connection") as create_connection:
  14. with patch.object(utils, "_set_default_tcpsock_options"):
  15. # Ensure a gap between calls to time.time() does not result in
  16. # create_connection being called with a negative timeout
  17. # argument.
  18. with patch.object(time, "time", side_effect=range(10)):
  19. create_tcp_connection(
  20. socket, ("127.0.0.1", 2181), timeout=1.5
  21. )
  22. for call_args in create_connection.call_args_list:
  23. timeout = call_args[0][1]
  24. assert timeout >= 0, "socket timeout must be nonnegative"
  25. def test_timeout_arg_eventlet(self):
  26. if not EVENTLET_HANDLER_AVAILABLE:
  27. pytest.skip("eventlet handler not available.")
  28. from kazoo.handlers import utils
  29. from kazoo.handlers.utils import create_tcp_connection, time
  30. with patch.object(socket, "create_connection") as create_connection:
  31. with patch.object(utils, "_set_default_tcpsock_options"):
  32. # Ensure a gap between calls to time.time() does not result in
  33. # create_connection being called with a negative timeout
  34. # argument.
  35. with patch.object(time, "time", side_effect=range(10)):
  36. create_tcp_connection(
  37. socket, ("127.0.0.1", 2181), timeout=1.5
  38. )
  39. for call_args in create_connection.call_args_list:
  40. timeout = call_args[0][1]
  41. assert timeout >= 0, "socket timeout must be nonnegative"
  42. def test_slow_connect(self):
  43. # Currently, create_tcp_connection will raise a socket timeout if it
  44. # takes longer than the specified "timeout" to create a connection.
  45. # In the future, "timeout" might affect only the created socket and not
  46. # the time it takes to create it.
  47. from kazoo.handlers.utils import create_tcp_connection, socket, time
  48. # Simulate a second passing between calls to check the current time.
  49. with patch.object(time, "time", side_effect=range(10)):
  50. with pytest.raises(socket.error):
  51. create_tcp_connection(socket, ("127.0.0.1", 2181), timeout=0.5)
  52. def test_negative_timeout(self):
  53. from kazoo.handlers.utils import create_tcp_connection, socket
  54. with pytest.raises(socket.error):
  55. create_tcp_connection(socket, ("127.0.0.1", 2181), timeout=-1)
  56. def test_zero_timeout(self):
  57. # Rather than pass '0' through as a timeout to
  58. # socket.create_connection, create_tcp_connection should raise
  59. # socket.error. This is because the socket library treats '0' as an
  60. # indicator to create a non-blocking socket.
  61. from kazoo.handlers.utils import create_tcp_connection, socket, time
  62. # Simulate no time passing between calls to check the current time.
  63. with patch.object(time, "time", return_value=time.time()):
  64. with pytest.raises(socket.error):
  65. create_tcp_connection(socket, ("127.0.0.1", 2181), timeout=0)