<commit_before>"""Defines various classes and definitions that provide assistance for
unit testing Actors in an ActorSystem."""

import unittest
import logging
from thespian.actors import ActorSystem


def simpleActorTestLogging():
    """This function returns a logging dictionary that can be passed as
       the logDefs argument for ActorSystem() initialization to get
       simple stdout logging configuration.  This is not necessary for
       typical unit testing that uses the simpleActorSystemBase, but
       it can be useful for multiproc.. ActorSystems where the
       separate processes created should have a very simple logging
       configuration.
    """
    import sys
    if sys.platform == 'win32':
        # Windows will not allow sys.stdout to be passed to a child
        # process, which breaks the startup/config for some of the
        # tests.
        handler = { 'class': 'logging.handlers.RotatingFileHandler',
                    'filename': 'nosetests.log',
                    'maxBytes': 256*1024,
                    'backupCount':3,
        }
    else:
        handler = { 'class': 'logging.StreamHandler',
                    'stream': sys.stdout,
        }
    return {
        'version' : 1,
        'handlers': { #'discarder': {'class': 'logging.NullHandler' },
            'testStream' : handler,
        },
        'root': { 'handlers': ['testStream'] },
        'disable_existing_loggers': False,
    }

<commit_msg>Added TestSystem context manager.<commit_after>"""Defines various classes and definitions that provide assistance for
unit testing Actors in an ActorSystem."""

import unittest
import logging
from thespian.actors import ActorSystem


def simpleActorTestLogging():
    """This function returns a logging dictionary that can be passed as
       the logDefs argument for ActorSystem() initialization to get
       simple stdout logging configuration.  This is not necessary for
       typical unit testing that uses the simpleActorSystemBase, but
       it can be useful for multiproc.. ActorSystems where the
       separate processes created should have a very simple logging
       configuration.
    """
    import sys
    if sys.platform == 'win32':
        # Windows will not allow sys.stdout to be passed to a child
        # process, which breaks the startup/config for some of the
        # tests.
        handler = { 'class': 'logging.handlers.RotatingFileHandler',
                    'filename': 'nosetests.log',
                    'maxBytes': 256*1024,
                    'backupCount':3,
        }
    else:
        handler = { 'class': 'logging.StreamHandler',
                    'stream': sys.stdout,
        }
    return {
        'version' : 1,
        'handlers': { #'discarder': {'class': 'logging.NullHandler' },
            'testStream' : handler,
        },
        'root': { 'handlers': ['testStream'] },
        'disable_existing_loggers': False,
    }


class TestSystem(object):
    "Functions as a context manager for a transient system base"
    def __init__(self, newBase='simpleSystemBase',
                 systemCapabilities=None,
                 logDefs='BestForBase'):
            self._asys = ActorSystem(systemBase=newBase,
                                     capabilities=systemCapabilities,
                                     logDefs=logDefs if logDefs != 'BestForBase' else (
                                         simpleActorTestLogging() if newBase.startswith('multiproc')
                                         else False),
                                     transientUnique=True)

    def __enter__(self):
        return self._asys
    def __exit__(self, exc_type, exc_val, exc_tb):
        self._asys.shutdown()
        self._asys = None


{'fraction_non_alphanumeric': 0.08446576072475354, 'fraction_numerical': 0.005861977084998668, 'mean_word_length': 2.112769485903814, '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': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1175141', 'n_tokens_mistral': 948, 'n_tokens_neox': 915, 'n_words': 314}