# Path: django_prometheus/exports.py
# def SetupPrometheusEndpointOnPortRange(port_range, addr=""):
#     """Like SetupPrometheusEndpointOnPort, but tries several ports.
# 
#     This is useful when you're running Django as a WSGI application
#     with multiple processes and you want Prometheus to discover all
#     workers. Each worker will grab a port and you can use Prometheus
#     to aggregate across workers.
# 
#     port_range may be any iterable object that contains a list of
#     ports. Typically this would be a `range` of contiguous ports.
# 
#     As soon as one port is found that can serve, use this one and stop
#     trying.
# 
#     Returns the port chosen (an `int`), or `None` if no port in the
#     supplied range was available.
# 
#     The same caveats regarding autoreload apply. Do not use this when
#     Django's autoreloader is active.
#     """
#     assert os.environ.get("RUN_MAIN") != "true", (
#         "The thread-based exporter can't be safely used when django's "
#         "autoreloader is active. Use the URL exporter, or start django "
#         "with --noreload. See documentation/exports.md."
#     )
#     for port in port_range:
#         try:
#             httpd = HTTPServer((addr, port), prometheus_client.MetricsHandler)
#         except OSError:
#             # Python 2 raises socket.error, in Python 3 socket.error is an
#             # alias for OSError
#             continue  # Try next port
#         thread = PrometheusEndpointServer(httpd)
#         thread.daemon = True
#         thread.start()
#         logger.info("Exporting Prometheus /metrics/ on port %s" % port)
#         return port  # Stop trying ports at this point
#     logger.warning(
#         "Cannot export Prometheus /metrics/ - " "no available ports in supplied range"
#     )
#     return None


import socket
from unittest.mock import ANY, MagicMock, call, patch
from django_prometheus.exports import SetupPrometheusEndpointOnPortRange
#!/usr/bin/env python



@patch("django_prometheus.exports.HTTPServer")
def test_port_range_available(httpserver_mock):
    """Test port range setup with an available port."""
    httpserver_mock.side_effect = [socket.error, MagicMock()]
    port_range = [8000, 8001]

# Path: django_prometheus/exports.py
# def SetupPrometheusEndpointOnPortRange(port_range, addr=""):
#     """Like SetupPrometheusEndpointOnPort, but tries several ports.
# 
#     This is useful when you're running Django as a WSGI application
#     with multiple processes and you want Prometheus to discover all
#     workers. Each worker will grab a port and you can use Prometheus
#     to aggregate across workers.
# 
#     port_range may be any iterable object that contains a list of
#     ports. Typically this would be a `range` of contiguous ports.
# 
#     As soon as one port is found that can serve, use this one and stop
#     trying.
# 
#     Returns the port chosen (an `int`), or `None` if no port in the
#     supplied range was available.
# 
#     The same caveats regarding autoreload apply. Do not use this when
#     Django's autoreloader is active.
#     """
#     assert os.environ.get("RUN_MAIN") != "true", (
#         "The thread-based exporter can't be safely used when django's "
#         "autoreloader is active. Use the URL exporter, or start django "
#         "with --noreload. See documentation/exports.md."
#     )
#     for port in port_range:
#         try:
#             httpd = HTTPServer((addr, port), prometheus_client.MetricsHandler)
#         except OSError:
#             # Python 2 raises socket.error, in Python 3 socket.error is an
#             # alias for OSError
#             continue  # Try next port
#         thread = PrometheusEndpointServer(httpd)
#         thread.daemon = True
#         thread.start()
#         logger.info("Exporting Prometheus /metrics/ on port %s" % port)
#         return port  # Stop trying ports at this point
#     logger.warning(
#         "Cannot export Prometheus /metrics/ - " "no available ports in supplied range"
#     )
#     return None


# Path: django_prometheus/tests/test_exports.py
import socket
from unittest.mock import ANY, MagicMock, call, patch
from django_prometheus.exports import SetupPrometheusEndpointOnPortRange

#!/usr/bin/env python



@patch("django_prometheus.exports.HTTPServer")
def test_port_range_available(httpserver_mock):
    """Test port range setup with an available port."""
    httpserver_mock.side_effect = [socket.error, MagicMock()]
    port_range = [8000, 8001]

    port_chosen = SetupPrometheusEndpointOnPortRange(port_range)