<commit_before># Copyright 2012 10gen, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test Motor, an asynchronous driver for MongoDB and Tornado."""

import functools
import os
import time
import sys
import types
import unittest

import pymongo
import pymongo.errors
from tornado import gen, ioloop
from nose.plugins.skip import SkipTest
from pymongo.mongo_client import MongoClient, _partition_node

import motor

have_ssl = True
try:
    import ssl
except ImportError:
    have_ssl = False


host = os.environ.get("DB_IP", "localhost")
port = int(os.environ.get("DB_PORT", 27017))


# TODO: replicate asyncmongo's whole test suite?

class AsyncTestRunner(gen.Runner):
    def __init__(self, gen, timeout):
        # Tornado 2.3 added a second argument to Runner()
        super(AsyncTestRunner, self).__init__(gen, lambda: None)
        self.timeout = timeout

    def run(self):
        loop = ioloop.IOLoop.instance()

        try:
            super(AsyncTestRunner, self).run()
        except:
            loop.remove_timeout(self.timeout)
            loop.stop()
            raise

        if self.finished:
            loop.remove_timeout(self.timeout)
            loop.stop()


def async_test_engine(timeout_sec=None, io_loop=None):
    if (
        timeout_sec is not None
        and not isinstance(timeout_sec, int) and
        not isinstance(timeout_sec, float)
    ):
        raise TypeError(
"""Expected int or float, got %s
Use async_test_engine like:
    @async_test_engine()
or:
    @async_test_engine(timeout_sec=10)""" % (
        repr(timeout_sec)))

    if timeout_sec is None:
        timeout_sec = float(os.environ.get('TIMEOUT_SEC', 5))

    is_done = [False]

    def decorator(func):
        def done():
            is_done[0] = True

        @functools.wraps(func)
        def _async_test(self):
            # Uninstall previous loop
            if hasattr(ioloop.IOLoop, '_instance'):
                del ioloop.IOLoop._instance

            loop = io_loop or ioloop.IOLoop.instance()
            assert not loop._stopped
            if io_loop:
                io_loop.install()

            def on_timeout():
                loop.stop()
                raise AssertionError("%s timed out" % func)

            timeout = loop.add_timeout(time.time() + timeout_sec, on_timeout)

            try:
                generator = func(self, done)
                assert isinstance(generator, types.GeneratorType), (
                    "%s should be a generator, include a yield "
                    "statement" % func
                )

                runner = AsyncTestRunner(generator, timeout)
                runner.run()
                loop.start()
                if not runner.finished:
                    # Something stopped the loop before func could finish or
                    # throw an exception.
                    raise Exception('%s did not finish' % func)

                if not is_done[0]:
                    raise Exception('%s did not call done()' % func)
            finally:
                del ioloop.IOLoop._instance # Uninstall

        return _async_test
    return decorator

async_test_engine.__test__ = False # Nose otherwise mistakes it for a test


class AssertRaises(gen.Task):
    def __init__(self, exc_type, func, *args, **kwargs):
        super(AssertRaises, self).__init__(func, *args, **kwargs)
        if not isinstance(exc_type, type):
            raise TypeError("%s is not a class" % repr(exc_type))

        if not issubclass(exc_type, Exception):
            raise TypeError(
                "%s is not a subclass of Exception" % repr(exc_type))
        self.exc_type = exc_type

    def get_result(self):
        (result, error), _ = self.runner.pop_result(self.key)
        if not isinstance(error, self.exc_type):
            if error:
                raise AssertionError("%s raised instead of %s" % (
                    repr(error), self.exc_type.__name__))
            else:
                raise AssertionError("%s not raised" % self.exc_type.__name__)
        return result


class AssertEqual(gen.Task):
    def __init__(self, expected, func, *args, **kwargs):
        super(AssertEqual, self).__init__(func, *args, **kwargs)
        self.expected = expected

    def get_result(self):
        (result, error), _ = self.runner.pop_result(self.key)
        if error:
            raise error

        if self.expected != result:
            raise AssertionError("%s returned %s\nnot\n%s" % (
                self.func, repr(result), repr(self.expected)))

        return result


class AssertTrue(AssertEqual):
    def __init__(self, func, *args, **kwargs):
        super(AssertTrue, self).__init__(True, func, *args, **kwargs)


class AssertFalse(AssertEqual):
    def __init__(self, func, *args, **kwargs):
        super(AssertFalse, self).__init__(False, func, *args, **kwargs)


class MotorTest(unittest.TestCase):
    longMessage = True # Used by unittest.TestCase
    ssl = False # If True, connect with SSL, skip if mongod isn't SSL

    def setUp(self):
        super(MotorTest, self).setUp()

        # Store a regular synchronous pymongo Connection for convenience while
        # testing. Low timeouts so we don't hang a test because, say, Mongo
        # isn't up or is hung by a long-running $where clause.
        connectTimeoutMS = socketTimeoutMS = 2000
        if self.ssl:
            if not have_ssl:
                raise SkipTest("Python compiled without SSL")
            try:
                self.sync_cx = pymongo.MongoClient(
                    host, port, connectTimeoutMS=connectTimeoutMS,
                    socketTimeoutMS=socketTimeoutMS,<commit_msg>Longer socket timeouts for slow Jenkins slaves<commit_after># Copyright 2012 10gen, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test Motor, an asynchronous driver for MongoDB and Tornado."""

import functools
import os
import time
import sys
import types
import unittest

import pymongo
import pymongo.errors
from tornado import gen, ioloop
from nose.plugins.skip import SkipTest
from pymongo.mongo_client import MongoClient, _partition_node

import motor

have_ssl = True
try:
    import ssl
except ImportError:
    have_ssl = False


host = os.environ.get("DB_IP", "localhost")
port = int(os.environ.get("DB_PORT", 27017))


# TODO: replicate asyncmongo's whole test suite?

class AsyncTestRunner(gen.Runner):
    def __init__(self, gen, timeout):
        # Tornado 2.3 added a second argument to Runner()
        super(AsyncTestRunner, self).__init__(gen, lambda: None)
        self.timeout = timeout

    def run(self):
        loop = ioloop.IOLoop.instance()

        try:
            super(AsyncTestRunner, self).run()
        except:
            loop.remove_timeout(self.timeout)
            loop.stop()
            raise

        if self.finished:
            loop.remove_timeout(self.timeout)
            loop.stop()


def async_test_engine(timeout_sec=None, io_loop=None):
    if (
        timeout_sec is not None
        and not isinstance(timeout_sec, int) and
        not isinstance(timeout_sec, float)
    ):
        raise TypeError(
"""Expected int or float, got %s
Use async_test_engine like:
    @async_test_engine()
or:
    @async_test_engine(timeout_sec=10)""" % (
        repr(timeout_sec)))

    if timeout_sec is None:
        timeout_sec = float(os.environ.get('TIMEOUT_SEC', 5))

    is_done = [False]

    def decorator(func):
        def done():
            is_done[0] = True

        @functools.wraps(func)
        def _async_test(self):
            # Uninstall previous loop
            if hasattr(ioloop.IOLoop, '_instance'):
                del ioloop.IOLoop._instance

            loop = io_loop or ioloop.IOLoop.instance()
            assert not loop._stopped
            if io_loop:
                io_loop.install()

            def on_timeout():
                loop.stop()
                raise AssertionError("%s timed out" % func)

            timeout = loop.add_timeout(time.time() + timeout_sec, on_timeout)

            try:
                generator = func(self, done)
                assert isinstance(generator, types.GeneratorType), (
                    "%s should be a generator, include a yield "
                    "statement" % func
                )

                runner = AsyncTestRunner(generator, timeout)
                runner.run()
                loop.start()
                if not runner.finished:
                    # Something stopped the loop before func could finish or
                    # throw an exception.
                    raise Exception('%s did not finish' % func)

                if not is_done[0]:
                    raise Exception('%s did not call done()' % func)
            finally:
                del ioloop.IOLoop._instance # Uninstall

        return _async_test
    return decorator

async_test_engine.__test__ = False # Nose otherwise mistakes it for a test


class AssertRaises(gen.Task):
    def __init__(self, exc_type, func, *args, **kwargs):
        super(AssertRaises, self).__init__(func, *args, **kwargs)
        if not isinstance(exc_type, type):
            raise TypeError("%s is not a class" % repr(exc_type))

        if not issubclass(exc_type, Exception):
            raise TypeError(
                "%s is not a subclass of Exception" % repr(exc_type))
        self.exc_type = exc_type

    def get_result(self):
        (result, error), _ = self.runner.pop_result(self.key)
        if not isinstance(error, self.exc_type):
            if error:
                raise AssertionError("%s raised instead of %s" % (
                    repr(error), self.exc_type.__name__))
            else:
                raise AssertionError("%s not raised" % self.exc_type.__name__)
        return result


class AssertEqual(gen.Task):
    def __init__(self, expected, func, *args, **kwargs):
        super(AssertEqual, self).__init__(func, *args, **kwargs)
        self.expected = expected

    def get_result(self):
        (result, error), _ = self.runner.pop_result(self.key)
        if error:
            raise error

        if self.expected != result:
            raise AssertionError("%s returned %s\nnot\n%s" % (
                self.func, repr(result), repr(self.expected)))

        return result


class AssertTrue(AssertEqual):
    def __init__(self, func, *args, **kwargs):
        super(AssertTrue, self).__init__(True, func, *args, **kwargs)


class AssertFalse(AssertEqual):
    def __init__(self, func, *args, **kwargs):
        super(AssertFalse, self).__init__(False, func, *args, **kwargs)


class MotorTest(unittest.TestCase):
    longMessage = True # Used by unittest.TestCase
    ssl = False # If True, connect with SSL, skip if mongod isn't SSL

    def setUp(self):
        super(MotorTest, self).setUp()

        # Store a regular synchronous pymongo Connection for convenience while
        # testing. Set a timeout so we don't hang a test because, say, Mongo
        # isn't up or is hung by a long-running $where clause.
        connectTimeoutMS = socketTimeoutMS = 30 * 1000
        if self.ssl:
            if not have_ssl:
                raise SkipTest("Python compiled without SSL")
            try:
                self.sync_cx = pymongo.MongoClient(
                    host, port, connectTimeoutMS=connectTimeoutMS,
                    socketTimeoutMS=socketTimeoutMS,
{'fraction_non_alphanumeric': 0.09136440746425398, 'fraction_numerical': 0.004362226351078439, 'mean_word_length': 2.266490765171504, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '6048431', 'n_tokens_mistral': 3550, 'n_tokens_neox': 3370, 'n_words': 1063}