Fix tests using too many connections or deadlock issue