class GenericTestCase(unittest.TestCase): _DEVICE_DB = enable_dax_sim( { 'core': { 'type': 'local', 'module': 'artiq.coredevice.core', 'class': 'Core', 'arguments': { 'host': None, 'ref_period': 1e-9 } }, "generic": { "type": "local", "module": "non.existing.module", "class": "non.existing.class" }, }, enable=True, output='null', moninj_service=False) def setUp(self) -> None: self.managers = get_managers(device_db=self._DEVICE_DB) self.generic = dax.sim.coredevice.generic.Generic( self.managers.device_mgr, _key='generic') def tearDown(self) -> None: # Close managers self.managers.close() def test_direct_call(self): with self.assertRaises( TypeError, msg='Direct call on generic object did not raise'): # Note that the driver itself is not callable, which should raise an exception self.generic() def test_attributes(self): foo = self.generic.foo self.assertIsInstance(foo, dax.sim.coredevice.generic._GenericBase) self.assertEqual( foo, self.generic.foo) # Test if same attribute is returned self.assertIsInstance(self.generic.foo.bar.baz, dax.sim.coredevice.generic._GenericBase) self.assertIsInstance(self.generic.bar.baz.foo, dax.sim.coredevice.generic._GenericBase) self.assertIsInstance(self.generic.baz.foo.bar, dax.sim.coredevice.generic._GenericBase) def test_method_call(self): foo = self.generic.foo self.assertIsInstance(foo, dax.sim.coredevice.generic._GenericBase) self.assertTrue(callable(foo), 'Generic object method/attribute is not callable') self.generic.bar() self.generic.foo.bar.baz() self.generic.bar.baz.foo() self.generic.baz.foo.bar()
def setUp(self) -> None: self.managers = get_managers( enable_dax_sim(ddb=_DEVICE_DB, enable=True, logging_level=30, output='null', moninj_service=False))
def setUp(self) -> None: # Create the system ddb = enable_dax_sim(_DEVICE_DB.copy(), enable=True, output='peek', moninj_service=False) self.managers = get_managers(ddb) self.sys = _TestSystem(self.managers) # Get the peek signal manager self.sm = typing.cast(PeekSignalManager, get_signal_manager()) self.assertIsInstance(self.sm, PeekSignalManager)
class CoreCacheTestCase(unittest.TestCase): _CACHE = None _DEVICE_DB = enable_dax_sim({ 'core': { 'type': 'local', 'module': 'artiq.coredevice.core', 'class': 'Core', 'arguments': {'host': None, 'ref_period': 1e-9} }, "core_cache": { "type": "local", "module": "artiq.coredevice.cache", "class": "CoreCache" }, }, enable=True, output='null', moninj_service=False) def setUp(self) -> None: self.managers = get_managers(device_db=self._DEVICE_DB) self.cache = dax.sim.coredevice.cache.CoreCache(self.managers.device_mgr, cache=self._CACHE, _key='core_cache') def tearDown(self) -> None: # Close managers self.managers.close() def test_cache(self): data = { 'foo': [0], 'bar': [np.int32(3), 0, 4], 'baz': [4, 6, 3, np.int64(99)], } for k, v in data.items(): with self.subTest(key=k, value=v): # Put data self.cache.put(k, v) # Get data self.assertListEqual(self.cache.get(k), v, 'Data does not match earlier added data') def test_non_existing_key(self): self.assertListEqual(self.cache.get('non_existing_key'), [], 'Non-existing key did not return empty list') def test_erase(self): key = 'foo' # Add self.cache.put(key, [2, 3, 4]) # Erase self.cache.put(key, []) self.assertListEqual(self.cache.get(key), [], 'Extracting erased key did not return empty list')
def setUp(self) -> None: self._temp_dir = temp_dir() self._temp_dir.__enter__() # Create the system ddb = enable_dax_sim(_DEVICE_DB.copy(), enable=True, output='vcd', moninj_service=False) self.managers = get_managers(ddb) self.sys = _TestSystem(self.managers) # Get the signal manager self.sm: DaxSignalManager = typing.cast(VcdSignalManager, get_signal_manager()) self.assertIsInstance(self.sm, VcdSignalManager)
def test_gtk_wave_save_generator(self): with temp_dir(): ddb = enable_dax_sim(ddb=_DEVICE_DB.copy(), enable=True, output='vcd', moninj_service=False) with get_managers(ddb) as managers: system = _TestSystem(managers) self.assertTrue(system.dax_sim_enabled) # Create GTKWave save generator object, which immediately writes the waves file GTKWSaveGenerator(system) # Manually close signal manager before leaving temp dir get_signal_manager().close()
def test_gtk_wave_save_generator_invalid_signal_manager(self): with temp_dir(): ddb = enable_dax_sim(ddb=_DEVICE_DB.copy(), enable=True, output='null', moninj_service=False) with get_managers(ddb) as managers: system = _TestSystem(managers) self.assertTrue(system.dax_sim_enabled) with self.assertRaises( RuntimeError, msg='Not using VCD signal manager did not raise'): # Create GTKWave save generator object, which immediately writes the waves file GTKWSaveGenerator(system) # Manually close signal manager before leaving temp dir get_signal_manager().close()
def _get_managers(**kwargs): return get_managers(enable_dax_sim(ddb=_DEVICE_DB, enable=True, logging_level=30, output='null', moninj_service=False), **kwargs)
def setUp(self) -> None: ddb = enable_dax_sim(_DEVICE_DB.copy(), enable=True, output='null', moninj_service=False) self.managers = get_managers(ddb)
def construct_env( self, env_class: typing.Type[__E_T], *, device_db: typing.Union[str, typing.Dict[str, typing.Any], None] = None, logging_level: typing.Union[int, str] = logging.NOTSET, build_args: typing.Sequence[typing.Any] = (), build_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None, env_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None, **kwargs: typing.Any) -> __E_T: """Construct an ARTIQ environment based on the given class. The constructed environment can be used for testing. Devices in the device manager are automatically closed by a finalizer. It is not required to close the devices in the device manager explicitly. :param env_class: The environment class to construct :param device_db: The device DB to use (defaults to file configured in :attr:`DEFAULT_DEVICE_DB`) :param logging_level: The desired logging level :param build_args: Positional arguments passed to the build function of the environment :param build_kwargs: Keyword arguments passed to the build function of the environment :param env_kwargs: Keyword arguments passed to the argument parser of the environment :param kwargs: Keyword arguments passed to the argument parser of the environment (updates ``env_kwargs``) :return: The constructed ARTIQ environment object """ # Set default values if build_kwargs is None: build_kwargs = {} if env_kwargs is None: env_kwargs = {} assert issubclass( env_class, HasEnvironment ), 'The environment class must be a subclass of HasEnvironment' assert isinstance( device_db, (str, dict) ) or device_db is None, 'Device DB must be of type str, dict, or None' assert isinstance( logging_level, (int, str)), 'Logging level must be of type int or str' assert isinstance( build_args, collections.abc.Sequence), 'Build arguments must be a sequence' assert isinstance(build_kwargs, dict), 'Build keyword arguments must be a dict' assert all( isinstance(k, str) for k in build_kwargs), 'Keys of the build kwargs dict must be of type str' assert isinstance(env_kwargs, dict), 'Environment keyword arguments must be a dict' # Set level of module logger _logger.setLevel(logging_level) # Construct an expid object expid: typing.Dict[str, typing.Any] = { 'log_level': logging_level, 'class_name': env_class.__name__, 'repo_rev': 'N/A' } if isinstance(device_db, dict): # Copy the device DB to not mutate the given one device_db = device_db.copy() else: # Obtain device DB from file _logger.debug('Obtaining device DB from file') with warnings.catch_warnings(): # Ignore resource warnings that could be raised from evaluating the device DB # These warnings appear when starting the MonInjDummyService warnings.simplefilter('ignore', category=ResourceWarning) device_db = device_db_from_file( self.DEFAULT_DEVICE_DB if device_db is None else device_db) # Convert and configure device DB _logger.debug('Converting device DB') enable_dax_sim(ddb=device_db, enable=True, logging_level=logging_level, output='peek', moninj_service=False) # Construct environment, which will also construct a new signal manager _logger.debug('Constructing environment') env = env_class( get_managers(device_db, expid=expid, arguments=env_kwargs, **kwargs), *build_args, **build_kwargs) # Store the new signal manager _logger.debug('Retrieving peek signal manager') self.__signal_manager = typing.cast(PeekSignalManager, get_signal_manager()) assert isinstance( self.__signal_manager, PeekSignalManager), 'Did not obtained correct signal manager type' # Return the environment return env