def test_configure(self): """Driver configuration tests""" channels = Some.VALID_CHANNELS + Some.INVALID_CHANNELS mi_logger.debug("\n configure: %s" % str(channels)) configs = {} for c in channels: configs[c] = { 'method': 'ethernet', 'device_addr': '1.1.1.1', 'device_port': 1, 'server_addr': '2.2.2.2', 'server_port': 2 } result = self.driver.configure(configs) _print_dict("\n configure result", result) for c in channels: self.assertTrue(c in result) for c in Some.INVALID_CHANNELS: self.assertEqual(result[c], InstErrorCode.INVALID_CHANNEL) for c in Some.VALID_CHANNELS: self.assertEqual(result[c], InstErrorCode.OK)
def set(self, params, *args, **kwargs): mi_logger.debug("MyProtocol(%s).set: params=%s" % (self._channel, str(params))) assert isinstance(params, dict) updated_params = 0 result = {} for (param, value) in params.items(): if param in self._values: if isinstance(value, int): self._values[param] = value result[param] = InstErrorCode.OK updated_params += 1 else: result[param] = InstErrorCode.INVALID_PARAM_VALUE else: result[param] = InstErrorCode.INVALID_PARAMETER # self.announce_to_driver(DriverAnnouncement.CONFIG_CHANGE, # msg="%s parameter(s) successfully set." % # updated_params) return result
def test_configure(self): """Driver configuration tests""" channels = Some.VALID_CHANNELS + Some.INVALID_CHANNELS mi_logger.debug("\n configure: %s" % str(channels)) configs = {} for c in channels: configs[c] = {'method': 'ethernet', 'device_addr': '1.1.1.1', 'device_port': 1, 'server_addr': '2.2.2.2', 'server_port': 2} result = self.driver.configure(configs) _print_dict("\n configure result", result) for c in channels: self.assertTrue(c in result) for c in Some.INVALID_CHANNELS: self.assertEqual(result[c], InstErrorCode.INVALID_CHANNEL) for c in Some.VALID_CHANNELS: self.assertEqual(result[c], InstErrorCode.OK)
def send_evt_msg(zmq_driver_process): """ Await events on the driver process event queue and publish them on a ZMQ PUB socket to the driver process client. """ context = zmq.Context() sock = context.socket(zmq.PUB) zmq_driver_process.evt_port = sock.bind_to_random_port( zmq_driver_process.event_host_string) mi_logger.info('Driver process event socket bound to %i', zmq_driver_process.evt_port) file(zmq_driver_process.evt_port_fname, 'w+').write(str(zmq_driver_process.evt_port) + '\n') zmq_driver_process.stop_evt_thread = False while not zmq_driver_process.stop_evt_thread: try: evt = zmq_driver_process.events.pop(0) mi_logger.debug('Event thread sending event %s', str(evt)) while evt: try: sock.send_pyobj(evt, flags=zmq.NOBLOCK) evt = None mi_logger.debug('Event sent!') except zmq.ZMQError: time.sleep(.1) if zmq_driver_process.stop_evt_thread: break except IndexError: time.sleep(.1) sock.close() context.term() mi_logger.info('Driver process event socket closed')
def recv_cmd_msg(zmq_driver_process): """ Await commands on a ZMQ REP socket, forwaring them to the driver for processing and returning the result. """ context = zmq.Context() sock = context.socket(zmq.REP) zmq_driver_process.cmd_port = sock.bind_to_random_port( zmq_driver_process.cmd_host_string) mi_logger.info('Driver process cmd socket bound to %i', zmq_driver_process.cmd_port) file(zmq_driver_process.cmd_port_fname, 'w+').write(str(zmq_driver_process.cmd_port) + '\n') zmq_driver_process.stop_cmd_thread = False while not zmq_driver_process.stop_cmd_thread: try: msg = sock.recv_pyobj(flags=zmq.NOBLOCK) mi_logger.debug('Processing message %s', str(msg)) reply = zmq_driver_process.cmd_driver(msg) while True: try: sock.send_pyobj(reply, flags=zmq.NOBLOCK) break except zmq.ZMQError: time.sleep(.1) if zmq_driver_process.stop_cmd_thread: break except zmq.ZMQError: time.sleep(.1) sock.close() context.term() mi_logger.info('Driver process cmd socket closed.')
def send_evt_msg(zmq_driver_process): """ Await events on the driver process event queue and publish them on a ZMQ PUB socket to the driver process client. """ context = zmq.Context() sock = context.socket(zmq.PUB) zmq_driver_process.evt_port = sock.bind_to_random_port(zmq_driver_process.event_host_string) mi_logger.info('Driver process event socket bound to %i', zmq_driver_process.evt_port) file(zmq_driver_process.evt_port_fname,'w+').write(str(zmq_driver_process.evt_port)+'\n') zmq_driver_process.stop_evt_thread = False while not zmq_driver_process.stop_evt_thread: try: evt = zmq_driver_process.events.pop(0) mi_logger.debug('Event thread sending event %s', str(evt)) while evt: try: sock.send_pyobj(evt, flags=zmq.NOBLOCK) evt = None mi_logger.debug('Event sent!') except zmq.ZMQError: time.sleep(.1) if zmq_driver_process.stop_evt_thread: break except IndexError: time.sleep(.1) sock.close() context.term() mi_logger.info('Driver process event socket closed')
def recv_cmd_msg(zmq_driver_process): """ Await commands on a ZMQ REP socket, forwaring them to the driver for processing and returning the result. """ context = zmq.Context() sock = context.socket(zmq.REP) zmq_driver_process.cmd_port = sock.bind_to_random_port(zmq_driver_process.cmd_host_string) mi_logger.info('Driver process cmd socket bound to %i', zmq_driver_process.cmd_port) file(zmq_driver_process.cmd_port_fname,'w+').write(str(zmq_driver_process.cmd_port)+'\n') zmq_driver_process.stop_cmd_thread = False while not zmq_driver_process.stop_cmd_thread: try: msg = sock.recv_pyobj(flags=zmq.NOBLOCK) mi_logger.debug('Processing message %s', str(msg)) reply = zmq_driver_process.cmd_driver(msg) while True: try: sock.send_pyobj(reply, flags=zmq.NOBLOCK) break except zmq.ZMQError: time.sleep(.1) if zmq_driver_process.stop_cmd_thread: break except zmq.ZMQError: time.sleep(.1) sock.close() context.term() mi_logger.info('Driver process cmd socket closed.')
def get(self, params, *args, **kwargs): mi_logger.debug("MyProtocol(%s).get: params=%s" % (self._channel, str(params))) assert isinstance(params, (list, tuple)) result = {} for param in params: if param in self._values: value = self._values[param] else: value = InstErrorCode.INVALID_PARAMETER result[param] = value return result
def test_connect_disconnect(self): """Test state change when connecting and disconnecting""" result = self.driver.get_current_state() mi_logger.debug("Initial state result: %s", result) self.assertEquals(result[Channel.INSTRUMENT], DriverState.UNCONFIGURED) self.driver.chan_map[Channel.INSTRUMENT].connect = Mock(return_value = 12) result = self.driver.connect() result = self.driver.get_current_state() # Verify we hit the protocol since we are "connected" self.assertEquals(result[Channel.INSTRUMENT], DriverState.UNCONFIGURED) result = self.driver.disconnect() result = self.driver.get_current_state() # driver FSM should intercept self.assertEquals(result[Channel.INSTRUMENT], DriverConnectionState.DISCONNECTED)
def test_get_params_channel_all(self): """Driver get all params tests""" params = [(Channel.ALL, Parameter.PARAM1), (Channel.ALL, Parameter.PARAM2)] mi_logger.debug("\nGET: %s" % str(params)) get_result = self.driver.get(params) _print_dict("\nGET get_result", get_result) for c in Channel.list(): if c != Channel.ALL: self.assertTrue((c, Parameter.PARAM1) in get_result) self.assertTrue((c, Parameter.PARAM2) in get_result)
def test_connect_disconnect(self): """Test state change when connecting and disconnecting""" result = self.driver.get_current_state() mi_logger.debug("Initial state result: %s", result) self.assertEquals(result[Channel.INSTRUMENT], DriverState.UNCONFIGURED) self.driver.chan_map[Channel.INSTRUMENT].connect = Mock( return_value=12) result = self.driver.connect() result = self.driver.get_current_state() # Verify we hit the protocol since we are "connected" self.assertEquals(result[Channel.INSTRUMENT], DriverState.UNCONFIGURED) result = self.driver.disconnect() result = self.driver.get_current_state() # driver FSM should intercept self.assertEquals(result[Channel.INSTRUMENT], DriverConnectionState.DISCONNECTED)
def test_get_params(self): """Driver get params tests""" params = Some.VALID_PARAMS + Some.INVALID_PARAMS mi_logger.debug("\nGET: %s" % str(params)) get_result = self.driver.get(params) _print_dict("\nGET get_result", get_result) self.assertEqual(get_result[("invalid_chan", Parameter.PARAM1)], InstErrorCode.INVALID_CHANNEL) self.assertEqual(get_result[(Channel.CHAN1, "invalid_param")], InstErrorCode.INVALID_PARAMETER) for cp in Some.VALID_PARAMS: self.assertTrue(cp in get_result)
def test_connect(self): """Driver connection tests""" channels = Some.VALID_CHANNELS + Some.INVALID_CHANNELS mi_logger.debug("\n connect: %s" % str(channels)) result = self.driver.connect(channels) _print_dict("\n connect result", result) for c in channels: self.assertTrue(c in result) for c in Some.INVALID_CHANNELS: self.assertEqual(result[c], InstErrorCode.INVALID_CHANNEL) for c in Some.VALID_CHANNELS: self.assertEqual(result[c], InstErrorCode.OK)
def _print_dict(title, d): mi_logger.debug("%s:" % title) for item in d.items(): mi_logger.debug("\t%s" % str(item))