Browse Source

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

The MMGen Project 3 years ago
parent
commit
c4cad63a4e
5 changed files with 23 additions and 4 deletions
  1. 13 1
      mmgen/rpc.py
  2. 1 0
      test/gentest.py
  3. 3 2
      test/include/common.py
  4. 2 0
      test/include/pexpect.py
  5. 4 1
      test/test.py

+ 13 - 1
mmgen/rpc.py

@@ -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):
 

+ 1 - 0
test/gentest.py

@@ -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)))

+ 3 - 2
test/include/common.py

@@ -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
 

+ 2 - 0
test/include/pexpect.py

@@ -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}'))

+ 4 - 1
test/test.py

@@ -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':