def thread_proc_counter(self, tmpdir_factory): """ Return a transcended thread/process counter """ class ThreadProcCounter: """ Sample class to log current thread/proc. """ def __init__(self): self.path = Path(tmpdir_factory.mktemp("base")) def get_log_name(self): proc = psutil.Process().pid thread = threading.get_ident() return self.path / f"{proc}_{thread}.txt" def log(self): """ write an empty file with the process id and thread id. """ path = self.get_log_name() path.parent.mkdir(parents=True, exist_ok=True) with path.open("w") as fi: fi.write("something.txt") def remove_all_logs(self): """ remove all the logs. """ for p in self.path.parent.rglob("*.txt"): p.unlink() @property def log_count(self): """ return the count of files created. """ path = self.get_log_name().parent paths = list(path.rglob("*.txt")) return len(paths) proxy = transcend(ThreadProcCounter(), "threadproccount") yield proxy proxy.close()
def test_object_appears_in_registry(self, registry_path): """Ensure the object appears in the registry.""" _ = srpo.transcend("bob", name="transcended_bob") cmd = f"srpo ls --registry-path {registry_path}" res = run(cmd, shell=True, capture_output=True) output_str = res.stdout.decode("utf8").split("\n")[1] assert "transcended_bob" in output_str
def transcended_bank(tmpdir_factory): """ Transcend a bank server. """ tmpdir = Path(tmpdir_factory.mktemp("bob")) ds = obsplus.load_dataset("bingham_test").copy_to(tmpdir) bank = obsplus.WaveBank(ds.waveform_path) proxy = transcend(bank, "wavebank") yield proxy proxy.close()
def test_kill_by_name(self, registry_path): """Ensure srpo objects can be killed by name.""" name = "transcended_bill" _ = srpo.transcend("Bill", name) assert name in dict(srpo.get_registry()) # now issue kill command, ensure bill goes away cmd = f"srpo kill --name {name} --registry-path {registry_path}" run(cmd, shell=True) assert name not in srpo.get_registry()
def test_create_and_shutdown_server(self): """ Ensure when the last proxy terminates the server shuts down """ # create an object and transcend it name = "simple_obj_from_test" obj = {} proxy = transcend(obj, name) # delete the proxy and make sure the server shuts down proxy.close() assert name not in get_registry() with pytest.raises(Exception): # Note: Must use Exception here get_proxy(name)
def transcended_simple_namespace(): ns = SimpleNamespace(bob=2) with transcend(ns, "bob") as tns: yield tns
def test_transcend(self, corrupted_registry): """ However, a transcended """ out = transcend({1: 2, 3: 4}, self.name) assert out[1] == 2 and out[3] == 4
def transcended_dict(): """ Transcend a simple dictionary, return proxy, then cleanup. """ obj = {"simple": True} name = "simple_dict" yield transcend(obj, name, remote=True) terminate(name)