CHOP-42. Cleanup and add tests for doAndRelease, doAndClose.

Signed-off-by: Kiyan Ahmadizadeh <kiyan@wibidata.com>