def test_get_pids(self, lib): "Tests get_pids() method." pids_uint = [ctypes.c_uint(pid) for pid in [1000, 1500, 3000, 5600]] pid_array = ctypes_build_array(pids_uint) def pqos_pid_get_pid_assoc_m(class_id, count_ref): "Mock pqos_pid_get_pid_assoc()." self.assertEqual(class_id, 7) ctypes_ref_set_uint(count_ref, len(pid_array)) return ctypes.cast(pid_array, ctypes.POINTER(ctypes.c_uint)) func_mock = MagicMock(side_effect=pqos_pid_get_pid_assoc_m) lib.pqos_pid_get_pid_assoc = func_mock alloc = PqosAlloc() with patch('pqos.allocation.free_memory'): pids = alloc.get_pids(7) lib.pqos_pid_get_pid_assoc.assert_called_once() self.assertEqual(len(pids), 4) self.assertEqual(pids[0], 1000) self.assertEqual(pids[1], 1500) self.assertEqual(pids[2], 3000) self.assertEqual(pids[3], 5600)
# Initialize PQoS library pqos = Pqos() pqos.init("OS") alloc = PqosAlloc() # Get PID of the current process pid = os.getpid() # Associate process with COS 1 print('Associating process %d with COS 1...' % pid) alloc.assoc_set_pid(pid, 1) # Get all processes associated with COS 1 print('Processes associated with COS 1:') print(alloc.get_pids(1)) # Release association print('Releasing association for process %d...' % pid) alloc.release_pid([pid]) # Current process should not be associated with COS 1 print('Processes associated with COS 1:') print(alloc.get_pids(1)) print('Is PID %d associated with COS 1?' % pid) print('Yes' if pid in alloc.get_pids(1) else 'No') # Finalize PQoS library pqos.fini()