Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify two different ports by unused_tcp_port fixture #10

Closed
asvetlov opened this issue Jul 23, 2015 · 12 comments
Closed

How to specify two different ports by unused_tcp_port fixture #10

asvetlov opened this issue Jul 23, 2015 · 12 comments
Assignees

Comments

@asvetlov
Copy link
Contributor

I have functional tests for my system which requires several components running on different ports
As I see unused_tcp_port has functional scope, so it's impossible to acquire two different ports (for different purposes, sure) in the same test.
Or maybe I missed something?

@Tinche
Copy link
Member

Tinche commented Jul 23, 2015

Nope, that's the current limitation. I'm not aware for any smaller scope than function, so I suppose we have two options here:

  • create a free port factory fixture (but then it's not as nice to use)
  • create more fixtures, unused_tcp_port_{1, 2, 3...}

Any ideas?

@nicoddemus
Copy link
Member

I think creating a port factory fixture is a nice approach, while keeping unused_tcp_port around to support the common case of needing a single port.

@asvetlov, a quick workaround you can use meanwhile is to create two intermediate fixtures:

@pytest.fixture
def port1(unused_tcp_port):
    return unused_tcp_port

@pytest.fixture
def port2(unused_tcp_port):
    return unused_tcp_port

def test_foo(port1, port2):
   ...

@asvetlov
Copy link
Contributor Author

@nicoddemus please describe what do you mean under factory fixture?
Sorry, I'm a newbie in pytest.

Isn't your workaroud return the same value for port1 and port2?

@nicoddemus
Copy link
Member

please describe what do you mean under factory fixture?

Oh it's not a pytest term, I just meant a factory in the general sense. Something like this would work nicely IMO:

def test_foo(unused_port_factory):
    port1 = unused_port_factory.get_port()
    port2 = unused_port_factory.get_port()

Isn't your workaroud return the same value for port1 and port2?

Argh, of course it is, my bad. Please disregard this. 😅

@asvetlov
Copy link
Contributor Author

Is unused_port_factory a fixture? Can fixture have a methods?
If it's not a fixture should I import the name?

@Tinche
Copy link
Member

Tinche commented Jul 29, 2015

The idea would be to create it as a fixture, yeah. Fixtures can be any object you like, so yes, they can have methods :)

@Tinche Tinche self-assigned this Jul 29, 2015
@nicoddemus
Copy link
Member

The idea would be to create it as a fixture, yeah.

Yes, sorry for not being more clear. 😄

@Tinche
Copy link
Member

Tinche commented Jul 29, 2015

I'll get this done over the weekend. Beside get_port (I think I prefer next_port instead, so it's clearer it can be called n times and is not idempotent), a method for generating several ports at once might be useful. Like this:

p1, p2, p3 = unused_port_factory.next_ports(3)

@nicoddemus
Copy link
Member

I think I prefer next_port instead

👍

a method for generating several ports at once might be useful

Also 👍

@asvetlov
Copy link
Contributor Author

Sounds perfect!

@Tinche
Copy link
Member

Tinche commented Aug 2, 2015

I've just pushed out 0.2.0 to PyPI, which includes the unused_tcp_port_factory fixture. The end result is slightly simpler than what we discussed here (see the README), but it should suffice for this use case.

@asvetlov
Copy link
Contributor Author

asvetlov commented Aug 2, 2015

I believe you even don't need produced set -- bound port will be inaccessible for reusing for a while unless SO_REUSEADDR specified.

@Tinche Tinche closed this as completed Nov 15, 2015
seifertm pushed a commit that referenced this issue Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants