Exemplo n.º 1
0
    def test_counter_subscribe(self):
        sync_factory = SyncFactory("sched")
        process = Process("proc", sync_factory)
        b = Counter(process, dict(mri="counting"))[0]
        process.start()
        # wait until block is Ready
        task = Task("counter_ready_task", process)
        task.when_matches(b["state"], "Ready", timeout=1)
        q = sync_factory.create_queue()

        sub = Subscribe(response_queue=q, context="ClientConnection",
                        endpoint=["counting", "counter"],
                        delta=False)
        process.q.put(sub)
        resp = q.get(timeout=1)
        self.assertIsInstance(resp, Update)
        attr = NTScalar.from_dict(resp.value)
        self.assertEqual(0, attr.value)

        post = Post(response_queue=q, context="ClientConnection",
                    endpoint=["counting", "increment"])
        process.q.put(post)

        resp = q.get(timeout=1)
        self.assertIsInstance(resp, Update)
        self.assertEqual(resp.value["value"], 1)
        resp = q.get(timeout=1)
        self.assertIsInstance(resp, Return)

        process.stop()
Exemplo n.º 2
0
class TestBlock(unittest.TestCase):

    @patch("malcolm.core.syncfactory.ThreadPool")
    def setUp(self, mock_pool):
        self.s = SyncFactory("sched")
        mock_pool.assert_called_once_with()
        self.assertEqual(self.s.pool, mock_pool.return_value)

    @patch("malcolm.core.syncfactory.Queue")
    def test_queue_creation(self, mock_queue):
        q = self.s.create_queue()
        mock_queue.assert_called_once_with()
        self.assertEqual(q, mock_queue.return_value)

    @patch("malcolm.core.syncfactory.Lock")
    def test_lock_creation(self, mock_lock):
        l = self.s.create_lock()
        mock_lock.assert_called_once_with()
        self.assertEqual(l, mock_lock.return_value)

    def test_spawned_calls_pool_apply(self):
        r = self.s.spawn(callable, "fred", b=43)
        self.s.pool.apply_async.assert_called_once_with(
            callable, ("fred",), dict(b=43))
        self.assertEqual(r, self.s.pool.apply_async.return_value)
Exemplo n.º 3
0
class TestBlock(unittest.TestCase):
    @patch("malcolm.core.syncfactory.ThreadPool")
    def setUp(self, mock_pool):
        self.s = SyncFactory("sched")
        mock_pool.assert_called_once_with(128)
        self.assertEqual(self.s.pool, mock_pool.return_value)

    def tearDown(self):
        del self.s

    @patch("malcolm.core.syncfactory.InterruptableQueue")
    def test_queue_creation(self, mock_queue):
        q = self.s.create_queue()
        mock_queue.assert_called_once_with()
        self.assertEqual(q, mock_queue.return_value)

    @patch("malcolm.core.syncfactory.Lock")
    def test_lock_creation(self, mock_lock):
        l = self.s.create_lock()
        mock_lock.assert_called_once_with()
        self.assertEqual(l, mock_lock.return_value)

    def test_spawned_calls_pool_apply(self):
        r = self.s.spawn(callable, "fred", b=43)
        self.s.pool.apply_async.assert_called_once_with(
            callable, ("fred", ), dict(b=43))
        self.assertEqual(r, self.s.pool.apply_async.return_value)
Exemplo n.º 4
0
 def setUp(self):
     self.sync = SyncFactory("threads")
     self.process = Process("proc", self.sync)
     self.block = Block("block")
     self.host = socket.gethostname().split('.')[0]
     self.prefix = "%s-AD-SIM-01" % self.host
     pass
Exemplo n.º 5
0
    def setUp(self):
        s = SyncFactory("sync")
        self.p = Process("process", s)
        self.b = Block("blockname")
        self.comms = MagicMock()
        serialized = dict(say_hello=dict(
            description="Says hello",
            takes=dict(
                elements=dict(name=dict(
                    description="A name",
                    metaOf="malcolm:core/String:1.0",
                ), ),
                required=["name"],
            ),
            defaults={},
            returns=dict(
                elements=dict(greeting=dict(
                    description="A greeting",
                    metaOf="malcolm:core/String:1.0",
                ), ),
                required=["response"],
            ),
        ), )

        def f(request):
            request.respond_with_return(serialized)

        self.comms.q.put.side_effect = f
        self.cc = ClientController(self.p, self.b, self.comms)
Exemplo n.º 6
0
    def setUp(self):
        self.p = Process('process1', SyncFactory('threading'))

        # create a child ManagerController block
        params = ManagerController.MethodMeta. \
            prepare_input_map(mri='childBlock', configDir="/tmp")
        self.c_child = ManagerController(self.p, [], params)
        self.b_child = self.c_child.block

        self.sm = self.c_child.stateMachine

        params = Part.MethodMeta.prepare_input_map(name='part1')
        part1 = Part(self.p, params)
        params = {'name': 'part2', 'mri': 'childBlock'}
        params = ChildPart.MethodMeta.prepare_input_map(**params)
        part2 = ChildPart(self.p, params)

        # create a root block for the ManagerController block to reside in
        parts = [part1, part2]
        params = ManagerController.MethodMeta.prepare_input_map(
            mri='mainBlock', configDir="/tmp")
        self.c = ManagerController(self.p, parts, params)
        self.b = self.c.block

        # check that do_initial_reset works asynchronously
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.b["state"], self.sm.READY, timeout=1)

        self.checkState(self.sm.READY)
Exemplo n.º 7
0
    def setUp(self):
        self.maxDiff = 5000

        self.p = Process('process1', SyncFactory('threading'))

        # Make a ticker block to act as our child
        params = Ticker.MethodMeta.prepare_input_map(mri="childBlock",
                                                     configDir="/tmp")
        self.b_child = Ticker(self.p, params)[-1]

        # Make an empty part for our parent
        params = Part.MethodMeta.prepare_input_map(name='part1')
        part1 = Part(self.p, params)

        # Make a RunnableChildPart to control the ticker
        params = RunnableChildPart.MethodMeta.prepare_input_map(
            mri='childBlock', name='part2')
        part2 = RunnableChildPart(self.p, params)

        # create a root block for the RunnableController block to reside in
        params = RunnableController.MethodMeta.prepare_input_map(
            mri='mainBlock', configDir="/tmp")
        self.c = RunnableController(self.p, [part1, part2], params)
        self.b = self.c.block
        self.sm = self.c.stateMachine

        # start the process off
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.b["state"], self.sm.IDLE, timeout=1)

        self.checkState(self.sm.IDLE)
Exemplo n.º 8
0
    def setUp(self):
        self.p = Process('process1', SyncFactory('threading'))

        self.p1, self.c1 = self.makeChildBlock('child1')
        self.p2, self.c2 = self.makeChildBlock('child2')
        self.p3, self.c3 = self.makeChildBlock('child3')

        # create a root block for the child blocks to reside in
        parts = [self.p1, self.p2, self.p3]
        params = RunnableController.MethodMeta.prepare_input_map(
            mri='mainBlock', configDir="/tmp")
        self.c = RunnableController(self.p, parts, params)
        self.b = self.c.block

        params = ChildPart.MethodMeta.prepare_input_map(mri='mainBlock',
                                                        name='mainPart')
        self.part = ChildPart(self.p, params)

        # Get the parent block into idle state
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.c.block["state"], sm.IDLE, timeout=1)

        self.checkState(sm.IDLE)
Exemplo n.º 9
0
 def setUp(self):
     self.sf = SyncFactory("sync")
     self.process = Process("proc", self.sf)
     block = Block()
     HelloController(self.process, block, 'hello')
     self.sc = WSServerComms("sc", self.process, self.socket)
     self.process.start()
     self.sc.start()
Exemplo n.º 10
0
 def setUp(self):
     sync_factory = SyncFactory("sync")
     self.process = Process("proc", sync_factory)
     block = Block("hello")
     self.process.add_block(block)
     HelloController(block)
     self.sc = WSServerComms("sc", self.process, 8888)
     self.process.start()
     self.sc.start()
Exemplo n.º 11
0
 def test_hello_controller_with_process(self):
     sync_factory = SyncFactory("sched")
     process = Process("proc", sync_factory)
     block = Block("hello")
     HelloController(block)
     process.add_block(block)
     process.start()
     q = sync_factory.create_queue()
     req = Request.Post(response_queue=q, context="ClientConnection",
                        endpoint=["hello", "say_hello"],
                        parameters=dict(name="thing"))
     req.set_id(44)
     process.q.put(req)
     resp = q.get(timeout=1)
     self.assertEqual(resp.id_, 44)
     self.assertEqual(resp.context, "ClientConnection")
     self.assertEqual(resp.type_, "Return")
     self.assertEqual(resp.value, dict(greeting="Hello thing"))
Exemplo n.º 12
0
 def test_add_block_calls_handle(self):
     s = SyncFactory("sched")
     p = Process("proc", s)
     b = Block()
     b.set_parent(p, "myblock")
     p.add_block(b)
     p.start()
     p.stop()
     self.assertEqual(len(p._blocks), 2)
     self.assertEqual(p._blocks, dict(myblock=b, proc=p.process_block))
Exemplo n.º 13
0
 def test_add_block_calls_handle(self):
     s = SyncFactory("sched")
     p = Process("proc", s)
     b = MagicMock()
     b.name = "myblock"
     p.add_block("myblock", b)
     p.start()
     p.stop()
     b.set_parent.assert_called_once_with(p, "myblock")
     self.assertEqual(len(p._blocks), 2)
     self.assertEqual(p._blocks, dict(myblock=b, proc=p.process_block))
Exemplo n.º 14
0
 def test_error(self):
     s = SyncFactory("sched")
     p = Process("proc", s)
     p.log_exception = MagicMock()
     p.start()
     request = MagicMock()
     request.endpoint = ["anything"]
     p.q.put(request)
     p.stop()
     p.log_exception.assert_called_once_with("Exception while handling %s",
                                             request)
Exemplo n.º 15
0
 def test_hello_with_process(self):
     sync_factory = SyncFactory("sched")
     process = Process("proc", sync_factory)
     b = Hello(process, dict(mri="hello"))[0]
     process.start()
     # wait until block is Ready
     task = Task("hello_ready_task", process)
     task.when_matches(b["state"], "Ready", timeout=1)
     q = sync_factory.create_queue()
     req = Post(response_queue=q, context="ClientConnection",
                endpoint=["hello", "greet"],
                parameters=dict(name="thing"))
     req.set_id(44)
     process.q.put(req)
     resp = q.get(timeout=1)
     self.assertEqual(resp.id, 44)
     self.assertEqual(resp.context, "ClientConnection")
     self.assertEqual(resp.typeid, "malcolm:core/Return:1.0")
     self.assertEqual(resp.value["greeting"], "Hello thing")
     process.stop()
Exemplo n.º 16
0
 def test_starting_process(self):
     s = SyncFactory("sched")
     p = Process("proc", s)
     b = MagicMock()
     p._handle_block_add(BlockAdd(b, "myblock", None))
     self.assertEqual(p._blocks, dict(myblock=b, proc=ANY))
     p.start()
     request = Post(MagicMock(), MagicMock(), ["myblock", "foo"])
     p.q.put(request)
     # wait for spawns to have done their job
     p.stop()
     b.handle_request.assert_called_once_with(request)
Exemplo n.º 17
0
    def test_spawned_adds_to_other_spawned(self):
        s = SyncFactory("sched")
        p = Process("proc", s)

        def sleep(n, a=None):
            time.sleep(a)

        f = MagicMock(side_effect=sleep)

        spawned = p.spawn(f, "fred", a=0.05)
        p.start()
        p.stop()
        self.assertEqual(p._other_spawned, [(spawned, f)])
        f.assert_called_once_with("fred", a=0.05)
Exemplo n.º 18
0
 def test_start_stop(self):
     self.process.sync_factory = SyncFactory("s")
     self.process.spawn = self.process.sync_factory.spawn
     self.process.create_queue = self.process.sync_factory.create_queue
     server = ServerComms("server", self.process)
     server.send_loop = Mock(side_effect = server.send_loop)
     server.start_recv_loop = Mock()
     server.stop_recv_loop = Mock()
     server.start()
     self.assertFalse(server._send_spawned.ready())
     server.start_recv_loop.assert_called_once_with()
     server.stop(0.1)
     self.assertTrue(server._send_spawned.ready())
     server.send_loop.assert_called_once_with()
     server.stop_recv_loop.assert_called_once_with()
Exemplo n.º 19
0
 def test_start_stop(self):
     sync_factory = SyncFactory("s")
     process = Mock()
     process.spawn = sync_factory.spawn
     process.create_queue = sync_factory.create_queue
     client = ClientComms("c", process)
     client.send_loop = Mock(side_effect = client.send_loop)
     client.start_recv_loop = Mock()
     client.stop_recv_loop = Mock()
     client.log_exception = Mock()
     client.start()
     self.assertFalse(client._send_spawned.ready())
     client.start_recv_loop.assert_called_once_with()
     client.stop(0.1)
     self.assertTrue(client._send_spawned.ready())
     client.send_loop.assert_called_once_with()
     client.stop_recv_loop.assert_called_once_with()
     client.log_exception.assert_not_called()
Exemplo n.º 20
0
 def setUp(self, mock_pool):
     self.s = SyncFactory("sched")
     mock_pool.assert_called_once_with()
     self.assertEqual(self.s.pool, mock_pool.return_value)
Exemplo n.º 21
0
 def setUp(self, mock_pool):
     self.s = SyncFactory("sched")
     mock_pool.assert_called_once_with()
     self.assertEqual(self.s.pool, mock_pool.return_value)