<commit_before>import os
import json
import pytest
from volttrontesting.utils.platformwrapper import PlatformWrapper


def get_rand_port():
    from random import randint
    port = randint(5000, 6000)
    while is_port_open(port):
        port = randint(5000, 6000)
    return port


def is_port_open(port):
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('127.0.0.1',port))
    return result == 0


@pytest.fixture(scope="module")
def instance1_config():
    port = get_rand_port()
    return {"vip-address": "tcp://127.0.0.1:{}".format(port)}


@pytest.fixture(scope="module")
def instance2_config():
    port = get_rand_port()
    return {"vip-address": "tcp://127.0.0.1:{}".format(port)}


def build_wrapper(vip_address, **kwargs):
    wrapper = PlatformWrapper()
    print('BUILD_WRAPPER: {}'.format(vip_address))
    wrapper.startup_platform(vip_address=vip_address, **kwargs)
    return wrapper


@pytest.fixture(scope="module")
def volttron_instance1(request, instance1_config):
    wrapper = build_wrapper(instance1_config['vip-address'])

    def cleanup():
        print('Shutting down instance: {}'.format(wrapper.volttron_home))
        wrapper.shutdown_platform(True)
    request.addfinalizer(cleanup)
    return wrapper


@pytest.fixture(scope="module")
def volttron_instance2(request, instance2_config):
    print("building instance 2")
    wrapper = build_wrapper(instance2_config['vip-address'])

    def cleanup():
        print('Shutting down instance: {}'.format(wrapper.volttron_home))
        wrapper.shutdown_platform(True)
    request.addfinalizer(cleanup)
    return wrapper


@pytest.fixture(scope="function")
def volttron_instance1_encrypt(request):
    print("building instance 1 (using encryption)")
    address = "tcp://127.0.0.1:{}".format(get_rand_port()) 
    wrapper = build_wrapper(address, encrypt=True)

    def cleanup():
        print('Shutting down instance: {}'.format(wrapper.volttron_home))
        wrapper.shutdown_platform(True)
    request.addfinalizer(cleanup)
    return wrapper<commit_msg>Added printing of the logfile if necessary for the test methods.<commit_after>import os
import json
import pytest
from volttrontesting.utils.platformwrapper import PlatformWrapper

PRINT_LOG_ON_SHUTDOWN = True

def print_log(volttron_home):
    if PRINT_LOG_ON_SHUTDOWN:
        with open(volttron_home+"/volttron.log") as fin:
            print(fin.read())


def get_rand_port():
    from random import randint
    port = randint(5000, 6000)
    while is_port_open(port):
        port = randint(5000, 6000)
    return port


def is_port_open(port):
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex(('127.0.0.1',port))
    return result == 0


@pytest.fixture(scope="module")
def instance1_config():
    port = get_rand_port()
    return {"vip-address": "tcp://127.0.0.1:{}".format(port)}


@pytest.fixture(scope="module")
def instance2_config():
    port = get_rand_port()
    return {"vip-address": "tcp://127.0.0.1:{}".format(port)}


def build_wrapper(vip_address, **kwargs):
    wrapper = PlatformWrapper()
    print('BUILD_WRAPPER: {}'.format(vip_address))
    wrapper.startup_platform(vip_address=vip_address, **kwargs)
    return wrapper


@pytest.fixture(scope="module")
def volttron_instance1(request, instance1_config):
    wrapper = build_wrapper(instance1_config['vip-address'])

    def cleanup():
        print('Shutting down instance: {}'.format(wrapper.volttron_home))
        wrapper.shutdown_platform(True)
    request.addfinalizer(cleanup)
    return wrapper


@pytest.fixture(scope="module")
def volttron_instance2(request, instance2_config):
    print("building instance 2")
    wrapper = build_wrapper(instance2_config['vip-address'])

    def cleanup():
        print('Shutting down instance: {}'.format(wrapper.volttron_home))
        wrapper.shutdown_platform(True)
    request.addfinalizer(cleanup)
    return wrapper


@pytest.fixture(scope="function")
def volttron_instance1_encrypt(request):
    print("building instance 1 (using encryption)")
    address = "tcp://127.0.0.1:{}".format(get_rand_port()) 
    wrapper = build_wrapper(address, encrypt=True)

    def cleanup():
        print('Shutting down instance: {}'.format(wrapper.volttron_home))
        print_log(wrapper.volttron_home)
        wrapper.shutdown_platform(True)
    request.addfinalizer(cleanup)
    return wrapper
{'fraction_non_alphanumeric': 0.12790697674418605, 'fraction_numerical': 0.023255813953488372, 'mean_word_length': 4.382671480144404, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '527081', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1412, 'n_words': 267}