33from decimal import Decimal
44from fixtures import * # noqa: F401,F403
55from fixtures import LightningNode , TEST_NETWORK
6+ from pathlib import Path
67from pyln .client import RpcError , Millisatoshi
78from threading import Event
89from pyln .testing .utils import (
@@ -1899,11 +1900,13 @@ def test_logging(node_factory):
18991900 wait_for (lambda : os .path .exists (logpath_moved ))
19001901 wait_for (lambda : os .path .exists (logpath ))
19011902
1902- log1 = open (logpath_moved ).readlines ()
1903+ with open (logpath_moved ) as f :
1904+ log1 = f .readlines ()
19031905 assert log1 [- 1 ].endswith ("Ending log due to SIGHUP\n " )
19041906
19051907 def check_new_log ():
1906- log2 = open (logpath ).readlines ()
1908+ with open (logpath ) as f :
1909+ log2 = f .readlines ()
19071910 return len (log2 ) > 0 and log2 [0 ].endswith ("Started log due to SIGHUP\n " )
19081911 wait_for (check_new_log )
19091912
@@ -3106,7 +3109,7 @@ def test_recover_plugin(node_factory, bitcoind):
31063109
31073110 # Save copy of the db.
31083111 dbpath = os .path .join (l2 .daemon .lightning_dir , TEST_NETWORK , "lightningd.sqlite3" )
3109- orig_db = open (dbpath , "rb" ). read ()
3112+ orig_db = Path (dbpath ). read_bytes ()
31103113
31113114 l2 .start ()
31123115
@@ -3121,7 +3124,7 @@ def test_recover_plugin(node_factory, bitcoind):
31213124 l2 .stop ()
31223125
31233126 # Overwrite with OLD db.
3124- open (dbpath , "wb" ). write (orig_db )
3127+ Path (dbpath ). write_bytes (orig_db )
31253128
31263129 l2 .start ()
31273130
@@ -4917,45 +4920,46 @@ def test_tracing(node_factory):
49174920 traces = set ()
49184921 suspended = set ()
49194922 for fname in glob .glob (f"{ trace_fnamebase } .*" ):
4920- for linenum , l in enumerate (open (fname , "rt" ).readlines (), 1 ):
4921- # In case an assertion fails
4922- print (f"Parsing { fname } :{ linenum } " )
4923- parts = l .split (maxsplit = 2 )
4924- cmd = parts [0 ]
4925- spanid = parts [1 ]
4926- if cmd == 'span_emit' :
4927- assert spanid in traces
4928- assert spanid not in suspended
4929- # Should be valid JSON
4930- res = json .loads (parts [2 ])
4931-
4932- # This is an array for some reason
4933- assert len (res ) == 1
4934- res = res [0 ]
4935- assert res ['id' ] == spanid
4936- assert res ['localEndpoint' ] == {"serviceName" : "lightningd" }
4937- expected_keys = ['id' , 'name' , 'timestamp' , 'duration' , 'tags' , 'traceId' , 'localEndpoint' ]
4938- if 'parentId' in res :
4939- assert res ['parentId' ] in traces
4940- expected_keys .append ('parentId' )
4941- assert set (res .keys ()) == set (expected_keys )
4942- traces .remove (spanid )
4943- elif cmd == 'span_end' :
4944- assert spanid in traces
4945- elif cmd == 'span_start' :
4946- assert spanid not in traces
4947- traces .add (spanid )
4948- elif cmd == 'span_suspend' :
4949- assert spanid in traces
4950- assert spanid not in suspended
4951- suspended .add (spanid )
4952- elif cmd == 'span_resume' :
4953- assert spanid in traces
4954- suspended .remove (spanid )
4955- elif cmd == 'destroying' :
4956- pass
4957- else :
4958- assert False , "Unknown trace line"
4923+ with open (fname , "rt" ) as f :
4924+ for linenum , l in enumerate (f .readlines (), 1 ):
4925+ # In case an assertion fails
4926+ print (f"Parsing { fname } :{ linenum } " )
4927+ parts = l .split (maxsplit = 2 )
4928+ cmd = parts [0 ]
4929+ spanid = parts [1 ]
4930+ if cmd == 'span_emit' :
4931+ assert spanid in traces
4932+ assert spanid not in suspended
4933+ # Should be valid JSON
4934+ res = json .loads (parts [2 ])
4935+
4936+ # This is an array for some reason
4937+ assert len (res ) == 1
4938+ res = res [0 ]
4939+ assert res ['id' ] == spanid
4940+ assert res ['localEndpoint' ] == {"serviceName" : "lightningd" }
4941+ expected_keys = ['id' , 'name' , 'timestamp' , 'duration' , 'tags' , 'traceId' , 'localEndpoint' ]
4942+ if 'parentId' in res :
4943+ assert res ['parentId' ] in traces
4944+ expected_keys .append ('parentId' )
4945+ assert set (res .keys ()) == set (expected_keys )
4946+ traces .remove (spanid )
4947+ elif cmd == 'span_end' :
4948+ assert spanid in traces
4949+ elif cmd == 'span_start' :
4950+ assert spanid not in traces
4951+ traces .add (spanid )
4952+ elif cmd == 'span_suspend' :
4953+ assert spanid in traces
4954+ assert spanid not in suspended
4955+ suspended .add (spanid )
4956+ elif cmd == 'span_resume' :
4957+ assert spanid in traces
4958+ suspended .remove (spanid )
4959+ elif cmd == 'destroying' :
4960+ pass
4961+ else :
4962+ assert False , "Unknown trace line"
49594963
49604964 assert suspended == set ()
49614965 assert traces == set ()
@@ -4969,20 +4973,21 @@ def test_tracing(node_factory):
49694973
49704974 # The parent should set all the trace ids and span ids
49714975 for fname in glob .glob (f"{ trace_fnamebase } .*" ):
4972- for linenum , l in enumerate (open (fname , "rt" ).readlines (), 1 ):
4973- # In case an assertion fails
4974- print (f"Parsing { fname } :{ linenum } " )
4975- parts = l .split (maxsplit = 2 )
4976- cmd = parts [0 ]
4977- spanid = parts [1 ]
4978- # This span doesn't actually appear anywhere
4979- assert spanid != '0123456789abcdef'
4980- if cmd == 'span_emit' :
4981- # Should be valid JSON
4982- res = json .loads (parts [2 ])
4983- assert res [0 ]['traceId' ] == '00112233445566778899aabbccddeeff'
4984- # Everyone has a parent!
4985- assert 'parentId' in res [0 ]
4976+ with open (fname , "rt" ) as f :
4977+ for linenum , l in enumerate (f .readlines (), 1 ):
4978+ # In case an assertion fails
4979+ print (f"Parsing { fname } :{ linenum } " )
4980+ parts = l .split (maxsplit = 2 )
4981+ cmd = parts [0 ]
4982+ spanid = parts [1 ]
4983+ # This span doesn't actually appear anywhere
4984+ assert spanid != '0123456789abcdef'
4985+ if cmd == 'span_emit' :
4986+ # Should be valid JSON
4987+ res = json .loads (parts [2 ])
4988+ assert res [0 ]['traceId' ] == '00112233445566778899aabbccddeeff'
4989+ # Everyone has a parent!
4990+ assert 'parentId' in res [0 ]
49864991
49874992
49884993def test_zero_locktime_blocks (node_factory , bitcoind ):
0 commit comments