def _run(self): # Start the program: self.pWrap = self._createPWrap([join(".", self.executionDirectory, self.executable)]) self._startPWrap(self.pWrap) # Wait for child being ready: printTester("Waiting for: 'Hello world!'") expected: str = "Hello world!" while True: if self.pWrap.hasTerminated() and not self.pWrap.canReadLineStdout(): self._progTerminatedUnexpectedly() # Read a single line form the program output: line: str = self.pWrap.readLineStdout() # Perform a "student save" compare: if studSaveStrComp(expected, line): break else: printTester(f"Expected '{expected}' but received read '{line}'") # Wait reading until the program terminates: printTester("Waiting for the program to terminate...") if not self.pWrap.waitUntilTerminationReading(3): printTester("Program did not terminate - killing it!") self.pWrap.kill() self._failWith("Program did not terminate at the end.") # Always cleanup to make sure all threads get joined: self.pWrap.cleanup()
def __matchChildHello(self, line: str): # Get PPID: ppid = self.pWrap.getPID() try: process: Process = Process(ppid) except NoSuchProcess: self.__progTerminatedUnexpectedly() # Get child PID: pidRegex = r"\D*(\d+)\D*(" + str(ppid) + r")" m = search(pidRegex, line) if m is None: printTester("No PID found in string: '{}'".format(line)) return False childPid = int(m.group(1)) # Child PID # Check if string read matches: expected: str = "I'm your child! PID: {}, PPID: {}".format(childPid, ppid) if not studSaveStrComp(expected, line): printTester("Expected: '{}' but received: '{}'".format(expected, line)) return False # Check if process exists for the child PID: if not self.__existsChildWithPid(childPid): printTester("No child process with PID {} exists".format(childPid)) return False return True
def __waitForInput(self, expected: str): printTester("Waiting for: '{}'".format(expected)) while True: if self.pWrap.hasTerminated( ) and not self.pWrap.canReadLineStdout(): self._progTerminatedUnexpectedly() # Read a single line form the programm output: line: str = self.pWrap.readLineStdout() # Perform a "student save" compare: if studSaveStrComp(expected, line): break else: printTester("Expected '{}' but received '{}'".format( expected, line))
def __matchChildBye(self, line: str, pids: List[int]): # Get child PID: pidRegex = r"\D*(\d+)\D*" m = search(pidRegex, line) if m is None: printTester("No PID found in string: '{}'".format(line)) return False childPid = int(m.group(1)) # Child PID # Check if the processes PID is one of those that said hello: if not childPid in pids: self._failWith("PID {} unknown. We are only aware of: {}".format(childPid, str(pids))) pids.remove(childPid) # Check if string read matches: expected: str = "Child with PID {} terminated.".format(childPid) if not studSaveStrComp(expected, line): printTester("Expected: '{}' but received: '{}'".format(expected, line)) return False return True
def _run(self): # Start the program: self.pWrap = self._createPWrap([join(".", self.makefileLocation, "main")]) self._startPWrap(self.pWrap) # Wait to make sure the child processes has started: sleep(0.5) if self.pWrap.hasTerminated(): self.__progTerminatedUnexpectedly() # Wait for child beeing ready: printTester("Waiting for: 'Enter process count:'") while True: if self.pWrap.hasTerminated(): self.__progTerminatedUnexpectedly() if studSaveStrComp("Enter process count:", self.pWrap.readLineStdout()): self.pWrap.writeStdin("{}\n".format(self.count)) break # Give the programm some time to start it's child processes: sleep(0.25) printTester("Waiting for: \"I'm your child! PID: %i, PPID: %i\\n\"") for i in range(0, self.count): while True: if self.pWrap.hasTerminated(): self.__progTerminatedUnexpectedly() if self.__matchChildHello(self.pWrap.readLineStdout()): break # Get all child process PIDs: pids: List[int] = self.__getChildPids() # Send 'END' to terminate processes: printTester("Sending 'END's...") for i in range(0, self.count): if self.pWrap.hasTerminated(): self.__progTerminatedUnexpectedly() # Check child process count: self.__testChildProcessCount(self.count - i) self.pWrap.writeStdin("END\n") sleep(0.25) printTester("Waiting for: \"Child with PID %i terminated.\\n\"") while True: if self.pWrap.hasTerminated() and not self.pWrap.canReadLineStdout(): self.__progTerminatedUnexpectedly() if self.__matchChildBye(self.pWrap.readLineStdout(), pids): break # Check if all children terminated: self.__testChildProcessCount(0) # Wait reading until the programm terminates: printTester("Waiting for the programm to terminate...") if(not self.pWrap.waitUntilTerminationReading(3)): printTester("Programm did not terminate - killing it!") pWrap.kill() if not self.pWrap.hasTerminated(): self._failWith("Programm did not terminate at the end.") # Allways cleanup to make sure all threads get joined: self.pWrap.cleanup()