summary refs log tree commit diff
path: root/tests/test_state.py
blob: aaf873a8567e4138fcf475a2d817be7e8a0dff92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
# Copyright 2014 matrix.org
#
# 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.

from twisted.internet import defer
from twisted.trial import unittest

from synapse.state import StateHandler
from synapse.storage.pdu import PduEntry
from synapse.federation.pdu_codec import encode_event_id

from collections import namedtuple

from mock import Mock


ReturnType = namedtuple(
    "StateReturnType", ["new_branch", "current_branch"]
)


class StateTestCase(unittest.TestCase):
    def setUp(self):
        self.persistence = Mock(spec=[
            "get_unresolved_state_tree",
            "update_current_state",
            "get_latest_pdus_in_context",
            "get_current_state",
            "get_pdu",
        ])
        self.replication = Mock(spec=["get_pdu"])

        hs = Mock(spec=["get_datastore", "get_replication_layer"])
        hs.get_datastore.return_value = self.persistence
        hs.get_replication_layer.return_value = self.replication
        hs.hostname = "bob.com"

        self.state = StateHandler(hs)

    @defer.inlineCallbacks
    def test_new_state_key(self):
        # We've never seen anything for this state before
        new_pdu = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)

        self.persistence.get_unresolved_state_tree.return_value = (
            ReturnType([new_pdu], [])
        )

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertTrue(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_once_with(
            new_pdu
        )

        self.assertEqual(1, self.persistence.update_current_state.call_count)

        self.assertFalse(self.replication.get_pdu.called)

    @defer.inlineCallbacks
    def test_direct_overwrite(self):
        # We do a direct overwriting of the old state, i.e., the new state
        # points to the old state.

        old_pdu = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)
        new_pdu = new_fake_pdu_entry("B", "test", "mem", "x", "A", 5)

        self.persistence.get_unresolved_state_tree.return_value = (
            ReturnType([new_pdu, old_pdu], [old_pdu])
        )

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertTrue(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_once_with(
            new_pdu
        )

        self.assertEqual(1, self.persistence.update_current_state.call_count)

        self.assertFalse(self.replication.get_pdu.called)

    @defer.inlineCallbacks
    def test_power_level_fail(self):
        # We try to update the state based on an outdated state, and have a
        # too low power level.

        old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)
        old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10)
        new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 5)

        self.persistence.get_unresolved_state_tree.return_value = (
            ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1])
        )

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertFalse(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_once_with(
            new_pdu
        )

        self.assertEqual(0, self.persistence.update_current_state.call_count)

        self.assertFalse(self.replication.get_pdu.called)

    @defer.inlineCallbacks
    def test_power_level_succeed(self):
        # We try to update the state based on an outdated state, but have
        # sufficient power level to force the update.

        old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)
        old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10)
        new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 15)

        self.persistence.get_unresolved_state_tree.return_value = (
            ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1])
        )

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertTrue(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_once_with(
            new_pdu
        )

        self.assertEqual(1, self.persistence.update_current_state.call_count)

        self.assertFalse(self.replication.get_pdu.called)

    @defer.inlineCallbacks
    def test_power_level_equal_same_len(self):
        # We try to update the state based on an outdated state, the power
        # levels are the same and so are the branch lengths

        old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)
        old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10)
        new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 10)

        self.persistence.get_unresolved_state_tree.return_value = (
            ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1])
        )

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertTrue(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_once_with(
            new_pdu
        )

        self.assertEqual(1, self.persistence.update_current_state.call_count)

        self.assertFalse(self.replication.get_pdu.called)

    @defer.inlineCallbacks
    def test_power_level_equal_diff_len(self):
        # We try to update the state based on an outdated state, the power
        # levels are the same but the branch length of the new one is longer.

        old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)
        old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10)
        old_pdu_3 = new_fake_pdu_entry("C", "test", "mem", "x", "A", 10)
        new_pdu = new_fake_pdu_entry("D", "test", "mem", "x", "C", 10)

        self.persistence.get_unresolved_state_tree.return_value = (
            ReturnType([new_pdu, old_pdu_3, old_pdu_1], [old_pdu_2, old_pdu_1])
        )

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertTrue(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_once_with(
            new_pdu
        )

        self.assertEqual(1, self.persistence.update_current_state.call_count)

        self.assertFalse(self.replication.get_pdu.called)

    @defer.inlineCallbacks
    def test_missing_pdu(self):
        # We try to update state against a PDU we haven't yet seen,
        # triggering a get_pdu request

        # The pdu we haven't seen
        old_pdu_1 = new_fake_pdu_entry("A", "test", "mem", "x", None, 10)

        old_pdu_2 = new_fake_pdu_entry("B", "test", "mem", "x", None, 10)
        new_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 20)

        # The return_value of `get_unresolved_state_tree`, which changes after
        # the call to get_pdu
        tree_to_return = [ReturnType([new_pdu], [old_pdu_2])]

        def return_tree(p):
            return tree_to_return[0]

        def set_return_tree(*args, **kwargs):
            tree_to_return[0] = ReturnType(
                [new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]
            )

        self.persistence.get_unresolved_state_tree.side_effect = return_tree

        self.replication.get_pdu.side_effect = set_return_tree

        self.persistence.get_pdu.return_value = None

        is_new = yield self.state.handle_new_state(new_pdu)

        self.assertTrue(is_new)

        self.persistence.get_unresolved_state_tree.assert_called_with(
            new_pdu
        )

        self.assertEquals(
            2, self.persistence.get_unresolved_state_tree.call_count
        )

        self.assertEqual(1, self.persistence.update_current_state.call_count)

    @defer.inlineCallbacks
    def test_new_event(self):
        event = Mock()

        state_pdu = new_fake_pdu_entry("C", "test", "mem", "x", "A", 20)

        tup = ("pdu_id", "origin.com", 5)
        pdus = [tup]

        self.persistence.get_latest_pdus_in_context.return_value = pdus
        self.persistence.get_current_state.return_value = state_pdu

        yield self.state.handle_new_event(event)

        self.assertLess(tup[2], event.depth)

        self.assertEquals(1, len(event.prev_events))

        prev_id = event.prev_events[0]

        self.assertEqual(encode_event_id(tup[0], tup[1]), prev_id)

        self.assertEqual(
            encode_event_id(state_pdu.pdu_id, state_pdu.origin),
            event.prev_state
        )


def new_fake_pdu_entry(pdu_id, context, pdu_type, state_key, prev_state_id,
                 power_level):
    new_pdu = PduEntry(
        pdu_id=pdu_id,
        pdu_type=pdu_type,
        state_key=state_key,
        power_level=power_level,
        prev_state_id=prev_state_id,
        origin="example.com",
        context="context",
        ts=1405353060021,
        depth=0,
        content_json="{}",
        unrecognized_keys="{}",
        outlier=True,
        is_state=True,
        prev_state_origin="example.com",
        have_processed=True,
    )

    return new_pdu