lnd
lnd copied to clipboard
testing: `initSwitchWithDB` does not Close database
The htlcswitch test function initSwitchWithDB
may create its own database if nil
is passed in. Several tests that pass in nil
never remove the temporary directory. I don't think any of the tests call db.Close
so the test process will still have the file handle. This leads to too many open files...
on my machine. The fix here is to remove the temp directory and call Close
on the database. This should probably be done by threading the temporary directory back to the calling test. This doesn't need to be done for the database itself since it can be accessed by the Switch's config struct.
Each newMockServer
also is generating new DBs in some cases. Might be useful to give only one function to handle init of databases and initSwitch and newMockServer
require it? 🤔
newMockServer
calls initSwitchWithDB
so there is still only one function generating the database if nil is passed in (no need for a refactor here imo).
From either the *Switch
returned from initSwitchWithDB
or the htlcSwitch
member from *mockServer
, it should be possible to access *Switch.cfg.DB
to close it and also to call Path()
on the database to remove the directory when the test finishes
Hi, first time contributor here :wave:. Gonna give this issue a try.
My initial approach is to ensure db.Close()
and os.RemoveAll(tempPath)
is done on any case where initSwitchWithDB
is called, including cases where nil
is passed, and also cases where db
is non-nil (there are cases where we don't properly clean up the temp files).
For functions which call initSwitchWithDB
, such as newMockServer
, we can use t.Cleanup()
to make our lives easier.
@kklash that sounds like a good approach