Ejemplo n.º 1
0
    def test_repr(self):
        self.skip()
        connection = Connection.paired(self.left, self.right)

        self.assertEqual(
            repr(connection), "Connection(['%s:%s', '%s:%s'])" %
            (self.left[0], self.left[1], self.right[0], self.right[1]))
Ejemplo n.º 2
0
    def _connect(self):
        self._conn = None
        try:
            if len(self.master_args) == 2:
                self._conn = Connection.paired(
                    (str(self.master_args[0]['host']), int(self.master_args[0]['port'])),
                    (str(self.master_args[1]['host']), int(self.master_args[1]['port'])),
                    pool_size=int(self.master_args[0]['query'].get('pool_size','16')))
            else:
                if self.master_args:
                    try:
                        network_timeout = self.master_args[0]['query'].get('network_timeout')
                        if network_timeout is not None:
                            network_timeout = float(network_timeout)
                        master = Connection(str(self.master_args[0]['host']), int(self.master_args[0]['port']),
                                            pool_size=int(self.master_args[0]['query'].get('pool_size','16')),
                                            network_timeout=network_timeout)
                        ###authenticating  when the db requires it to be done.
                        if self.master_args[0].get('username') and self.master_args[0].get('password'):
                            db = database.Database(master, self.master_args[0]['path'][1:])
                            db.authenticate(self.master_args[0]['username'], 
                                            self.master_args[0]['password'])
                        ###
                    except:
                        if self.slave_args:
                            log.exception('Cannot connect to master: %s will use slave: %s' % (self.master_args, self.slave_args))
                            # and continue... to use the slave only
                            master = None
                        else:
                            raise
                else:
                    log.info('No master connection specified, using slaves only: %s' % self.slave_args)
                    master = None

                if self.slave_args:
                    slave = []
                    for a in self.slave_args:
                        network_timeout = a['query'].get('network_timeout')
                        if network_timeout is not None:
                            network_timeout = float(network_timeout)
                        slave.append(
                            Connection(str(a['host']), int(a['port']),
                                       pool_size=int(a['query'].get('pool_size','16')),
                                       slave_okay=True,
                                       network_timeout=network_timeout,
                                      )
                        )
                    if master:
                        self._conn = MasterSlaveConnection(master, slave)
                    else:
                        self._conn = slave[0]

                else:
                    self._conn = master
        except:
            log.exception('Cannot connect to %s %s' % (self.master_args, self.slave_args))
        else:
            #log.info('Connected to %s %s' % (self.master_args, self.slave_args))
            pass
        return self._conn
Ejemplo n.º 3
0
    def test_basic(self):
        self.skip()
        connection = Connection.paired(self.left, self.right)

        db = connection.pymongo_test

        db.drop_collection("test")
        a = {"x": 1}
        db.test.save(a)
        self.assertEqual(a, db.test.find_one())
Ejemplo n.º 4
0
    def test_basic(self):
        self.skip()
        connection = Connection.paired(self.left, self.right)

        db = connection.pymongo_test

        db.drop_collection("test")
        a = {"x": 1}
        db.test.save(a)
        self.assertEqual(a, db.test.find_one())
Ejemplo n.º 5
0
    def test_end_request(self):
        self.skip()
        connection = Connection.paired(self.left, self.right)
        db = connection.pymongo_test

        for _ in range(100):
            db.test.remove({})
            db.test.insert({})
            self.assert_(db.test.find_one())
            connection.end_request()
Ejemplo n.º 6
0
def get_mongo_collection():
    "Open a connection to MongoDB and return the collection to use."
    if settings.RIGHT_MONGODB_HOST:
        connection = Connection.paired(
                left=(settings.MONGODB_HOST, settings.MONGODB_PORT),
                right=(settings.RIGHT_MONGODB_HOST, settings.RIGHT_MONGODB_PORT)
            )
    else:
        connection = Connection(host=settings.MONGODB_HOST, port=settings.MONGODB_PORT)
    return connection[settings.MONGODB_DB][settings.MONGODB_COLLECTION]
Ejemplo n.º 7
0
    def test_end_request(self):
        self.skip()
        connection = Connection.paired(self.left, self.right)
        db = connection.pymongo_test

        for _ in range(100):
            db.test.remove({})
            db.test.insert({})
            self.assert_(db.test.find_one())
            connection.end_request()
Ejemplo n.º 8
0
    def test_repr(self):
        self.skip()
        connection = Connection.paired(self.left, self.right)

        self.assertEqual(repr(connection),
                         "Connection.paired(('%s', %s), ('%s', %s))" %
                         (self.left[0],
                          self.left[1],
                          self.right[0],
                          self.right[1]))
Ejemplo n.º 9
0
    def test_connect(self):
        self.skip()
        self.assertRaises(ConnectionFailure, Connection.paired, self.bad,
                          self.bad)

        connection = Connection.paired(self.left, self.right)
        self.assert_(connection)

        host = connection.host
        port = connection.port

        connection = Connection.paired(self.right, self.left)
        self.assert_(connection)
        self.assertEqual(host, connection.host)
        self.assertEqual(port, connection.port)

        slave = self.left == (host, port) and self.right or self.left
        self.assertRaises(ConnectionFailure, Connection.paired, slave,
                          self.bad)
        self.assertRaises(ConnectionFailure, Connection.paired, self.bad,
                          slave)
Ejemplo n.º 10
0
    def test_connect(self):
        self.skip()
        self.assertRaises(ConnectionFailure, Connection.paired,
                          self.bad, self.bad)

        connection = Connection.paired(self.left, self.right)
        self.assert_(connection)

        host = connection.host
        port = connection.port

        connection = Connection.paired(self.right, self.left)
        self.assert_(connection)
        self.assertEqual(host, connection.host)
        self.assertEqual(port, connection.port)

        slave = self.left == (host, port) and self.right or self.left
        self.assertRaises(ConfigurationError, Connection.paired,
                          slave, self.bad)
        self.assertRaises(ConfigurationError, Connection.paired,
                          self.bad, slave)
Ejemplo n.º 11
0
    def test_pooling(self):
        self.skip()
        connection = Connection.paired(self.left, self.right,
                                       pool_size=3,
                                       auto_start_request=False)
        db = connection.pymongo_test

        for _ in range(100):
            connection.start_request()
            db.test.remove({})
            db.test.insert({})
            self.assert_(db.test.find_one())
            connection.end_request()
Ejemplo n.º 12
0
    def _connect(self):
        self._conn = None
        try:
            if len(self.master_args) == 2:
                self._conn = Connection.paired(
                    (str(self.master_args[0]['host']), int(self.master_args[0]['port'])),
                    (str(self.master_args[1]['host']), int(self.master_args[1]['port'])))
            else:
                if self.master_args:
                    try:
                        network_timeout = self.master_args[0]['query'].get('network_timeout')
                        if network_timeout is not None:
                            network_timeout = float(network_timeout)
                        master = Connection(str(self.master_args[0]['host']), int(self.master_args[0]['port']),
                                            network_timeout=network_timeout)
                    except:
                        if self.slave_args:
                            log.exception('Cannot connect to master: %s will use slave: %s' % (self.master_args, self.slave_args))
                            # and continue... to use the slave only
                            master = None
                        else:
                            raise
                else:
                    log.info('No master connection specified, using slaves only: %s' % self.slave_args)
                    master = None

                if self.slave_args:
                    slave = []
                    for a in self.slave_args:
                        network_timeout = a['query'].get('network_timeout')
                        if network_timeout is not None:
                            network_timeout = float(network_timeout)
                        slave.append(
                            Connection(str(a['host']), int(a['port']),
                                       slave_okay=True,
                                       network_timeout=network_timeout,
                                      )
                        )
                    if master:
                        self._conn = MasterSlaveConnection(master, slave)
                    else:
                        self._conn = slave[0]

                else:
                    self._conn = master
        except:
            log.exception('Cannot connect to %s %s' % (self.master_args, self.slave_args))
        else:
            #log.info('Connected to %s %s' % (self.master_args, self.slave_args))
            pass
        return self._conn
Ejemplo n.º 13
0
    def test_pooling(self):
        self.skip()
        connection = Connection.paired(self.left,
                                       self.right,
                                       pool_size=3,
                                       auto_start_request=False)
        db = connection.pymongo_test

        for _ in range(100):
            connection.start_request()
            db.test.remove({})
            db.test.insert({})
            self.assert_(db.test.find_one())
            connection.end_request()
Ejemplo n.º 14
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.
"""Simple script to help test auto-reconnection."""

import sys
import threading
import time
sys.path[0:0] = [""]

from pymongo.errors import AutoReconnect
from pymongo.connection import Connection

db = Connection.paired(("localhost", 27018)).test
db.test.remove({})


class Something(threading.Thread):
    def run(self):
        while True:
            time.sleep(1)
            try:
                id = db.test.save({"x": 1}, safe=True)
                assert db.test.find_one(id)["x"] == 1
                db.test.remove(id)
                db.connection.end_request()
                print "Y"
            except AutoReconnect, e:
                print e
# 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.

"""Simple script to help test auto-reconnection."""

import sys
import threading
import time
sys.path[0:0] = [""]

from pymongo.errors import AutoReconnect
from pymongo.connection import Connection

db = Connection.paired(("localhost", 27018)).test
db.test.remove({})


class Something(threading.Thread):
    def run(self):
        while True:
            time.sleep(1)
            try:
                id = db.test.save({"x": 1}, safe=True)
                assert db.test.find_one(id)["x"] == 1
                db.test.remove(id)
                db.connection.end_request()
                print "Y"
            except AutoReconnect, e:
                print e
Ejemplo n.º 16
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.

"""Simple script to help test auto-reconnection."""

import threading
import time

from pymongo.errors import ConnectionFailure
from pymongo.connection import Connection

db = Connection.paired(("localhost", 27018), pool_size=10).test
db.test.remove({})

class Something(threading.Thread):
    def run(self):
        while True:
            time.sleep(1)
            try:
                id = db.test.save({"x": 1})
                assert db.test.find_one(id)["x"] == 1
                db.test.remove(id)
                db.connection().end_request()
                print "Y"
            except ConnectionFailure, e:
                print e
                print "N"
Ejemplo n.º 17
0
# 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.
"""Simple script to help test auto-reconnection."""

import threading
import time

from pymongo.errors import ConnectionFailure
from pymongo.connection import Connection

db = Connection.paired(("localhost", 27018), pool_size=10).test
db.test.remove({})


class Something(threading.Thread):
    def run(self):
        while True:
            time.sleep(1)
            try:
                id = db.test.save({"x": 1})
                assert db.test.find_one(id)["x"] == 1
                db.test.remove(id)
                db.connection().end_request()
                print "Y"
            except ConnectionFailure, e:
                print e
Ejemplo n.º 18
0
 def test_paired_connections_pass_individual_connargs(self):
     c = Connection.paired(self.left, self.right, network_timeout=5)
     self.assertEqual(5, c._Connection__network_timeout)
Ejemplo n.º 19
0
 def test_paired_connections_pass_individual_connargs(self):
     c = Connection.paired(self.left, self.right, slave_okay=True)
     self.assertTrue(c.__slave_okay)
Ejemplo n.º 20
0
 def test_paired_connections_pass_individual_connargs(self):
     c = Connection.paired(self.left, self.right, network_timeout=5)
     self.assertEqual(5, c._Connection__net_timeout)