2014-08-12 08:10:52 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 20:32:18 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
from mock import Mock
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
from synapse.api.auth import Auth
|
2019-04-01 03:24:38 -06:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
|
|
|
from synapse.api.room_versions import RoomVersions
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.events import FrozenEvent
|
2018-01-27 02:15:45 -07:00
|
|
|
from synapse.state import StateHandler, StateResolutionHandler
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
from tests import unittest
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2019-04-08 10:10:55 -06:00
|
|
|
from .utils import MockClock, default_config
|
2014-09-12 10:11:00 -06:00
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
_next_event_id = 1000
|
|
|
|
|
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
def create_event(
|
|
|
|
name=None,
|
|
|
|
type=None,
|
|
|
|
state_key=None,
|
|
|
|
depth=2,
|
|
|
|
event_id=None,
|
|
|
|
prev_events=[],
|
|
|
|
**kwargs
|
|
|
|
):
|
2015-01-21 09:27:04 -07:00
|
|
|
global _next_event_id
|
|
|
|
|
|
|
|
if not event_id:
|
|
|
|
_next_event_id += 1
|
2015-09-01 08:57:35 -06:00
|
|
|
event_id = "$%s:test" % (_next_event_id,)
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
if not name:
|
|
|
|
if state_key is not None:
|
2018-08-10 07:54:09 -06:00
|
|
|
name = "<%s-%s, %s>" % (type, state_key, event_id)
|
2015-01-21 09:27:04 -07:00
|
|
|
else:
|
2018-08-10 07:54:09 -06:00
|
|
|
name = "<%s, %s>" % (type, event_id)
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
d = {
|
|
|
|
"event_id": event_id,
|
|
|
|
"type": type,
|
|
|
|
"sender": "@user_id:example.com",
|
|
|
|
"room_id": "!room_id:example.com",
|
|
|
|
"depth": depth,
|
|
|
|
"prev_events": prev_events,
|
|
|
|
}
|
|
|
|
|
|
|
|
if state_key is not None:
|
|
|
|
d["state_key"] = state_key
|
|
|
|
|
|
|
|
d.update(kwargs)
|
|
|
|
|
|
|
|
event = FrozenEvent(d)
|
|
|
|
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
|
|
|
class StateGroupStore(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._event_to_state_group = {}
|
|
|
|
self._group_to_state = {}
|
|
|
|
|
2016-08-26 03:15:52 -06:00
|
|
|
self._event_id_to_event = {}
|
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
self._next_group = 1
|
|
|
|
|
2016-08-25 10:32:22 -06:00
|
|
|
def get_state_groups_ids(self, room_id, event_ids):
|
2015-01-21 09:27:04 -07:00
|
|
|
groups = {}
|
|
|
|
for event_id in event_ids:
|
|
|
|
group = self._event_to_state_group.get(event_id)
|
|
|
|
if group:
|
|
|
|
groups[group] = self._group_to_state[group]
|
|
|
|
|
|
|
|
return defer.succeed(groups)
|
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
def store_state_group(
|
|
|
|
self, event_id, room_id, prev_group, delta_ids, current_state_ids
|
|
|
|
):
|
2018-02-06 07:31:24 -07:00
|
|
|
state_group = self._next_group
|
|
|
|
self._next_group += 1
|
2015-01-21 09:27:04 -07:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
self._group_to_state[state_group] = dict(current_state_ids)
|
2015-01-21 09:27:04 -07:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
return state_group
|
2015-01-21 09:27:04 -07:00
|
|
|
|
2016-08-26 03:15:52 -06:00
|
|
|
def get_events(self, event_ids, **kwargs):
|
|
|
|
return {
|
2018-08-10 07:54:09 -06:00
|
|
|
e_id: self._event_id_to_event[e_id]
|
|
|
|
for e_id in event_ids
|
2016-08-26 03:15:52 -06:00
|
|
|
if e_id in self._event_id_to_event
|
|
|
|
}
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
def get_state_group_delta(self, name):
|
2019-08-30 09:28:26 -06:00
|
|
|
return None, None
|
2018-02-06 07:31:24 -07:00
|
|
|
|
2016-08-26 03:15:52 -06:00
|
|
|
def register_events(self, events):
|
|
|
|
for e in events:
|
|
|
|
self._event_id_to_event[e.event_id] = e
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
def register_event_context(self, event, context):
|
|
|
|
self._event_to_state_group[event.event_id] = context.state_group
|
|
|
|
|
|
|
|
def register_event_id_state_group(self, event_id, state_group):
|
|
|
|
self._event_to_state_group[event_id] = state_group
|
|
|
|
|
2018-08-09 07:33:49 -06:00
|
|
|
def get_room_version(self, room_id):
|
2019-04-01 03:24:38 -06:00
|
|
|
return RoomVersions.V1.identifier
|
2018-08-09 07:33:49 -06:00
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
class DictObj(dict):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super(DictObj, self).__init__(kwargs)
|
|
|
|
self.__dict__ = self
|
|
|
|
|
|
|
|
|
|
|
|
class Graph(object):
|
|
|
|
def __init__(self, nodes, edges):
|
|
|
|
events = {}
|
|
|
|
clobbered = set(events.keys())
|
|
|
|
|
|
|
|
for event_id, fields in nodes.items():
|
|
|
|
refs = edges.get(event_id)
|
|
|
|
if refs:
|
|
|
|
clobbered.difference_update(refs)
|
|
|
|
prev_events = [(r, {}) for r in refs]
|
|
|
|
else:
|
|
|
|
prev_events = []
|
|
|
|
|
|
|
|
events[event_id] = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
event_id=event_id, prev_events=prev_events, **fields
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
self._leaves = clobbered
|
|
|
|
self._events = sorted(events.values(), key=lambda e: e.depth)
|
|
|
|
|
|
|
|
def walk(self):
|
|
|
|
return iter(self._events)
|
|
|
|
|
|
|
|
def get_leaves(self):
|
|
|
|
return (self._events[i] for i in self._leaves)
|
|
|
|
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
class StateTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store = StateGroupStore()
|
2019-10-23 10:25:54 -06:00
|
|
|
storage = Mock(main=self.store, state=self.store)
|
2018-08-10 07:54:09 -06:00
|
|
|
hs = Mock(
|
|
|
|
spec_set=[
|
2019-04-08 10:10:55 -06:00
|
|
|
"config",
|
2018-08-10 07:54:09 -06:00
|
|
|
"get_datastore",
|
2019-10-23 10:25:54 -06:00
|
|
|
"get_storage",
|
2018-08-10 07:54:09 -06:00
|
|
|
"get_auth",
|
|
|
|
"get_state_handler",
|
|
|
|
"get_clock",
|
|
|
|
"get_state_resolution_handler",
|
|
|
|
]
|
|
|
|
)
|
2019-05-13 14:01:14 -06:00
|
|
|
hs.config = default_config("tesths", True)
|
2014-11-11 07:16:48 -07:00
|
|
|
hs.get_datastore.return_value = self.store
|
2015-01-21 09:27:04 -07:00
|
|
|
hs.get_state_handler.return_value = None
|
2015-02-09 07:23:57 -07:00
|
|
|
hs.get_clock.return_value = MockClock()
|
2016-04-18 09:08:32 -06:00
|
|
|
hs.get_auth.return_value = Auth(hs)
|
2018-01-27 02:15:45 -07:00
|
|
|
hs.get_state_resolution_handler = lambda: StateResolutionHandler(hs)
|
2019-10-23 10:25:54 -06:00
|
|
|
hs.get_storage.return_value = storage
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
self.state = StateHandler(hs)
|
|
|
|
self.event_id = 0
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_branch_no_conflict(self):
|
|
|
|
graph = Graph(
|
|
|
|
nodes={
|
|
|
|
"START": DictObj(
|
2018-09-06 10:58:18 -06:00
|
|
|
type=EventTypes.Create, state_key="", content={}, depth=1
|
2015-01-21 09:27:04 -07:00
|
|
|
),
|
2018-08-10 07:54:09 -06:00
|
|
|
"A": DictObj(type=EventTypes.Message, depth=2),
|
|
|
|
"B": DictObj(type=EventTypes.Message, depth=3),
|
|
|
|
"C": DictObj(type=EventTypes.Name, state_key="", depth=3),
|
|
|
|
"D": DictObj(type=EventTypes.Message, depth=4),
|
2015-01-21 09:27:04 -07:00
|
|
|
},
|
2018-08-10 07:54:09 -06:00
|
|
|
edges={"A": ["START"], "B": ["A"], "C": ["A"], "D": ["B", "C"]},
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_events(graph.walk())
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
context_store = {}
|
|
|
|
|
|
|
|
for event in graph.walk():
|
|
|
|
context = yield self.state.compute_event_context(event)
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_event_context(event, context)
|
2015-01-21 09:27:04 -07:00
|
|
|
context_store[event.event_id] = context
|
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
prev_state_ids = yield context_store["D"].get_prev_state_ids(self.store)
|
|
|
|
self.assertEqual(2, len(prev_state_ids))
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_branch_basic_conflict(self):
|
|
|
|
graph = Graph(
|
|
|
|
nodes={
|
|
|
|
"START": DictObj(
|
|
|
|
type=EventTypes.Create,
|
2015-09-01 08:42:03 -06:00
|
|
|
state_key="",
|
|
|
|
content={"creator": "@user_id:example.com"},
|
2015-01-21 09:27:04 -07:00
|
|
|
depth=1,
|
|
|
|
),
|
|
|
|
"A": DictObj(
|
|
|
|
type=EventTypes.Member,
|
|
|
|
state_key="@user_id:example.com",
|
|
|
|
content={"membership": Membership.JOIN},
|
|
|
|
membership=Membership.JOIN,
|
|
|
|
depth=2,
|
|
|
|
),
|
2018-08-10 07:54:09 -06:00
|
|
|
"B": DictObj(type=EventTypes.Name, state_key="", depth=3),
|
|
|
|
"C": DictObj(type=EventTypes.Name, state_key="", depth=4),
|
|
|
|
"D": DictObj(type=EventTypes.Message, depth=5),
|
2015-01-21 09:27:04 -07:00
|
|
|
},
|
2018-08-10 07:54:09 -06:00
|
|
|
edges={"A": ["START"], "B": ["A"], "C": ["A"], "D": ["B", "C"]},
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_events(graph.walk())
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
context_store = {}
|
|
|
|
|
|
|
|
for event in graph.walk():
|
|
|
|
context = yield self.state.compute_event_context(event)
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_event_context(event, context)
|
2015-01-21 09:27:04 -07:00
|
|
|
context_store[event.event_id] = context
|
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
prev_state_ids = yield context_store["D"].get_prev_state_ids(self.store)
|
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
self.assertSetEqual(
|
2018-08-10 07:54:09 -06:00
|
|
|
{"START", "A", "C"}, {e_id for e_id in prev_state_ids.values()}
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_branch_have_banned_conflict(self):
|
|
|
|
graph = Graph(
|
|
|
|
nodes={
|
|
|
|
"START": DictObj(
|
|
|
|
type=EventTypes.Create,
|
2015-09-01 08:42:03 -06:00
|
|
|
state_key="",
|
|
|
|
content={"creator": "@user_id:example.com"},
|
2015-01-21 09:27:04 -07:00
|
|
|
depth=1,
|
|
|
|
),
|
|
|
|
"A": DictObj(
|
|
|
|
type=EventTypes.Member,
|
|
|
|
state_key="@user_id:example.com",
|
|
|
|
content={"membership": Membership.JOIN},
|
|
|
|
membership=Membership.JOIN,
|
|
|
|
depth=2,
|
|
|
|
),
|
2018-08-10 07:54:09 -06:00
|
|
|
"B": DictObj(type=EventTypes.Name, state_key="", depth=3),
|
2015-01-21 09:27:04 -07:00
|
|
|
"C": DictObj(
|
|
|
|
type=EventTypes.Member,
|
|
|
|
state_key="@user_id_2:example.com",
|
|
|
|
content={"membership": Membership.BAN},
|
|
|
|
membership=Membership.BAN,
|
|
|
|
depth=4,
|
|
|
|
),
|
|
|
|
"D": DictObj(
|
|
|
|
type=EventTypes.Name,
|
|
|
|
state_key="",
|
|
|
|
depth=4,
|
|
|
|
sender="@user_id_2:example.com",
|
|
|
|
),
|
2018-08-10 07:54:09 -06:00
|
|
|
"E": DictObj(type=EventTypes.Message, depth=5),
|
2015-01-21 09:27:04 -07:00
|
|
|
},
|
2018-08-10 07:54:09 -06:00
|
|
|
edges={"A": ["START"], "B": ["A"], "C": ["B"], "D": ["B"], "E": ["C", "D"]},
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_events(graph.walk())
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
context_store = {}
|
|
|
|
|
|
|
|
for event in graph.walk():
|
|
|
|
context = yield self.state.compute_event_context(event)
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_event_context(event, context)
|
2015-01-21 09:27:04 -07:00
|
|
|
context_store[event.event_id] = context
|
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
prev_state_ids = yield context_store["E"].get_prev_state_ids(self.store)
|
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
self.assertSetEqual(
|
2018-08-10 07:54:09 -06:00
|
|
|
{"START", "A", "B", "C"}, {e for e in prev_state_ids.values()}
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
|
|
|
|
2015-11-12 06:10:25 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_branch_have_perms_conflict(self):
|
|
|
|
userid1 = "@user_id:example.com"
|
|
|
|
userid2 = "@user_id2:example.com"
|
|
|
|
|
|
|
|
nodes = {
|
|
|
|
"A1": DictObj(
|
|
|
|
type=EventTypes.Create,
|
|
|
|
state_key="",
|
|
|
|
content={"creator": userid1},
|
|
|
|
depth=1,
|
|
|
|
),
|
|
|
|
"A2": DictObj(
|
|
|
|
type=EventTypes.Member,
|
|
|
|
state_key=userid1,
|
|
|
|
content={"membership": Membership.JOIN},
|
|
|
|
membership=Membership.JOIN,
|
|
|
|
),
|
|
|
|
"A3": DictObj(
|
|
|
|
type=EventTypes.Member,
|
|
|
|
state_key=userid2,
|
|
|
|
content={"membership": Membership.JOIN},
|
|
|
|
membership=Membership.JOIN,
|
|
|
|
),
|
|
|
|
"A4": DictObj(
|
|
|
|
type=EventTypes.PowerLevels,
|
|
|
|
state_key="",
|
|
|
|
content={
|
|
|
|
"events": {"m.room.name": 50},
|
2018-08-10 07:54:09 -06:00
|
|
|
"users": {userid1: 100, userid2: 60},
|
2015-11-12 06:10:25 -07:00
|
|
|
},
|
|
|
|
),
|
2018-08-10 07:54:09 -06:00
|
|
|
"A5": DictObj(type=EventTypes.Name, state_key=""),
|
2015-11-12 06:10:25 -07:00
|
|
|
"B": DictObj(
|
|
|
|
type=EventTypes.PowerLevels,
|
|
|
|
state_key="",
|
2018-08-10 07:54:09 -06:00
|
|
|
content={"events": {"m.room.name": 50}, "users": {userid2: 30}},
|
2015-11-12 06:10:25 -07:00
|
|
|
),
|
2018-08-10 07:54:09 -06:00
|
|
|
"C": DictObj(type=EventTypes.Name, state_key="", sender=userid2),
|
|
|
|
"D": DictObj(type=EventTypes.Message),
|
2015-11-12 06:10:25 -07:00
|
|
|
}
|
|
|
|
edges = {
|
|
|
|
"A2": ["A1"],
|
|
|
|
"A3": ["A2"],
|
|
|
|
"A4": ["A3"],
|
|
|
|
"A5": ["A4"],
|
|
|
|
"B": ["A5"],
|
|
|
|
"C": ["A5"],
|
2018-08-10 07:54:09 -06:00
|
|
|
"D": ["B", "C"],
|
2015-11-12 06:10:25 -07:00
|
|
|
}
|
|
|
|
self._add_depths(nodes, edges)
|
|
|
|
graph = Graph(nodes, edges)
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_events(graph.walk())
|
2015-11-12 06:10:25 -07:00
|
|
|
|
|
|
|
context_store = {}
|
|
|
|
|
|
|
|
for event in graph.walk():
|
|
|
|
context = yield self.state.compute_event_context(event)
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_event_context(event, context)
|
2015-11-12 06:10:25 -07:00
|
|
|
context_store[event.event_id] = context
|
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
prev_state_ids = yield context_store["D"].get_prev_state_ids(self.store)
|
|
|
|
|
2015-11-12 06:10:25 -07:00
|
|
|
self.assertSetEqual(
|
2018-08-10 07:54:09 -06:00
|
|
|
{"A1", "A2", "A3", "A5", "B"}, {e for e in prev_state_ids.values()}
|
2015-11-12 06:10:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
def _add_depths(self, nodes, edges):
|
|
|
|
def _get_depth(ev):
|
|
|
|
node = nodes[ev]
|
2019-06-20 03:32:02 -06:00
|
|
|
if "depth" not in node:
|
2015-11-12 06:10:25 -07:00
|
|
|
prevs = edges[ev]
|
|
|
|
depth = max(_get_depth(prev) for prev in prevs) + 1
|
2019-06-20 03:32:02 -06:00
|
|
|
node["depth"] = depth
|
|
|
|
return node["depth"]
|
2015-11-12 06:10:25 -07:00
|
|
|
|
|
|
|
for n in nodes:
|
|
|
|
_get_depth(n)
|
|
|
|
|
2014-09-12 10:11:00 -06:00
|
|
|
@defer.inlineCallbacks
|
2014-11-11 07:16:48 -07:00
|
|
|
def test_annotate_with_old_message(self):
|
2015-01-21 09:27:04 -07:00
|
|
|
event = create_event(type="test_message", name="event")
|
2014-09-12 10:11:00 -06:00
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
old_state = [
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test1", state_key="2"),
|
|
|
|
create_event(type="test2", state_key=""),
|
2014-11-11 07:16:48 -07:00
|
|
|
]
|
2014-09-12 10:11:00 -06:00
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
context = yield self.state.compute_event_context(event, old_state=old_state)
|
2014-09-12 10:11:00 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
|
|
|
|
2014-12-16 08:07:38 -07:00
|
|
|
self.assertEqual(
|
2018-07-23 06:33:49 -06:00
|
|
|
set(e.event_id for e in old_state), set(current_state_ids.values())
|
2014-12-16 08:07:38 -07:00
|
|
|
)
|
2014-09-12 10:11:00 -06:00
|
|
|
|
2016-08-31 06:55:02 -06:00
|
|
|
self.assertIsNotNone(context.state_group)
|
2014-09-12 10:11:00 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
2014-11-11 07:16:48 -07:00
|
|
|
def test_annotate_with_old_state(self):
|
2015-01-21 09:27:04 -07:00
|
|
|
event = create_event(type="state", state_key="", name="event")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
old_state = [
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test1", state_key="2"),
|
|
|
|
create_event(type="test2", state_key=""),
|
2014-11-11 07:16:48 -07:00
|
|
|
]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
context = yield self.state.compute_event_context(event, old_state=old_state)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
prev_state_ids = yield context.get_prev_state_ids(self.store)
|
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
self.assertEqual(
|
2018-07-23 06:33:49 -06:00
|
|
|
set(e.event_id for e in old_state), set(prev_state_ids.values())
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-11-11 07:16:48 -07:00
|
|
|
def test_trivial_annotate_message(self):
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_event_id = "prev_event_id"
|
|
|
|
event = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type="test_message", name="event2", prev_events=[(prev_event_id, {})]
|
2018-02-06 07:31:24 -07:00
|
|
|
)
|
2014-11-11 07:16:48 -07:00
|
|
|
|
|
|
|
old_state = [
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test1", state_key="2"),
|
|
|
|
create_event(type="test2", state_key=""),
|
2014-11-11 07:16:48 -07:00
|
|
|
]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
group_name = self.store.store_state_group(
|
2018-08-10 07:54:09 -06:00
|
|
|
prev_event_id,
|
|
|
|
event.room_id,
|
|
|
|
None,
|
|
|
|
None,
|
2018-02-06 07:31:24 -07:00
|
|
|
{(e.type, e.state_key): e.event_id for e in old_state},
|
|
|
|
)
|
|
|
|
self.store.register_event_id_state_group(prev_event_id, group_name)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-12-16 08:59:17 -07:00
|
|
|
context = yield self.state.compute_event_context(event)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
self.assertEqual(
|
2018-08-10 07:54:09 -06:00
|
|
|
set([e.event_id for e in old_state]), set(current_state_ids.values())
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
|
2014-12-16 08:07:38 -07:00
|
|
|
self.assertEqual(group_name, context.state_group)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-11-11 07:16:48 -07:00
|
|
|
def test_trivial_annotate_state(self):
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_event_id = "prev_event_id"
|
|
|
|
event = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type="state", state_key="", name="event2", prev_events=[(prev_event_id, {})]
|
2018-02-06 07:31:24 -07:00
|
|
|
)
|
2014-11-11 07:16:48 -07:00
|
|
|
|
|
|
|
old_state = [
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test1", state_key="2"),
|
|
|
|
create_event(type="test2", state_key=""),
|
2014-11-11 07:16:48 -07:00
|
|
|
]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
group_name = self.store.store_state_group(
|
2018-08-10 07:54:09 -06:00
|
|
|
prev_event_id,
|
|
|
|
event.room_id,
|
|
|
|
None,
|
|
|
|
None,
|
2018-02-06 07:31:24 -07:00
|
|
|
{(e.type, e.state_key): e.event_id for e in old_state},
|
|
|
|
)
|
|
|
|
self.store.register_event_id_state_group(prev_event_id, group_name)
|
2014-08-15 04:47:01 -06:00
|
|
|
|
2014-12-16 08:59:17 -07:00
|
|
|
context = yield self.state.compute_event_context(event)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
prev_state_ids = yield context.get_prev_state_ids(self.store)
|
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
self.assertEqual(
|
2018-08-10 07:54:09 -06:00
|
|
|
set([e.event_id for e in old_state]), set(prev_state_ids.values())
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
|
2016-08-31 06:55:02 -06:00
|
|
|
self.assertIsNotNone(context.state_group)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-09-08 12:50:46 -06:00
|
|
|
@defer.inlineCallbacks
|
2014-11-11 07:16:48 -07:00
|
|
|
def test_resolve_message_conflict(self):
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_event_id1 = "event_id1"
|
|
|
|
prev_event_id2 = "event_id2"
|
|
|
|
event = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type="test_message",
|
|
|
|
name="event3",
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
|
|
|
|
)
|
2014-11-11 07:16:48 -07:00
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
creation = create_event(type=EventTypes.Create, state_key="")
|
2015-09-01 08:42:03 -06:00
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
old_state_1 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test1", state_key="2"),
|
|
|
|
create_event(type="test2", state_key=""),
|
2014-09-08 12:50:46 -06:00
|
|
|
]
|
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
old_state_2 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test3", state_key="2"),
|
|
|
|
create_event(type="test4", state_key=""),
|
2014-11-11 07:16:48 -07:00
|
|
|
]
|
2014-09-08 12:50:46 -06:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
self.store.register_events(old_state_1)
|
|
|
|
self.store.register_events(old_state_2)
|
2016-08-26 03:15:52 -06:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
context = yield self._get_context(
|
2018-08-10 07:54:09 -06:00
|
|
|
event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
|
2018-02-06 07:31:24 -07:00
|
|
|
)
|
2014-09-08 12:50:46 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
|
|
|
|
|
|
|
self.assertEqual(len(current_state_ids), 6)
|
2014-09-08 12:50:46 -06:00
|
|
|
|
2016-08-31 06:55:02 -06:00
|
|
|
self.assertIsNotNone(context.state_group)
|
2014-09-08 12:50:46 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-11-11 07:16:48 -07:00
|
|
|
def test_resolve_state_conflict(self):
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_event_id1 = "event_id1"
|
|
|
|
prev_event_id2 = "event_id2"
|
|
|
|
event = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type="test4",
|
|
|
|
state_key="",
|
|
|
|
name="event",
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
|
|
|
|
)
|
2014-11-11 07:16:48 -07:00
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
creation = create_event(type=EventTypes.Create, state_key="")
|
2015-09-01 08:42:03 -06:00
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
old_state_1 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test1", state_key="2"),
|
|
|
|
create_event(type="test2", state_key=""),
|
2014-09-08 12:50:46 -06:00
|
|
|
]
|
|
|
|
|
2014-11-11 07:16:48 -07:00
|
|
|
old_state_2 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2015-01-21 09:27:04 -07:00
|
|
|
create_event(type="test1", state_key="1"),
|
|
|
|
create_event(type="test3", state_key="2"),
|
|
|
|
create_event(type="test4", state_key=""),
|
2014-11-11 07:16:48 -07:00
|
|
|
]
|
2014-09-08 13:13:27 -06:00
|
|
|
|
2016-08-26 03:15:52 -06:00
|
|
|
store = StateGroupStore()
|
|
|
|
store.register_events(old_state_1)
|
|
|
|
store.register_events(old_state_2)
|
|
|
|
self.store.get_events = store.get_events
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
context = yield self._get_context(
|
2018-08-10 07:54:09 -06:00
|
|
|
event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
|
2018-02-06 07:31:24 -07:00
|
|
|
)
|
2014-09-08 13:13:27 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
|
|
|
|
|
|
|
self.assertEqual(len(current_state_ids), 6)
|
2014-09-08 13:13:27 -06:00
|
|
|
|
2016-08-31 06:55:02 -06:00
|
|
|
self.assertIsNotNone(context.state_group)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_standard_depth_conflict(self):
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_event_id1 = "event_id1"
|
|
|
|
prev_event_id2 = "event_id2"
|
|
|
|
event = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type="test4",
|
|
|
|
name="event",
|
2018-02-06 07:31:24 -07:00
|
|
|
prev_events=[(prev_event_id1, {}), (prev_event_id2, {})],
|
|
|
|
)
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
member_event = create_event(
|
|
|
|
type=EventTypes.Member,
|
|
|
|
state_key="@user_id:example.com",
|
2018-08-10 07:54:09 -06:00
|
|
|
content={"membership": Membership.JOIN},
|
2015-01-21 09:27:04 -07:00
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-06-14 07:21:29 -06:00
|
|
|
power_levels = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type=EventTypes.PowerLevels,
|
|
|
|
state_key="",
|
|
|
|
content={"users": {"@foo:bar": "100", "@user_id:example.com": "100"}},
|
2018-06-14 07:21:29 -06:00
|
|
|
)
|
|
|
|
|
2015-09-01 08:42:03 -06:00
|
|
|
creation = create_event(
|
2018-08-10 07:54:09 -06:00
|
|
|
type=EventTypes.Create, state_key="", content={"creator": "@foo:bar"}
|
2015-09-01 08:42:03 -06:00
|
|
|
)
|
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
old_state_1 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2018-06-14 07:21:29 -06:00
|
|
|
power_levels,
|
2015-01-21 09:27:04 -07:00
|
|
|
member_event,
|
|
|
|
create_event(type="test1", state_key="1", depth=1),
|
|
|
|
]
|
|
|
|
|
|
|
|
old_state_2 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2018-06-14 07:21:29 -06:00
|
|
|
power_levels,
|
2015-01-21 09:27:04 -07:00
|
|
|
member_event,
|
|
|
|
create_event(type="test1", state_key="1", depth=2),
|
|
|
|
]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-08-26 03:15:52 -06:00
|
|
|
store = StateGroupStore()
|
|
|
|
store.register_events(old_state_1)
|
|
|
|
store.register_events(old_state_2)
|
|
|
|
self.store.get_events = store.get_events
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
context = yield self._get_context(
|
2018-08-10 07:54:09 -06:00
|
|
|
event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
|
2018-02-06 07:31:24 -07:00
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
self.assertEqual(old_state_2[3].event_id, current_state_ids[("test1", "1")])
|
2015-01-21 09:27:04 -07:00
|
|
|
|
|
|
|
# Reverse the depth to make sure we are actually using the depths
|
|
|
|
# during state resolution.
|
|
|
|
|
|
|
|
old_state_1 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2018-06-14 07:21:29 -06:00
|
|
|
power_levels,
|
2015-01-21 09:27:04 -07:00
|
|
|
member_event,
|
|
|
|
create_event(type="test1", state_key="1", depth=2),
|
|
|
|
]
|
|
|
|
|
|
|
|
old_state_2 = [
|
2015-09-01 08:42:03 -06:00
|
|
|
creation,
|
2018-06-14 07:21:29 -06:00
|
|
|
power_levels,
|
2015-01-21 09:27:04 -07:00
|
|
|
member_event,
|
|
|
|
create_event(type="test1", state_key="1", depth=1),
|
|
|
|
]
|
|
|
|
|
2016-08-26 03:15:52 -06:00
|
|
|
store.register_events(old_state_1)
|
|
|
|
store.register_events(old_state_2)
|
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
context = yield self._get_context(
|
2018-08-10 07:54:09 -06:00
|
|
|
event, prev_event_id1, old_state_1, prev_event_id2, old_state_2
|
2018-02-06 07:31:24 -07:00
|
|
|
)
|
2015-01-21 09:27:04 -07:00
|
|
|
|
2018-07-23 06:33:49 -06:00
|
|
|
current_state_ids = yield context.get_current_state_ids(self.store)
|
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
self.assertEqual(old_state_1[3].event_id, current_state_ids[("test1", "1")])
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2018-08-10 07:54:09 -06:00
|
|
|
def _get_context(
|
|
|
|
self, event, prev_event_id_1, old_state_1, prev_event_id_2, old_state_2
|
|
|
|
):
|
2018-02-06 07:31:24 -07:00
|
|
|
sg1 = self.store.store_state_group(
|
2018-08-10 07:54:09 -06:00
|
|
|
prev_event_id_1,
|
|
|
|
event.room_id,
|
|
|
|
None,
|
|
|
|
None,
|
2018-02-06 07:31:24 -07:00
|
|
|
{(e.type, e.state_key): e.event_id for e in old_state_1},
|
|
|
|
)
|
|
|
|
self.store.register_event_id_state_group(prev_event_id_1, sg1)
|
2014-12-16 08:07:38 -07:00
|
|
|
|
2018-02-06 07:31:24 -07:00
|
|
|
sg2 = self.store.store_state_group(
|
2018-08-10 07:54:09 -06:00
|
|
|
prev_event_id_2,
|
|
|
|
event.room_id,
|
|
|
|
None,
|
|
|
|
None,
|
2018-02-06 07:31:24 -07:00
|
|
|
{(e.type, e.state_key): e.event_id for e in old_state_2},
|
|
|
|
)
|
|
|
|
self.store.register_event_id_state_group(prev_event_id_2, sg2)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2015-01-21 09:27:04 -07:00
|
|
|
return self.state.compute_event_context(event)
|