Beispiel #1
0
    def test_execute_action(self):
        from coopy.foundation import Action, RecordClock
        from datetime import datetime
        from coopy.utils import inject

        class Dummy(object):
            def __init__(self):
                self.exec_count = 0

            def business_method_noargs(self):
                self.exec_count += 1

            def business_method_args(self, arg):
                self.exec_count += 2

            def business_method_kwargs(self, keyword_arg="test"):
                self.exec_count += 3


        dummy = Dummy()
        # force clock into dummy
        inject(dummy, '_clock', RecordClock())

        action = Action('caller_id',
                        'business_method_noargs',
                        datetime.now(),
                        (),
                        {})

        action.execute_action(dummy)

        self.assertEquals(1, dummy.exec_count)

        action = Action('caller_id',
                        'business_method_args',
                        datetime.now(),
                        ([1]),
                        {})

        action.execute_action(dummy)

        self.assertEquals(3, dummy.exec_count)

        action = Action('caller_id',
                        'business_method_kwargs',
                        datetime.now(),
                        (),
                        {'keyword_arg' : 'test'})

        action.execute_action(dummy)

        self.assertEquals(6, dummy.exec_count)
Beispiel #2
0
        def method(*args, **kwargs):
            exception = None
            try:
                if not unlocked:
                    self.lock.acquire(1)

                #record all calls to clock.now()
                self.obj._clock = RecordClock()

                thread_ident = thread.get_ident()
                action = Action(thread_ident,
                                name,
                                datetime.now(),
                                args,
                                kwargs)
                system = None
                if not readonly:
                    self.publisher.publish_before(action)

                try:
                    system = action.execute_action(self.obj)
                except Exception as e:
                    logger.debug(CORE_LOG_PREFIX + 'Error: ' + str(e))
                    if abort_exception:
                        logger.debug(CORE_LOG_PREFIX +
                                'Aborting action' + str(action))
                    if not abort_exception:
                        self.publisher.publish_exception(action)
                    exception = e

                #restore clock
                action.results = self.obj._clock.results

                if not readonly and not abort_exception:
                    self.publisher.publish(action)

            finally:
                if not unlocked:
                    self.lock.release()

            if exception:
                raise exception

            return system