Exemplo n.º 1
0
 def test_call_method(self):
     self.p.create_queue.return_value = queue.Queue()
     def f(request):
         request.respond_with_return(dict(
             greeting="Hello %s" % request.parameters.name))
     self.comms.q.put.side_effect = f
     ret = self.b.say_hello(name="me")
     self.assertEqual(ret.greeting, "Hello me")
Exemplo n.º 2
0
    def __init__(self, user_facing=False):
        self.user_facing = user_facing
        self.cothread = maybe_import_cothread()
        if self.cothread:
            self._event_queue = self.cothread.EventQueue()
            if user_facing:
                # Install a signal handler that will make sure we are the
                # thing that is interrupted
                def signal_exception(signum, frame):
                    self._event_queue.Signal(self.INTERRUPTED)

                signal.signal(signal.SIGINT, signal_exception)
        else:
            self._queue = queue.Queue()
Exemplo n.º 3
0
 def setUp(self):
     self.callback_result = 0
     self.callback_value = ''
     meta = VMeta("meta for unit tests")
     self.block = MagicMock()
     self.proc = MagicMock(q=queue.Queue())
     self.proc.create_queue = MagicMock(side_effect=queue.Queue)
     self.attr = Attribute(meta)
     self.attr.set_parent(self.block, "testAttr")
     self.attr2 = Attribute(meta)
     self.attr2.set_parent(self.block, "testAttr2")
     self.method = Method("method for unit tests")
     self.method.set_parent(self.block, "testFunc")
     self.method2 = Method("method for unit tests")
     self.method2.set_parent(self.block, "testFunc")
     self.bad_called_back = False
Exemplo n.º 4
0
 def setUp(self):
     self.callback_result = 0
     self.callback_value = ''
     meta = StringMeta("meta for unit tests")
     self.proc = MagicMock(q=queue.Queue())
     self.proc.create_queue = MagicMock(side_effect=queue.Queue)
     self.block = Block()
     self.block.set_process_path(self.proc, ("testBlock", ))
     self.attr = meta.make_attribute()
     self.attr2 = meta.make_attribute()
     self.method = MethodMeta("method for unit tests")
     self.method.returns.set_elements(ElementMap(dict(ret=StringMeta())))
     self.method2 = MethodMeta("method for unit tests")
     self.block.replace_endpoints(
         dict(testFunc=self.method,
              testFunc2=self.method2,
              testAttr=self.attr,
              testAttr2=self.attr2))
     self.bad_called_back = False
Exemplo n.º 5
0
def make_async_logging(log_config):
    # Now we have our user specified logging config, pipe all logging messages
    # through a queue to make it asynchronous
    from malcolm.compat import QueueListener, queue
    import logging.config

    # These are the handlers for our root logger, they should go through a queue
    root_handlers = log_config["root"].pop("handlers")

    # Create a new handler to replace all the above that just pops messages on
    # a queue, and set it as the handler for the root logger (and children)
    q = queue.Queue()
    log_config["handlers"]["queue"] = {
        "class": "malcolm.compat.QueueHandler", "queue": q}
    log_config["root"]["handlers"] = ["queue"]
    logging.config.dictConfig(log_config)

    # Now make a queue listener that consumes messages on the queue and forwards
    # them to any of the appropriate original root handlers
    handlers = [logging._handlers[h] for h in root_handlers]
    listener = QueueListener(q, *handlers, respect_handler_level=True)
    return listener
Exemplo n.º 6
0
def main():
    from malcolm.compat import queue
    from malcolm.profiler import Profiler

    args = parse_args()

    # Make some log config, either fron command line or
    log_config = make_logging_config(args)

    # Start it off, and tell it to stop when we quit
    listener = make_async_logging(log_config)
    listener.start()
    atexit.register(listener.stop)

    # Setup profiler dir
    profiler = Profiler()
    # profiler.start()

    # If using p4p then set cothread to use the right ca libs before it is
    try:
        import epicscorelibs.path.cothread
    except ImportError:
        pass

    # Import the Malcolm process
    q = queue.Queue()
    t = threading.Thread(target=try_prepare_locals, args=(q, args))
    t.start()
    process = q.get(timeout=65)
    if isinstance(process, Exception):
        # Startup failed, exit now
        sys.exit(1)

    # Now its safe to import Malcolm and cothread
    import cothread

    from malcolm.core import Context
    from malcolm.modules.builtin.blocks import proxy_block

    # Make a user context
    class UserContext(Context):
        def post(self, path, params=None, timeout=None, event_timeout=None):
            try:
                return super(UserContext, self).post(path, params, timeout,
                                                     event_timeout)
            except KeyboardInterrupt:
                self.post([path[0], "abort"])

        def _make_proxy(self, comms, mri):
            self._process.add_controller(proxy_block(comms=comms, mri=mri)[-1])

        def make_proxy(self, comms, mri):
            # Need to do this in cothread's thread
            cothread.CallbackResult(self._make_proxy, comms, mri)

        def block_view(self, mri):
            return cothread.CallbackResult(
                super(UserContext, self).block_view, mri)

        def make_view(self, controller, data, child_name):
            return cothread.CallbackResult(
                super(UserContext, self).make_view, controller, data,
                child_name)

        def handle_request(self, controller, request):
            cothread.CallbackResult(
                super(UserContext, self).handle_request, controller, request)

    self = UserContext(process)

    header = """Welcome to iMalcolm.

self.mri_list:
    %s

Try:
hello = self.block_view("HELLO")
hello.greet("me")

or

gui(self.block_view("COUNTER"))

or

self.make_proxy("localhost:8008", "HELLO")
self.block_view("HELLO").greet("me")
""" % (self.mri_list, )

    try:
        import IPython
    except ImportError:
        import code
        code.interact(header, local=locals())
    else:
        IPython.embed(header=header)
    if profiler.running and not profiler.stopping:
        profiler.stop()

    cothread.CallbackResult(process.stop)
    cothread.CallbackResult(cothread.Quit)
Exemplo n.º 7
0
 def setUp(self):
     self.proc = MagicMock(q=queue.Queue())
     self.proc.create_queue = MagicMock(return_value=queue.Queue())
     self.task = Task("testTask", self.proc)
Exemplo n.º 8
0
 def create_queue(self):
     """Creates a new Queue object"""
     return queue.Queue()