import random
import pytest
from Pyro4.errors import NamingError
from osbrain.nameserver import NameServer
from osbrain.address import SocketAddress


@pytest.fixture(scope='function')
def nsaddr(request):
    while True:
        try:
            # Bind to random port
            host = '127.0.0.1'
            port = random.randrange(10000, 20000)
            addr = SocketAddress(host, port)
            nameserver = NameServer(addr)
            def terminate():
                nameserver.shutdown()
            request.addfinalizer(terminate)
            nameserver.start()
            return addr
        except NamingError:
            continue
        except PermissionError:
            continue
        except:
            raise

import random
import pytest
from Pyro4.errors import NamingError
from osbrain.nameserver import NameServer
from osbrain.address import SocketAddress


@pytest.yield_fixture(scope='function')
def nsaddr(request):
    while True:
        try:
            # Bind to random port
            host = '127.0.0.1'
            port = random.randrange(10000, 20000)
            addr = SocketAddress(host, port)
            nameserver = NameServer(addr)
            nameserver.start()
            break
        except RuntimeError:
            continue
        except:
            raise
    yield addr
    nameserver.shutdown()

Use `yield_fixture` instead of classic `fixture` with pytest
Use `yield_fixture` instead of classic `fixture` with pytest

  import random
  import pytest
  from Pyro4.errors import NamingError
  from osbrain.nameserver import NameServer
  from osbrain.address import SocketAddress
  
  
- @pytest.fixture(scope='function')
+ @pytest.yield_fixture(scope='function')
  def nsaddr(request):
      while True:
          try:
              # Bind to random port
              host = '127.0.0.1'
              port = random.randrange(10000, 20000)
              addr = SocketAddress(host, port)
              nameserver = NameServer(addr)
-             def terminate():
-                 nameserver.shutdown()
-             request.addfinalizer(terminate)
              nameserver.start()
-             return addr
+             break
-         except NamingError:
+         except RuntimeError:
-             continue
-         except PermissionError:
              continue
          except:
              raise
+     yield addr
+     nameserver.shutdown()
  
Use `yield_fixture` instead of classic `fixture` with pytest
## Code Before:
import random
import pytest
from Pyro4.errors import NamingError
from osbrain.nameserver import NameServer
from osbrain.address import SocketAddress


@pytest.fixture(scope='function')
def nsaddr(request):
    while True:
        try:
            # Bind to random port
            host = '127.0.0.1'
            port = random.randrange(10000, 20000)
            addr = SocketAddress(host, port)
            nameserver = NameServer(addr)
            def terminate():
                nameserver.shutdown()
            request.addfinalizer(terminate)
            nameserver.start()
            return addr
        except NamingError:
            continue
        except PermissionError:
            continue
        except:
            raise

## Instruction:
Use `yield_fixture` instead of classic `fixture` with pytest
## Code After:
import random
import pytest
from Pyro4.errors import NamingError
from osbrain.nameserver import NameServer
from osbrain.address import SocketAddress


@pytest.yield_fixture(scope='function')
def nsaddr(request):
    while True:
        try:
            # Bind to random port
            host = '127.0.0.1'
            port = random.randrange(10000, 20000)
            addr = SocketAddress(host, port)
            nameserver = NameServer(addr)
            nameserver.start()
            break
        except RuntimeError:
            continue
        except:
            raise
    yield addr
    nameserver.shutdown()
