test_share.py 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_update_list_with_pointer__composite_key(self):
  93. shared = share.SharedDataManager()
  94. class Foo(object):
  95. data = shared.create(['{id}', 'data'], [])
  96. def __init__(self):
  97. self.id = id(self)
  98. foo = Foo()
  99. foo.data = ['foo']
  100. assert shared.get(str(id(foo)) + '_data') == ['foo']
  101. foo.data.append('bar')
  102. assert shared.get(str(id(foo)) + '_data') == ['foo', 'bar']
  103. shared.commit()
  104. assert shared.get(str(id(foo)) + '_data') == ['foo', 'bar']
  105. assert pickle.loads(shared._r.get(str(id(foo)) + '_data')) == ['foo', 'bar']
  106. foo.data.append('bAr')
  107. shared.commit()
  108. assert shared.get(str(id(foo)) + '_data') == ['foo', 'bar', 'bAr']
  109. assert pickle.loads(shared._r.get(str(id(foo)) + '_data')) == ['foo', 'bar', 'bAr']
  110. def test_refresh_without_commit(self):
  111. shared = share.SharedDataManager()
  112. class Foo(object):
  113. counter = shared.create('counter', 0)
  114. foo = Foo()
  115. foo.counter = 42
  116. assert shared.get('counter') == 42
  117. shared.refresh()
  118. with pytest.raises(UnknownSharedData):
  119. shared.get('counter')
  120. def test_commit(self):
  121. shared = share.SharedDataManager()
  122. class Foo(object):
  123. counter = shared.create('counter', 0)
  124. foo = Foo()
  125. foo.counter = 42
  126. assert shared.get('counter') == 42
  127. shared.commit()
  128. assert shared.get('counter') == 42
  129. def test_commit_then_refresh(self):
  130. shared = share.SharedDataManager()
  131. class Foo(object):
  132. counter = shared.create('counter', 0)
  133. foo = Foo()
  134. foo.counter = 42
  135. assert shared.get('counter') == 42
  136. shared.commit()
  137. shared.refresh()
  138. assert shared.get('counter') == 42
  139. def test_position_index(self):
  140. class ListIndex(share.SharedDataIndex):
  141. def add(self, value):
  142. try:
  143. values = self.shared_data_manager.get(self.key)
  144. except UnknownSharedData:
  145. values = []
  146. values.append(value)
  147. self.shared_data_manager.set(self.key, values)
  148. def remove(self, value):
  149. values = self.shared_data_manager.get(self.key)
  150. values.remove(value)
  151. self.shared_data_manager.set(self.key, values)
  152. shared = share.SharedDataManager()
  153. class Foo(object):
  154. position = shared.create(
  155. '{id}_position',
  156. (0, 0, 0),
  157. indexes=[shared.make_index(ListIndex, 'positions')],
  158. )
  159. @property
  160. def id(self):
  161. return id(self)
  162. with pytest.raises(UnknownSharedData):
  163. shared.get('positions')
  164. foo = Foo()
  165. foo.position = (0, 1, 2)
  166. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  167. assert shared.get('positions') == [(0, 1, 2)]
  168. foo2 = Foo()
  169. foo2.position = (3, 4, 5)
  170. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  171. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  172. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  173. foo2.position = (6, 7, 8)
  174. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  175. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]
  176. class TestIndexes(BaseTest):
  177. def test_list_index(self):
  178. shared = share.SharedDataManager()
  179. class Foo(object):
  180. position = shared.create(
  181. '{id}_position',
  182. (0, 0, 0),
  183. indexes=[shared.make_index(share.ListIndex, 'positions')],
  184. )
  185. @property
  186. def id(self):
  187. return id(self)
  188. with pytest.raises(UnknownSharedData):
  189. shared.get('positions')
  190. foo = Foo()
  191. foo.position = (0, 1, 2)
  192. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  193. assert shared.get('positions') == [(0, 1, 2)]
  194. foo2 = Foo()
  195. foo2.position = (3, 4, 5)
  196. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  197. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  198. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  199. foo2.position = (6, 7, 8)
  200. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  201. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]