[fix] server closeing logic and add tests