rpc.py, test suite: close files and sockets properly

This commit is contained in:
The MMGen Project 2022-01-06 20:24:22 +00:00
commit c4cad63a4e
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 23 additions and 4 deletions

View file

@ -99,6 +99,9 @@ class RPCBackends:
class requests(base):
def __del__(self):
self.session.close()
def __init__(self,caller):
super().__init__(caller)
import requests,urllib3
@ -125,6 +128,9 @@ class RPCBackends:
class httplib(base):
def __del__(self):
self.session.close()
def __init__(self,caller):
super().__init__(caller)
import http.client
@ -156,7 +162,13 @@ class RPCBackends:
r = s.getresponse() # => http.client.HTTPResponse instance
except Exception as e:
raise RPCFailure(str(e))
return (r.read(),r.status)
if timeout:
ret = ( r.read(), r.status )
s.close()
return ret
else:
return ( r.read(), r.status )
class curl(base):

View file

@ -342,6 +342,7 @@ def dump_test(kg,ag,fh):
dump = [[*(e.split()[0] for e in line.split('addr='))] for line in fh.readlines() if 'addr=' in line]
if not dump:
die(1,f'File {fh.name!r} appears not to be a wallet dump')
fh.close()
m = 'Comparing output of address generator {!r} against wallet dump {!r}'
qmsg(green(m.format(kg.desc,fh.name)))

View file

@ -160,13 +160,14 @@ def init_coverage():
except: pass
return coverdir,acc_file
devnull_fh = open(('/dev/null','null.out')[g.platform == 'win'],'w')
def silence():
if not (opt.verbose or getattr(opt,'exact_output',None)):
g.stdout = g.stderr = devnull_fh
devnull_fn = ('/dev/null','null.out')[g.platform == 'win']
g.stdout = g.stderr = open(devnull_fn,'w')
def end_silence():
if not (opt.verbose or getattr(opt,'exact_output',None)):
g.stdout.close()
g.stdout = sys.stdout
g.stderr = sys.stderr

View file

@ -81,6 +81,8 @@ class MMGenPexpect(object):
self.expect('Comment: ',add_comment+'\n')
def ok(self):
self.p.sendeof()
self.p.read()
ret = self.p.wait()
if ret != self.req_exit_val and not opt.coverage:
die(1,red(f'test.py: spawned program exited with value {ret}'))

View file

@ -618,6 +618,10 @@ class CmdGroupMgr(object):
class TestSuiteRunner(object):
'test suite runner'
def __del__(self):
if opt.log:
self.log_fd.close()
def __init__(self,data_dir,trash_dir):
self.data_dir = data_dir
@ -949,7 +953,6 @@ class TestSuiteRunner(object):
def process_retval(self,cmd,ret):
if type(ret).__name__ == 'MMGenPexpect':
ret.read()
ret.ok()
self.cmd_total += 1
elif ret == 'ok':