Fix a race condition when running the port allocator
Add tests of tcp port allocator