2014-08-12 18:10:52 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-03 20:29:13 +04:00
|
|
|
# Copyright 2014 OpenMarket Ltd
|
2014-08-13 06:32:18 +04: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.
|
|
|
|
|
2014-09-12 21:24:53 +04:00
|
|
|
from tests import unittest
|
2014-08-12 18:10:52 +04:00
|
|
|
from twisted.internet import defer
|
2014-09-12 20:11:00 +04:00
|
|
|
from twisted.python.log import PythonLoggingObserver
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
from synapse.state import StateHandler
|
|
|
|
from synapse.storage.pdu import PduEntry
|
|
|
|
from synapse.federation.pdu_codec import encode_event_id
|
2014-09-12 20:11:00 +04:00
|
|
|
from synapse.federation.units import Pdu
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
2014-09-08 22:50:46 +04:00
|
|
|
import mock
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
ReturnType = namedtuple(
|
|
|
|
"StateReturnType", ["new_branch", "current_branch"]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
def _gen_get_power_level(power_level_list):
|
|
|
|
def get_power_level(room_id, user_id):
|
|
|
|
return defer.succeed(power_level_list.get(user_id, None))
|
|
|
|
return get_power_level
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
class StateTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.persistence = Mock(spec=[
|
|
|
|
"get_unresolved_state_tree",
|
|
|
|
"update_current_state",
|
|
|
|
"get_latest_pdus_in_context",
|
2014-08-15 20:42:21 +04:00
|
|
|
"get_current_state_pdu",
|
2014-08-15 14:47:01 +04:00
|
|
|
"get_pdu",
|
2014-09-12 20:11:00 +04:00
|
|
|
"get_power_level",
|
2014-08-12 18:10:52 +04:00
|
|
|
])
|
|
|
|
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
|
2014-09-12 20:11:00 +04:00
|
|
|
new_pdu = new_fake_pdu("A", "test", "mem", "x", None, "u")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({})
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
2014-09-08 22:50:46 +04:00
|
|
|
(ReturnType([new_pdu], []), None)
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
is_new = yield self.state.handle_new_state(new_pdu)
|
|
|
|
|
2014-10-09 18:53:40 +04:00
|
|
|
self.assertTrue(is_new)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
new_pdu = new_fake_pdu("B", "test", "mem", "x", "A", "u2")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 5,
|
|
|
|
})
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
2014-09-08 22:50:46 +04:00
|
|
|
(ReturnType([new_pdu, old_pdu], [old_pdu]), None)
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_overwrite(self):
|
|
|
|
old_pdu_1 = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
old_pdu_2 = new_fake_pdu("B", "test", "mem", "x", "A", "u2")
|
|
|
|
new_pdu = new_fake_pdu("C", "test", "mem", "x", "B", "u3")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 5,
|
|
|
|
"u3": 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
|
|
|
(ReturnType([new_pdu, old_pdu_2, old_pdu_1], [old_pdu_1]), None)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
@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.
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
old_pdu_2 = new_fake_pdu("B", "test", "mem", "x", None, "u2")
|
|
|
|
new_pdu = new_fake_pdu("C", "test", "mem", "x", "A", "u3")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 5,
|
|
|
|
})
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
2014-09-08 22:50:46 +04:00
|
|
|
(ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]), None)
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
old_pdu_2 = new_fake_pdu("B", "test", "mem", "x", None, "u2")
|
|
|
|
new_pdu = new_fake_pdu("C", "test", "mem", "x", "A", "u3")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 15,
|
|
|
|
})
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
2014-09-08 22:50:46 +04:00
|
|
|
(ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]), None)
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
old_pdu_2 = new_fake_pdu("B", "test", "mem", "x", None, "u2")
|
|
|
|
new_pdu = new_fake_pdu("C", "test", "mem", "x", "A", "u3")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 10,
|
|
|
|
})
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
2014-09-08 22:50:46 +04:00
|
|
|
(ReturnType([new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]), None)
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
old_pdu_2 = new_fake_pdu("B", "test", "mem", "x", None, "u2")
|
|
|
|
old_pdu_3 = new_fake_pdu("C", "test", "mem", "x", "A", "u3")
|
|
|
|
new_pdu = new_fake_pdu("D", "test", "mem", "x", "C", "u4")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 10,
|
|
|
|
"u4": 10,
|
|
|
|
})
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
2014-09-08 22:50:46 +04:00
|
|
|
(
|
|
|
|
ReturnType(
|
|
|
|
[new_pdu, old_pdu_3, old_pdu_1],
|
|
|
|
[old_pdu_2, old_pdu_1]
|
|
|
|
),
|
|
|
|
None
|
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu(
|
|
|
|
"A", "test", "mem", "x", None, "u1", depth=0
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_2 = new_fake_pdu(
|
|
|
|
"B", "test", "mem", "x", "A", "u2", depth=1
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-09-12 20:11:00 +04:00
|
|
|
new_pdu = new_fake_pdu(
|
|
|
|
"C", "test", "mem", "x", "A", "u3", depth=2
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 20,
|
|
|
|
})
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
# The return_value of `get_unresolved_state_tree`, which changes after
|
|
|
|
# the call to get_pdu
|
2014-09-08 22:50:46 +04:00
|
|
|
tree_to_return = [(ReturnType([new_pdu], [old_pdu_2]), 0)]
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
def return_tree(p):
|
|
|
|
return tree_to_return[0]
|
|
|
|
|
2014-09-08 22:50:46 +04:00
|
|
|
def set_return_tree(destination, pdu_origin, pdu_id, outlier=False):
|
|
|
|
tree_to_return[0] = (
|
|
|
|
ReturnType(
|
|
|
|
[new_pdu, old_pdu_1], [old_pdu_2, old_pdu_1]
|
|
|
|
),
|
|
|
|
None
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
2014-09-08 22:50:46 +04:00
|
|
|
return defer.succeed(None)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.side_effect = return_tree
|
|
|
|
|
|
|
|
self.replication.get_pdu.side_effect = set_return_tree
|
|
|
|
|
2014-08-15 14:47:01 +04:00
|
|
|
self.persistence.get_pdu.return_value = None
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
is_new = yield self.state.handle_new_state(new_pdu)
|
|
|
|
|
|
|
|
self.assertTrue(is_new)
|
|
|
|
|
2014-09-08 22:50:46 +04:00
|
|
|
self.replication.get_pdu.assert_called_with(
|
|
|
|
destination=new_pdu.origin,
|
|
|
|
pdu_origin=old_pdu_1.origin,
|
|
|
|
pdu_id=old_pdu_1.pdu_id,
|
|
|
|
outlier=True
|
|
|
|
)
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
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)
|
|
|
|
|
2014-09-08 22:50:46 +04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_missing_pdu_depth_1(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
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu(
|
|
|
|
"A", "test", "mem", "x", None, "u1", depth=0
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_2 = new_fake_pdu(
|
|
|
|
"B", "test", "mem", "x", "A", "u2", depth=2
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_3 = new_fake_pdu(
|
|
|
|
"C", "test", "mem", "x", "B", "u3", depth=3
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-09-12 20:11:00 +04:00
|
|
|
new_pdu = new_fake_pdu(
|
|
|
|
"D", "test", "mem", "x", "A", "u4", depth=4
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 10,
|
|
|
|
"u4": 20,
|
|
|
|
})
|
|
|
|
|
2014-09-08 22:50:46 +04:00
|
|
|
# The return_value of `get_unresolved_state_tree`, which changes after
|
|
|
|
# the call to get_pdu
|
|
|
|
tree_to_return = [
|
|
|
|
(
|
|
|
|
ReturnType([new_pdu], [old_pdu_3]),
|
|
|
|
0
|
|
|
|
),
|
|
|
|
(
|
|
|
|
ReturnType(
|
|
|
|
[new_pdu, old_pdu_1], [old_pdu_3]
|
|
|
|
),
|
|
|
|
1
|
|
|
|
),
|
|
|
|
(
|
|
|
|
ReturnType(
|
|
|
|
[new_pdu, old_pdu_1], [old_pdu_3, old_pdu_2, old_pdu_1]
|
|
|
|
),
|
|
|
|
None
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
to_return = [0]
|
|
|
|
|
|
|
|
def return_tree(p):
|
|
|
|
return tree_to_return[to_return[0]]
|
|
|
|
|
|
|
|
def set_return_tree(destination, pdu_origin, pdu_id, outlier=False):
|
|
|
|
to_return[0] += 1
|
|
|
|
return defer.succeed(None)
|
|
|
|
|
|
|
|
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.assertEqual(2, self.replication.get_pdu.call_count)
|
|
|
|
|
|
|
|
self.replication.get_pdu.assert_has_calls(
|
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
destination=new_pdu.origin,
|
|
|
|
pdu_origin=old_pdu_1.origin,
|
|
|
|
pdu_id=old_pdu_1.pdu_id,
|
|
|
|
outlier=True
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
destination=old_pdu_3.origin,
|
|
|
|
pdu_origin=old_pdu_2.origin,
|
|
|
|
pdu_id=old_pdu_2.pdu_id,
|
|
|
|
outlier=True
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.assert_called_with(
|
|
|
|
new_pdu
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(
|
|
|
|
3, self.persistence.get_unresolved_state_tree.call_count
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(1, self.persistence.update_current_state.call_count)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_missing_pdu_depth_2(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
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_1 = new_fake_pdu(
|
|
|
|
"A", "test", "mem", "x", None, "u1", depth=0
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_2 = new_fake_pdu(
|
|
|
|
"B", "test", "mem", "x", "A", "u2", depth=2
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu_3 = new_fake_pdu(
|
|
|
|
"C", "test", "mem", "x", "B", "u3", depth=3
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
2014-09-12 20:11:00 +04:00
|
|
|
new_pdu = new_fake_pdu(
|
|
|
|
"D", "test", "mem", "x", "A", "u4", depth=1
|
2014-09-08 22:50:46 +04:00
|
|
|
)
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 10,
|
|
|
|
"u2": 10,
|
|
|
|
"u3": 10,
|
|
|
|
"u4": 20,
|
|
|
|
})
|
|
|
|
|
2014-09-08 22:50:46 +04:00
|
|
|
# The return_value of `get_unresolved_state_tree`, which changes after
|
|
|
|
# the call to get_pdu
|
|
|
|
tree_to_return = [
|
|
|
|
(
|
|
|
|
ReturnType([new_pdu], [old_pdu_3]),
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
ReturnType(
|
|
|
|
[new_pdu], [old_pdu_3, old_pdu_2]
|
|
|
|
),
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
ReturnType(
|
|
|
|
[new_pdu, old_pdu_1], [old_pdu_3, old_pdu_2, old_pdu_1]
|
|
|
|
),
|
|
|
|
None
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
to_return = [0]
|
|
|
|
|
|
|
|
def return_tree(p):
|
|
|
|
return tree_to_return[to_return[0]]
|
|
|
|
|
|
|
|
def set_return_tree(destination, pdu_origin, pdu_id, outlier=False):
|
|
|
|
to_return[0] += 1
|
|
|
|
return defer.succeed(None)
|
|
|
|
|
|
|
|
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.assertEqual(2, self.replication.get_pdu.call_count)
|
|
|
|
|
|
|
|
self.replication.get_pdu.assert_has_calls(
|
|
|
|
[
|
|
|
|
mock.call(
|
|
|
|
destination=old_pdu_3.origin,
|
|
|
|
pdu_origin=old_pdu_2.origin,
|
|
|
|
pdu_id=old_pdu_2.pdu_id,
|
|
|
|
outlier=True
|
|
|
|
),
|
|
|
|
mock.call(
|
|
|
|
destination=new_pdu.origin,
|
|
|
|
pdu_origin=old_pdu_1.origin,
|
|
|
|
pdu_id=old_pdu_1.pdu_id,
|
|
|
|
outlier=True
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.assert_called_with(
|
|
|
|
new_pdu
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals(
|
|
|
|
3, self.persistence.get_unresolved_state_tree.call_count
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(1, self.persistence.update_current_state.call_count)
|
|
|
|
|
2014-09-08 23:13:27 +04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_no_common_ancestor(self):
|
|
|
|
# We do a direct overwriting of the old state, i.e., the new state
|
|
|
|
# points to the old state.
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
old_pdu = new_fake_pdu("A", "test", "mem", "x", None, "u1")
|
|
|
|
new_pdu = new_fake_pdu("B", "test", "mem", "x", None, "u2")
|
|
|
|
|
|
|
|
self.persistence.get_power_level.side_effect = _gen_get_power_level({
|
|
|
|
"u1": 5,
|
|
|
|
"u2": 10,
|
|
|
|
})
|
2014-09-08 23:13:27 +04:00
|
|
|
|
|
|
|
self.persistence.get_unresolved_state_tree.return_value = (
|
|
|
|
(ReturnType([new_pdu], [old_pdu]), None)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-08-12 18:10:52 +04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_new_event(self):
|
|
|
|
event = Mock()
|
2014-09-06 10:44:00 +04:00
|
|
|
event.event_id = "12123123@test"
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
state_pdu = new_fake_pdu("C", "test", "mem", "x", "A", 20)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-08-27 18:11:51 +04:00
|
|
|
snapshot = Mock()
|
|
|
|
snapshot.prev_state_pdu = state_pdu
|
|
|
|
event_id = "pdu_id@origin.com"
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-08-27 18:11:51 +04:00
|
|
|
def fill_out_prev_events(event):
|
|
|
|
event.prev_events = [event_id]
|
|
|
|
event.depth = 6
|
|
|
|
snapshot.fill_out_prev_events = fill_out_prev_events
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-08-27 18:11:51 +04:00
|
|
|
yield self.state.handle_new_event(event, snapshot)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
2014-08-27 18:11:51 +04:00
|
|
|
self.assertLess(5, event.depth)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.assertEquals(1, len(event.prev_events))
|
|
|
|
|
|
|
|
prev_id = event.prev_events[0]
|
|
|
|
|
2014-08-27 18:11:51 +04:00
|
|
|
self.assertEqual(event_id, prev_id)
|
2014-08-12 18:10:52 +04:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
encode_event_id(state_pdu.pdu_id, state_pdu.origin),
|
|
|
|
event.prev_state
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-09-12 20:11:00 +04:00
|
|
|
def new_fake_pdu(pdu_id, context, pdu_type, state_key, prev_state_id,
|
|
|
|
user_id, depth=0):
|
|
|
|
new_pdu = Pdu(
|
2014-08-12 18:10:52 +04:00
|
|
|
pdu_id=pdu_id,
|
|
|
|
pdu_type=pdu_type,
|
|
|
|
state_key=state_key,
|
2014-09-12 20:11:00 +04:00
|
|
|
user_id=user_id,
|
2014-08-12 18:10:52 +04:00
|
|
|
prev_state_id=prev_state_id,
|
|
|
|
origin="example.com",
|
|
|
|
context="context",
|
2014-10-17 20:12:25 +04:00
|
|
|
origin_server_ts=1405353060021,
|
2014-09-08 22:50:46 +04:00
|
|
|
depth=depth,
|
2014-08-12 18:10:52 +04:00
|
|
|
content_json="{}",
|
|
|
|
unrecognized_keys="{}",
|
|
|
|
outlier=True,
|
|
|
|
is_state=True,
|
|
|
|
prev_state_origin="example.com",
|
|
|
|
have_processed=True,
|
2014-09-12 20:11:00 +04:00
|
|
|
content={},
|
2014-08-12 18:10:52 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
return new_pdu
|