def expect(self, pattern, raise_on_error=True, title=""): self.trace("expect " + pattern) r = re.compile(pattern, re.I) refresh_cnt = 0 while True: line = self.proc.stdout.readline() if line == "": raise inc.TestError(self.name + ": Premature EOF") # Print the line if echo is ON if self.echo: print self.name + ": " + line, # Trap assertion error if self.ra.search(line) != None: if raise_on_error: raise inc.TestError(self.name + ": " + line) else: return None # Count stdout refresh text. if self.rr.search(line) != None: refresh_cnt = refresh_cnt+1 if refresh_cnt >= 6: self.trace("Timed-out!") if raise_on_error: raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"") else: return None # timeout # Search for expected text if r.search(line) != None: return line
def expect(self, pattern, raise_on_error=True, title=""): # no prompt for telnet if self.use_telnet and pattern == const.PROMPT: return self.trace("expect " + pattern) r = re.compile(pattern, re.I) found_at = -1 t0 = time.time() while found_at < 0: self.lock.acquire() lines = self.output.splitlines() for i, line in enumerate(lines): # Search for expected text if r.search(line) != None: found_at = i break # Trap assertion error if raise_on_error: if self.ra.search(line) != None: self.lock.release() raise inc.TestError(self.name + ": " + line) self.output = '\n'.join(lines[found_at + 1:]) if found_at >= 0 else "" self.lock.release() if found_at >= 0: return line if not self.running: if raise_on_error: raise inc.TestError(self.name + ": Premature EOF") break else: t1 = time.time() dur = int(t1 - t0) if dur > 15: self.trace("Timed-out!") if raise_on_error: raise inc.TestError(self.name + " " + title + ": Timeout expecting pattern: \"" + pattern + "\"") break else: time.sleep(0.01) return None
def run(self): if self.use_telnet: fullcmd = G_EXE + " " + self.inst_param.arg + " --use-cli --no-cli-console --cli-telnet-port=%d" % (self.inst_param.telnet_port) self.trace("Popen " + fullcmd) self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX) # start telnet-ing to pjsua, raise exception if telnet fails after 5s t0 = time.time() while self.proc.poll() is None and self.telnet is None: try: time.sleep(0.01) self.telnet = telnetlib.Telnet('127.0.0.1', port=self.inst_param.telnet_port, timeout=60) except Exception as e: t1 = time.time() dur = int(t1 - t0) if dur > 5: raise inc.TestError(self.name + ": Timeout connecting to pjsua: " + repr(e)) self.running = True while self.proc.poll() is None: line = self.telnet.read_until('\n', 60) if line == "" or const.DESTROYED in line: break; #Print the line if echo is ON if self.echo: print self.name + ": " + line.rstrip() self.lock.acquire() self.output += line self.lock.release() self.running = False else: fullcmd = G_EXE + " " + self.inst_param.arg + " --stdout-refresh=5 --stdout-refresh-text=" + const.STDOUT_REFRESH if not self.inst_param.enable_buffer: fullcmd = fullcmd + " --stdout-no-buf" self.trace("Popen " + fullcmd) self.proc = subprocess.Popen(fullcmd, shell=G_INUNIX, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=False) self.running = True while self.proc.poll() is None: line = self.proc.stdout.readline() if line == "": break; #Print the line if echo is ON if self.echo: print self.name + ": " + line.rstrip() self.lock.acquire() self.output += line self.lock.release() self.running = False