def test_request_one(self): """Test request with no defaults.""" req = mock.Mock() req.msg.name = 'one' self.assertEqual(str(self.device.request_one(req, Message.request( "one", "2", "on", "0"))), "!one ok 2 on 0") self.assertRaises(FailReply, self.device.request_one, req, Message.request("one", "14", "on", "0")) self.assertRaises(FailReply, self.device.request_one, req, Message.request("one", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_one, req, Message.request("one", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_one, req, Message.request("one", "2", "on", "0", "3")) self.assertRaises(FailReply, self.device.request_one, req, Message.request("one", "2", "on")) self.assertEqual(str(self.device.request_one(req, Message.request( "one", "3", "on", "0"))), "!one fail I\\_failed!") self.assertRaises(ValueError, self.device.request_one, req, Message.request("one", "5", "on", "0")) self.assertRaises(ValueError, self.device.request_one, req, Message.request("one", "6", "on", "0")) req.reset_mock() self.assertRaises(AsyncReply, self.device.request_one, req, Message.request("one", "9", "on", "0")) self.assertEqual(req.reply_with_message.call_count, 1) req.reply_with_message.assert_called_once_with(Message.reply( 'one', 'ok', '9', 'on', '0'))
def test_request_six(self): """Test client request with no sock and decorators in reverse order.""" self.assertEqual(str(self.device.request_six(Message.request("six", "2", "on", "0"))), "!six ok 2 on 0") self.assertRaises(FailReply, self.device.request_six, Message.request("six", "4", "on", "0")) self.assertRaises(FailReply, self.device.request_six, Message.request("six", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_six, Message.request("six", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_six, Message.request("six", "2", "on"))
def test_request_three(self): """Test request with no defaults and decorators in reverse order.""" sock = "" self.assertEqual(str(self.device.request_three(sock, Message.request("three", "2", "on", "0"))), "!three ok 2 on 0") self.assertRaises(FailReply, self.device.request_three, sock, Message.request("three", "4", "on", "0")) self.assertRaises(FailReply, self.device.request_three, sock, Message.request("three", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_three, sock, Message.request("three", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_three, sock, Message.request("three", "2", "on"))
def test_request_int_multifloat(self): req = self.device.request_int_multifloat desired_i, desired_floats = (7, (1.2, 999, 71.43)) self.assertEqual(str(req('req', Message.request( 'int-multifloat', desired_i, *desired_floats))), '!int-multifloat ok 7 1.2 999 71.43') with self.assertRaises(FailReply) as ex: req('req', Message.request('int-multifloat', desired_i, 1.2, 'abc')) self.assertEqual( ex.exception.message, "Error in parameter 3 (): Could not parse value 'abc' as float.")
def test_request_six(self): """Test client request with no req and decorators in reverse order.""" self.assertEqual(str(self.device.request_six(Message.request( "six", "2", "on", "0"))), "!six ok 2 on 0") self.assertRaises(FailReply, self.device.request_six, Message.request("six", "4", "on", "0")) self.assertRaises(FailReply, self.device.request_six, Message.request("six", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_six, Message.request("six", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_six, Message.request("six", "2", "on"))
def fringe_stop(self, enable): # Stop fringe stopping message = Message.request(SWARM_FSTOP_STOP_CMD) reply, informs = self.roach2.blocking_request(message, timeout=60) if not reply.reply_ok(): self.logger.error("Stopping fringe stopping failed!") # Start it again (if requested) if enable: message = Message.request(SWARM_FSTOP_START_CMD) reply, informs = self.roach2.blocking_request(message, timeout=60) if not reply.reply_ok(): self.logger.error("Starting fringe stopping failed!")
def test_request_three(self): """Test request with no defaults and decorators in reverse order.""" req = "" self.assertEqual(str(self.device.request_three(req, Message.request( "three", "2", "on", "0"))), "!three ok 2 on 0") self.assertRaises(FailReply, self.device.request_three, req, Message.request("three", "4", "on", "0")) self.assertRaises(FailReply, self.device.request_three, req, Message.request("three", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_three, req, Message.request("three", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_three, req, Message.request("three", "2", "on"))
def send_request(self, name, *args): if not self.transport.connected: raise DeviceNotConnected() d = Deferred() self.send_message(Message.request(name, *args)) self.queries.append((name, d, [])) return d
def test_request_eight(self): """Test server request with a message argument.""" sock = "" self.assertEqual( str(self.device.request_eight(sock, Message.request("eight", "8"))), "!eight ok 8 eight")
def set_delay(self, input_n, value): # Set the delay value in ns message = Message.request(SWARM_DELAY_SET_CMD, str(input_n), str(value)) reply, informs = self.roach2.blocking_request(message, timeout=60) if not reply.reply_ok(): self.logger.error("Setting the delay failed!")
def get_sensor_dict(self): ''' Create or update sensor dictionary ''' if self.fpga.is_connected(): # Multiplicative factor to apply to each. This assumes specific # ROACH2 sensor sampling--this could change if ROACH # tcpborphserver3 is updated factor = [0] + [0.001] * 3 + [1] * 4 + [0.001] * 18 keys = [] values = [] reply, sensors = self.fpga.blocking_request( Message.request('sensor-list')) if reply.arguments[0] == 'ok': # Got sensor list okay n = int(reply.arguments[1]) reply, vals = self.fpga.blocking_request( Message.request('sensor-value')) self.sensor_dict = {} if reply.arguments[0] == 'ok': # Got sensor values okay if n == int(reply.arguments[1]): # The numbers of sensors and values agree! # Skip first "sensor" which is the mode for i in range(1, n): name = sensors[i].arguments[0][4:] keys.append(name) values.append( int(vals[i].arguments[-1]) * factor[i]) keys.append(name + '.status') values.append(vals[i].arguments[-2]) else: self.sensors = {} self.msg = 'Could not init sensors--names,values are different lengths' return else: self.sensors = {} self.msg = 'Could not init sensors:', reply.arguments[0] return else: self.sensors = {} self.msg = 'Could not init sensors:', reply.arguments[0] return self.sensors = dict(zip(keys, values)) self.msg = 'Success' else: self.msg = 'Could not update sensors--client not connected'
def test_request_two(self): """Test request with defaults.""" sock = "" self.assertEqual( str( self.device.request_two(sock, Message.request("two", "2", "on", "0"))), "!two ok 2 on 0") self.assertRaises(FailReply, self.device.request_two, sock, Message.request("two", "4", "on", "0")) self.assertRaises(FailReply, self.device.request_two, sock, Message.request("two", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_two, sock, Message.request("two", "2", "on", "3")) self.assertEqual( str( self.device.request_two(sock, Message.request("two", "2", "on"))), "!two ok 2 on 1") self.assertEqual( str(self.device.request_two(sock, Message.request("two", "2"))), "!two ok 2 off 1") self.assertEqual( str(self.device.request_two(sock, Message.request("two"))), "!two ok 2 off 1")
def historical_sensor_list(self, sensor_filter=''): reply, informs = self.blocking_request( Message.request('historical-sensor-list', sensor_filter)) if reply.arguments[0] == 'ok': result = [inform.arguments for inform in informs] else: logger.warn(reply) result = [] return result
def test_katcpv4_multi(self): tss = (1234, 5678, 9012) # In milliseconds req = mock.Mock() ret_msg = self.device.request_katcpv4_time_multi(req, Message.request( 'katcpv4-time-multi', *(str(ts) for ts in tss) )) for i, ts in enumerate(tss): self.assertAlmostEqual(float(ret_msg.arguments[i+1]), ts) self.assertAlmostEqual(self.device.katcpv4_time_multi[i], ts*MS_TO_SEC_FAC)
def get_delay(self, input_n): # Get the delay value in ns message = Message.request(SWARM_DELAY_GET_CMD, str(input_n)) reply, informs = self.roach2.blocking_request(message, timeout=60) if not reply.reply_ok(): self.logger.error("Getting the delay failed!") else: return float(reply.arguments[1])
def test_request_one(self): """Test request with no defaults.""" sock = "" self.assertEqual(str(self.device.request_one(sock, Message.request("one", "2", "on", "0"))), "!one ok 2 on 0") self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "14", "on", "0")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "on", "0", "3")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "on")) self.assertEqual(str(self.device.request_one(sock, Message.request("one", "3", "on", "0"))), "!one fail I\\_failed!") self.assertRaises(ValueError, self.device.request_one, sock, Message.request("one", "5", "on", "0")) self.assertRaises(ValueError, self.device.request_one, sock, Message.request("one", "6", "on", "0")) self.assertRaises(AsyncReply, self.device.request_one, "mysock", Message.request("one", "9", "on", "0")) self.assertEqual(len(self.device.sent_messages), 1) self.assertEqual(self.device.sent_messages[0][0], "mysock") self.assertEqual(str(self.device.sent_messages[0][1]), "!one ok 9 on 0")
def test_katcpv4(self): ts = 12345678 # In milliseconds req = mock.Mock() ret_msg = self.device.request_katcpv4_time(req, Message.request( 'katcpv4-time', str(ts))) self.assertTrue(ret_msg.reply_ok()) self.assertAlmostEqual(float(ret_msg.arguments[1]), ts) # Test decorator default value self.assertAlmostEqual(float(ret_msg.arguments[2]), 321*SEC_TO_MS_FAC) self.assertAlmostEqual(self.device.katcpv4_time1*SEC_TO_MS_FAC, ts) self.assertEqual(self.device.katcpv4_time2, None) ts1 = 1234 ts2 = 2345 ret_msg = self.device.request_katcpv4_time(req, Message.request( 'katcpv4-time', str(ts1), str(ts2))) self.assertTrue(ret_msg.reply_ok()) self.assertAlmostEqual(float(ret_msg.arguments[1]), ts1) self.assertAlmostEqual(float(ret_msg.arguments[2]), ts2) self.assertAlmostEqual(self.device.katcpv4_time1*SEC_TO_MS_FAC, ts1) self.assertAlmostEqual(self.device.katcpv4_time2*SEC_TO_MS_FAC, ts2)
def test_request_int_multifloat(self): req = self.device.request_int_multifloat desired_i, desired_floats = (7, (1.2, 999, 71.43)) self.assertEqual( str( req( 'req', Message.request('int-multifloat', desired_i, *desired_floats))), '!int-multifloat ok 7 1.2 999.0 71.43') with self.assertRaises(FailReply) as ex: req('req', Message.request('int-multifloat', desired_i, 1.2, 'abc')) # storing Message.arguments as byte string results in slightly different # reprs for PY2 compared to PY3. if future.utils.PY2: expected = "Error in parameter 3 (): Could not parse value 'abc' as float." else: expected = "Error in parameter 3 (): Could not parse value 'b'abc'' as float." self.assertEqual(str(ex.exception), expected)
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)
def test_request_two(self): """Test request with defaults.""" sock = "" self.assertEqual(str(self.device.request_two(sock, Message.request("two", "2", "on", "0"))), "!two ok 2 on 0") self.assertRaises(FailReply, self.device.request_two, sock, Message.request("two", "4", "on", "0")) self.assertRaises(FailReply, self.device.request_two, sock, Message.request("two", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_two, sock, Message.request("two", "2", "on", "3")) self.assertEqual(str(self.device.request_two(sock, Message.request("two", "2", "on"))), "!two ok 2 on 1") self.assertEqual(str(self.device.request_two(sock, Message.request("two", "2"))), "!two ok 2 off 1") self.assertEqual(str(self.device.request_two(sock, Message.request("two"))), "!two ok 2 off 1")
def test_request_one(self): """Test request with no defaults.""" sock = "" self.assertEqual( str( self.device.request_one(sock, Message.request("one", "2", "on", "0"))), "!one ok 2 on 0") self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "14", "on", "0")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "on", "0", "3")) self.assertRaises(FailReply, self.device.request_one, sock, Message.request("one", "2", "on")) self.assertEqual( str( self.device.request_one(sock, Message.request("one", "3", "on", "0"))), "!one fail I\\_failed!") self.assertRaises(ValueError, self.device.request_one, sock, Message.request("one", "5", "on", "0")) self.assertRaises(ValueError, self.device.request_one, sock, Message.request("one", "6", "on", "0")) self.assertRaises(AsyncReply, self.device.request_one, "mysock", Message.request("one", "9", "on", "0")) self.assertEqual(len(self.device.sent_messages), 1) self.assertEqual(self.device.sent_messages[0][0], "mysock") self.assertEqual(str(self.device.sent_messages[0][1]), "!one ok 9 on 0")
def _request(self, name, *args, **kwargs): request = Message.request(name, *args) try: reply, informs = self.blocking_request(request, keepalive=True) except TypeError: retry = kwargs.pop('retry', 0) self.logger.error("Error using blocking_request, try number %d" % retry) return self._request(name, *args, retry=retry+1) if reply.arguments[0] != Message.OK: self._logger.error("Request %s failed.\n Request: %s\n Reply: %s." % (request.name, request, reply)) raise RuntimeError("Request %s failed.\n Request: %s\n Reply: %s." % (request.name, request, reply)) return reply, informs
def send_katcp_cmd(self, cmd, *args): # Create the message object message = Message.request(cmd, *args) # Send the request, and block for 60 seconds reply, informs = self.roach2.blocking_request(message, timeout=60) # Check for error, and raise one if present if not reply.reply_ok(): raise RuntimeError(reply) # Otherwise return what we got return reply, informs
def test_request_five(self): """Test client request with no sock.""" self.assertEqual(str(self.device.request_five(Message.request("five", "2", "on", "0"))), "!five ok 2 on 0") self.assertRaises(FailReply, self.device.request_five, Message.request("five", "14", "on", "0")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "on", "0", "3")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "on"))
def historical_sensor_data(self, sensor_names_filter, start_seconds, end_seconds, period_seconds=-1, strategy='stepwise', fetch_last_value=False, timeout=None): timeout = max(15, timeout or (end_seconds - start_seconds) / 1000) reply, informs = self.blocking_request(Message.request( 'historical-sensor-data', sensor_names_filter, start_seconds, end_seconds, period_seconds, strategy, int(fetch_last_value), timeout), timeout=timeout) if reply.arguments[0] != 'ok' or int(reply.arguments[1]) == 0: return self._results(reply, None) else: return self._results(reply, informs)
def nb_request(self, request, inform_cb=None, reply_cb=None, *args): """Make a non-blocking request. @param self This object. @param request The request string. @param inform_cb An optional callback function, called upon receipt of every inform to the request. @param inform_cb An optional callback function, called upon receipt of the reply to the request. @param args Arguments to the katcp.Message object. """ if len(self._nb_requests) == self._nb_max_requests: oldreq = self.nb_pop_oldest_request() LOGGER.debug('Request list full, removing oldest one(%s,%s).' % (oldreq.request, oldreq.request_id)) request_id = self.nb_get_next_request_id() self.nb_add_request(request, request_id, inform_cb, reply_cb) request_msg = Message.request(request, *args) self._nb_request_func(msg=request_msg, reply_cb=self.nb_replycb, inform_cb=self.nb_informcb, user_data=request_id) return {'host': self.host, 'request': request, 'id': request_id}
def test_request_five(self): """Test client request with no req.""" self.assertEqual(str(self.device.request_five(Message.request( "five", "2", "on", "0"))), "!five ok 2 on 0") self.assertRaises(FailReply, self.device.request_five, Message.request("five", "14", "on", "0")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "dsfg", "0")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "on", "3")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "on", "0", "3")) self.assertRaises(FailReply, self.device.request_five, Message.request("five", "2", "on"))
def nb_request(self, request, inform_cb=None, reply_cb=None, *args): """ Make a non-blocking request. :param self - this object. :param request - the request string. :param inform_cb - an optional callback function, called upon receipt of every inform to the request. :param reply_cb - an optional callback function, called upon receipt of the reply to the request. :param args - arguments to the katcp.Message object. """ if len(self._nb_requests) == self._nb_max_requests: oldreq = self.nb_pop_oldest_request() LOGGER.debug('Request list full, removing oldest one(%s,%s).' % (oldreq.request, oldreq.request_id)) request_id = self.nb_get_next_request_id() self.nb_add_request(request, request_id, inform_cb, reply_cb) request_msg = Message.request(request, *args) self._nb_request_func(msg=request_msg, reply_cb=self.nb_replycb, inform_cb=self.nb_informcb, user_data=request_id) return {'host': self.host, 'request': request, 'id': request_id}
def test_request_four(self): """Test request with no defaults and no parameters or return values""" req = "" self.assertEqual( str(self.device.request_four(req, Message.request("four"))), "!four ok")
def test_request_eight(self): """Test server request with a message argument.""" req = "" self.assertEqual(str(self.device.request_eight(req, Message.request( "eight", "8"))), "!eight ok 8 eight")
def test_request_four(self): """Test request with no defaults and no parameters or return values""" req = "" self.assertEqual(str(self.device.request_four(req, Message.request( "four"))), "!four ok")
def test_request_four(self): """Test request with no defaults and no parameters / return parameters""" sock = "" self.assertEqual(str(self.device.request_four(sock, Message.request("four"))), "!four ok")
def test_request_seven(self): """Test client request with no req but with a message.""" self.assertEqual(str(self.device.request_seven(Message.request( "seven", "7"))), "!seven ok 7 seven")
def test_request_seven(self): """Test client request with no sock but with a message.""" self.assertEqual( str(self.device.request_seven(Message.request("seven", "7"))), "!seven ok 7 seven")
def test_request_four(self): """Test request with no defaults and no parameters / return parameters""" sock = "" self.assertEqual( str(self.device.request_four(sock, Message.request("four"))), "!four ok")