* Fixing uexpect.py close() method and forcing to close the process

by default
* Updating tests to be more robust
This commit is contained in:
Vitaliy Zakaznikov 2019-06-10 19:56:12 -04:00
parent 1ebe3047ff
commit 79989a766c
11 changed files with 32 additions and 28 deletions

View File

@ -17,7 +17,7 @@ import time
import sys
import re
from threading import Thread
from threading import Thread, Event
from subprocess import Popen
from Queue import Queue, Empty
@ -61,7 +61,7 @@ class IO(object):
def flush(self):
self._logger.flush()
def __init__(self, process, master, queue):
def __init__(self, process, master, queue, reader):
self.process = process
self.master = master
self.queue = queue
@ -70,6 +70,7 @@ class IO(object):
self.after = None
self.match = None
self.pattern = None
self.reader = reader
self._timeout = None
self._logger = None
self._eol = ''
@ -95,7 +96,11 @@ class IO(object):
self._eol = eol
return self._eol
def close(self):
def close(self, force=True):
self.reader['kill_event'].set()
if force:
self.process.kill()
else:
self.process.terminate()
os.close(self.master)
if self._logger:
@ -133,9 +138,9 @@ class IO(object):
raise exception
timeleft -= (time.time() - start_time)
if data:
self.buffer = self.buffer + data if self.buffer else data
self.buffer = (self.buffer + data) if self.buffer else data
self.match = pattern.search(self.buffer, 0)
if self.match:
if self.match is not None:
self.after = self.buffer[self.match.start():self.match.end()]
self.before = self.buffer[:self.match.start()]
self.buffer = self.buffer[self.match.end():]
@ -163,11 +168,9 @@ class IO(object):
if data:
return data
if raise_exception:
print 'DEBUG...', timeleft, repr(data),
raise TimeoutError(timeout)
pass
if not data and raise_exception:
print 'DEBUG.... here'
raise TimeoutError(timeout)
return data
@ -178,20 +181,21 @@ def spawn(command):
os.close(slave)
queue = Queue()
thread = Thread(target=reader, args=(process, master, queue))
thread.daemon = True
reader_kill_event = Event()
thread = Thread(target=reader, args=(process, master, queue, reader_kill_event))
thread.start()
return IO(process, master, queue)
return IO(process, master, queue, reader={'thread':thread, 'kill_event':reader_kill_event})
def reader(process, out, queue):
def reader(process, out, queue, kill_event):
while True:
if process.poll() is not None:
data = os.read(out)
queue.put(data)
break
try:
data = os.read(out, 65536)
queue.put(data)
except OSError, e:
if e.errno == 5 and kill_event.is_set():
break
raise
if __name__ == '__main__':
io = spawn(['/bin/bash','--noediting'])

View File

@ -17,7 +17,7 @@ def client(name=''):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>') as client2:
client1.expect(prompt)

View File

@ -17,7 +17,7 @@ def client(name=''):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>') as client2:
client1.expect(prompt)

View File

@ -17,7 +17,7 @@ def client(name=''):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>') as client2:
client1.expect(prompt)

View File

@ -17,7 +17,7 @@ def client(name=''):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>') as client2:
client1.expect(prompt)

View File

@ -17,7 +17,7 @@ def client(name=''):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>') as client2:
client1.expect(prompt)

View File

@ -17,7 +17,7 @@ def client(name=''):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>') as client2:
client1.expect(prompt)

View File

@ -20,7 +20,7 @@ def client(name='', command=None):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>', ['bash', '--noediting']) as client2:
client1.expect(prompt)

View File

@ -20,7 +20,7 @@ def client(name='', command=None):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>', ['bash', '--noediting']) as client2:
client1.expect(prompt)

View File

@ -20,7 +20,7 @@ def client(name='', command=None):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>', ['bash', '--noediting']) as client2:
client1.expect(prompt)

View File

@ -20,7 +20,7 @@ def client(name='', command=None):
return client
prompt = ':\) '
end_of_block = r'.*\xe2\x94\x82\r\n.*\xe2\x94\x98\r\n'
end_of_block = r'.*\r\n.*\r\n'
with client('client1>') as client1, client('client2>', ['bash', '--noediting']) as client2:
client1.expect(prompt)
@ -49,7 +49,7 @@ with client('client1>') as client1, client('client2>', ['bash', '--noediting'])
client2.expect('{"row":{"sum(a)":"6","_version":"2"}}\r\n', escape=True)
## send Ctrl-C
os.kill(client2.process.pid,signal.SIGINT)
os.kill(client2.process.pid, signal.SIGINT)
client1.send('DROP TABLE test.lv')
client1.expect(prompt)