def random_victims(self, times, delay):
        """ Spawn a thread and send a potential victim instead of using a
            mock Publisher which is not so predictable or easy to configure.
        """
        visited = [mock_msgs.create_victim_info() for i in range(0, 3)]
        victim = [mock_msgs.create_victim_info()]

        model = mock_msgs.create_world_model(victim, visited)
        for i in range(times):
            sleep(delay)
            self.agent.receive_world_model(model)
    def test_choose_new_victim(self):
        """ Tests that the target is updated. """

        target = mock_msgs.create_victim_info(id=1, probability=0.2)

        visited = [mock_msgs.create_victim_info() for i in range(2)]
        current_victims = [target]
        model = mock_msgs.create_world_model(current_victims, visited)

        with patch.object(self.agent.target, 'update') as mock_update:
            with patch.object(self.agent.dispatcher, 'emit') as emitter:
                self.agent.receive_world_model(model)

                self.assertFalse(mock_update.called)
                self.assertTrue(emitter.called)
                self.assertEqual(self.agent.target.info, target)
                self.assertFalse(self.agent.target.is_verified())
                self.assertFalse(self.agent.target.is_identified())
    def test_non_existent_victim(self):
        target = mock_msgs.create_victim_info(id=1, probability=0.2)
        target2 = mock_msgs.create_victim_info(id=2, probability=0.2)

        visited = [mock_msgs.create_victim_info() for i in range(2)]
        current_victims = [target2]
        model = mock_msgs.create_world_model(current_victims, visited)
        self.agent.target.set(target)

        with patch.object(self.agent.target, 'update') as mock_update:
            with patch.object(self.agent.dispatcher, 'emit') as emitter:
                self.agent.receive_world_model(model)

                self.assertFalse(mock_update.called)
                self.assertFalse(emitter.called)
                self.assertTrue(self.agent.target.is_empty)
                self.assertFalse(self.agent.target.is_verified())
                self.assertFalse(self.agent.target.is_identified())
    def test_verification_threshold(self):
        """ Tests that the target is verified. """

        conf.IDENTIFICATION_THRESHOLD = 0.6
        conf.VERIFICATION_THRESHOLD = 0.8

        target = mock_msgs.create_victim_info(probability=0.9)
        self.agent.target.set(target)

        visited = [mock_msgs.create_victim_info() for i in range(2)]
        current_victims = [target]
        model = mock_msgs.create_world_model(current_victims, visited)

        self.agent.receive_world_model(model)

        self.assertFalse(self.agent.target.is_empty)
        self.assertTrue(self.agent.target.is_identified())
        self.assertTrue(self.agent.target.is_verified())