test_share.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # coding: utf-8
  2. import pytest
  3. from synergine2.exceptions import UnknownSharedData
  4. from synergine2.share import SharedDataManager
  5. from synergine2.share import SharedDataIndex
  6. from tests import BaseTest
  7. class TestShare(BaseTest):
  8. def test_simple_share_with_class(self):
  9. shared = SharedDataManager()
  10. class Foo(object):
  11. counter = shared.create('counter', 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_dynamic_key(self):
  18. shared = SharedDataManager()
  19. class Foo(object):
  20. counter = shared.create(
  21. '{id}_counter',
  22. 0,
  23. indexes=[],
  24. )
  25. @property
  26. def id(self):
  27. return id(self)
  28. foo = Foo()
  29. foo.counter = 42
  30. assert shared.get('{}_counter'.format(foo.id)) == 42
  31. foo.counter = 48
  32. assert shared.get('{}_counter'.format(foo.id)) == 48
  33. def test_multiple_uses(self):
  34. shared = SharedDataManager()
  35. class Foo(object):
  36. position = shared.create(
  37. '{id}_position',
  38. (0, 0, 0),
  39. indexes=[],
  40. )
  41. @property
  42. def id(self):
  43. return id(self)
  44. foo = Foo()
  45. foo.position = (0, 1, 2)
  46. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  47. foo2 = Foo()
  48. foo2.position = (3, 4, 5)
  49. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  50. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  51. def test_update_dict_with_pointer(self):
  52. shared = SharedDataManager()
  53. class Foo(object):
  54. data = shared.create('data', {})
  55. foo = Foo()
  56. foo.data = {'foo': 'bar'}
  57. assert shared.get('data') == {'foo': 'bar'}
  58. foo.data['foo'] = 'buz'
  59. assert shared.get('data') == {'foo': 'buz'}
  60. def test_refresh_without_commit(self):
  61. shared = SharedDataManager()
  62. class Foo(object):
  63. counter = shared.create('counter', 0)
  64. foo = Foo()
  65. foo.counter = 42
  66. assert shared.get('counter') == 42
  67. shared.refresh()
  68. with pytest.raises(UnknownSharedData):
  69. shared.get('counter')
  70. def test_commit(self):
  71. shared = SharedDataManager()
  72. class Foo(object):
  73. counter = shared.create('counter', 0)
  74. foo = Foo()
  75. foo.counter = 42
  76. assert shared.get('counter') == 42
  77. shared.commit()
  78. assert shared.get('counter') == 42
  79. def test_commit_then_refresh(self):
  80. shared = SharedDataManager()
  81. class Foo(object):
  82. counter = shared.create('counter', 0)
  83. foo = Foo()
  84. foo.counter = 42
  85. assert shared.get('counter') == 42
  86. shared.commit()
  87. shared.refresh()
  88. assert shared.get('counter') == 42
  89. def test_position_index(self):
  90. class ListIndex(SharedDataIndex):
  91. def add(self, value):
  92. try:
  93. values = self.shared_data_manager.get(self.key)
  94. except UnknownSharedData:
  95. values = []
  96. values.append(value)
  97. self.shared_data_manager.set(self.key, values)
  98. def remove(self, value):
  99. values = self.shared_data_manager.get(self.key)
  100. values.remove(value)
  101. self.shared_data_manager.set(self.key, values)
  102. shared = SharedDataManager()
  103. class Foo(object):
  104. position = shared.create(
  105. '{id}_position',
  106. (0, 0, 0),
  107. indexes=[shared.make_index(ListIndex, 'positions')],
  108. )
  109. @property
  110. def id(self):
  111. return id(self)
  112. with pytest.raises(UnknownSharedData):
  113. shared.get('positions')
  114. foo = Foo()
  115. foo.position = (0, 1, 2)
  116. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  117. assert shared.get('positions') == [(0, 1, 2)]
  118. foo2 = Foo()
  119. foo2.position = (3, 4, 5)
  120. assert shared.get('{}_position'.format(foo.id)) == (0, 1, 2)
  121. assert shared.get('{}_position'.format(foo2.id)) == (3, 4, 5)
  122. assert shared.get('positions') == [(0, 1, 2), (3, 4, 5)]
  123. foo2.position = (6, 7, 8)
  124. assert shared.get('{}_position'.format(foo2.id)) == (6, 7, 8)
  125. assert shared.get('positions') == [(0, 1, 2), (6, 7, 8)]