Exemplo n.º 1
0
    def test_transit_set(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # All is well
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.stale_v = MagicMock(return_value=False)
        pred_module.transition_cases = MagicMock(return_value=True)
        self.assertEqual(
            pred_module.transit_set(0, 1, ViewEstablishmentEnums.FOLLOW),
            {0, 1})

        # Different phases, node 0 in phase 1 and node 1 in phase 0
        view_est_mod.get_phs = MagicMock(side_effect=lambda x: (x + 1) % 2)
        self.assertEqual(
            pred_module.transit_set(0, 1, ViewEstablishmentEnums.FOLLOW), {1})

        # Transition case returns false
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.transition_cases = MagicMock(return_value=False)
        self.assertEqual(
            pred_module.transit_set(0, 1, ViewEstablishmentEnums.FOLLOW),
            set())

        # Both processors are stale
        pred_module.stale_v = MagicMock(return_value=True)
        pred_module.transition_cases = MagicMock(return_value=True)
        self.assertEqual(
            pred_module.transit_set(0, 1, ViewEstablishmentEnums.FOLLOW),
            set())
Exemplo n.º 2
0
    def test_same_v_set(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Both processors are in the same view and phase
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.stale_v = MagicMock(return_value=False)
        pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 0, NEXT: 0}]
        self.assertEqual(pred_module.same_v_set(0, 0), {0, 1})

        # Processor 1 is not in the same view and but same phase
        pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 0, NEXT: 1}]
        self.assertEqual(pred_module.same_v_set(0, 0), {0})

        # Processor 1 is in the same view and but not in same phase
        view_est_mod.get_phs = MagicMock(
            side_effect=lambda x: x
        )  # Returns the input value (works for this specific case)
        pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 0, NEXT: 0}]
        self.assertEqual(pred_module.same_v_set(0, 0), {0})

        # Processors are stale, should return empty set
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.stale_v = MagicMock(return_value=True)
        pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 0, NEXT: 0}]
        self.assertEqual(pred_module.same_v_set(0, 0), set())
Exemplo n.º 3
0
 def test_added_logic_phase_0_case_1_b(self):
     # No mocking needed, establishable should return true
     view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
     pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)
     pred_module.vChange = [False for i in range(2)]
     pred_module.views = [pred_module.RST_PAIR, pred_module.RST_PAIR]
     view_est_mod.phs = [0, 0]
     self.assertTrue(
         pred_module.automation(ViewEstablishmentEnums.PREDICATE, 0, 1))
Exemplo n.º 4
0
    def test_reset_all(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 1, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 1, 0)
        view_est_mod.init_module = MagicMock()
        self.resolver.execute = MagicMock()

        self.assertEqual(pred_module.reset_all(), ViewEstablishmentEnums.RESET)
        # The views has been reset to DEFAULT view pair
        self.assertEqual(pred_module.views, [pred_module.RST_PAIR])
        # Assert that the init(reset) method at algorithm 1 and algorithm 3 are being called
        view_est_mod.init_module.assert_any_call()
        self.resolver.execute.assert_called_once_with(
            module=Module.REPLICATION_MODULE, func=Function.REP_REQUEST_RESET)
Exemplo n.º 5
0
    def test_automaton_phase_0_actions(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Case 0 should call adopt and next_view in view Establishment module
        view_est_mod.next_phs = Mock()
        pred_module.adopt = Mock()
        pred_module.reset_v_change = Mock()
        pred_module.view_pair_to_adopt = pred_module.RST_PAIR
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 0),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        view_est_mod.next_phs.assert_any_call()
        #pred_module.reset_v_change.assert_any_call()
        pred_module.adopt.assert_called_once_with(pred_module.RST_PAIR)

        # Case 1a should call next_view and next_phs in view Establishment module
        pred_module.next_view = Mock()
        view_est_mod.next_phs = Mock()
        pred_module.vChange = [True for i in range(2)]
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 1),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        pred_module.next_view.assert_any_call()
        view_est_mod.next_phs.assert_any_call()

        # Case 1b should call next_phs in View Establishment module
        pred_module.vChange = [False for i in range(2)]
        view_est_mod.next_phs = Mock()
        pred_module.views[pred_module.id] = pred_module.RST_PAIR
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 1),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        view_est_mod.next_phs.assert_any_call()

        # Case 2 should return "No action" and call reset_v_change
        pred_module.reset_v_change = Mock()
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 2),
            ViewEstablishmentEnums.NO_ACTION)
        # pred_module.reset_v_change.assert_any_call()

        # Case 3 should return "Reset"
        pred_module.reset_all = MagicMock(
            return_value=ViewEstablishmentEnums.RESET)
        #pred_module.reset_v_change = Mock()
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 0, 3),
            ViewEstablishmentEnums.RESET)
Exemplo n.º 6
0
    def test_transit_adopble(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 6, 1)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 6, 1)

        view_est_mod.get_phs = MagicMock(return_value=0)
        # Mocks sets which union does not add up to 4, should return false
        pred_module.transit_set = MagicMock(return_value={1})
        pred_module.same_v_set = MagicMock(return_value={1, 2, 3})
        self.assertFalse(
            pred_module.transit_adopble(0, 0, ViewEstablishmentEnums.FOLLOW))

        # Mocks sets which union adds up to 4, should return true
        pred_module.same_v_set = MagicMock(return_value={2, 3, 4})
        self.assertTrue(
            pred_module.transit_adopble(0, 0, ViewEstablishmentEnums.REMAIN))
Exemplo n.º 7
0
    def test_stale_v(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Stale and in phase 0
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.legit_phs_zero = MagicMock(return_value=False)
        pred_module.legit_phs_one = MagicMock(return_value=False)
        self.assertTrue(pred_module.stale_v(0))
        # Stale and in phase 1
        view_est_mod.get_phs = MagicMock(return_value=1)
        self.assertTrue(pred_module.stale_v(0))
        # Not stale
        pred_module.legit_phs_one = MagicMock(return_value=True)
        self.assertFalse(pred_module.stale_v(0))
Exemplo n.º 8
0
    def test_establishable(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Mocks sets that does not add up to , should return false
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.transit_set = MagicMock(return_value=set())
        pred_module.same_v_set = MagicMock(return_value=set())
        self.assertFalse(
            pred_module.establishable(0, ViewEstablishmentEnums.FOLLOW))

        # Mocks sets that adds up to over 1 , should return true
        pred_module.transit_set = MagicMock(return_value={1})
        pred_module.same_v_set = MagicMock(return_value={1})
        self.assertTrue(
            pred_module.establishable(0, ViewEstablishmentEnums.REMAIN))
Exemplo n.º 9
0
def start_modules(resolver):
    """Starts all modules in separate threads."""
    n = int(os.getenv("NUMBER_OF_NODES", 0))
    f = int(os.getenv("NUMBER_OF_BYZANTINE", 0))
    k = int(os.getenv("NUMBER_OF_CLIENTS", 0))

    if n == 0:
        logger.warning("Env var NUMBER_OF_NODES not set or set to 0")
    if f == 0:
        logger.warning("Env var NUMBER_OF_BYZANTINE not set or set to 0")
    if k == 0:
        logger.warning("Env var NUMBER_OF_CLIENTS not set or set to 0")

    if os.getenv("INJECT_START_STATE"):
        logger.warning("Node will load state from conf/start_state.json")

    modules = {
        Module.REPLICATION_MODULE:
        ReplicationModule(id, resolver, n, f, k),
        Module.PRIMARY_MONITORING_MODULE:
        PrimaryMonitoringModule(id, resolver, n, f),
        Module.FAILURE_DETECTOR_MODULE:
        FailureDetectorModule(id, resolver, n, f)
    }

    if not os.getenv("NON_SELF_STAB"):
        modules[Module.VIEW_ESTABLISHMENT_MODULE] = ViewEstablishmentModule(
            id, resolver, n, f)

    resolver.set_modules(modules)

    # start threads and attach to resolver
    for m in modules.values():
        t = Thread(target=m.run)
        t.start()
Exemplo n.º 10
0
    def test_transition_cases(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)
        view_pair_to_test = {CURRENT: 0, NEXT: 1}

        # Mode REMAIN both phases, one
        pred_module.views[pred_module.id].update({CURRENT: 1, NEXT: 1})
        self.assertTrue(
            pred_module.transition_cases(0, view_pair_to_test, 0,
                                         ViewEstablishmentEnums.REMAIN))
        pred_module.views[pred_module.id].update({CURRENT: 0, NEXT: 0})
        self.assertFalse(
            pred_module.transition_cases(0, view_pair_to_test, 1,
                                         ViewEstablishmentEnums.REMAIN))

        # Mode FOLLOW, phase 0 should return true
        self.assertTrue(
            pred_module.transition_cases(0, view_pair_to_test, 0,
                                         ViewEstablishmentEnums.FOLLOW))
        # Mode FOLLOW, phase 0 should return false
        pred_module.views[pred_module.id].update({CURRENT: 1, NEXT: 1})
        self.assertFalse(
            pred_module.transition_cases(0, view_pair_to_test, 0,
                                         ViewEstablishmentEnums.FOLLOW))

        # Mode FOLLOW, phase 1 should return false
        self.assertFalse(
            pred_module.transition_cases(0, view_pair_to_test, 1,
                                         ViewEstablishmentEnums.FOLLOW))

        # Mode FOLLOW, phase 1 should return true
        pred_module.views[pred_module.id].update({CURRENT: 0, NEXT: 0})
        self.assertTrue(
            pred_module.transition_cases(0, view_pair_to_test, 1,
                                         ViewEstablishmentEnums.FOLLOW))
Exemplo n.º 11
0
 def test_set_info(self):
     view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
     pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)
     pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 0, NEXT: 0}]
     # Should change the view pair of node 1
     pred_module.set_info({CURRENT: 1, NEXT: 1}, False, 1)
     self.assertEqual(pred_module.views[1], {CURRENT: 1, NEXT: 1})
Exemplo n.º 12
0
    def test_interface_get_current_view(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)
        pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 1, NEXT: 1}]
        view_est_mod.get_phs = MagicMock(return_value=1)
        view_est_mod.witnes_seen = MagicMock(return_value=True)
        pred_module.allow_service = MagicMock(return_value=True)

        # Should return the view of the current node
        self.assertEqual(pred_module.get_current_view(0), 0)
        # Should return the view of an other processor
        self.assertEqual(pred_module.get_current_view(1), 1)
        # Should give back the TEE (None) by denying service
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.allow_service = MagicMock(return_value=False)
        self.assertEqual(pred_module.get_current_view(0),
                         ViewEstablishmentEnums.TEE)
Exemplo n.º 13
0
    def test_interface_allow_service(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 6, 1)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 6, 1)
        # Allow service settings
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.views = [{CURRENT: 0, NEXT: 0}]
        pred_module.same_v_set = MagicMock(return_value={0, 1, 2, 3, 4, 5})
        self.assertTrue(pred_module.allow_service())

        #Deny service settings, creating a false statement for each condition
        view_est_mod.get_phs = MagicMock(return_value=1)
        self.assertFalse(pred_module.allow_service())
        view_est_mod.get_phs = MagicMock(return_value=0)
        pred_module.views = [{CURRENT: 0, NEXT: 1}]
        self.assertFalse(pred_module.allow_service())
        pred_module.views = [{CURRENT: 0, NEXT: 0}]
        pred_module.same_v_set = MagicMock(return_value={})
        self.assertFalse(pred_module.allow_service())
Exemplo n.º 14
0
    def test_establish(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Should update to view 1 in current
        pred_module.views = [{CURRENT: 0, NEXT: 1}]
        self.resolver.on_view_established = Mock()
        pred_module.establish()
        self.assertEqual(pred_module.views, [{CURRENT: 1, NEXT: 1}])
Exemplo n.º 15
0
 def test_get_info(self):
     view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
     pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)
     pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 1, NEXT: 1}]
     # Should return node 1's view
     self.assertEqual(pred_module.get_info(1), ({
         CURRENT: 1,
         NEXT: 1
     }, False))
Exemplo n.º 16
0
    def test_automaton_phase_1_actions(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Case 0 should call adopt and next_view in view Establishment module
        pred_module.adopt = Mock()
        pred_module.reset_v_change = Mock()
        pred_module.view_pair_to_adopt = pred_module.RST_PAIR

        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 0),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        pred_module.adopt.assert_called_once_with(pred_module.RST_PAIR)
        pred_module.reset_v_change.assert_any_call()

        # Case 1 should call next_view and next_phs in view Establishment module
        view_est_mod.next_phs = Mock()
        pred_module.reset_v_change = Mock()
        pred_module.establish = Mock()
        self.resolver.execute = Mock()
        pred_module.views[pred_module.id] = pred_module.RST_PAIR

        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 1),
            ViewEstablishmentEnums.NO_RETURN_VALUE)
        view_est_mod.next_phs.assert_any_call()
        pred_module.reset_v_change.assert_any_call()
        pred_module.establish.assert_any_call()
        # self.resolver.execute.assert_called_once_with(module=Module.REPLICATION_MODULE,
        #                 func=Function.REPLICA_FLUSH)

        # Case 2 should return "No action" and call reset_v_change
        pred_module.reset_v_change = Mock()
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 2),
            ViewEstablishmentEnums.NO_ACTION)
        pred_module.reset_v_change.assert_any_call()

        # Case 3 should return "Reset"
        pred_module.reset_all = MagicMock(
            return_value=ViewEstablishmentEnums.RESET)
        self.assertEqual(
            pred_module.automation(ViewEstablishmentEnums.ACTION, 1, 3),
            ViewEstablishmentEnums.RESET)
Exemplo n.º 17
0
 def test_valid(self):
     view_est_mod = ViewEstablishmentModule(0, self.resolver, 5, 0)
     v_pair = {
         CURRENT: ViewEstablishmentEnums.TEE,
         NEXT: ViewEstablishmentEnums.DF_VIEW
     }
     msg_to_valid = [0, False, v_pair]
     self.assertTrue(view_est_mod.pred_and_action.valid(msg_to_valid))
     msg_to_valid = [1, False, {CURRENT: 1, NEXT: 1}]
     self.assertFalse(view_est_mod.pred_and_action.valid(msg_to_valid))
Exemplo n.º 18
0
    def test_init(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        view_est_mod.phs = [0, 1]
        view_est_mod.witnesses_set = {0}
        view_est_mod.witnesses = [True, True]

        view_est_mod.init_module()
        self.assertEqual(view_est_mod.phs, [0, 0])
        self.assertEqual(view_est_mod.witnesses_set, set())
        self.assertEqual(view_est_mod.witnesses, [False, False])
Exemplo n.º 19
0
    def test_adopt(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)

        # Default pred_module.views = {self.CURRENT: TEE, self.NEXT: DF_VIEW}
        vpair_to_test = {CURRENT: 1, NEXT: 1}
        view_est_mod.pred_and_action.adopt(vpair_to_test)
        self.assertEqual(
            view_est_mod.pred_and_action.views[
                view_est_mod.pred_and_action.id], {
                    CURRENT: ViewEstablishmentEnums.TEE,
                    NEXT: 1
                })
Exemplo n.º 20
0
    def test_noticed_recent_value(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 1)

        # All have noticed
        view_est_mod.echo_no_witn = MagicMock(return_value=True)
        self.assertTrue(view_est_mod.noticed_recent_value())

        # Processor 1 has not noticed (0 False, 1 True)
        view_est_mod.echo_no_witn = MagicMock(side_effect=lambda x: not x)
        self.assertTrue(view_est_mod.noticed_recent_value())

        # None have noticed
        view_est_mod.echo_no_witn = MagicMock(return_value=False)
        self.assertFalse(view_est_mod.noticed_recent_value())
Exemplo n.º 21
0
    def test_get_witnesses(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)

        # Both processors have been witnessed
        view_est_mod.witnesses = [True, True]
        self.assertEqual(view_est_mod.get_witnesses(), {0, 1})

        # Both processor 1 has been witnessed, not processor 0
        view_est_mod.witnesses = [False, True]
        self.assertEqual(view_est_mod.get_witnesses(), {1})

        # None of the processors have been witnessed
        view_est_mod.witnesses = [False, False]
        self.assertEqual(view_est_mod.get_witnesses(), set())
Exemplo n.º 22
0
    def test_next_view(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Should update to view 1 in next
        pred_module.views = [{CURRENT: 0, NEXT: 0}]
        pred_module.next_view()
        self.assertEqual(pred_module.views, [{CURRENT: 0, NEXT: 1}])

        # number of byzantine nodes = 2 so "next" should flip to view 0
        pred_module.views = [{CURRENT: 1, NEXT: 1}]
        pred_module.next_view()
        self.assertEqual(pred_module.views, [{CURRENT: 1, NEXT: 0}])
Exemplo n.º 23
0
    def test_need_reset(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        pred_module.stale_v = MagicMock(return_value=True)
        self.resolver.execute = MagicMock(return_value=True)
        self.assertTrue(pred_module.need_reset())

        pred_module.stale_v = MagicMock(return_value=False)
        self.assertTrue(pred_module.need_reset())

        self.resolver.execute = MagicMock(return_value=False)
        self.assertFalse(pred_module.need_reset())
Exemplo n.º 24
0
    def test_valid(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        # A msg has the following structure:[phase, witnesses, view_pair]

        # The view pairs are allowed in the corresponding phase
        msg_to_valid = [0, {}, {"current": 0, "next": 0}]
        self.assertTrue(view_est_mod.pred_and_action.valid(msg_to_valid))
        msg_to_valid = [1, {}, {"current": 0, "next": 1}]
        self.assertTrue(view_est_mod.pred_and_action.valid(msg_to_valid))
        # The view pairs are not allowed in the corresponding phase
        msg_to_valid = [0, {}, {"current": 0, "next": 1}]
        self.assertFalse(view_est_mod.pred_and_action.valid(msg_to_valid))
        msg_to_valid = [1, {}, {"current": 0, "next": 0}]
        self.assertFalse(view_est_mod.pred_and_action.valid(msg_to_valid))
Exemplo n.º 25
0
    def test_while_true_no_case_is_true(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)

        # (1)Predicates and action reset all should not be called
        view_est_mod.pred_and_action.need_reset = MagicMock(return_value=False)
        view_est_mod.pred_and_action.reset_all = Mock()

        # (2) Processor i recent values are noticed and both processors have been witnessed
        view_est_mod.noticed_recent_value = MagicMock(return_value=True)
        view_est_mod.get_witnesses = MagicMock(return_value={0, 1})

        # (3) No predicate is true
        view_est_mod.witnes_seen = MagicMock(return_value=True)
        view_est_mod.pred_and_action.automation = MagicMock(return_value=False)

        # (4) Mocks the final calls
        view_est_mod.next_phs = Mock()
        view_est_mod.send_msg = Mock()

        # Run the method and check all statements above
        view_est_mod.run(testing=True)

        # (1) Predicates and action reset all should not be called
        view_est_mod.pred_and_action.reset_all.assert_not_called()

        # (3)
        calls_automaton = [
            call(ViewEstablishmentEnums.PREDICATE,
                 view_est_mod.phs[view_est_mod.id], 0),
            call(ViewEstablishmentEnums.PREDICATE,
                 view_est_mod.phs[view_est_mod.id], 1),
            call(ViewEstablishmentEnums.PREDICATE,
                 view_est_mod.phs[view_est_mod.id], 2),
            call(ViewEstablishmentEnums.PREDICATE,
                 view_est_mod.phs[view_est_mod.id], 3)
        ]
        view_est_mod.pred_and_action.automation.assert_has_calls(
            calls_automaton, any_order=False)

        # (4) Check that next_phs is not called and send_msg are called with correct arguments
        view_est_mod.next_phs.assert_not_called()
Exemplo n.º 26
0
    def test_next_phs(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        view_est_mod.phs[view_est_mod.id] = 0

        # Move to phase 1
        view_est_mod.next_phs()
        self.assertEqual(view_est_mod.phs[view_est_mod.id], 1)

        # Move to phase 2
        view_est_mod.next_phs()
        self.assertEqual(view_est_mod.phs[view_est_mod.id], 0)
Exemplo n.º 27
0
    def test_legit_phs_one(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)
        pred_module.type_check = MagicMock(return_value=True)
        vpair_to_test = {CURRENT: 1, NEXT: 2}

        # Legit to be in phase 1
        self.assertTrue(pred_module.legit_phs_one(vpair_to_test))

        # Not in a view change
        vpair_to_test = {CURRENT: 1, NEXT: 1}
        self.assertFalse(pred_module.legit_phs_one(vpair_to_test))

        # Type check fails
        vpair_to_test = {CURRENT: 1, NEXT: 2}
        pred_module.type_check = MagicMock(return_value=False)
        self.assertFalse(pred_module.legit_phs_one(vpair_to_test))
Exemplo n.º 28
0
    def test_while_true_case_1_is_true_and_return_is_no_action(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)

        # (1)Predicates and action reset all should be called
        view_est_mod.pred_and_action.need_reset = MagicMock(return_value=True)
        view_est_mod.pred_and_action.reset_all = Mock()

        # (2) Processor i recent values are noticed and both processors have been witnessed
        view_est_mod.noticed_recent_value = MagicMock(return_value=False)
        view_est_mod.get_witnesses = MagicMock(return_value=set())

        # (3)Let predicate of case 0 be false and case 1 true
        view_est_mod.witnes_seen = MagicMock(return_value=True)
        view_est_mod.pred_and_action.automation = MagicMock(
            side_effect=self.side_effect_case_1_return_no_action)

        # (4) Mocks the final calls
        view_est_mod.next_phs = Mock()
        view_est_mod.send_msg = Mock()

        # Run the method and check all statements above
        view_est_mod.run(testing=True)

        # (3) Let predicate of case 0 be false and case 1 true, make sure function is called
        calls_automaton = [
            call(ViewEstablishmentEnums.PREDICATE,
                 view_est_mod.phs[view_est_mod.id], 0),
            call(ViewEstablishmentEnums.PREDICATE,
                 view_est_mod.phs[view_est_mod.id], 1),
            call(ViewEstablishmentEnums.ACTION,
                 view_est_mod.phs[view_est_mod.id], 1)
        ]
        # any_order means that no other calls to the function should be made
        view_est_mod.pred_and_action.automation.assert_has_calls(
            calls_automaton, any_order=False)

        # (4) Check that the functions are called with correct input
        view_est_mod.next_phs.assert_not_called()
Exemplo n.º 29
0
    def test_echo_no_witn(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)

        # Both conditions are fulfilled
        view_est_mod.phs[view_est_mod.id] = 0
        view_est_mod.pred_and_action.get_info = MagicMock(
            return_value=({
                "current": 0,
                "next": 1
            }, False, False))
        view_est_mod.echo[1] = {
            VIEWS: {
                "current": 0,
                "next": 1
            },
            PHASE: 0,
            WITNESSES: None,
            VCHANGE: False
        }
        self.assertTrue(view_est_mod.echo_no_witn(1))

        # The view in the echo is not correct
        view_est_mod.echo[1] = {
            VIEWS: {
                "current": 0,
                "next": 0
            },
            PHASE: 0,
            WITNESSES: None
        }
        self.assertFalse(view_est_mod.echo_no_witn(1))

        # The phase in the echo is not correct
        view_est_mod.echo[1] = {
            VIEWS: {
                "current": 0,
                "next": 1
            },
            PHASE: 1,
            WITNESSES: None
        }
        self.assertFalse(view_est_mod.echo_no_witn(1))
Exemplo n.º 30
0
    def test_automaton_phase_1_predicates(self):
        view_est_mod = ViewEstablishmentModule(0, self.resolver, 2, 0)
        pred_module = PredicatesAndAction(view_est_mod, 0, self.resolver, 2, 0)

        # Case 0
        pred_module.views = [{CURRENT: 0, NEXT: 4}, {CURRENT: 1, NEXT: 1}]
        pred_module.transit_adopble = MagicMock(return_value=True)
        self.assertTrue(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 0))

        # Not a adoptable view in transit
        pred_module.transit_adopble = MagicMock(return_value=False)
        self.assertFalse(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 0))
        # The adoptable view is the same as processor i
        pred_module.views = [{CURRENT: 0, NEXT: 0}, {CURRENT: 0, NEXT: 0}]
        pred_module.transit_adopble = MagicMock(return_value=True)
        self.assertFalse(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 0))

        # Case 1
        pred_module.establishable = MagicMock(return_value=True)
        self.assertTrue(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 1))
        # The threshold to move to a view change is not fulfilled
        pred_module.establishable = MagicMock(return_value=False)
        self.assertFalse(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 1))

        # Case 2
        # There is not an adoptable view in transit
        self.assertTrue(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 2))
        # There is not an adoptable view in transit
        pred_module.transit_adopble = MagicMock(return_value=False)
        self.assertFalse(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 2))

        # Case 3 should return True
        self.assertTrue(
            pred_module.automation(ViewEstablishmentEnums.PREDICATE, 1, 3))