test_default.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.internet.default}.
  5. """
  6. from __future__ import division, absolute_import
  7. import select, sys
  8. from twisted.trial.unittest import SynchronousTestCase
  9. from twisted.python.runtime import Platform
  10. from twisted.python.reflect import requireModule
  11. from twisted.internet import default
  12. from twisted.internet.default import _getInstallFunction, install
  13. from twisted.internet.test.test_main import NoReactor
  14. from twisted.internet.interfaces import IReactorCore
  15. unix = Platform('posix', 'other')
  16. linux = Platform('posix', 'linux2')
  17. windows = Platform('nt', 'win32')
  18. osx = Platform('posix', 'darwin')
  19. class PollReactorTests(SynchronousTestCase):
  20. """
  21. Tests for the cases of L{twisted.internet.default._getInstallFunction}
  22. in which it picks the poll(2) or epoll(7)-based reactors.
  23. """
  24. def assertIsPoll(self, install):
  25. """
  26. Assert the given function will install the poll() reactor, or select()
  27. if poll() is unavailable.
  28. """
  29. if hasattr(select, "poll"):
  30. self.assertEqual(
  31. install.__module__, 'twisted.internet.pollreactor')
  32. else:
  33. self.assertEqual(
  34. install.__module__, 'twisted.internet.selectreactor')
  35. def test_unix(self):
  36. """
  37. L{_getInstallFunction} chooses the poll reactor on arbitrary Unix
  38. platforms, falling back to select(2) if it is unavailable.
  39. """
  40. install = _getInstallFunction(unix)
  41. self.assertIsPoll(install)
  42. def test_linux(self):
  43. """
  44. L{_getInstallFunction} chooses the epoll reactor on Linux, or poll if
  45. epoll is unavailable.
  46. """
  47. install = _getInstallFunction(linux)
  48. if requireModule('twisted.internet.epollreactor') is None:
  49. self.assertIsPoll(install)
  50. else:
  51. self.assertEqual(
  52. install.__module__, 'twisted.internet.epollreactor')
  53. class SelectReactorTests(SynchronousTestCase):
  54. """
  55. Tests for the cases of L{twisted.internet.default._getInstallFunction}
  56. in which it picks the select(2)-based reactor.
  57. """
  58. def test_osx(self):
  59. """
  60. L{_getInstallFunction} chooses the select reactor on OS X.
  61. """
  62. install = _getInstallFunction(osx)
  63. self.assertEqual(
  64. install.__module__, 'twisted.internet.selectreactor')
  65. def test_windows(self):
  66. """
  67. L{_getInstallFunction} chooses the select reactor on Windows.
  68. """
  69. install = _getInstallFunction(windows)
  70. self.assertEqual(
  71. install.__module__, 'twisted.internet.selectreactor')
  72. class InstallationTests(SynchronousTestCase):
  73. """
  74. Tests for actual installation of the reactor.
  75. """
  76. def test_install(self):
  77. """
  78. L{install} installs a reactor.
  79. """
  80. with NoReactor():
  81. install()
  82. self.assertIn("twisted.internet.reactor", sys.modules)
  83. def test_reactor(self):
  84. """
  85. Importing L{twisted.internet.reactor} installs the default reactor if
  86. none is installed.
  87. """
  88. installed = []
  89. def installer():
  90. installed.append(True)
  91. return install()
  92. self.patch(default, "install", installer)
  93. with NoReactor():
  94. from twisted.internet import reactor
  95. self.assertTrue(IReactorCore.providedBy(reactor))
  96. self.assertEqual(installed, [True])