test_share.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # coding: utf-8
  2. import pickle
  3. import pytest
  4. from synergine2.exceptions import UnknownSharedData
  5. from synergine2 import share
  6. from tests import BaseTest
  7. class TestShare(BaseTest):
  8. def test_simple_share_with_class(self):
  9. shared = share.SharedDataManager()
  10. class Foo(object):
  11. counter = shared.create('counter', value=0)
  12. foo = Foo()
  13. foo.counter = 42
  14. assert shared.get('counter') == 42
  15. foo.counter = 48
  16. assert shared.get('counter') == 48
  17. def test_default_value(self):
  18. shared = share.SharedDataManager()
  19. class Foo(object):
  20. counter = shared.create('counter', 0)
  21. foo = Foo()
  22. foo.counter = 42
  23. assert shared.get('counter') == 42
  24. foo.counter = 48
  25. assert shared.get('counter') == 48
  26. def test_dynamic_key(self):
  27. shared = share.SharedDataManager()
  28. class Foo(object):
  29. counter = shared.create(
  30. ['{id}', 'counter'],
  31. value=0,
  32. indexes=[],
  33. )
  34. @property
  35. def id(self):
  36. return id(self)
  37. foo = Foo()
  38. foo.counter = 42
  39. assert shared.get(foo.id, 'counter') == 42
  40. foo.counter = 48
  41. assert shared.get(foo.id, 'counter') == 48
  42. def test_multiple_uses(self):
  43. shared = share.SharedDataManager()
  44. class Foo(object):
  45. position = shared.create(
  46. '{id}_position',
  47. (0, 0, 0),
  48. indexes=[],
  49. )
  50. @property
  51. def id(self):
  52. return id(self)
  53. foo = Foo()
  54. foo.position = (0, 1, 2)
  55. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  56. foo2 = Foo()
  57. foo2.position = (3, 4, 5)
  58. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  59. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  60. def test_update_dict_with_pointer(self):
  61. shared = share.SharedDataManager()
  62. class Foo(object):
  63. data = shared.create('data', {})
  64. foo = Foo()
  65. foo.data = {'foo': 'bar'}
  66. assert shared.get('data') == {'foo': 'bar'}
  67. foo.data['foo'] = 'buz'
  68. assert shared.get('data') == {'foo': 'buz'}
  69. shared.commit()
  70. assert shared.get('data') == {'foo': 'buz'}
  71. assert pickle.loads(shared._r.get('data')) == {'foo': 'buz'}
  72. foo.data['foo'] = 'bAz'
  73. shared.commit()
  74. assert shared.get('data') == {'foo': 'bAz'}
  75. assert pickle.loads(shared._r.get('data')) == {'foo': 'bAz'}
  76. def test_update_list_with_pointer(self):
  77. shared = share.SharedDataManager()
  78. class Foo(object):
  79. data = shared.create('data', [])
  80. foo = Foo()
  81. foo.data = ['foo']
  82. assert shared.get('data') == ['foo']
  83. foo.data.append('bar')
  84. assert shared.get('data') == ['foo', 'bar']
  85. shared.commit()
  86. assert shared.get('data') == ['foo', 'bar']
  87. assert pickle.loads(shared._r.get('data')) == ['foo', 'bar']
  88. foo.data.append('bAr')
  89. shared.commit()
  90. assert shared.get('data') == ['foo', 'bar', 'bAr']
  91. assert pickle.loads(shared._r.get('data')) == ['foo', 'bar', 'bAr']
  92. def test_refresh_without_commit(self):
  93. shared = share.SharedDataManager()
  94. class Foo(object):
  95. counter = shared.create('counter', 0)
  96. foo = Foo()
  97. foo.counter = 42
  98. assert shared.get('counter') == 42
  99. shared.refresh()
  100. with pytest.raises(UnknownSharedData):
  101. shared.get('counter')
  102. def test_commit(self):
  103. shared = share.SharedDataManager()
  104. class Foo(object):
  105. counter = shared.create('counter', 0)
  106. foo = Foo()
  107. foo.counter = 42
  108. assert shared.get('counter') == 42
  109. shared.commit()
  110. assert shared.get('counter') == 42
  111. def test_commit_then_refresh(self):
  112. shared = share.SharedDataManager()
  113. class Foo(object):
  114. counter = shared.create('counter', 0)
  115. foo = Foo()
  116. foo.counter = 42
  117. assert shared.get('counter') == 42
  118. shared.commit()
  119. shared.refresh()
  120. assert shared.get('counter') == 42
  121. def test_position_index(self):
  122. class ListIndex(share.SharedDataIndex):
  123. def add(self, value):
  124. try:
  125. values = self.shared_data_manager.get(self.key)
  126. except UnknownSharedData:
  127. values = []
  128. values.append(value)
  129. self.shared_data_manager.set(self.key, values)
  130. def remove(self, value):
  131. values = self.shared_data_manager.get(self.key)
  132. values.remove(value)
  133. self.shared_data_manager.set(self.key, values)
  134. shared = share.SharedDataManager()
  135. class Foo(object):
  136. position = shared.create(
  137. '{id}_position',
  138. (0, 0, 0),
  139. indexes=[shared.make_index(ListIndex, 'positions')],
  140. )
  141. @property
  142. def id(self):
  143. return id(self)
  144. with pytest.raises(UnknownSharedData):
  145. shared.get('positions')
  146. foo = Foo()
  147. foo.position = (0, 1, 2)
  148. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  149. assert shared.get('positions') == [(0, 1, 2)]
  150. foo2 = Foo()
  151. foo2.position = (3, 4, 5)
  152. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  153. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  154. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  155. foo2.position = (6, 7, 8)
  156. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  157. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
  158. class TestIndexes(BaseTest):
  159. def test_list_index(self):
  160. shared = share.SharedDataManager()
  161. class Foo(object):
  162. position = shared.create(
  163. '{id}_position',
  164. (0, 0, 0),
  165. indexes=[shared.make_index(share.ListIndex, 'positions')],
  166. )
  167. @property
  168. def id(self):
  169. return id(self)
  170. with pytest.raises(UnknownSharedData):
  171. shared.get('positions')
  172. foo = Foo()
  173. foo.position = (0, 1, 2)
  174. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  175. assert shared.get('positions') == [(0, 1, 2)]
  176. foo2 = Foo()
  177. foo2.position = (3, 4, 5)
  178. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  179. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  180. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  181. foo2.position = (6, 7, 8)
  182. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  183. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]