<commit_before>#!/usr/bin/env python
##
# Tests the driver API for making connections and excercizes the
# networking code
###

from __future__ import print_function

import datetime
import os
import random
import re
import sys
import tempfile
import time
import traceback
import unittest
import socket
import asyncio

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                os.pardir, os.pardir, "common"))
import driver
import utils

try:
    xrange
except NameError:
    xrange = range

# -- import the rethinkdb driver

r = utils.import_python_driver()

# -- use asyncio subdriver

r.set_loop_type("asyncio")

# -- get settings

DEFAULT_DRIVER_PORT = 28015

rethinkdb_exe = (sys.argv[1]
                 if len(sys.argv) > 1
                 else utils.find_rethinkdb_executable())
use_default_port = bool(int(sys.argv[2])) if len(sys.argv) > 2 else 0

# -- shared server

sharedServer = None
sharedServerOutput = None
sharedServerHost = None
sharedServerDriverPort = None
if 'RDB_DRIVER_PORT' in os.environ:
    sharedServerDriverPort = int(os.environ['RDB_DRIVER_PORT'])
    if 'RDB_SERVER_HOST' in os.environ:
        sharedServerHost = os.environ['RDB_SERVER_HOST']
    else:
        sharedServerHost = 'localhost'


@asyncio.coroutine
def checkSharedServer():
    if sharedServerDriverPort is not None:
        conn = yield from r.connect(host=sharedServerHost,
                               port=sharedServerDriverPort)
        if 'test' not in (yield from r.db_list().run(conn)):
            yield from r.db_create('test').run(conn)


@asyncio.coroutine
def closeSharedServer():
    global sharedServer, sharedServerOutput, sharedServerHost, \
        sharedServerDriverPort

    if sharedServer is not None:
        try:
            sharedServer.close()
        except Exception as e:
            sys.stderr.write('Got error while shutting down server: %s'
                             % str(e))
    sharedServer = None
    sharedServerOutput = None
    sharedServerHost = None
    sharedServerDriverPort = None


def just_do(coroutine, *args, **kwargs):
    asyncio.get_event_loop().run_until_complete(coroutine(*args, **kwargs))


# == Test Base Classes

class TestCaseCompatible(unittest.TestCase):
    # @asyncio.coroutine
    # def asyncAssertRaisesRegexp(self, exception, regexp, generator):
    #     try:
    #         yield from generator
    #     except Exception as e:
    #         self.assertTrue(isinstance(e, exception),
    #                         '%s expected to raise %s but '
    #                         'instead raised %s: %s\n%s'
    #                         % (repr(generator), repr(exception),
    #                            e.__class__.__name__, str(e),
    #                            traceback.format_exc()))
    #         self.assertTrue(re.search(regexp, str(e)),
    #                         '%s did not raise the expected '
    #                         'message "%s", but rather: %s'
    #                         % (repr(generator), str(regexp), str(e)))
    #     else:
    #         self.fail('%s failed to raise a %s'
    #                   % (repr(generator), repr(exception)))
    #
    # @asyncio.coroutine
    # def asyncAssertRaises(self, exception, generator):
    #     try:
    #         yield from generator
    #     except Exception as e:
    #         self.assertTrue(isinstance(e, exception),
    #                         '%s expected to raise %s but '
    #                         'instead raised %s: %s\n%s'
    #                         % (repr(generator), repr(exception),
    #                            e.__class__.__name__, str(e),
    #                            traceback.format_exc()))
    #     else:
    #         self.fail('%s failed to raise a %s'
    #                   % (repr(generator), repr(exception)))

    # can't use standard TestCase run here because async.
    def run(self, result=None):
        return just_do(self.arun, result)

    @asyncio.coroutine
    def setUp(self):
        return None

    @asyncio.coroutine
    def tearDown(self):
        return None

    @asyncio.coroutine<commit_msg>Remove asyncAssert methods<commit_after>#!/usr/bin/env python
##
# Tests the driver API for making connections and excercizes the
# networking code
###

from __future__ import print_function

import datetime
import os
import random
import re
import sys
import tempfile
import time
import traceback
import unittest
import socket
import asyncio

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                os.pardir, os.pardir, "common"))
import driver
import utils

try:
    xrange
except NameError:
    xrange = range

# -- import the rethinkdb driver

r = utils.import_python_driver()

# -- use asyncio subdriver

r.set_loop_type("asyncio")

# -- get settings

DEFAULT_DRIVER_PORT = 28015

rethinkdb_exe = (sys.argv[1]
                 if len(sys.argv) > 1
                 else utils.find_rethinkdb_executable())
use_default_port = bool(int(sys.argv[2])) if len(sys.argv) > 2 else 0

# -- shared server

sharedServer = None
sharedServerOutput = None
sharedServerHost = None
sharedServerDriverPort = None
if 'RDB_DRIVER_PORT' in os.environ:
    sharedServerDriverPort = int(os.environ['RDB_DRIVER_PORT'])
    if 'RDB_SERVER_HOST' in os.environ:
        sharedServerHost = os.environ['RDB_SERVER_HOST']
    else:
        sharedServerHost = 'localhost'


@asyncio.coroutine
def checkSharedServer():
    if sharedServerDriverPort is not None:
        conn = yield from r.connect(host=sharedServerHost,
                               port=sharedServerDriverPort)
        if 'test' not in (yield from r.db_list().run(conn)):
            yield from r.db_create('test').run(conn)


@asyncio.coroutine
def closeSharedServer():
    global sharedServer, sharedServerOutput, sharedServerHost, \
        sharedServerDriverPort

    if sharedServer is not None:
        try:
            sharedServer.close()
        except Exception as e:
            sys.stderr.write('Got error while shutting down server: %s'
                             % str(e))
    sharedServer = None
    sharedServerOutput = None
    sharedServerHost = None
    sharedServerDriverPort = None


def just_do(coroutine, *args, **kwargs):
    asyncio.get_event_loop().run_until_complete(coroutine(*args, **kwargs))


# == Test Base Classes

class TestCaseCompatible(unittest.TestCase):
    # can't use standard TestCase run here because async.
    def run(self, result=None):
        return just_do(self.arun, result)

    @asyncio.coroutine
    def setUp(self):
        return None

    @asyncio.coroutine
    def tearDown(self):
        return None

    @asyncio.coroutine
{'fraction_non_alphanumeric': 0.09680772086117298, 'fraction_numerical': 0.0032665181885671865, 'mean_word_length': 2.368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5892977', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1964, 'n_words': 500}