def setUp(self):
     self.factory = Factory()
     self.storage = InMemoryDataStorage(None)
     self.storage._log = MagicMock(Log)
     self.simulation = self.factory.create_simulation(self.storage)
     service = MagicMock()
     service.name = self.CALLER
     self.simulation.environment.define(Symbols.SELF, service)
     self.simulation.environment.define(Symbols.LISTENER, MagicMock(Dispatcher))
     self.logger = Logger(self.simulation.environment)
class MonitorTests(TestCase):

    def setUp(self):
        self.factory = Factory()
        self.storage = InMemoryDataStorage(None)
        self.simulation = self.factory.create_simulation(self.storage)

    def test_throughput_calculation(self):
        period = 10
        self.monitor = self._create_monitor(period)

        (success, rejection, error) = (10, 3, 4)
        self._run_scenario(success, rejection, error)

        expected = (success) / period
        self.assertEqual(expected, self.monitor._throughput())

    def _run_scenario(self, total, rejected, errors):
        for i in range(total):
            self.monitor.statistics.success_replied_to(_a_fake_request())
        for i in range(rejected):
            self.monitor.statistics.rejection_of(_a_fake_request())
        for i in range(errors):
            self.monitor.statistics.error_replied_to(_a_fake_request())

    def test_runs_with_the_proper_period(self):
        with patch.object(Monitor, 'monitor') as trigger:
            period = 50
            self._create_monitor(period)

            test_duration = 200
            self.simulation.run_until(test_duration)

            expected_call_count = int(test_duration / period)
            self.assertEqual(expected_call_count, trigger.call_count)

    def test_setting_probes(self):
        fake_report = MagicMock()
        self.storage.report_for = MagicMock(return_value=fake_report)
        monitor = self._create_monitor()
        monitor.set_probes([Probe("time", 5, "{:d}", lambda self: 10),
                            Probe("weather", 10, "{:s}", lambda self: "cloudy")])

        monitor.monitor()

        fake_report.assert_called_once_with(time="   10", weather="    cloudy")

    def _create_monitor(self, period=50):
        environment = self.simulation.environment.create_local_environment()
        environment.define(Symbols.LISTENER, Dispatcher())
        self._create_fake_service(environment)
        monitor = Monitor(Symbols.MONITOR, environment, period)
        self.simulation.environment.define(Symbols.MONITOR, monitor)
        return monitor

    def _create_fake_service(self, environment):
        fake_service = MagicMock()
        fake_service.name = "Bidon"
        environment.define(Symbols.SERVICE, fake_service)
Beispiel #3
0
    def test_evaluation_of_lifo(self):
        environment = Environment()
        queue = LIFO()

        Evaluation(environment, queue, Factory()).result

        queue = environment.look_up(Symbols.QUEUE).delegate
        self.assertIsInstance(queue, LIFOTaskPool)
class LoggerTest(TestCase):
    CALLER = "Client"
    CALLEE = "DB"
    OPERATION = "Select"
    REQUEST_ID = 123

    def setUp(self):
        self.factory = Factory()
        self.storage = InMemoryDataStorage(None)
        self.storage._log = MagicMock(Log)
        self.simulation = self.factory.create_simulation(self.storage)
        service = MagicMock()
        service.name = self.CALLER
        self.simulation.environment.define(Symbols.SELF, service)
        self.simulation.environment.define(Symbols.LISTENER, MagicMock(Dispatcher))
        self.logger = Logger(self.simulation.environment)

    def test_logging_request_arrival(self):
        self.logger.arrival_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_ARRIVAL % self.REQUEST_ID)

    def test_logging_request_stored(self):
        self.logger.storage_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_STORED % self.REQUEST_ID)

    def test_logging_failure_of(self):
        self.logger.failure_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_FAILURE % self.REQUEST_ID)

    def test_logging_success_of(self):
        self.logger.success_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_SUCCESS % self.REQUEST_ID)

    def test_logging_posting_of(self):
        self.logger.posting_of(self.CALLEE, self._fake_request())
        self.verify_log_call(Logger.REQUEST_SENT % (self.REQUEST_ID, self.CALLEE, self.OPERATION))

    def test_logging_timeout_of(self):
        self.logger.timeout_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_TIMEOUT % self.REQUEST_ID)

    def test_logging_error_replied(self):
        self.logger.error_replied_to(self._fake_request())
        self.verify_log_call(Logger.ERROR_REPLIED % self.REQUEST_ID)

    def test_logging_success_replied(self):
        self.logger.success_replied_to(self._fake_request())
        self.verify_log_call(Logger.SUCCESS_REPLIED % self.REQUEST_ID)

    def verify_log_call(self, message):
        self.simulation.log.record.assert_called_once_with(0, self.CALLER, message)

    def _fake_request(self):
        request = MagicMock(Request)
        request.identifier = self.REQUEST_ID
        request.operation = self.OPERATION
        return request
Beispiel #5
0
 def setUp(self):
     self.factory = Factory()
     self.storage = InMemoryDataStorage(None)
     self.storage._log = MagicMock(Log)
     self.simulation = self.factory.create_simulation(self.storage)
     service = MagicMock()
     service.name = self.CALLER
     self.simulation.environment.define(Symbols.SELF, service)
     self.simulation.environment.define(Symbols.LISTENER, MagicMock(Dispatcher))
     self.logger = Logger(self.simulation.environment)
Beispiel #6
0
    def test_evaluation_of_sequence_stops_at_the_first_failure(self):
        environment = Environment()

        succeed = MagicMock(Expression)
        succeed.accept = MagicMock(return_value=Success())

        def check_result(status):
            self.assertTrue(status.is_erroneous)

        Evaluation(environment,
                   Sequence(Fail(), succeed),
                   Factory(),
                   continuation=check_result).result
        succeed.accept.assert_not_called()
 def setUp(self):
     self.factory = Factory()
     self.storage = InMemoryDataStorage(None)
     self.simulation = self.factory.create_simulation(self.storage)
Beispiel #8
0
class LoggerTest(TestCase):
    CALLER = "Client"
    CALLEE = "DB"
    OPERATION = "Select"
    REQUEST_ID = 123

    def setUp(self):
        self.factory = Factory()
        self.storage = InMemoryDataStorage(None)
        self.storage._log = MagicMock(Log)
        self.simulation = self.factory.create_simulation(self.storage)
        service = MagicMock()
        service.name = self.CALLER
        self.simulation.environment.define(Symbols.SELF, service)
        self.simulation.environment.define(Symbols.LISTENER, MagicMock(Dispatcher))
        self.logger = Logger(self.simulation.environment)

    def test_logging_request_arrival(self):
        self.logger.task_created(self._fake_request())
        self.verify_log_call(Logger.REQUEST_RECEIVED.format(request=self.REQUEST_ID))

    def test_logging_request_stored(self):
        self.logger.task_activated(self._fake_request())
        self.verify_log_call(Logger.TASK_ACTIVATED.format(task=self.REQUEST_ID))

    def test_logging_failure_of(self):
        self.logger.failure_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_FAILURE.format(request=self.REQUEST_ID))

    def test_logging_success_of(self):
        self.logger.success_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_SUCCESS.format(request=self.REQUEST_ID))

    def test_logging_posting_of(self):
        self.logger.posting_of(self.CALLEE, self._fake_request())
        self.verify_log_call(Logger.REQUEST_SENT.format(request=self.REQUEST_ID, service=self.CALLEE, operation=self.OPERATION))

    def test_logging_timeout_of(self):
        self.logger.timeout_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_TIMEOUT.format(request=self.REQUEST_ID))

    def test_logging_error_replied(self):
        self.logger.task_failed(self._fake_request())
        self.verify_log_call(Logger.ERROR_REPLIED.format(request=self.REQUEST_ID))

    def test_logging_success_replied(self):
        self.logger.task_successful(self._fake_request())
        self.verify_log_call(Logger.SUCCESS_REPLIED.format(request=self.REQUEST_ID))

    def verify_logging_acceptance(self):
        self.logger.acceptance_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_ACCEPTED.format(request=self.REQUEST_ID))

    def verify_logging_acceptance(self):
        self.logger.rejection_of(self._fake_request())
        self.verify_log_call(Logger.REQUEST_REJECTED.format(request=self.REQUEST_ID))

    def verify_log_call(self, message):
        self.simulation.log.record.assert_called_once_with(0, self.CALLER, message)

    def _fake_request(self):
        request = MagicMock(Request)
        request.identifier = self.REQUEST_ID
        request.operation = self.OPERATION
        return request
Beispiel #9
0
 def setUp(self):
     self.factory = Factory()
     self.storage = InMemoryDataStorage(None)
     self.simulation = self.factory.create_simulation(self.storage)
Beispiel #10
0
class MonitorTests(TestCase):

    def setUp(self):
        self.factory = Factory()
        self.storage = InMemoryDataStorage(None)
        self.simulation = self.factory.create_simulation(self.storage)

    def test_cumulaive_success_calculation(self):
        period = 10
        self.monitor = self._create_monitor(period)

        (success, rejection, error) = (10, 3, 4)
        self._run_scenario(success, rejection, error)

        expected = success
        self.assertEqual(expected, self.monitor._throughput())

    def test_reliability_with_only_errors(self):
        period = 10
        self.monitor = self._create_monitor(period)

        (success, rejection, error) = (0, 0, 5)
        self._run_scenario(success, rejection, error)

        expected = 0.
        self.assertEqual(expected, self.monitor._throughput())

    def _run_scenario(self, total, rejected, errors):
        for i in range(total):
            self.monitor.statistics.task_successful(a_request())
        for i in range(rejected):
            self.monitor.statistics.task_rejected(a_request())
        for i in range(errors):
            self.monitor.statistics.task_failed(a_request())

    def test_runs_with_the_proper_period(self):
        with patch.object(Monitor, 'monitor') as trigger:
            period = 50
            self._create_monitor(period)

            test_duration = 200
            self.simulation.run_until(test_duration)

            expected_call_count = int(test_duration / period)
            self.assertEqual(expected_call_count, trigger.call_count)

    def test_setting_probes(self):
        fake_report = MagicMock()
        self.storage.report_for = MagicMock(return_value=fake_report)
        monitor = self._create_monitor()
        monitor.set_probes([Probe("time", 5, "{:d}", lambda self: 10),
                            Probe("weather", 10, "{:s}", lambda self: "cloudy")])

        monitor.monitor()

        fake_report.assert_called_once_with(time="   10", weather="    cloudy")

    def _create_monitor(self, period=50):
        environment = self.simulation.environment.create_local_environment()
        environment.define(Symbols.LISTENER, Dispatcher())
        self._create_fake_service(environment)
        monitor = Monitor(Symbols.MONITOR, environment, period)
        self.simulation.environment.define(Symbols.MONITOR, monitor)
        return monitor

    def _create_fake_service(self, environment):
        fake_service = MagicMock()
        fake_service.name = "Bidon"
        environment.define(Symbols.SERVICE, fake_service)
Beispiel #11
0
    def test_evaluation_of_fail(self):
        environment = Environment()

        result = Evaluation(environment, Fail(), Factory()).result

        self.assertTrue(result.is_erroneous)