def setUp(self): basic = Bool() default = Bool(default=True) optional = Bool(optional=True) default_optional = Bool(default=True, optional=True) self._pack = [ (basic, True, "1"), (basic, False, "0"), (basic, 1, "1"), (basic, 0, "0"), (basic, "a", "1"), (basic, None, ValueError), (default, None, "1"), (default_optional, None, "1"), (optional, None, ValueError), ] self._unpack = [ (basic, "1", True), (basic, "0", False), (basic, "a", ValueError), (basic, None, ValueError), (default, None, True), (default_optional, None, True), (optional, None, None), ]
class PafBackendController(AsyncDeviceServer): VERSION_INFO = ("paf-backend-controller-api", 0, 1) BUILD_INFO = ("paf-backend-controller-implementation", 0, 1, "rc1") DEVICE_STATUSES = ["ok", "degraded", "fail"] def __init__(self, ip, port): super(PafBackendController, self).__init__(ip, port) def setup_sensors(self): self._device_status = Sensor.discrete( "device-status", description="Health status of PafBackendController", params=self.DEVICE_STATUSES, default="ok", initial_status=Sensor.UNKNOWN) self.add_sensor(self._device_status) def start(self): super(PafBackendController, self).start() @request(Int(), Float(), Bool()) @return_reply(Int(), Float()) def request_myreq(self, req, my_int, my_float, my_bool): '''?myreq my_int my_float my_bool''' return ("ok", my_int + 1, my_float / 2.0) @request(Str()) @return_reply(Str()) def request_echo(self, req, message): """ @brief A request that echos a message """ return ("ok", message) @request(Str()) @return_reply(Str()) def request_echomine(self, req, message): """ @brief A request that echos a message """ return ("ok", message)
class TestDevice(object): def __init__(self): self.sent_messages = [] @request(Int(min=1, max=10), Discrete(("on", "off")), Bool()) @return_reply(Int(min=1, max=10), Discrete(("on", "off")), Bool()) def request_one(self, sock, i, d, b): if i == 3: return ("fail", "I failed!") if i == 5: return ("bananas", "This should never be sent") if i == 6: return ("ok", i, d, b, "extra parameter") if i == 9: # This actually gets put in the callback params automatically orig_msg = Message.request("one", "foo", "bar") self.finish_request_one(orig_msg, sock, i, d, b) raise AsyncReply() return ("ok", i, d, b) @send_reply(Int(min=1, max=10), Discrete(("on", "off")), Bool()) def finish_request_one(self, msg, sock, i, d, b): return (sock, msg, "ok", i, d, b) def reply(self, sock, msg, orig_msg): self.sent_messages.append([sock, msg]) @request(Int(min=1, max=3, default=2), Discrete(("on", "off"), default="off"), Bool(default=True)) @return_reply(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def request_two(self, sock, i, d, b): return ("ok", i, d, b) @return_reply(Int(min=1, max=3), Discrete(("on", "off")), Bool()) @request(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def request_three(self, sock, i, d, b): return ("ok", i, d, b) @return_reply() @request() def request_four(self, sock): return ["ok"] @inform(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def inform_one(self, sock, i, d, b): pass @request(Int(min=1, max=3), Discrete(("on", "off")), Bool()) @return_reply(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def request_five(self, i, d, b): return ("ok", i, d, b) @return_reply(Int(min=1, max=3), Discrete(("on", "off")), Bool()) @request(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def request_six(self, i, d, b): return ("ok", i, d, b) @return_reply(Int(), Str()) @request(Int(), include_msg=True) def request_seven(self, msg, i): return ("ok", i, msg.name) @return_reply(Int(), Str()) @request(Int(), include_msg=True) def request_eight(self, sock, msg, i): return ("ok", i, msg.name)
def test_return_reply_multi(self): with self.assertRaises(TypeError) as ex: return_reply(Bool(multiple=True), Int()) self.assertEqual( ex.exception.message, 'Only the last parameter type can accept multiple arguments.')
class TestDevice(object): def __init__(self): self.sent_messages = [] @request(Int(min=1, max=10), Discrete(("on", "off")), Bool()) @return_reply(Int(min=1, max=10), Discrete(("on", "off")), Bool()) def request_one(self, req, i, d, b): if i == 3: return ("fail", "I failed!") if i == 5: return ("bananas", "This should never be sent") if i == 6: return ("ok", i, d, b, "extra parameter") if i == 9: self.finish_request_one(req, i, d, b) raise AsyncReply() return ("ok", i, d, b) @send_reply(Int(min=1, max=10), Discrete(("on", "off")), Bool()) def finish_request_one(self, req, i, d, b): return (req, "ok", i, d, b) def reply(self, req, msg, orig_msg): self.sent_messages.append([req, msg]) @request(Int(min=1, max=3, default=2), Discrete(("on", "off"), default="off"), Bool(default=True)) @return_reply(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def request_two(self, req, i, d, b): return ("ok", i, d, b) @return_reply(Int(min=1, max=3), Discrete(("on", "off")), Bool()) @request(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def request_three(self, req, i, d, b): return ("ok", i, d, b) @return_reply() @request() def request_four(self, req): return ["ok"] @inform(Int(min=1, max=3), Discrete(("on", "off")), Bool()) def inform_one(self, i, d, b): pass @request(Timestamp(), Timestamp(optional=True), major=4) @return_reply(Timestamp(), Timestamp(default=321), major=4) def request_katcpv4_time(self, req, timestamp1, timestamp2): self.katcpv4_time1 = timestamp1 self.katcpv4_time2 = timestamp2 if timestamp2: return ('ok', timestamp1, timestamp2) else: return ('ok', timestamp1) @request(Timestamp(multiple=True), major=4) @return_reply(Timestamp(multiple=True), major=4) def request_katcpv4_time_multi(self, req, *timestamps): self.katcpv4_time_multi = timestamps return ('ok', ) + timestamps @return_reply(Int(), Str()) @request(Int(), include_msg=True) def request_eight(self, req, msg, i): return ("ok", i, msg.name) @request(Int(), Float(multiple=True)) @return_reply(Int(), Float(multiple=True)) def request_int_multifloat(self, req, i, *floats): return ('ok', i) + floats
def test_unpack_types_more_types_than_args(self): expected = ['one', 2, True, None] self.check_unpacking( [Str(), Int(), Bool(default=True), Str(optional=True)], [b'one', b'2'], expected)
def test_pack_types_more_types_than_args(self): expected = [b'one', b'2', b'1', b'four'] self.check_packing( [Str(), Int(), Bool(default=True), Str(default='four')], ['one', 2], expected)
def test_request_multi(self): with self.assertRaises(TypeError) as ex: request(Bool(multiple=True), Int()) self.assertEqual( str(ex.exception), 'Only the last parameter type can accept multiple arguments.')