<commit_before>import socket

from pytest_django.live_server_helper import parse_addr
from django.test.testcases import LiveServerThread as LiveServerThreadBase
from django.db import connections
from tornado import ioloop

from keybar.server import get_server


class LiveServerThread(LiveServerThreadBase):
    ports = range(49152, 65535)

    def run(self):
        if self.connections_override:
            # Override this thread's database connections with the ones
            # provided by the main thread.
            for alias, conn in self.connections_override.items():
                connections[alias] = conn
        try:
            # Go through the list of possible ports, hoping that we can find
            # one that is free to use for the WSGI server.
            try:
                server = get_server(debug=False)
                for port in self.ports:
                    try:
                        server.listen(port, 'keybar.local')
                    except OSError as exc:
                        if port == 65535:
                            raise exc
            except socket.error as exc:
                raise exc
            else:
                self.port = port

            self.is_ready.set()
            self.loop = ioloop.IOLoop.instance()
            self.loop.start()
        except Exception as exc:
            self.terminate()
            self.error = exc
            self.is_ready.set()<commit_msg>More sophisticated port-ranges, better error handling<commit_after>import socket

from pytest_django.live_server_helper import parse_addr
from django.test.testcases import LiveServerThread as LiveServerThreadBase
from django.db import connections
from tornado import ioloop

from keybar.server import get_server


class LiveServerThread(LiveServerThreadBase):
    ports = range(8990, 9999)

    def run(self):
        if self.connections_override:
            # Override this thread's database connections with the ones
            # provided by the main thread.
            for alias, conn in self.connections_override.items():
                connections[alias] = conn
        try:
            # Go through the list of possible ports, hoping that we can find
            # one that is free to use for the WSGI server.
            try:
                server = get_server(debug=False)
                for port in self.ports:
                    print('try listen on keybar.local:{}'.format(port))
                    try:
                        server.listen(port, 'keybar.local')
                    except OSError as exc:
                        print(exc)
                        if port == 65535:
                            raise exc
            except socket.error as exc:
                raise exc
            else:
                self.port = port

            self.is_ready.set()
            self.loop = ioloop.IOLoop.instance()
            self.loop.start()
        except Exception as exc:
            self.terminate()
            self.error = exc
            self.is_ready.set()
{'fraction_non_alphanumeric': 0.061533399144455415, 'fraction_numerical': 0.009213557091148404, 'mean_word_length': 1.6072041166380788, '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': '2200219', 'n_tokens_mistral': 778, 'n_tokens_neox': 721, 'n_words': 241}