Skip to content

Commit

Permalink
Update controllers to work with Map based function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Jun 29, 2016
1 parent 1a7bd74 commit d7d5d54
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
11 changes: 7 additions & 4 deletions malcolm/controllers/clientcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ def wrap_method(self, method_name, method_map):
self.log_debug("Wrapping method %s", method_name)
return method

def call_server_method(self, method_name, args):
def call_server_method(self, method_name, parameters, returns):
"""Call method_name on the server
Args:
method_name (str): Name of the method
args (dict): Map of arguments to be called with
parameters (Map): Map of arguments to be called with
returns (Map): Returns map to fill and return
"""
self.log_debug(dict(parameters))
request = Request.Post(None, self.q,
[self.block.name, method_name], args)
[self.block.name, method_name], parameters)
self.client_comms.q.put(request)
response = self.q.get()
assert response.type_ == response.RETURN, \
"Expected Return, got %s" % response.type_
return response.value
returns.update(response.value)
return returns
1 change: 1 addition & 0 deletions malcolm/controllers/countercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np


class CounterController(Controller):

def create_attributes(self):
Expand Down
13 changes: 8 additions & 5 deletions malcolm/controllers/hellocontroller.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from malcolm.core.controller import Controller
from malcolm.core.method import Method, takes, returns
from malcolm.core.method import takes, returns
from malcolm.core.mapmeta import REQUIRED
from malcolm.core.stringmeta import StringMeta


class HelloController(Controller):
@takes(StringMeta("name", "a name"), REQUIRED)
@returns(StringMeta("greeting", "a greeting"), REQUIRED)
def say_hello(self, args):
def say_hello(self, parameters, return_map):
"""Says Hello to name
Args:
name (str): The name of the person to say hello to
parameters(Map): The name of the person to say hello to
return_map(Map): Return structure to complete and return
Returns:
str: The greeting
Map: The greeting
"""
return dict(greeting="Hello %s" % args["name"])

return_map.greeting = "Hello %s" % parameters.name
return return_map
4 changes: 2 additions & 2 deletions tests/test_controllers/test_clientcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def test_methods_created(self):
def test_call_method(self):
def f(request):
request.respond_with_return(dict(
greeting="Hello %s" % request.parameters["name"]))
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")
self.assertEqual(ret.greeting, "Hello me")

if __name__ == "__main__":
unittest.main(verbosity=2)
22 changes: 14 additions & 8 deletions tests/test_controllers/test_hellocontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@


class TestHelloController(unittest.TestCase):

def setUp(self):
self.block = Mock()
self.c = HelloController(self.block)

def test_init(self):
block = Mock()
c = HelloController(block)
self.assertIs(block, c.block)
self.assertEquals(c.say_hello.Method, block.add_method.call_args[0][0])
self.assertIs(self.block, self.c.block)
self.assertEquals(self.c.say_hello.Method, self.block.add_method.call_args[0][0])

def test_say_hello(self):
c = HelloController(Mock())
args = {"name":"test_name"}
expected = {"greeting":"Hello test_name"}
self.assertEquals(expected, c.say_hello(args))
expected = "Hello test_name"

parameters_mock = Mock()
parameters_mock.name = "test_name"
returns_mock = Mock()
response = self.c.say_hello(parameters_mock, returns_mock)
self.assertEquals(expected, response.greeting)

if __name__ == "__main__":
unittest.main(verbosity=2)
5 changes: 3 additions & 2 deletions tests/test_core/test_system_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_hello_controller_good_input(self):
block = Block("hello")
HelloController(block)
result = block.say_hello(name="me")
self.assertEquals(result["greeting"], "Hello me")
self.assertEquals(result.greeting, "Hello me")

def test_hello_controller_with_process(self):
sync_factory = SyncFactory("sched")
Expand All @@ -47,6 +47,7 @@ def test_hello_controller_with_process(self):
self.assertEqual(resp.type_, "Return")
self.assertEqual(resp.value, dict(greeting="Hello thing"))


class TestCounterControllerSystem(unittest.TestCase):

def test_counter_controller_subscribe(self):
Expand Down Expand Up @@ -76,7 +77,7 @@ def test_counter_controller_subscribe(self):
resp = q.get(timeout=1)
self.assertEqual(Response.RETURN, resp.type_)


process.stop()

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit d7d5d54

Please sign in to comment.