Refactoring tests to use ephemeral port

Closes #376