Merge pull request #4021 from ostcar/fix_double_elements_in_autoupdate

fix douple elements
This commit is contained in:
Oskar Hahn 2018-11-20 22:34:02 +01:00 committed by GitHub
commit 619a589731
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 9 deletions

View File

@ -418,16 +418,18 @@ class MemmoryCacheProvider:
else: else:
cache_dict = self.restricted_data.get(user_id, {}) cache_dict = self.restricted_data.get(user_id, {})
all_element_ids: Set[str] = set()
for data_change_id, element_ids in self.change_id_data.items(): for data_change_id, element_ids in self.change_id_data.items():
if data_change_id < change_id or (max_change_id > -1 and data_change_id > max_change_id): if data_change_id >= change_id and (max_change_id == -1 or data_change_id <= max_change_id):
continue all_element_ids.update(element_ids)
for element_id in element_ids:
element_json = cache_dict.get(element_id, None) for element_id in all_element_ids:
if element_json is None: element_json = cache_dict.get(element_id, None)
deleted_elements.append(element_id) if element_json is None:
else: deleted_elements.append(element_id)
collection_string, id = split_element_id(element_id) else:
changed_elements[collection_string].append(element_json.encode()) collection_string, id = split_element_id(element_id)
changed_elements[collection_string].append(element_json.encode())
return changed_elements, deleted_elements return changed_elements, deleted_elements
async def del_restricted_data(self, user_id: int) -> None: async def del_restricted_data(self, user_id: int) -> None:

View File

@ -359,6 +359,32 @@ async def test_send_connect_twice_with_clear_change_id_cache_same_change_id_then
assert response2.get('content')['all_data'] assert response2.get('content')['all_data']
@pytest.mark.asyncio
async def test_request_changed_elements_no_douple_elements(communicator):
"""
Test, that when an elements is changed twice, it is only returned
onces when ask a range of change ids.
Test when all_data is false
"""
await set_config('general_system_enable_anonymous', True)
await communicator.connect()
# Change element twice
await set_config('general_event_name', 'Test Event')
await set_config('general_event_name', 'Other value')
# Ask for all elements
await communicator.send_json_to({'type': 'getElements', 'content': {'change_id': 2}, 'id': 'test_id'})
response = await communicator.receive_json_from()
type = response.get('type')
content = response.get('content')
assert type == 'autoupdate'
assert not response.get('content')['all_data']
config_ids = [e['id'] for e in content['changed']['core/config']]
# test that config_ids are unique
assert len(config_ids) == len(set(config_ids))
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_send_invalid_get_elements(communicator): async def test_send_invalid_get_elements(communicator):
await set_config('general_system_enable_anonymous', True) await set_config('general_system_enable_anonymous', True)