Best Python code snippet using lisa_python
test_cluster.py
Source:test_cluster.py
...18 head_node = cluster.add_node(num_cpus=3)19 @serve.deployment("D", version="1", num_replicas=1)20 def D(*args):21 return os.getpid()22 def get_pids(expected, timeout=30):23 pids = set()24 start = time.time()25 while len(pids) < expected:26 pids.add(requests.get("http://localhost:8000/D").text)27 if time.time() - start >= timeout:28 raise TimeoutError("Timed out waiting for pids.")29 return pids30 ray.init(head_node.address)31 serve.start(detached=True)32 client = serve.connect()33 D.deploy()34 pids1 = get_pids(1)35 goal_ref = D.options(num_replicas=3).deploy(_blocking=False)36 assert not client._wait_for_goal(goal_ref, timeout=0.1)37 assert get_pids(1) == pids138 # Add a node with another CPU, another replica should get placed.39 cluster.add_node(num_cpus=1)40 assert not client._wait_for_goal(goal_ref, timeout=0.1)41 pids2 = get_pids(2)42 assert pids1.issubset(pids2)43 # Add a node with another CPU, the final replica should get placed44 # and the deploy goal should be done.45 cluster.add_node(num_cpus=1)46 assert client._wait_for_goal(goal_ref)47 pids3 = get_pids(3)48 assert pids2.issubset(pids3)49@pytest.mark.skip("Currently hangs due to max_task_retries=-1.")50def test_node_failure(ray_cluster):51 cluster = ray_cluster52 cluster.add_node(num_cpus=3)53 worker_node = cluster.add_node(num_cpus=2)54 @serve.deployment("D", version="1", num_replicas=3)55 def D(*args):56 return os.getpid()57 def get_pids(expected, timeout=30):58 pids = set()59 start = time.time()60 while len(pids) < expected:61 pids.add(requests.get("http://localhost:8000/D").text)62 if time.time() - start >= timeout:63 raise TimeoutError("Timed out waiting for pids.")64 return pids65 ray.init(cluster.address)66 serve.start(detached=True)67 print("Initial deploy.")68 D.deploy()69 pids1 = get_pids(3)70 # Remove the node. There should still be one replica running.71 print("Kill node.")72 cluster.remove_node(worker_node)73 pids2 = get_pids(1)74 assert pids2.issubset(pids1)75 # Add a worker node back. One replica should get placed.76 print("Add back first node.")77 cluster.add_node(num_cpus=1)78 pids3 = get_pids(2)79 assert pids2.issubset(pids3)80 # Add another worker node. One more replica should get placed.81 print("Add back second node.")82 cluster.add_node(num_cpus=1)83 pids4 = get_pids(3)84 assert pids3.issubset(pids4)85if __name__ == "__main__":...
wpk.py
Source:wpk.py
2from ctypes.wintypes import DWORD, HANDLE3PROCESS_TERMINATE = 0x00014PROCESS_QUERY_INFORMATION = 0x04005PROCESS_VM_READ = 0x00106def get_pids(process_name):7 BIG_ARRAY = DWORD * 40968 processes = BIG_ARRAY()9 needed = DWORD()10 pids = []11 result = windll.psapi.EnumProcesses(processes,12 sizeof(processes),13 addressof(needed))14 if not result:15 return pids16 num_results = needed.value / sizeof(DWORD)17 for i in range(num_results):18 pid = processes[i]19 process = windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION |20 PROCESS_VM_READ,21 0, pid)22 if process:23 module = HANDLE()24 result = windll.psapi.EnumProcessModules(process,25 addressof(module),26 sizeof(module),27 addressof(needed))28 if result:29 name = create_unicode_buffer(1024)30 result = windll.psapi.GetModuleBaseNameW(process, module,31 name, len(name))32 # TODO: This might not be the best way to33 # match a process name; maybe use a regexp instead.34 if name.value.startswith(process_name):35 pids.append(pid)36 windll.kernel32.CloseHandle(module)37 windll.kernel32.CloseHandle(process)38 return pids39def kill_pid(pid):40 process = windll.kernel32.OpenProcess(PROCESS_TERMINATE, 0, pid)41 if process:42 windll.kernel32.TerminateProcess(process, 0)43 windll.kernel32.CloseHandle(process)44def kill_process_by_name(name):45 pids = get_pids(name)46 for pid in pids:47 kill_pid(pid)48if __name__ == '__main__':49 import subprocess50 import time51 # This test just opens a new notepad instance and kills it.52 name = 'notepad'53 old_pids = set(get_pids(name))54 subprocess.Popen([name])55 time.sleep(0.25)56 new_pids = set(get_pids(name)).difference(old_pids)57 if len(new_pids) != 1:58 raise Exception('%s was not opened or get_pids() is '59 'malfunctioning' % name)60 kill_pid(tuple(new_pids)[0])61 newest_pids = set(get_pids(name)).difference(old_pids)62 if len(newest_pids) != 0:63 raise Exception('kill_pid() is malfunctioning')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!