test_dispatch.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. from stevedore import dispatch
  13. from stevedore.tests import utils
  14. def check_dispatch(ep, *args, **kwds):
  15. return ep.name == 't2'
  16. class TestDispatch(utils.TestCase):
  17. def check_dispatch(ep, *args, **kwds):
  18. return ep.name == 't2'
  19. def test_dispatch(self):
  20. def invoke(ep, *args, **kwds):
  21. return (ep.name, args, kwds)
  22. em = dispatch.DispatchExtensionManager('stevedore.test.extension',
  23. lambda *args, **kwds: True,
  24. invoke_on_load=True,
  25. invoke_args=('a',),
  26. invoke_kwds={'b': 'B'},
  27. )
  28. self.assertEqual(len(em.extensions), 2)
  29. self.assertEqual(set(em.names()), set(['t1', 't2']))
  30. results = em.map(check_dispatch,
  31. invoke,
  32. 'first',
  33. named='named value',
  34. )
  35. expected = [('t2', ('first',), {'named': 'named value'})]
  36. self.assertEqual(results, expected)
  37. def test_dispatch_map_method(self):
  38. em = dispatch.DispatchExtensionManager('stevedore.test.extension',
  39. lambda *args, **kwds: True,
  40. invoke_on_load=True,
  41. invoke_args=('a',),
  42. invoke_kwds={'b': 'B'},
  43. )
  44. results = em.map_method(check_dispatch, 'get_args_and_data', 'first')
  45. self.assertEqual(results, [(('a',), {'b': 'B'}, 'first')])
  46. def test_name_dispatch(self):
  47. def invoke(ep, *args, **kwds):
  48. return (ep.name, args, kwds)
  49. em = dispatch.NameDispatchExtensionManager('stevedore.test.extension',
  50. lambda *args, **kwds: True,
  51. invoke_on_load=True,
  52. invoke_args=('a',),
  53. invoke_kwds={'b': 'B'},
  54. )
  55. self.assertEqual(len(em.extensions), 2)
  56. self.assertEqual(set(em.names()), set(['t1', 't2']))
  57. results = em.map(['t2'], invoke, 'first', named='named value',)
  58. expected = [('t2', ('first',), {'named': 'named value'})]
  59. self.assertEqual(results, expected)
  60. def test_name_dispatch_ignore_missing(self):
  61. def invoke(ep, *args, **kwds):
  62. return (ep.name, args, kwds)
  63. em = dispatch.NameDispatchExtensionManager(
  64. 'stevedore.test.extension',
  65. lambda *args, **kwds: True,
  66. invoke_on_load=True,
  67. invoke_args=('a',),
  68. invoke_kwds={'b': 'B'},
  69. )
  70. results = em.map(['t3', 't1'], invoke, 'first', named='named value',)
  71. expected = [('t1', ('first',), {'named': 'named value'})]
  72. self.assertEqual(results, expected)
  73. def test_name_dispatch_map_method(self):
  74. em = dispatch.NameDispatchExtensionManager(
  75. 'stevedore.test.extension',
  76. lambda *args, **kwds: True,
  77. invoke_on_load=True,
  78. invoke_args=('a',),
  79. invoke_kwds={'b': 'B'},
  80. )
  81. results = em.map_method(['t3', 't1'], 'get_args_and_data', 'first')
  82. self.assertEqual(results, [(('a',), {'b': 'B'}, 'first')])