import contextlib
import socket


def pick_unused_port(host):
    """ Return a port that is currently unused.

    There is a race condition here in that the returned port can be grabbed
    by any other process. This should only be used for testing.
    """
    with contextlib.closing(
        socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ) as s:
        s.bind((host, 0))
        _, port = s.getsockname()
        return port