<commit_before>import os
import time
import signal

import pytest

from eth_rpc_client import Client

from populus.utils import (
    get_open_port,
    ensure_path_exists,
    wait_for_popen,
)
from populus.geth import (
    is_geth_available,
    run_geth_node,
    get_geth_data_dir,
    get_geth_accounts,
    reset_chain,
    ensure_account_exists,
)


PROJECTS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'projects')


skip_if_no_geth = pytest.mark.skipif(
    not is_geth_available(),
    reason="'geth' not available",
)


@pytest.fixture
def project_test04(monkeypatch):
    project_dir = os.path.join(PROJECTS_DIR, 'test-04')
    monkeypatch.chdir(project_dir)

    data_dir = get_geth_data_dir(project_dir, 'default')
    ensure_path_exists(data_dir)
    reset_chain(data_dir)

    ensure_account_exists(data_dir)

    return project_dir


@pytest.fixture
def open_port():
    return get_open_port()


@skip_if_no_geth
def test_running_node_without_mining(project_test04, open_port):
    data_dir = get_geth_data_dir(project_test04, 'default')

    command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=False)
    time.sleep(2)
    rpc_client = Client('127.0.0.1', port=open_port)
    coinbase = rpc_client.get_coinbase()
    proc.send_signal(signal.SIGINT)
    wait_for_popen(proc)
    assert coinbase == get_geth_accounts(data_dir)[0]


@skip_if_no_geth
def test_running_node_and_mining(project_test04, open_port):
    data_dir = get_geth_data_dir(project_test04, 'default')

    command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=True)
    time.sleep(2)
    rpc_client = Client('127.0.0.1', port=open_port)
    block_num = rpc_client.get_block_number()
    start = time.time()
    while time.time() < start + 10:
        time.sleep(0.2)
        if rpc_client.get_block_number() > block_num:
            break
    assert block_num < rpc_client.get_block_number()
    proc.send_signal(signal.SIGINT)
    wait_for_popen(proc)<commit_msg>handle timeouts on slow geth nodes a bit better<commit_after>import os
import time
import signal

import pytest

from eth_rpc_client import Client

from populus.utils import (
    get_open_port,
    ensure_path_exists,
    wait_for_popen,
)
from populus.geth import (
    is_geth_available,
    run_geth_node,
    get_geth_data_dir,
    get_geth_accounts,
    reset_chain,
    ensure_account_exists,
)


PROJECTS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'projects')


skip_if_no_geth = pytest.mark.skipif(
    not is_geth_available(),
    reason="'geth' not available",
)


@pytest.fixture
def project_test04(monkeypatch):
    project_dir = os.path.join(PROJECTS_DIR, 'test-04')
    monkeypatch.chdir(project_dir)

    data_dir = get_geth_data_dir(project_dir, 'default')
    ensure_path_exists(data_dir)
    reset_chain(data_dir)

    ensure_account_exists(data_dir)

    return project_dir


@pytest.fixture
def open_port():
    return get_open_port()


@skip_if_no_geth
def test_running_node_without_mining(project_test04, open_port):
    data_dir = get_geth_data_dir(project_test04, 'default')

    command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=False)
    wait_for_popen(proc)
    rpc_client = Client('127.0.0.1', port=open_port)
    coinbase = rpc_client.get_coinbase()
    proc.send_signal(signal.SIGINT)
    wait_for_popen(proc)
    assert coinbase == get_geth_accounts(data_dir)[0]


@skip_if_no_geth
def test_running_node_and_mining(project_test04, open_port):
    data_dir = get_geth_data_dir(project_test04, 'default')

    command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=True)
    wait_for_popen(proc)
    rpc_client = Client('127.0.0.1', port=open_port)
    block_num = rpc_client.get_block_number()
    start = time.time()
    while rpc_client.get_block_number() <= block_num:
        time.sleep(0.2)
        if time.time() > start + 20:
            raise Exception('Could not mine block within 20 seconds')
    assert block_num < rpc_client.get_block_number()
    proc.send_signal(signal.SIGINT)
    wait_for_popen(proc)
{'fraction_non_alphanumeric': 0.09718482252141983, 'fraction_numerical': 0.015177478580171358, 'mean_word_length': 4.448, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7133468', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1525, 'n_words': 242}