def test_custom_type_nobackend(servicename, keyword_name): """Test custom type with no backend set up.""" from Cauldron.types import DispatcherKeywordType class CustomKeyword(DispatcherKeywordType): """A custom keyword type""" counter = 0 def __init__(self, *args, **kwargs): print("__init__") self.counter += 1 super(CustomKeyword, self).__init__(*args, **kwargs) with pytest.raises(RuntimeError): CustomKeyword(keyword_name, "") from Cauldron.api import use, teardown try: use("mock") from Cauldron.DFW import Service service = Service(servicename, setup=lambda s: CustomKeyword(keyword_name, s), config=None) keyword = service[keyword_name] assert isinstance(keyword, CustomKeyword) assert keyword.counter == 1 finally: teardown()
def test_double_teardown(): """Test teardown twice""" from Cauldron.api import use, install, teardown use("local") from Cauldron import ktl del ktl from Cauldron import DFW del DFW install() teardown() teardown() with pytest.raises(ImportError): from Cauldron import ktl with pytest.raises(ImportError): from Cauldron import DFW assert "DFW" not in sys.modules assert "ktl" not in sys.modules use("local") from Cauldron import ktl, DFW
def fail_if_not_teardown(): """Fail a pytest teardown if Cauldron has not ended properly. First, calls :func:`~Cauldron.api.teardown`. Then checks that modules can't be imported any more. Next, checks that all threads that are supposed to die, actually die. """ from Cauldron.api import teardown, CAULDRON_SETUP teardown() # Check modules not in sys.modules failures = ["DFW", "ktl", "_DFW", "_ktl"] if CAULDRON_SETUP: raise ValueError("Cauldron is marked as 'setup'.") for module in sys.modules: for failure in failures: if failure in module.split("."): mod = sys.modules[module] if mod is not None: raise ValueError("Module {0}/{1} not properly torn down.".format(module, sys.modules[module])) # Check for importability try: from Cauldron import DFW except ImportError as e: pass else: raise ValueError("Shouldn't be able to import DFW now!") try: from Cauldron import ktl except ImportError as e: pass else: raise ValueError("Shouldn't be able to import ktl now!") # Check for cycles. import gc gc.collect() if len(gc.garbage): print(gc.garbage) raise ValueError("There is garbage: {0!r}".format(gc.garbage)) # Check for zombie threads. import threading, time if threading.active_count() > 1: time.sleep(0.1) #Allow zombies to die! count = 0 for thread in threading.enumerate(): if not thread.daemon and thread not in SEEN_THREADS: count += 1 SEEN_THREADS.add(thread) # If there are new, non-daemon threads, cause an error. if count > 1: threads_str = "\n".join(["{0}:{1}".format(repr(thread), gc.get_referrers(thread)) for thread in threading.enumerate()]) raise ValueError("{0:d} non-deamon thread{1:s} left alive!\n{2!s}".format( count-1, "s" if (count-1)>1 else "", threads_str))
def test_teardown(servicename, teardown_cauldron, keyword_name2, servicename2): """Check that teardown really does tear things down, in local mode.""" from Cauldron.api import teardown, use use("local") from Cauldron.DFW import Service svc = Service(servicename2, None) svc[keyword_name2].modify('10') teardown() del svc use("local") from Cauldron.DFW import Service svc2 = Service(servicename2, None) assert svc2[keyword_name2].read() == None
def test_guard(): """Test that imports are guarded.""" from Cauldron.api import use, teardown with pytest.raises(ImportError): from Cauldron import ktl with pytest.raises(ImportError): from Cauldron import DFW use("local") from Cauldron import DFW, ktl del DFW, ktl teardown() with pytest.raises(ImportError): from Cauldron import ktl with pytest.raises(ImportError): from Cauldron import DFW
def test_teardown(servicename, teardown_cauldron, keyword_name2, servicename2): """Check that teardown really does tear things down, in local mode.""" from Cauldron.api import teardown, use use("local") from Cauldron.DFW import Service svc = Service(servicename2, None) svc[keyword_name2].modify("10") teardown() del svc use("local") from Cauldron.DFW import Service svc2 = Service(servicename2, None) assert svc2[keyword_name2].read() == None
def test_teardown(): """Test that imports are guarded after calling .teardown()""" from Cauldron.api import use, install, teardown use("local") from Cauldron import ktl del ktl from Cauldron import DFW del DFW install() teardown() with pytest.raises(ImportError): from Cauldron import ktl with pytest.raises(ImportError): from Cauldron import DFW assert "DFW" not in sys.modules assert "ktl" not in sys.modules
def test_custom_type_nobackend(servicename, keyword_name): """Test custom type with no backend set up.""" from Cauldron.types import DispatcherKeywordType class CustomKeyword(DispatcherKeywordType): """A custom keyword type""" counter = 0 def __init__(self, *args, **kwargs): print("__init__") self.counter += 1 super(CustomKeyword, self).__init__(*args, **kwargs) with pytest.raises(RuntimeError): CustomKeyword(keyword_name, "") from Cauldron.api import use, teardown try: use("mock") from Cauldron.DFW import Service service = Service(servicename, setup=lambda s : CustomKeyword(keyword_name, s), config=None) keyword = service[keyword_name] assert isinstance(keyword, CustomKeyword) assert keyword.counter == 1 finally: teardown()
def test_double_teardown(): """Test teardown twice""" from Cauldron.api import use, install, teardown use("local") from Cauldron import ktl del ktl from Cauldron import DFW del DFW install() teardown() teardown() with pytest.raises(ImportError): from Cauldron import ktl with pytest.raises(ImportError): from Cauldron import DFW assert "DFW" not in sys.modules assert "ktl" not in sys.modules use('local') from Cauldron import ktl, DFW