def test_program_that_will_not_die(self): """ SubProcess for a process that will never stop """ # Set up our threaded process to sleep for 1 day sp = SubProcess(['sleep', '1d']) # Now run our process sp.start() # Wait half a second (give thread time to start and run) sp.join(timeout=0.5) # PID is always None unless the process is still running assert sp.pid() is not None assert isinstance(sp.pid(), int) # The process is still running assert sp.is_complete() is False # The process is not successful, but we shouldn't be relying on this # value anyway because it's not even complete. assert sp.successful() is False # time should be elapsing assert sp.elapsed() > 0.0 # No output to report at this time because one of the downsides of # using subprocess is you can't retrieve the pipes content until the # end. Thus these functions will simply return nothing. assert len(sp.stdout()) > 0 assert len(sp.stderr()) > 0 # Currently the response code is still unknown assert sp.response_code() is ReturnCode.Unknown # We'll abort the process now sp.abort() # Wait for it to complete (because it will complete now) sp.join() # PID is always None when process is stopped assert sp.pid() is None # The response code should have change to Aborted assert sp.response_code() is ReturnCode.Aborted # The process is now complete assert sp.is_complete() is True # But we should not be successful at this point assert sp.successful() is False
def test_simple_list(self): """ SubProcess for listing files """ # Set up our threaded process sp = SubProcess(['ls', '-1', '/tmp']) # Now run our process sp.start() # Wait for it to complete sp.join() assert sp.is_complete() is True assert sp.response_code() is 0 assert sp.successful() is True assert sp.elapsed() > 0.0 assert isinstance(sp.stdout(), list) assert isinstance(sp.stderr(), list) assert isinstance(sp.stdout(as_list=False), basestring) assert isinstance(sp.stderr(as_list=False), basestring) # Because under the hood stdout and stderr are stored into # Stream objects, we need to be able to check that multple # calls to the same object still return the same results assert len(sp.stdout()) > 1 assert len(sp.stdout()) > 1 assert len(sp.stderr()) > 0 assert len(sp.stderr()) > 0
def test_program_timeout(self): """ SubProcess for a process that will be killed simply because it took too long to execute. """ # Set up our threaded process to sleep for 1 day # Bu sp = SubProcess(['sleep', '1d'], timeout=1.0) # Now run our process sp.start() # Wait until it expires assert sp.is_complete(timeout=5.0) is True # PID is always None unless the process is still running assert sp.pid() is None # The process is not successful, but we shouldn't be relying on this # value anyway because it's not even complete. assert sp.successful() is False # time should be elapsing assert sp.elapsed() > 0.0 # No output to report at this time because one of the downsides of # using subprocess is you can't retrieve the pipes content until the # end. Thus these functions will simply return nothing. assert len(sp.stdout()) > 0 assert len(sp.stderr()) > 0 # A Timeout assert sp.response_code() is ReturnCode.Timeout
def test_bad_execution(self): """ SubProcess for listing files with bad arguments """ # Set up our threaded process sp = SubProcess(['ls', '-wtf', '-not-a-valid-argument']) # Now run our process sp.start() # Wait for it to complete sp.join() assert sp.is_complete() is True assert sp.response_code() is not 0 assert sp.successful() is False assert sp.elapsed() > 0.0 # Because under the hood stdout and stderr are stored into # Stream objects, we need to be able to check that multple # calls to the same object still return the same results assert len(sp.stdout()) > 0 assert len(sp.stdout()) > 0 assert len(sp.stderr()) > 1 assert len(sp.stderr()) > 1
def test(self, content=None): """ content must be pointing to a directory containing par files that can be easily sorted on. Alternatively, path can be of type NNTPContent() or a set/list of. This function just tests an archive to see if it can be properly prepared (it is effectively a wrapper to verify) If anything but True is returned then there was a problem verifying the results and a code identified in ParReturnCode() is returned instead. """ if content is not None: paths = self.get_paths(content) elif len(self.archive): # Get first item in archive paths = iter(self.archive) else: raise AttributeError("CodecPar: No par file detected.") if not self.can_exe(self._par): return None # filter our results by indexes indexes = self.__filter_pars(paths, indexes=True, volumes=False) if not len(indexes): logger.warning('Archive contained no PAR files.') return ParReturnCode.NoParFiles # Initialize our command execute = [ # Our Executable PAR Application self._par, # Use Test Flag 'verify', ] if self.cpu_cores is not None and self.cpu_cores > 1: # to checksum concurrently - uses multiple threads execute.append('-t+') # Stop Switch Parsing execute.append('--') for _path in indexes: # Get the directory the par file resides in par_path = dirname(_path) with pushd(par_path): # Create our SubProcess Instance sp = SubProcess(list(execute) + [basename(_path)]) # Start our execution now sp.start() sp.join() # Let the caller know our status if sp.response_code() is not ParReturnCode.NoRepairRequired: return sp.response_code() return True