| 
 | 1 | +# Copyright 2009-2010 10gen, Inc.  | 
 | 2 | +#  | 
 | 3 | +# Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 4 | +# you may not use this file except in compliance with the License.  | 
 | 5 | +# You may obtain a copy of the License at  | 
 | 6 | +#  | 
 | 7 | +# http://www.apache.org/licenses/LICENSE-2.0  | 
 | 8 | +#  | 
 | 9 | +# Unless required by applicable law or agreed to in writing, software  | 
 | 10 | +# distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 12 | +# See the License for the specific language governing permissions and  | 
 | 13 | +# limitations under the License.  | 
 | 14 | + | 
 | 15 | +"""Test that pymongo is thread safe."""  | 
 | 16 | + | 
 | 17 | +import unittest  | 
 | 18 | +import os  | 
 | 19 | + | 
 | 20 | +from nose.plugins.skip import SkipTest  | 
 | 21 | +from pymongo.replica_set_connection import ReplicaSetConnection  | 
 | 22 | +from test.test_threads import IgnoreAutoReconnect  | 
 | 23 | +from test.test_threads import TestThreads as _testThreads  | 
 | 24 | +from test.test_threads import TestThreadsAuth as _testThreadsAuth  | 
 | 25 | + | 
 | 26 | +from test.test_replica_set_connection import (TestConnectionReplicaSetBase,  | 
 | 27 | +                                              pair)  | 
 | 28 | + | 
 | 29 | +from pymongo.errors import AutoReconnect  | 
 | 30 | + | 
 | 31 | +class TestThreadsReplicaSet(TestConnectionReplicaSetBase, _testThreads):  | 
 | 32 | +    def setUp(self):  | 
 | 33 | +        """  | 
 | 34 | +        Prepare to test all the same things that TestThreads tests, but do it  | 
 | 35 | +        with a replica-set connection  | 
 | 36 | +        """  | 
 | 37 | +        TestConnectionReplicaSetBase.setUp(self)  | 
 | 38 | +        _testThreads.setUp(self)  | 
 | 39 | + | 
 | 40 | +    def _get_connection(self):  | 
 | 41 | +        """  | 
 | 42 | +        Override TestThreads, so its tests run on a ReplicaSetConnection  | 
 | 43 | +        instead of a regular Connection.  | 
 | 44 | +        """  | 
 | 45 | +        return ReplicaSetConnection(pair, replicaSet=self.name)  | 
 | 46 | + | 
 | 47 | +    def test_low_network_timeout(self):  | 
 | 48 | +        """  | 
 | 49 | +        Override the similar test as TestThreads.test_low_connect_timeout(),  | 
 | 50 | +        but use the newer connectTimeoutMS and socketTimeoutMS arguments instead  | 
 | 51 | +        of the soon-to-be-deprecated network_timeout.  | 
 | 52 | +        """  | 
 | 53 | +        db = None  | 
 | 54 | +        n = 10  | 
 | 55 | + | 
 | 56 | +        # First test connection timeout  | 
 | 57 | +        i = 0  | 
 | 58 | +        while db is None and i < n:  | 
 | 59 | +            try:  | 
 | 60 | +                db = ReplicaSetConnection(  | 
 | 61 | +                    pair,  | 
 | 62 | +                    replicaSet=self.name,  | 
 | 63 | +                    connectTimeoutMS=1  | 
 | 64 | +                ).pymongo_test  | 
 | 65 | +            except AutoReconnect:  | 
 | 66 | +                i += 1  | 
 | 67 | +        if i == n:  | 
 | 68 | +            raise SkipTest()  | 
 | 69 | + | 
 | 70 | +        # ... then test socket timeout  | 
 | 71 | +        i = 0  | 
 | 72 | +        while db is None and i < n:  | 
 | 73 | +            try:  | 
 | 74 | +                db = ReplicaSetConnection(  | 
 | 75 | +                    pair,  | 
 | 76 | +                    replicaSet=self.name,  | 
 | 77 | +                    socketTimeoutMS=1  | 
 | 78 | +                ).pymongo_test  | 
 | 79 | +            except AutoReconnect:  | 
 | 80 | +                i += 1  | 
 | 81 | +        if i == n:  | 
 | 82 | +            raise SkipTest()  | 
 | 83 | + | 
 | 84 | +        threads = []  | 
 | 85 | +        for _ in range(4):  | 
 | 86 | +            t = IgnoreAutoReconnect(db.test, 100)  | 
 | 87 | +            t.start()  | 
 | 88 | +            threads.append(t)  | 
 | 89 | + | 
 | 90 | +        for t in threads:  | 
 | 91 | +            t.join()  | 
 | 92 | + | 
 | 93 | + | 
 | 94 | +class TestThreadsAuthReplicaSet(TestConnectionReplicaSetBase, _testThreadsAuth):  | 
 | 95 | + | 
 | 96 | +    def setUp(self):  | 
 | 97 | +        """  | 
 | 98 | +        Prepare to test all the same things that TestThreads tests, but do it  | 
 | 99 | +        with a replica-set connection  | 
 | 100 | +        """  | 
 | 101 | +        TestConnectionReplicaSetBase.setUp(self)  | 
 | 102 | +        _testThreadsAuth.setUp(self)  | 
 | 103 | + | 
 | 104 | +    def _get_connection(self):  | 
 | 105 | +        """  | 
 | 106 | +        Override TestThreadsAuth, so its tests run on a ReplicaSetConnection  | 
 | 107 | +        instead of a regular Connection.  | 
 | 108 | +        """  | 
 | 109 | +        return ReplicaSetConnection(pair, replicaSet=self.name)  | 
 | 110 | + | 
 | 111 | +if __name__ == "__main__":  | 
 | 112 | +    suite = unittest.TestSuite([  | 
 | 113 | +        unittest.makeSuite(TestThreadsReplicaSet),  | 
 | 114 | +        unittest.makeSuite(TestThreadsAuthReplicaSet)  | 
 | 115 | +    ])  | 
 | 116 | +    unittest.TextTestRunner(verbosity=2).run(suite)  | 
0 commit comments