1
0

test_context.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.unit.context_test
  4. ~~~~~~~~~~~~~~~~~~~~
  5. """
  6. # Import python libs
  7. from __future__ import absolute_import
  8. import threading
  9. import time
  10. import pytest
  11. import salt.ext.tornado.gen
  12. import salt.ext.tornado.stack_context
  13. # Import Salt libs
  14. import salt.utils.json
  15. from salt.ext.six.moves import range
  16. from salt.ext.tornado.testing import AsyncTestCase, gen_test
  17. from salt.utils.context import ContextDict, NamespacedDictWrapper
  18. # Import Salt Testing libs
  19. from tests.support.unit import TestCase
  20. class ContextDictTests(AsyncTestCase):
  21. # how many threads/coroutines to run at a time
  22. num_concurrent_tasks = 5
  23. def setUp(self):
  24. super(ContextDictTests, self).setUp()
  25. self.cd = ContextDict()
  26. # set a global value
  27. self.cd["foo"] = "global"
  28. @pytest.mark.slow_test(seconds=10) # Test takes >5 and <=10 seconds
  29. def test_threads(self):
  30. """Verify that ContextDict overrides properly within threads
  31. """
  32. rets = []
  33. def tgt(x, s):
  34. inner_ret = []
  35. over = self.cd.clone()
  36. inner_ret.append(self.cd.get("foo"))
  37. with over:
  38. inner_ret.append(over.get("foo"))
  39. over["foo"] = x
  40. inner_ret.append(over.get("foo"))
  41. time.sleep(s)
  42. inner_ret.append(over.get("foo"))
  43. rets.append(inner_ret)
  44. threads = []
  45. for x in range(0, self.num_concurrent_tasks):
  46. s = self.num_concurrent_tasks - x
  47. t = threading.Thread(target=tgt, args=(x, s))
  48. t.start()
  49. threads.append(t)
  50. for t in threads:
  51. t.join()
  52. for r in rets:
  53. self.assertEqual(r[0], r[1])
  54. self.assertEqual(r[2], r[3])
  55. @gen_test
  56. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  57. def test_coroutines(self):
  58. """Verify that ContextDict overrides properly within coroutines
  59. """
  60. @salt.ext.tornado.gen.coroutine
  61. def secondary_coroutine(over):
  62. raise salt.ext.tornado.gen.Return(over.get("foo"))
  63. @salt.ext.tornado.gen.coroutine
  64. def tgt(x, s, over):
  65. inner_ret = []
  66. # first grab the global
  67. inner_ret.append(self.cd.get("foo"))
  68. # grab the child's global (should match)
  69. inner_ret.append(over.get("foo"))
  70. # override the global
  71. over["foo"] = x
  72. inner_ret.append(over.get("foo"))
  73. # sleep for some time to let other coroutines do this section of code
  74. yield salt.ext.tornado.gen.sleep(s)
  75. # get the value of the global again.
  76. inner_ret.append(over.get("foo"))
  77. # Call another coroutine to verify that we keep our context
  78. r = yield secondary_coroutine(over)
  79. inner_ret.append(r)
  80. raise salt.ext.tornado.gen.Return(inner_ret)
  81. futures = []
  82. for x in range(0, self.num_concurrent_tasks):
  83. s = self.num_concurrent_tasks - x
  84. over = self.cd.clone()
  85. # pylint: disable=cell-var-from-loop
  86. f = salt.ext.tornado.stack_context.run_with_stack_context(
  87. salt.ext.tornado.stack_context.StackContext(lambda: over),
  88. lambda: tgt(x, s / 5.0, over),
  89. )
  90. # pylint: enable=cell-var-from-loop
  91. futures.append(f)
  92. wait_iterator = salt.ext.tornado.gen.WaitIterator(*futures)
  93. while not wait_iterator.done():
  94. r = yield wait_iterator.next() # pylint: disable=incompatible-py3-code
  95. self.assertEqual(r[0], r[1]) # verify that the global value remails
  96. self.assertEqual(r[2], r[3]) # verify that the override sticks locally
  97. self.assertEqual(
  98. r[3], r[4]
  99. ) # verify that the override sticks across coroutines
  100. def test_basic(self):
  101. """Test that the contextDict is a dict
  102. """
  103. # ensure we get the global value
  104. self.assertEqual(
  105. dict(self.cd), {"foo": "global"},
  106. )
  107. def test_override(self):
  108. over = self.cd.clone()
  109. over["bar"] = "global"
  110. self.assertEqual(
  111. dict(over), {"foo": "global", "bar": "global"},
  112. )
  113. self.assertEqual(
  114. dict(self.cd), {"foo": "global"},
  115. )
  116. with over:
  117. self.assertEqual(
  118. dict(over), {"foo": "global", "bar": "global"},
  119. )
  120. self.assertEqual(
  121. dict(self.cd), {"foo": "global", "bar": "global"},
  122. )
  123. over["bar"] = "baz"
  124. self.assertEqual(
  125. dict(over), {"foo": "global", "bar": "baz"},
  126. )
  127. self.assertEqual(
  128. dict(self.cd), {"foo": "global", "bar": "baz"},
  129. )
  130. self.assertEqual(
  131. dict(over), {"foo": "global", "bar": "baz"},
  132. )
  133. self.assertEqual(
  134. dict(self.cd), {"foo": "global"},
  135. )
  136. def test_multiple_contexts(self):
  137. cds = []
  138. for x in range(0, 10):
  139. cds.append(self.cd.clone(bar=x))
  140. for x, cd in enumerate(cds):
  141. self.assertNotIn("bar", self.cd)
  142. with cd:
  143. self.assertEqual(
  144. dict(self.cd), {"bar": x, "foo": "global"},
  145. )
  146. self.assertNotIn("bar", self.cd)
  147. class NamespacedDictWrapperTests(TestCase):
  148. PREFIX = "prefix"
  149. def setUp(self):
  150. self._dict = {}
  151. def test_single_key(self):
  152. self._dict["prefix"] = {"foo": "bar"}
  153. w = NamespacedDictWrapper(self._dict, "prefix")
  154. self.assertEqual(w["foo"], "bar")
  155. def test_multiple_key(self):
  156. self._dict["prefix"] = {"foo": {"bar": "baz"}}
  157. w = NamespacedDictWrapper(self._dict, ("prefix", "foo"))
  158. self.assertEqual(w["bar"], "baz")
  159. def test_json_dumps_single_key(self):
  160. self._dict["prefix"] = {"foo": {"bar": "baz"}}
  161. w = NamespacedDictWrapper(self._dict, "prefix")
  162. self.assertEqual(salt.utils.json.dumps(w), '{"foo": {"bar": "baz"}}')
  163. def test_json_dumps_multiple_key(self):
  164. self._dict["prefix"] = {"foo": {"bar": "baz"}}
  165. w = NamespacedDictWrapper(self._dict, ("prefix", "foo"))
  166. self.assertEqual(salt.utils.json.dumps(w), '{"bar": "baz"}')