<commit_before>try:
    import queue
except ImportError:
    import Queue as queue
import random
import re
import socket
import threading
import time
import uuid

import pytest

import networkzero as nw0
_logger = nw0.core.get_logger("networkzero.tests")
nw0.core._enable_debug_logging()

is_valid_port = nw0.core.is_valid_port
is_valid_address = nw0.core.is_valid_address

class SupportThread(threading.Thread):
    """Fake the other end of the message/command/notification chain
    
    NB we use as little as possible of the nw0 machinery here,
    mostly to avoid the possibility of complicated cross-thread
    interference but also to test our own code.
    """
    
    def __init__(self, context):
        threading.Thread.__init__(self)
        self.context = context
        self.queue = queue.Queue()
        self.setDaemon(True)
    
    def run(self):
        try:
            while True:
                test_name, args = self.queue.get()
                if test_name is None:
                    break
                function = getattr(self, "support_test_" + test_name)
                function(*args)
        except:
            _logger.exception("Problem in thread")

    def support_test_discover_before_advertise(self, service):
        time.sleep(1)
        nw0.advertise(service)

@pytest.fixture
def support(request):
    thread = SupportThread(nw0.sockets.context)
    def finalise():
        thread.queue.put((None, None))
        thread.join()
    thread.start()
    return thread

@pytest.fixture
def beacon(request):
    port = random.choice(nw0.config.DYNAMIC_PORTS)
    nw0.discovery._start_beacon(port=port)
    request.addfinalizer(nw0.discovery._stop_beacon)

def test_beacon_already_running():
    #
    # NB this one has to run without the beacon fixture
    #
    # Bind a socket on a random port before attempting<commit_msg>Reset the beacon rather than stopping it, in the hope that it will avoid bind-related issues & delays on the CI tests<commit_after>try:
    import queue
except ImportError:
    import Queue as queue
import random
import re
import socket
import threading
import time
import uuid

import pytest

import networkzero as nw0
_logger = nw0.core.get_logger("networkzero.tests")
nw0.core._enable_debug_logging()

is_valid_port = nw0.core.is_valid_port
is_valid_address = nw0.core.is_valid_address

class SupportThread(threading.Thread):
    """Fake the other end of the message/command/notification chain
    
    NB we use as little as possible of the nw0 machinery here,
    mostly to avoid the possibility of complicated cross-thread
    interference but also to test our own code.
    """
    
    def __init__(self, context):
        threading.Thread.__init__(self)
        self.context = context
        self.queue = queue.Queue()
        self.setDaemon(True)
    
    def run(self):
        try:
            while True:
                test_name, args = self.queue.get()
                if test_name is None:
                    break
                function = getattr(self, "support_test_" + test_name)
                function(*args)
        except:
            _logger.exception("Problem in thread")

    def support_test_discover_before_advertise(self, service):
        time.sleep(1)
        nw0.advertise(service)

@pytest.fixture
def support(request):
    thread = SupportThread(nw0.sockets.context)
    def finalise():
        thread.queue.put((None, None))
        thread.join()
    thread.start()
    return thread

@pytest.fixture
def beacon(request):
    port = random.choice(nw0.config.DYNAMIC_PORTS)
    nw0.discovery._start_beacon(port=port)
    request.addfinalizer(nw0.discovery.reset_beacon)

def test_beacon_already_running():
    #
    # NB this one has to run without the beacon fixture
    #
    # Bind a socket on a random port before attempting
{'fraction_non_alphanumeric': 0.07802713987473904, 'fraction_numerical': 0.006263048016701462, 'mean_word_length': 2.754162585700294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1843283', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1126, 'n_words': 317}