Package pysys :: Package process :: Module user :: Class ProcessUser
[hide private]
[frames] | no frames]

Class ProcessUser

source code

Known Subclasses:

Class providing basic operations over interacting with processes.

The ProcessUser class provides the mimimum set of operations for managing and interacting with processes. The class is designed to be extended by the pysys.baserunner.BaseRunner and pysys.basetest.BaseTest classes so that they prescribe a common set of process operations that any application helper classes can use, i.e. where an application helper class is instantiated with a call back reference to the runner or base test for the process operations.

Instance Methods [hide private]
 
__init__(self)
Default constructor.
source code
 
__getattr__(self, name)
Set self.input or self.output to the current working directory if not defined.
source code
integer
getInstanceCount(self, displayName)
Return the number of processes started within the testcase matching the supplied displayName.
source code
handle
startProcess(self, command, arguments, environs={}, workingDir=None, state=11, timeout=None, stdout=None, stderr=None, displayName=None)
Start a process running in the foreground or background, and return the process handle.
source code
 
stopProcess(self, process)
Send a soft or hard kill to a running process to stop its execution.
source code
 
signalProcess(self, process, signal)
Send a signal to a running process (Unix only).
source code
 
waitProcess(self, process, timeout)
Wait for a process to terminate, return on termination or expiry of the timeout.
source code
 
writeProcess(self, process, data, addNewLine=True)
Write data to the stdin of a process.
source code
 
waitForSocket(self, port, host='localhost', timeout=60)
Wait for a socket connection to be established.
source code
 
waitForFile(self, file, filedir=None, timeout=30)
Wait for a file to be written to disk.
source code
 
waitForSignal(self, file, filedir=None, expr='', condition='>=1', timeout=60, poll=0.25)
Wait for a particular regular expression to be seen on a set number of lines in a text file.
source code
 
__del__(self)
Class destructor which stops any running processes started by the class instance.
source code
Instance Variables [hide private]
string input
Location for input to any processes (defaults to current working directory)
string output
Location for output from any processes (defaults to current working directory)
Method Details [hide private]

getInstanceCount(self, displayName)

source code 

Return the number of processes started within the testcase matching the supplied displayName.

The ProcessUserInterface class maintains a reference count of processes started within the class instance via the startProcess() method. The reference count is maintained against a logical name for the process, which is the displayName used in the method call to startProcess(), or the basename of the command if no displayName was supplied. The method returns the number of processes started with the supplied logical name, or 0 if no processes have been started.

Parameters:
  • displayName - The process display name
Returns: integer
The number of processes started matching the command basename

startProcess(self, command, arguments, environs={}, workingDir=None, state=11, timeout=None, stdout=None, stderr=None, displayName=None)

source code 

Start a process running in the foreground or background, and return the process handle.

The method allows spawning of new processes in a platform independent way. The command, arguments, environment and working directory to run the process in can all be specified in the arguments to the method, along with the filenames used for capturing the stdout and stderr of the process. Processes may be started in the FOREGROUND, in which case the method does not return until the process has completed or a time out occurs, or in the BACKGROUND in which case the method returns immediately to the caller returning a handle to the process to allow manipulation at a later stage. All processes started in the BACKGROUND and not explicitly killed using the returned process handle are automatically killed on completion of the test via the __del__() destructor.

Parameters:
  • command - The command to start the process (should include the full path)
  • arguments - A list of arguments to pass to the command
  • environs - A dictionary of the environment to run the process in (defaults to clean environment)
  • workingDir - The working directory for the process to run in (defaults to the testcase output subdirectory)
  • state - Run the process either in the FOREGROUND or BACKGROUND (defaults to FOREGROUND)
  • timeout - The timeout period after which to termintate processes running in the FOREGROUND
  • stdout - The filename used to capture the stdout of the process
  • stderr - The filename user to capture the stderr of the process
  • displayName - Logical name of the process used for display and reference counting (defaults to the basename of the command)
Returns: handle
The process handle of the process (pysys.process.helper.ProcessWrapper)

stopProcess(self, process)

source code 

Send a soft or hard kill to a running process to stop its execution.

This method uses the pysys.process.helper module to stop a running process.

Parameters:
  • process - The process handle returned from the startProcess() method

signalProcess(self, process, signal)

source code 

Send a signal to a running process (Unix only).

This method uses the pysys.process.helper module to send a signal to a running process.

Parameters:
  • process - The process handle returned from the startProcess() method
  • signal - The integer value of the signal to send

waitProcess(self, process, timeout)

source code 

Wait for a process to terminate, return on termination or expiry of the timeout.

Parameters:
  • process - The process handle returned from the startProcess() method
  • timeout - The timeout value in seconds to wait before returning

writeProcess(self, process, data, addNewLine=True)

source code 

Write data to the stdin of a process.

This method uses the pysys.process.helper module to write a data string to the stdin of a process. This wrapper around the write method of the process helper only adds checking of the process running status prior to the write being performed, and logging to the testcase run log to detail the write.

Parameters:
  • process - The process handle returned from the startProcess() method
  • data - The data to write to the process
  • addNewLine - True if a new line character is to be added to the end of the data string

waitForSocket(self, port, host='localhost', timeout=60)

source code 

Wait for a socket connection to be established.

This method blocks until connection to a particular host:port pair can be established. This is useful for test timing where a component under test creates a socket for client server interaction - calling of this method ensures that on return of the method call the server process is running and a client is able to create connections to it. If a connection cannot be made within the specified timeout interval, the method returns to the caller.

Parameters:
  • port - The port value in the socket host:port pair
  • host - The host value in the socket host:port pair
  • timeout - The timeout in seconds to wait for connection to the socket

waitForFile(self, file, filedir=None, timeout=30)

source code 

Wait for a file to be written to disk.

This method blocks until a file is created on disk. This is useful for test timing where a component under test creates a file (e.g. for logging) indicating it has performed all initialisation actions and is ready for the test execution steps. If a file is not created on disk within the specified timeout interval, the method returns to the caller.

Parameters:
  • file - The basename of the file used to wait to be created
  • filedir - The dirname of the file (defaults to the testcase output subdirectory)
  • timeout - The timeout in seconds to wait for the file to be created

waitForSignal(self, file, filedir=None, expr='', condition='>=1', timeout=60, poll=0.25)

source code 

Wait for a particular regular expression to be seen on a set number of lines in a text file.

This method blocks until a particular regular expression is seen in a text file on a set number of lines. The number of lines which should match the regular expression is given by the condition argument in textual form i.e. for a match on more than 2 lines use condition =">2". If the regular expression is not seen in the file matching the supplied condition within the specified timeout interval, the method returns to the caller.

Parameters:
  • file - The basename of the file used to wait for the signal
  • filedir - The dirname of the file (defaults to the testcase output subdirectory)
  • expr - The regular expression to search for in the text file
  • condition - The condition to be met for the number of lines matching the regular expression
  • timeout - The timeout in seconds to wait for the regular expression and to check against the condition
  • poll - The time in seconds to poll the file looking for the regular expression and to check against the condition