lnd icon indicating copy to clipboard operation
lnd copied to clipboard

testing: `initSwitchWithDB` does not Close database

Open Crypt-iQ opened this issue 2 years ago • 4 comments

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.

Crypt-iQ avatar Jul 06 '22 20:07 Crypt-iQ

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? 🤔

mauricepoirrier avatar Jul 13 '22 02:07 mauricepoirrier

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

Crypt-iQ avatar Jul 13 '22 14:07 Crypt-iQ

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 avatar Aug 03 '22 03:08 kklash

@kklash that sounds like a good approach

Crypt-iQ avatar Aug 03 '22 15:08 Crypt-iQ