class TestSystemWSCommsServerAndClient(TestSystemWSCommsServerOnly):
    socket = 8882

    def setUp(self):
        super(TestSystemWSCommsServerAndClient, self).setUp()
        self.process2 = Process("proc2", self.sf)
        self.block2 = Block()
        ClientController(self.process2, self.block2, 'hello')
        self.cc = WSClientComms("cc", self.process2,
                                "ws://localhost:%s/ws" % self.socket)
        self.process2.start()
        self.cc.start()

    def tearDown(self):
        super(TestSystemWSCommsServerAndClient, self).tearDown()
        self.cc.stop()
        self.cc.wait()
        self.process2.stop()

    def test_server_with_malcolm_client(self):
        # Normally we would wait for it to be connected here, but it isn't
        # attached to a process so just sleep for a bit
        time.sleep(0.5)
        ret = self.block2.say_hello("me2")
        self.assertEqual(ret, dict(greeting="Hello me2"))
class TestSystemWSCommsServerAndClient(TestSystemWSCommsServerOnly):
    socket = 8882

    def setUp(self):
        super(TestSystemWSCommsServerAndClient, self).setUp()
        self.process2 = Process("proc2", self.sf)
        self.block2 = Block()
        ClientController(self.process2, self.block2, 'hello')
        self.cc = WSClientComms("cc", self.process2, "ws://localhost:%s/ws" %
                                self.socket)
        self.process2.start()
        self.cc.start()

    def tearDown(self):
        super(TestSystemWSCommsServerAndClient, self).tearDown()
        self.cc.stop()
        self.cc.wait()
        self.process2.stop()

    def test_server_with_malcolm_client(self):
        # Normally we would wait for it to be connected here, but it isn't
        # attached to a process so just sleep for a bit
        time.sleep(0.5)
        ret = self.block2.say_hello("me2")
        self.assertEqual(ret, dict(greeting="Hello me2"))
class TestClientController(unittest.TestCase):

    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)

    def test_methods_created(self):
        self.assertEqual(self.b._methods.keys(), ["say_hello"])
        m = self.b._methods["say_hello"]
        self.assertEqual(m.name, "say_hello")
        self.assertEqual(m.takes.elements.keys(), ["name"])
        self.assertEqual(type(m.takes.elements["name"]), StringMeta)
        self.assertEqual(m.returns.elements.keys(), ["greeting"])
        self.assertEqual(type(m.returns.elements["greeting"]), StringMeta)
        self.assertEqual(m.defaults, {})

    def test_call_method(self):
        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")
Beispiel #4
0
 def test_server_with_malcolm_client(self):
     self.cc = WSClientComms("cc", self.process, "ws://localhost:8888/ws")
     self.cc.start()
     # Wait for comms to be connected
     while not self.cc.conn.done():
         time.sleep(0.001)
     # Don't add to process as we already have a block of that name
     block2 = Block("hello")
     ClientController(self.process, block2, self.cc)
     ret = block2.say_hello("me2")
     self.assertEqual(ret, dict(greeting="Hello me2"))
     self.cc.stop()
 def test_server_with_malcolm_client(self):
     self.cc = WSClientComms("cc", self.process, "ws://localhost:8888/ws")
     self.cc.start()
     # Wait for comms to be connected
     while not self.cc.conn.done():
         time.sleep(0.001)
     # Don't add to process as we already have a block of that name
     block2 = Block("hello")
     ClientController(self.process, block2, self.cc)
     ret = block2.say_hello("me2")
     self.assertEqual(ret, dict(greeting="Hello me2"))
     self.cc.stop()
class TestClientController(unittest.TestCase):
    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)

    def test_methods_created(self):
        self.assertEqual(self.b._methods.keys(), ["say_hello"])
        m = self.b._methods["say_hello"]
        self.assertEqual(m.name, "say_hello")
        self.assertEqual(m.takes.elements.keys(), ["name"])
        self.assertEqual(type(m.takes.elements["name"]), StringMeta)
        self.assertEqual(m.returns.elements.keys(), ["greeting"])
        self.assertEqual(type(m.returns.elements["greeting"]), StringMeta)
        self.assertEqual(m.defaults, {})

    def test_call_method(self):
        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")
Beispiel #7
0
class TestClientController(unittest.TestCase):

    def setUp(self):
        # Serialized version of the block we want
        source = Block()
        HelloController(MagicMock(), source, "blockname")
        self.serialized = source.to_dict()
        # Setup client controller prerequisites
        self.b = Block()
        self.b.name = "blockname"
        self.p = MagicMock()
        self.comms = MagicMock()
        self.cc = ClientController(self.p, self.b, "blockname")
        # get process to give us comms
        self.p.get_client_comms.return_value = self.comms
        # tell our controller which blocks the process can talk to
        response = MagicMock(id_=self.cc.REMOTE_BLOCKS_ID, value=["blockname"])
        self.cc.put(response)
        # tell our controller the serialized state of the block
        response = MagicMock(id_=self.cc.BLOCK_ID, changes=[[[], self.serialized]])
        self.cc.put(response)

    def test_init(self):
        self.assertEqual(self.p.q.put.call_count, 1)
        req = self.p.q.put.call_args[0][0]
        self.assertEqual(req.typeid, "malcolm:core/Subscribe:1.0")
        self.assertEqual(req.endpoint, [self.p.name, "remoteBlocks", "value"])
        self.assertEqual(req.response_queue, self.cc)
        self.p.get_client_comms.assert_called_with("blockname")
        self.assertEqual(self.comms.q.put.call_count, 1)
        req = self.comms.q.put.call_args[0][0]
        self.assertEqual(req.typeid, "malcolm:core/Subscribe:1.0")
        self.assertEqual(req.delta, True)
        self.assertEqual(req.response_queue, self.cc)
        self.assertEqual(req.endpoint, ["blockname"])

    def test_methods_created(self):
        self.assertEqual(list(self.b.methods), ["disable", "reset", "say_hello"])
        m = self.b.methods["say_hello"]
        self.assertEqual(m.name, "say_hello")
        self.assertEqual(list(m.takes.elements), ["name"])
        self.assertEqual(type(m.takes.elements["name"]), StringMeta)
        self.assertEqual(list(m.returns.elements), ["greeting"])
        self.assertEqual(type(m.returns.elements["greeting"]), StringMeta)
        self.assertEqual(m.defaults, {})

    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")

    def test_put_update_response(self):
        response = MagicMock(
            id_=self.cc.BLOCK_ID,
            changes=[[["substructure"], "change"]])
        self.b.update = MagicMock()
        self.cc.put(response)
        self.b.update.assert_called_once_with([["substructure"], "change"])

    def test_put_root_update_response(self):
        attr1 = StringMeta("dummy")
        attr2 = StringMeta("dummy2")
        new_block_structure = {}
        new_block_structure["attr1"] = attr1.to_dict()
        new_block_structure["attr2"] = attr2.to_dict()
        self.b.replace_children = MagicMock()
        response = MagicMock(
            id_=self.cc.BLOCK_ID,
            changes=[[[], new_block_structure]])
        self.cc.put(response)
        self.assertIs(self.b, self.cc.block)
        deserialized_changes = self.b.replace_children.call_args_list[0][0][0]
        serialized_changes = [x.to_dict() for x in
                              deserialized_changes.values()]
        expected = [attr1.to_dict(), attr2.to_dict()]
        # dicts are not hashable, so cannot use set compare
        for x in expected:
            self.assertTrue(x in serialized_changes)
        for x in serialized_changes:
            self.assertTrue(x in expected)
 def test_hello_controller_good_input(self):
     block = Block("hello")
     HelloController(block)
     result = block.say_hello(name="me")
     self.assertEquals(result["greeting"], "Hello me")