예제 #1
0
    def test_multiple_connections(self):
        a = get_connection()
        b = get_connection()
        self.assertEqual(0, len(a._Connection__pool.sockets))
        self.assertEqual(0, len(b._Connection__pool.sockets))

        a.test.test.find_one()
        a.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(0, len(b._Connection__pool.sockets))
        a_sock = a._Connection__pool.sockets[0]

        b.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(0, len(b._Connection__pool.sockets))

        b.test.test.find_one()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(0, len(b._Connection__pool.sockets))

        b.end_request()
        b_sock = b._Connection__pool.sockets[0]
        b.test.test.find_one()
        a.test.test.find_one()
        self.assertEqual(b_sock, b._Connection__pool.socket())
        self.assertEqual(a_sock, a._Connection__pool.socket())
    def test_multiple_connections(self):
        a = get_connection()
        b = get_connection()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(1, len(b._Connection__pool.sockets))

        a.test.test.find_one()
        a.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(1, len(b._Connection__pool.sockets))
        a_sock = a._Connection__pool.sockets[0]

        b.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(1, len(b._Connection__pool.sockets))

        b.test.test.find_one()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(0, len(b._Connection__pool.sockets))

        b.end_request()
        b_sock = b._Connection__pool.sockets[0]
        b.test.test.find_one()
        a.test.test.find_one()
        self.assertEqual(b_sock,
                         b._Connection__pool.get_socket(b.host, b.port))
        self.assertEqual(a_sock,
                         a._Connection__pool.get_socket(a.host, a.port))
예제 #3
0
    def test_multiple_connections(self):
        a = get_connection(auto_start_request=False)
        b = get_connection(auto_start_request=False)
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(1, len(b._Connection__pool.sockets))

        a.start_request()
        a.test.test.find_one()
        self.assertEqual(0, len(a._Connection__pool.sockets))
        a.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(1, len(b._Connection__pool.sockets))
        a_sock = one(a._Connection__pool.sockets)

        b.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(1, len(b._Connection__pool.sockets))

        b.start_request()
        b.test.test.find_one()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        self.assertEqual(0, len(b._Connection__pool.sockets))

        b.end_request()
        b_sock = one(b._Connection__pool.sockets)
        b.test.test.find_one()
        a.test.test.find_one()

        self.assertEqual(b_sock,
                         b._Connection__pool.get_socket((b.host, b.port)))
        self.assertEqual(a_sock,
                         a._Connection__pool.get_socket((a.host, a.port)))
예제 #4
0
    def test_auto_auth_login(self):
        conn = get_connection()
        self.assertRaises(OperationFailure, conn.auth_test.test.find_one)

        # Admin auth
        conn = get_connection()
        conn.admin.authenticate("admin-user", "password")

        threads = []
        for _ in xrange(10):
            t = AutoAuthenticateThreads(conn.auth_test.test, 100)
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
            self.assertTrue(t.success)

        # Database-specific auth
        conn = get_connection()
        conn.auth_test.authenticate("test-user", "password")

        threads = []
        for _ in xrange(10):
            t = AutoAuthenticateThreads(conn.auth_test.test, 100)
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
            self.assertTrue(t.success)
예제 #5
0
 def setUp(self):
     self.db = get_connection().pymongo_test
     self.db.drop_collection("fs.files")
     self.db.drop_collection("fs.chunks")
     self.db.drop_collection("pymongo_test.files")
     self.db.drop_collection("pymongo_test.chunks")
     self.fs = gridfs.GridFS(self.db)
예제 #6
0
 def setUp(self):
     self.db = get_connection().pymongo_test
     self.db.drop_collection("fs.files")
     self.db.drop_collection("fs.chunks")
     self.db.drop_collection("pymongo_test.files")
     self.db.drop_collection("pymongo_test.chunks")
     self.fs = gridfs.GridFS(self.db)
예제 #7
0
 def test_auto_auth_login(self):
     conn = get_connection()
     try:
         conn.auth_test.test.find_one()
         assert False  # Find should have failed
     except OperationFailure, e:
         pass
예제 #8
0
    def test_uuid_queries(self):
        if not should_test_uuid:
            raise SkipTest()

        c = get_connection()
        coll = c.pymongo_test.test
        coll.drop()

        uu = uuid.uuid4()
        coll.insert({'uuid': Binary(uu.bytes, 3)})
        self.assertEqual(1, coll.count())

        # Test UUIDLegacy queries.
        coll.uuid_subtype = 4
        self.assertEqual(0, coll.find({'uuid': uu}).count())
        cur = coll.find({'uuid': UUIDLegacy(uu)})
        self.assertEqual(1, cur.count())
        retrieved = cur.next()['uuid']
        self.assertEqual(uu, retrieved)

        # Test regular UUID queries (using subtype 4).
        coll.insert({'uuid': uu})
        self.assertEqual(2, coll.count())
        cur = coll.find({'uuid': uu})
        self.assertEqual(1, cur.count())
        self.assertEqual(uu, cur.next()['uuid'])

        # Test both.
        cur = coll.find({'uuid': {'$in': [uu, UUIDLegacy(uu)]}})
        self.assertEqual(2, cur.count())
        coll.drop()
    def setUp(self):
        self.c = get_connection()

        # reset the db
        self.c.drop_database(DB)
        self.c[DB].unique.insert({"_id": "mike"})
        self.c[DB].unique.find_one()
예제 #10
0
 def loop(pipe):
     c = get_connection()
     self.assertEqual(0, len(c._Connection__pool.sockets))
     c.test.test.find_one()
     c.end_request()
     self.assertEqual(1, len(c._Connection__pool.sockets))
     pipe.send(c._Connection__pool.sockets[0].getsockname())
예제 #11
0
    def setUp(self):
        self.c = get_connection(auto_start_request=False)

        # reset the db
        self.c.drop_database(DB)
        self.c[DB].unique.insert({"_id": "mike"})
        self.c[DB].test.insert([{} for i in range(1000)])
예제 #12
0
    def setUp(self):
        self.c = get_connection()

        # reset the db
        self.c.drop_database(DB)
        self.c[DB].unique.insert({"_id": "mike"})
        self.c[DB].unique.find_one()
예제 #13
0
    def test_pool_with_greenlets(self):
        try:
            from greenlet import greenlet
        except ImportError:
            raise SkipTest()

        c = get_connection()
        c.test.test.find_one()
        c.end_request()
        self.assertEqual(1, len(c._Connection__pool.sockets))
        a_sock = c._Connection__pool.sockets[0]

        def loop(name, pipe):
            c.test.test.find_one()
            self.assertEqual(0, len(c._Connection__pool.sockets))
            greenlet.getcurrent().parent.switch()
            c.end_request()
            pipe.append(c._Connection__pool.sockets[-1])

        ga1 = []
        ga2 = []

        g1 = greenlet(loop)
        g2 = greenlet(loop)

        g1.switch('g1', ga1)
        g2.switch('g2', ga2)
        g1.switch()
        g2.switch()

        b_sock = ga1[0]
        c_sock = ga2[0]
        self.assert_(a_sock is b_sock)
        self.assert_(a_sock is not c_sock)
        self.assert_(b_sock is not c_sock)
예제 #14
0
    def test_uuid_queries(self):
        if not should_test_uuid:
            raise SkipTest()

        c = get_connection()
        coll = c.pymongo_test.test
        coll.drop()

        uu = uuid.uuid4()
        coll.insert({"uuid": uuid.uuid4()})
        coll.insert({"uuid": uuid.uuid4()})
        coll.insert({"uuid": Binary(uu.bytes, 3)})

        # Test UUIDLegacy queries.
        self.assertEquals(0, coll.find({"uuid": uu}).count())
        cur = coll.find({"uuid": UUIDLegacy(uu)})
        self.assertEquals(1, cur.count())
        retrieved = cur.next()["uuid"]
        self.assertEquals(uu, retrieved)

        # Test regular UUID queries.
        coll.insert({"uuid": uu})
        cur = coll.find({"uuid": uu})
        self.assertEquals(1, cur.count())
        self.assertEquals(uu, cur.next()["uuid"])

        # Test both.
        cur = coll.find({"uuid": {"$in": [uu, UUIDLegacy(uu)]}})
        self.assertEquals(2, cur.count())
        coll.drop()
예제 #15
0
파일: __init__.py 프로젝트: shitara/pymongo
def teardown():
    c = get_connection()

    c.drop_database("pymongo-pooling-tests")
    c.drop_database("pymongo_test")
    c.drop_database("pymongo_test1")
    c.drop_database("pymongo_test2")
    c.drop_database("pymongo_test_mike")
예제 #16
0
def teardown():
    c = get_connection()

    c.drop_database("pymongo-pooling-tests")
    c.drop_database("pymongo_test")
    c.drop_database("pymongo_test1")
    c.drop_database("pymongo_test2")
    c.drop_database("pymongo_test_mike")
예제 #17
0
 def _get_connection(self):
     """
     Intended for overriding in TestThreadsAuthReplicaSet. This method
     returns a Connection here, and a ReplicaSetConnection in
     test_threads_replica_set_connection.py.
     """
     # Regular test connection
     return get_connection()
예제 #18
0
 def _get_connection(self):
     """
     Intended for overriding in TestThreadsAuthReplicaSet. This method
     returns a Connection here, and a ReplicaSetConnection in
     test_threads_replica_set_connection.py.
     """
     # Regular test connection
     return get_connection()
예제 #19
0
 def loop(pipe):
     c = get_connection()
     self.assertEqual(1, len(c._Connection__pool.sockets))
     c.test.test.find_one()
     self.assertEqual(0, len(c._Connection__pool.sockets))
     c.end_request()
     self.assertEqual(1, len(c._Connection__pool.sockets))
     pipe.send(c._Connection__pool.sockets[0].getsockname())
예제 #20
0
    def setUp(self):
        self.c = get_connection(auto_start_request=False)

        # reset the db
        db = self.c[DB]
        db.unique.drop()
        db.test.drop()
        db.unique.insert({"_id": "mike"})
        db.test.insert([{} for i in range(1000)])
예제 #21
0
    def test_pool_with_fork(self):
        # Test that separate Connections have separate Pools, and that the
        # driver can create a new Connection after forking
        if sys.platform == "win32":
            raise SkipTest("Can't test forking on Windows")

        try:
            from multiprocessing import Process, Pipe
        except ImportError:
            raise SkipTest("No multiprocessing module")

        a = get_connection(auto_start_request=False)
        a.test.test.remove(safe=True)
        a.test.test.insert({'_id':1}, safe=True)
        a.test.test.find_one()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        a_sock = one(a._Connection__pool.sockets)

        def loop(pipe):
            c = get_connection(auto_start_request=False)
            self.assertEqual(1,len(c._Connection__pool.sockets))
            c.test.test.find_one()
            self.assertEqual(1,len(c._Connection__pool.sockets))
            pipe.send(one(c._Connection__pool.sockets).sock.getsockname())

        cp1, cc1 = Pipe()
        cp2, cc2 = Pipe()

        p1 = Process(target=loop, args=(cc1,))
        p2 = Process(target=loop, args=(cc2,))

        p1.start()
        p2.start()

        p1.join(1)
        p2.join(1)

        p1.terminate()
        p2.terminate()

        p1.join()
        p2.join()

        cc1.close()
        cc2.close()

        b_sock = cp1.recv()
        c_sock = cp2.recv()
        self.assertTrue(a_sock.sock.getsockname() != b_sock)
        self.assertTrue(a_sock.sock.getsockname() != c_sock)
        self.assertTrue(b_sock != c_sock)
        self.assertEqual(a_sock,
                         a._Connection__pool.get_socket((a.host, a.port)))
예제 #22
0
    def test_pool_with_fork(self):
        if sys.platform == "win32":
            raise SkipTest()

        try:
            from multiprocessing import Process, Pipe
        except ImportError:
            raise SkipTest()

        a = get_connection()
        a.test.test.find_one()
        a.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        a_sock = a._Connection__pool.sockets[0]

        def loop(pipe):
            c = get_connection()
            self.assertEqual(1, len(c._Connection__pool.sockets))
            c.test.test.find_one()
            self.assertEqual(0, len(c._Connection__pool.sockets))
            c.end_request()
            self.assertEqual(1, len(c._Connection__pool.sockets))
            pipe.send(c._Connection__pool.sockets[0].getsockname())

        cp1, cc1 = Pipe()
        cp2, cc2 = Pipe()

        p1 = Process(target=loop, args=(cc1, ))
        p2 = Process(target=loop, args=(cc2, ))

        p1.start()
        p2.start()

        p1.join(1)
        p2.join(1)

        p1.terminate()
        p2.terminate()

        p1.join()
        p2.join()

        cc1.close()
        cc2.close()

        b_sock = cp1.recv()
        c_sock = cp2.recv()
        self.assert_(a_sock.getsockname() != b_sock)
        self.assert_(a_sock.getsockname() != c_sock)
        self.assert_(b_sock != c_sock)
        self.assertEqual(a_sock,
                         a._Connection__pool.get_socket(a.host, a.port))
예제 #23
0
    def test_pool_with_fork(self):
        if sys.platform == "win32":
            raise SkipTest()

        try:
            from multiprocessing import Process, Pipe
        except ImportError:
            raise SkipTest()

        a = get_connection()
        a.test.test.find_one()
        a.end_request()
        self.assertEqual(1, len(a._Connection__pool.sockets))
        a_sock = a._Connection__pool.sockets[0]

        def loop(pipe):
            c = get_connection()
            self.assertEqual(1, len(c._Connection__pool.sockets))
            c.test.test.find_one()
            self.assertEqual(0, len(c._Connection__pool.sockets))
            c.end_request()
            self.assertEqual(1, len(c._Connection__pool.sockets))
            pipe.send(c._Connection__pool.sockets[0].getsockname())

        cp1, cc1 = Pipe()
        cp2, cc2 = Pipe()

        p1 = Process(target=loop, args=(cc1,))
        p2 = Process(target=loop, args=(cc2,))

        p1.start()
        p2.start()

        p1.join(1)
        p2.join(1)

        p1.terminate()
        p2.terminate()

        p1.join()
        p2.join()

        cc1.close()
        cc2.close()

        b_sock = cp1.recv()
        c_sock = cp2.recv()
        self.assert_(a_sock.getsockname() != b_sock)
        self.assert_(a_sock.getsockname() != c_sock)
        self.assert_(b_sock != c_sock)
        self.assertEqual(a_sock,
                         a._Connection__pool.get_socket(a.host, a.port)[0])
예제 #24
0
    def test_max_pool_size(self):
        c = get_connection()

        threads = []
        for i in range(40):
            t = CreateAndReleaseSocket(c)
            t.start()
            threads.append(t)

        for t in threads:
            t.join()

        # There's a race condition, so be lenient
        self.assert_(abs(10 - len(c._Connection__pool.sockets)) < 10)
예제 #25
0
    def test_max_pool_size(self):
        c = get_connection(max_pool_size=4)

        threads = []
        for i in range(40):
            t = CreateAndReleaseSocket(c)
            t.start()
            threads.append(t)

        for t in threads:
            t.join()

        # There's a race condition, so be lenient
        self.assert_(abs(4 - len(c._Connection__pool.sockets)) < 4)
예제 #26
0
    def test_dependent_pools(self):
        c = get_connection()
        self.assertEqual(1, len(c._Connection__pool.sockets))
        c.test.test.find_one()
        self.assertEqual(0, len(c._Connection__pool.sockets))
        c.end_request()
        self.assertEqual(1, len(c._Connection__pool.sockets))

        t = OneOp(c)
        t.start()
        t.join()

        self.assertEqual(1, len(c._Connection__pool.sockets))
        c.test.test.find_one()
        self.assertEqual(0, len(c._Connection__pool.sockets))
예제 #27
0
    def test_dependent_pools(self):
        c = get_connection()
        self.assertEqual(0, len(c._Connection__pool.sockets))
        c.test.test.find_one()
        self.assertEqual(0, len(c._Connection__pool.sockets))
        c.end_request()
        self.assertEqual(1, len(c._Connection__pool.sockets))

        t = OneOp(c)
        t.start()
        t.join()

        self.assertEqual(1, len(c._Connection__pool.sockets))
        c.test.test.find_one()
        self.assertEqual(0, len(c._Connection__pool.sockets))
예제 #28
0
    def setUp(self):
        conn = get_connection()
        self.conn = conn

        # Setup auth users
        conn.admin.system.users.remove({})
        conn.admin.add_user("admin-user", "password")
        try:
            conn.admin.system.users.find_one()
            # If we reach here auth must be disabled in server
            self.tearDown()
            raise SkipTest()
        except OperationFailure:
            pass
        conn.admin.authenticate("admin-user", "password")
        conn.auth_test.system.users.remove({})
        conn.auth_test.add_user("test-user", "password")
예제 #29
0
    def setUp(self):
        self.conn = get_connection()

        # Setup auth users
        self.conn.admin.system.users.remove({})
        self.conn.admin.add_user('admin-user', 'password')
        try:
            self.conn.admin.system.users.find_one()
            # If we reach here mongod was likely started
            # without --auth. Skip this test since it's
            # pointless without auth enabled.
            self.tearDown()
            raise SkipTest()
        except OperationFailure:
            pass
        self.conn.admin.authenticate("admin-user", "password")
        self.conn.auth_test.system.users.remove({})
        self.conn.auth_test.add_user("test-user", "password")
예제 #30
0
    def test_low_network_timeout(self):
        db = None
        i = 0
        n = 10
        while db is None and i < n:
            try:
                db = get_connection(network_timeout=0.0001).pymongo_test
            except AutoReconnect:
                i += 1
        if i == n:
            raise SkipTest()

        threads = []
        for _ in range(4):
            t = IgnoreAutoReconnect(db.test, 100)
            t.start()
            threads.append(t)

        for t in threads:
            t.join()
예제 #31
0
    def test_low_network_timeout(self):
        db = None
        i = 0
        n = 10
        while db is None and i < n:
            try:
                db = get_connection(network_timeout=0.0001).pymongo_test
            except AutoReconnect:
                i += 1
        if i == n:
            raise SkipTest()

        threads = []
        for _ in range(4):
            t = IgnoreAutoReconnect(db.test, 100)
            t.start()
            threads.append(t)

        for t in threads:
            t.join()
예제 #32
0
 def setUp(self):
     self.db = get_connection().pymongo_test
예제 #33
0
        def do_test():
            if use_greenlets:
                try:
                    from gevent import Greenlet
                    from gevent import monkey

                    # Note we don't do patch_thread() or patch_all() - we're
                    # testing here that patch_thread() is unnecessary for
                    # the connection pool to work properly.
                    monkey.patch_socket()
                except ImportError:
                    outcome.value = SKIP
                    return
    
            cx = get_connection(
                use_greenlets=use_greenlets,
                auto_start_request=False
            )

            db = cx.pymongo_test
            db.test.remove(safe=True)
            db.test.insert({'_id': 1})

            history = []

            def find_fast():
                if use_request:
                    cx.start_request()

                history.append('find_fast start')

                # With the old connection._Pool, this would throw
                # AssertionError: "This event is already used by another
                # greenlet"
                results['find_fast_result'] = list(db.test.find())
                history.append('find_fast done')

                if use_request:
                    cx.end_request()

            def find_slow():
                if use_request:
                    cx.start_request()

                history.append('find_slow start')

                # Javascript function that pauses for half a second
                where = delay(0.5)
                results['find_slow_result'] = list(db.test.find(
                    {'$where': where}
                ))

                history.append('find_slow done')

                if use_request:
                    cx.end_request()

            if use_greenlets:
                gr0, gr1 = Greenlet(find_slow), Greenlet(find_fast)
                gr0.start()
                gr1.start_later(.1)
            else:
                gr0 = threading.Thread(target=find_slow)
                gr1 = threading.Thread(target=find_fast)
                gr0.start()
                time.sleep(0.1)
                gr1.start()

            gr0.join()
            gr1.join()

            self.assertEqual([{'_id': 1}], results['find_slow_result'])

            # Fails, since find_fast doesn't complete
            self.assertEqual([{'_id': 1}], results['find_fast_result'])

            self.assertEqual([
                'find_slow start',
                'find_fast start',
                'find_fast done',
                'find_slow done',
            ], history)

            outcome.value = SUCCESS
예제 #34
0
 def setUp(self):
     self.db = get_connection().pymongo_test
     self.db.fs.files.remove({})
     self.db.fs.chunks.remove({})
예제 #35
0
 def setUp(self):
     self.db = get_connection(timeout=-1).pymongo_test
예제 #36
0
 def setUp(self):
     self.db = get_connection().pymongo_test
     self.db.fs.files.remove({})
     self.db.fs.chunks.remove({})
예제 #37
0
 def setUp(self):
     self.connection = get_connection()
     self.db = self.connection.pymongo_test
예제 #38
0
 def test_max_pool_size_with_end_request_only(self):
     # Call end_request() but not start_request()
     c = get_connection(max_pool_size=4, auto_start_request=False)
     self._test_max_pool_size(c, 0, 1)
예제 #39
0
 def setUp(self):
     self.connection = get_connection()
예제 #40
0
 def setUp(self):
     self.db = get_connection().pymongo_test
 def setUp(self):
     self.connection = get_connection()
     self.db = self.connection.pymongo_test
예제 #42
0
 def test_max_pool_size_with_leaked_request(self):
     # Call start_request() but not end_request() -- when threads die, they
     # should return their request sockets to the pool.
     c = get_connection(max_pool_size=4, auto_start_request=False)
     self._test_max_pool_size(c, 1, 0)
예제 #43
0
 def setUp(self):
     self.db = Database(get_connection(), "pymongo_test")
 def setUp(self):
     self.db = Database(get_connection(), "pymongo_test")
 def setUp(self):
     self.connection = get_connection()