<commit_before>#!/usr/bin/env python

import pytest

from time import sleep
from threading import Thread
from errno import ECONNREFUSED
from subprocess import Popen

from circuits.net.sockets import UDPServer, Close

from .helpers import urlopen, URLError, HTTPError

SERVER_CMD = ["python", "-m", "circuits.web.main"]


def find_free_port():
    server = UDPServer(0)

    server.start()
    waiter = pytest.WaitEvent(server, "ready")
    waiter.wait()

    port = server.port

    server.fire(Close())
    waiter = pytest.WaitEvent(server, "disconnected")
    waiter.wait()

    server.stop()

    return port


class Server(Thread):

    def __init__(self, *args):
        super(Server, self).__init__()

        self.args = list(args)

        self.setDaemon(True)

    def run(self):
        self.process = Popen(SERVER_CMD + self.args)

    def stop(self):
        self.process.terminate()
        self.process.wait()


def test():
    port = find_free_port()

    server = Server("-b", "0.0.0.0:%d" % port)
    server.start()

    sleep(1)

    f = None

    for _ in range(3):
        try:
            f = urlopen("http://127.0.0.1:%d/hello" % port)
            break
        except HTTPError as e:
            raise AssertionError(e)
        except URLError as e:
            if e.args[0][0] == ECONNREFUSED:
                sleep(1)
            else:
                raise AssertionError(e)

    assert f

    s = f.read()
    assert s == b"Hello World!"

    server.stop()<commit_msg>Fixed ``tests/web/test_main.py`` which was badly written.<commit_after>#!/usr/bin/env python

import pytest

from time import sleep
from subprocess import Popen
from errno import ECONNREFUSED

from circuits.net.sockets import UDPServer, Close

from .helpers import urlopen, URLError, HTTPError


def find_free_port():
    server = UDPServer(0)

    server.start()
    waiter = pytest.WaitEvent(server, "ready")
    waiter.wait()

    port = server.port

    server.fire(Close())
    waiter = pytest.WaitEvent(server, "disconnected")
    waiter.wait()

    server.stop()

    return port


def test():
    port = find_free_port()

    p = Popen(["python", "-m", "circuits.web.main", "-b", "0.0.0.0:%d" % port])

    sleep(1)

    f = None

    for _ in range(3):
        try:
            f = urlopen("http://127.0.0.1:%d/hello" % port)
            break
        except HTTPError as e:
            raise AssertionError(e)
        except URLError as e:
            if e.args[0][0] == ECONNREFUSED:
                sleep(1)
            else:
                raise AssertionError(e)

    assert f

    s = f.read()
    assert s == b"Hello World!"

    p.terminate()
    p.wait()
{'fraction_non_alphanumeric': 0.12481259370314843, 'fraction_numerical': 0.01199400299850075, 'mean_word_length': 2.2351515151515153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '2450020', 'n_tokens_mistral': 925, 'n_tokens_neox': 887, 'n_words': 218}