<commit_before>import contextlib
import tempfile
import shutil
import random

import pytest

import gevent
from gevent import monkey
from gevent import socket

# needed to use the requests library
monkey.patch_socket()

import requests  # noqa: E402

from geth import (  # noqa: E402
    LoggingMixin,
    DevGethProcess,
)


class GethProcess(LoggingMixin, DevGethProcess):
    pass


def get_open_port():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("", 0))
    s.listen(1)
    port = s.getsockname()[1]
    s.close()
    return port


def wait_for_http_connection(port, timeout=60):
    with gevent.Timeout(timeout):
        while True:
            try:
                requests.post("http://127.0.0.1:{0}".format(port))
            except requests.ConnectionError:
                gevent.sleep(0.1)
                continue
            else:
                break
        else:
            raise ValueError("Unable to establish HTTP connection")


@pytest.fixture()
def skip_if_testrpc():
    from web3.providers.rpc import TestRPCProvider

    def _skip_if_testrpc(web3):
        if isinstance(web3.currentProvider, TestRPCProvider):
            pytest.skip()
    return _skip_if_testrpc


@pytest.fixture()
def wait_for_miner_start():
    def _wait_for_miner_start(web3, timeout=60):
        with gevent.Timeout(timeout):
            while not web3.eth.mining or not web3.eth.hashrate:
                gevent.sleep(random.random())
    return _wait_for_miner_start


@pytest.fixture()
def wait_for_block():
    from web3.providers.rpc import TestRPCProvider

    def _wait_for_block(web3, block_number=1, timeout=60 * 10):
        with gevent.Timeout(timeout):
            while True:
                if web3.eth.blockNumber >= block_number:
                    break
                if isinstance(web3.currentProvider, TestRPCProvider):
                    web3._requestManager.request_blocking("evm_mine", [])
                gevent.sleep(1)
    return _wait_for_block


@pytest.fixture()
def wait_for_transaction():
    def _wait_for_transaction(web3, txn_hash, timeout=120):
        with gevent.Timeout(timeout):
            while True:
                txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
                if txn_receipt is not None:
                    break
                gevent.sleep(1)

        return txn_receipt
    return _wait_for_transaction


@contextlib.contextmanager
def tempdir():
    directory = tempfile.mkdtemp()

    try:
        yield directory
    finally:
        shutil.rmtree(directory)


@pytest.yield_fixture(scope="session")
def web3_tester_provider():
    from testrpc import testrpc

    from web3.providers.rpc import TestRPCProvider
    port = get_open_port()
    provider = TestRPCProvider(port=port)

    testrpc.full_reset()
    testrpc.rpc_configure('eth_mining', False)
    testrpc.rpc_configure('eth_protocolVersion', '0x3f')
    testrpc.rpc_configure('net_version', 1)
    testrpc.evm_mine()

    provider.testrpc = testrpc
    wait_for_http_connection(port)

    yield provider

    provider.server.shutdown()
    provider.server.server_close()


@pytest.fixture()
def web3_tester_empty(request, web3_tester_provider):
    from web3 import Web3

    if getattr(request, 'reset_chain', True):
        web3_tester_provider.testrpc.full_reset()

    web3 = Web3(web3_tester_provider)
    return web3


@pytest.fixture()
def web3_tester_persistent(request, web3_tester_provider):
    from web3 import Web3

    web3 = Web3(web3_tester_provider)
    return web3


@pytest.fixture()
def web3_tester(web3_tester_persistent):
    # alias
    return web3_tester_persistent


@contextlib.contextmanager
def setup_testing_geth():
    with tempdir() as base_dir:
        geth_process = GethProcess(<commit_msg>fix closing of testrpc server<commit_after>import contextlib
import tempfile
import shutil
import random

import pytest

import gevent
from gevent import monkey
from gevent import socket

# needed to use the requests library
monkey.patch_socket()

import requests  # noqa: E402

from geth import (  # noqa: E402
    LoggingMixin,
    DevGethProcess,
)


class GethProcess(LoggingMixin, DevGethProcess):
    pass


def get_open_port():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("", 0))
    s.listen(1)
    port = s.getsockname()[1]
    s.close()
    return port


def wait_for_http_connection(port, timeout=60):
    with gevent.Timeout(timeout):
        while True:
            try:
                requests.post("http://127.0.0.1:{0}".format(port))
            except requests.ConnectionError:
                gevent.sleep(0.1)
                continue
            else:
                break
        else:
            raise ValueError("Unable to establish HTTP connection")


@pytest.fixture()
def skip_if_testrpc():
    from web3.providers.rpc import TestRPCProvider

    def _skip_if_testrpc(web3):
        if isinstance(web3.currentProvider, TestRPCProvider):
            pytest.skip()
    return _skip_if_testrpc


@pytest.fixture()
def wait_for_miner_start():
    def _wait_for_miner_start(web3, timeout=60):
        with gevent.Timeout(timeout):
            while not web3.eth.mining or not web3.eth.hashrate:
                gevent.sleep(random.random())
    return _wait_for_miner_start


@pytest.fixture()
def wait_for_block():
    from web3.providers.rpc import TestRPCProvider

    def _wait_for_block(web3, block_number=1, timeout=60 * 10):
        with gevent.Timeout(timeout):
            while True:
                if web3.eth.blockNumber >= block_number:
                    break
                if isinstance(web3.currentProvider, TestRPCProvider):
                    web3._requestManager.request_blocking("evm_mine", [])
                gevent.sleep(1)
    return _wait_for_block


@pytest.fixture()
def wait_for_transaction():
    def _wait_for_transaction(web3, txn_hash, timeout=120):
        with gevent.Timeout(timeout):
            while True:
                txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
                if txn_receipt is not None:
                    break
                gevent.sleep(1)

        return txn_receipt
    return _wait_for_transaction


@contextlib.contextmanager
def tempdir():
    directory = tempfile.mkdtemp()

    try:
        yield directory
    finally:
        shutil.rmtree(directory)


@pytest.yield_fixture(scope="session")
def web3_tester_provider():
    from testrpc import testrpc

    from web3.providers.rpc import TestRPCProvider
    port = get_open_port()
    provider = TestRPCProvider(port=port)

    testrpc.full_reset()
    testrpc.rpc_configure('eth_mining', False)
    testrpc.rpc_configure('eth_protocolVersion', '0x3f')
    testrpc.rpc_configure('net_version', 1)
    testrpc.evm_mine()

    provider.testrpc = testrpc
    wait_for_http_connection(port)

    yield provider

    provider.server.stop()


@pytest.fixture()
def web3_tester_empty(request, web3_tester_provider):
    from web3 import Web3

    if getattr(request, 'reset_chain', True):
        web3_tester_provider.testrpc.full_reset()

    web3 = Web3(web3_tester_provider)
    return web3


@pytest.fixture()
def web3_tester_persistent(request, web3_tester_provider):
    from web3 import Web3

    web3 = Web3(web3_tester_provider)
    return web3


@pytest.fixture()
def web3_tester(web3_tester_persistent):
    # alias
    return web3_tester_persistent


@contextlib.contextmanager
def setup_testing_geth():
    with tempdir() as base_dir:
        geth_process = GethProcess(
{'fraction_non_alphanumeric': 0.08651938087048551, 'fraction_numerical': 0.018520968382061118, 'mean_word_length': 2.845371312309257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2516936', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2381, 'n_words': 490}