Add test for listener being removed when terminate
Fix race condition in test