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

from __future__ import print_function

import unittest
import subprocess
import contextlib
import requests
import random
import time
import threading


class FathomTest(unittest.TestCase):

    def test_syzygy_required(self):
        self.assertEqual(subprocess.call(["./fathom", "--syzygy", "gaviota"]), 78)

    @contextlib.contextmanager
    def fathom(self, *args, **kwargs):
        port = 30000 + random.randint(0, 9999)

        process = subprocess.Popen(
            ["./fathom", "--port", str(port),
             "--syzygy", kwargs.get("syzygy", "syzygy"),
             "--gaviota", kwargs.get("gaviota", "gaviota")] + list(args))

        time.sleep(1)

        yield "http://1192.168.127.12:%d/tablebase" % port

        process.terminate()

    def test_no_cors(self):
        with self.fathom() as endpoint:
            res = requests.get(endpoint, {"fen": "4k3/8/8/8/8/8/8/1R2K3 w - - 0 1"})
            self.assertFalse("Access-Control-Allow-Origin" in res.headers)

    def test_cors(self):
        with self.fathom("--cors") as endpoint:
            res = requests.get(endpoint, {"fen": "4k3/8/8/8/8/8/8/1R2K3 w - - 0 1"})
            self.assertEqual(res.headers["Access-Control-Allow-Origin"], "*")

    def test_kqk(self):
        with self.fathom() as endpoint:
            data = requests.get(endpoint, {"fen": "K7/8/8/4k3/8/2Q5/8/8 b - - 0 1"}).json()
            self.assertEqual(data["dtz"], -18)
            self.assertEqual(data["moves"][0]["dtm"], 17)
            self.assertEqual(data["moves"][0]["san"], "Ke4")
            self.assertEqual(data["moves"][0]["uci"], "e5e4")
            self.assertFalse(data["moves"][0]["checkmate"])<commit_msg>Test: wait for "listening" in stdout<commit_after>#!/usr/bin/env python

from __future__ import print_function

import unittest
import subprocess
import contextlib
import requests
import random
import time
import threading


class FathomTest(unittest.TestCase):

    def test_syzygy_required(self):
        self.assertEqual(subprocess.call(["./fathom", "--syzygy", "gaviota"]), 78)

    @contextlib.contextmanager
    def fathom(self, *args, **kwargs):
        port = 30000 + random.randint(0, 9999)

        process = subprocess.Popen(
            ["./fathom", "--port", str(port),
             "--syzygy", kwargs.get("syzygy", "syzygy"),
             "--gaviota", kwargs.get("gaviota", "gaviota")] + list(args),
            stdout=subprocess.PIPE,
            bufsize=1,
            universal_newlines=True)

        listening = threading.Event()

        def stdout_reader():
            for line in iter(process.stdout.readline, ""):
                print(line.rstrip())
                if line.startswith("listening"):
                    listening.set()

            listening.set()

        reader_thread = threading.Thread(target=stdout_reader)
        reader_thread.start()
        listening.wait()

        yield "http://127.0.0.1:%d/tablebase" % port

        process.terminate()
        reader_thread.join()

    def test_no_cors(self):
        with self.fathom() as endpoint:
            res = requests.get(endpoint, {"fen": "4k3/8/8/8/8/8/8/1R2K3 w - - 0 1"})
            self.assertFalse("Access-Control-Allow-Origin" in res.headers)

    def test_cors(self):
        with self.fathom("--cors") as endpoint:
            res = requests.get(endpoint, {"fen": "4k3/8/8/8/8/8/8/1R2K3 w - - 0 1"})
            self.assertEqual(res.headers["Access-Control-Allow-Origin"], "*")

    def test_kqk(self):
        with self.fathom() as endpoint:
            data = requests.get(endpoint, {"fen": "K7/8/8/4k3/8/2Q5/8/8 b - - 0 1"}).json()
            self.assertEqual(data["dtz"], -18)
            self.assertEqual(data["moves"][0]["dtm"], 17)
            self.assertEqual(data["moves"][0]["san"], "Ke4")
            self.assertEqual(data["moves"][0]["uci"], "e5e4")
            self.assertFalse(data["moves"][0]["checkmate"])
{'fraction_non_alphanumeric': 0.16662439766675122, 'fraction_numerical': 0.036013187927973624, 'mean_word_length': 2.4748898678414095, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6209977', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1309, 'n_words': 236}