本文整理汇总了Python中test.utils.connected函数的典型用法代码示例。如果您正苦于以下问题:Python connected函数的具体用法?Python connected怎么用?Python connected使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了connected函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_local_threshold
def test_local_threshold(self):
client = connected(self.mock_client(localThresholdMS=30))
self.assertEqual(30, client.local_threshold_ms)
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
topology = client._topology
# All are within a 30-ms latency window, see self.mock_client().
self.assertEqual(set([('a', 1), ('b', 2), ('c', 3)]),
writable_addresses(topology))
# No error
client.db.collection.find_one()
client = connected(self.mock_client(localThresholdMS=0))
self.assertEqual(0, client.local_threshold_ms)
# No error
client.db.collection.find_one()
# Our chosen mongos goes down.
client.kill_host('%s:%s' % next(iter(client.nodes)))
try:
client.db.collection.find_one()
except:
pass
# No error
client.db.collection.find_one()
开发者ID:wmanley,项目名称:mongo-python-driver,代码行数:25,代码来源:test_mongos_load_balancing.py
示例2: test_ssl_pem_passphrase
def test_ssl_pem_passphrase(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
vi = sys.version_info
if vi[0] == 2 and vi < (2, 7, 9) or vi[0] == 3 and vi < (3, 3):
self.assertRaises(
ConfigurationError,
MongoClient,
'localhost',
ssl=True,
ssl_certfile=CLIENT_ENCRYPTED_PEM,
ssl_pem_passphrase="clientpassword",
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100)
else:
connected(MongoClient('localhost',
ssl=True,
ssl_certfile=CLIENT_ENCRYPTED_PEM,
ssl_pem_passphrase="clientpassword",
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100,
**self.credentials))
uri_fmt = ("mongodb://localhost/?ssl=true"
"&ssl_certfile=%s&ssl_pem_passphrase=clientpassword"
"&ssl_ca_certs=%s&serverSelectionTimeoutMS=100")
connected(MongoClient(uri_fmt % (CLIENT_ENCRYPTED_PEM, CA_PEM),
**self.credentials))
开发者ID:ShaneHarvey,项目名称:mongo-python-driver,代码行数:30,代码来源:test_ssl.py
示例3: test_ssl_pem_passphrase
def test_ssl_pem_passphrase(self):
# Expects the server to be running with server.pem and ca.pem
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
if not CERT_SSL:
raise SkipTest("No mongod available over SSL with certs")
vi = sys.version_info
if vi[0] == 2 and vi < (2, 7, 9) or vi[0] == 3 and vi < (3, 3):
self.assertRaises(
ConfigurationError,
MongoClient,
'server',
ssl=True,
ssl_certfile=CLIENT_ENCRYPTED_PEM,
ssl_pem_passphrase="clientpassword",
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100)
else:
connected(MongoClient('server',
ssl=True,
ssl_certfile=CLIENT_ENCRYPTED_PEM,
ssl_pem_passphrase="clientpassword",
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100))
uri_fmt = ("mongodb://server/?ssl=true"
"&ssl_certfile=%s&ssl_pem_passphrase=clientpassword"
"&ssl_ca_certs=%s&serverSelectionTimeoutMS=100")
connected(MongoClient(uri_fmt % (CLIENT_ENCRYPTED_PEM, CA_PEM)))
开发者ID:HermogenesBatista,项目名称:mongo-python-driver,代码行数:31,代码来源:test_ssl.py
示例4: test_mongo_client
def test_mongo_client(self):
pair = client_context.pair
m = rs_or_single_client(w=0)
coll = m.pymongo_test.write_concern_test
coll.drop()
doc = {"_id": ObjectId()}
coll.insert_one(doc)
self.assertTrue(coll.insert_one(doc))
coll = coll.with_options(write_concern=WriteConcern(w=1))
self.assertRaises(OperationFailure, coll.insert_one, doc)
m = rs_or_single_client()
coll = m.pymongo_test.write_concern_test
new_coll = coll.with_options(write_concern=WriteConcern(w=0))
self.assertTrue(new_coll.insert_one(doc))
self.assertRaises(OperationFailure, coll.insert_one, doc)
m = rs_or_single_client("mongodb://%s/" % (pair,),
replicaSet=client_context.replica_set_name)
coll = m.pymongo_test.write_concern_test
self.assertRaises(OperationFailure, coll.insert_one, doc)
m = rs_or_single_client("mongodb://%s/?w=0" % (pair,),
replicaSet=client_context.replica_set_name)
coll = m.pymongo_test.write_concern_test
coll.insert_one(doc)
# Equality tests
direct = connected(single_client(w=0))
direct2 = connected(single_client("mongodb://%s/?w=0" % (pair,),
**self.credentials))
self.assertEqual(direct, direct2)
self.assertFalse(direct != direct2)
开发者ID:ShaneHarvey,项目名称:mongo-python-driver,代码行数:34,代码来源:test_common.py
示例5: test_alive
def test_alive(self):
primary = ha_tools.get_primary()
secondary = ha_tools.get_random_secondary()
primary_cx = connected(
MongoClient(
primary,
serverSelectionTimeoutMS=self.server_selection_timeout)),
secondary_cx = connected(
MongoClient(
secondary,
serverSelectionTimeoutMS=self.server_selection_timeout))
rsc = connected(
MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout))
self.assertTrue(primary_cx.alive())
self.assertTrue(secondary_cx.alive())
self.assertTrue(rsc.alive())
ha_tools.kill_primary()
time.sleep(0.5)
self.assertFalse(primary_cx.alive())
self.assertTrue(secondary_cx.alive())
self.assertFalse(rsc.alive())
ha_tools.kill_members([secondary], 2)
time.sleep(0.5)
self.assertFalse(primary_cx.alive())
self.assertFalse(secondary_cx.alive())
self.assertFalse(rsc.alive())
开发者ID:BiosPsucheZoe,项目名称:mongo-python-driver,代码行数:34,代码来源:test_ha.py
示例6: test_local_threshold
def test_local_threshold(self):
client = connected(self.mock_client(localThresholdMS=30))
self.assertEqual(30, client.local_threshold_ms)
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
topology = client._topology
# All are within a 30-ms latency window, see self.mock_client().
self.assertEqual(set([('a', 1), ('b', 2), ('c', 3)]),
writable_addresses(topology))
# No error
client.admin.command('ismaster')
client = connected(self.mock_client(localThresholdMS=0))
self.assertEqual(0, client.local_threshold_ms)
# No error
client.db.command('ismaster')
# Our chosen mongos goes down.
client.kill_host('%s:%s' % next(iter(client.nodes)))
try:
client.db.command('ismaster')
except:
pass
# We eventually connect to a new mongos.
def connect_to_new_mongos():
try:
return client.db.command('ismaster')
except AutoReconnect:
pass
wait_until(connect_to_new_mongos, 'connect to a new mongos')
开发者ID:nly,项目名称:mongo-python-driver,代码行数:31,代码来源:test_mongos_load_balancing.py
示例7: test_atexit_hook
def test_atexit_hook(self):
client = single_client(client_context.host, client_context.port)
executor = one(client._topology._servers.values())._monitor._executor
connected(client)
# The executor stores a weakref to itself in _EXECUTORS.
ref = one([r for r in _EXECUTORS.copy() if r() is executor])
del executor
del client
wait_until(partial(unregistered, ref), 'unregister executor',
timeout=5)
开发者ID:ShaneHarvey,项目名称:mongo-python-driver,代码行数:13,代码来源:test_monitor.py
示例8: test_auth_from_uri
def test_auth_from_uri(self):
self.client.admin.add_user("admin", "pass", roles=["root"])
self.addCleanup(self.client.admin.remove_user, "admin")
self.addCleanup(remove_all_users, self.client.pymongo_test)
self.client.pymongo_test.add_user("user", "pass", roles=["userAdmin", "readWrite"])
with self.assertRaises(OperationFailure):
connected(rs_or_single_client("mongodb://a:[email protected]%s:%d" % (host, port)))
# No error.
connected(rs_or_single_client_noauth("mongodb://admin:[email protected]%s:%d" % (host, port)))
# Wrong database.
uri = "mongodb://admin:[email protected]%s:%d/pymongo_test" % (host, port)
with self.assertRaises(OperationFailure):
connected(rs_or_single_client(uri))
# No error.
connected(rs_or_single_client_noauth("mongodb://user:[email protected]%s:%d/pymongo_test" % (host, port)))
# Auth with lazy connection.
rs_or_single_client(
"mongodb://user:[email protected]%s:%d/pymongo_test" % (host, port), connect=False
).pymongo_test.test.find_one()
# Wrong password.
bad_client = rs_or_single_client("mongodb://user:[email protected]%s:%d/pymongo_test" % (host, port), connect=False)
self.assertRaises(OperationFailure, bad_client.pymongo_test.test.find_one)
开发者ID:rychipman,项目名称:mongo-python-driver,代码行数:30,代码来源:test_client.py
示例9: test_ssl_crlfile_support
def test_ssl_crlfile_support(self):
if not hasattr(ssl, 'VERIFY_CRL_CHECK_LEAF'):
self.assertRaises(
ConfigurationError,
MongoClient,
'localhost',
ssl=True,
ssl_ca_certs=CA_PEM,
ssl_crlfile=CRL_PEM,
serverSelectionTimeoutMS=100)
else:
connected(MongoClient('localhost',
ssl=True,
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100,
**self.credentials))
with self.assertRaises(ConnectionFailure):
connected(MongoClient('localhost',
ssl=True,
ssl_ca_certs=CA_PEM,
ssl_crlfile=CRL_PEM,
serverSelectionTimeoutMS=100,
**self.credentials))
uri_fmt = ("mongodb://localhost/?ssl=true&"
"ssl_ca_certs=%s&serverSelectionTimeoutMS=100")
connected(MongoClient(uri_fmt % (CA_PEM,),
**self.credentials))
uri_fmt = ("mongodb://localhost/?ssl=true&ssl_crlfile=%s"
"&ssl_ca_certs=%s&serverSelectionTimeoutMS=100")
with self.assertRaises(ConnectionFailure):
connected(MongoClient(uri_fmt % (CRL_PEM, CA_PEM),
**self.credentials))
开发者ID:ShaneHarvey,项目名称:mongo-python-driver,代码行数:35,代码来源:test_ssl.py
示例10: test_auth_network_error
def test_auth_network_error(self):
# Make sure there's no semaphore leak if we get a network error
# when authenticating a new socket with cached credentials.
# Get a client with one socket so we detect if it's leaked.
c = connected(rs_or_single_client(maxPoolSize=1,
waitQueueTimeoutMS=1))
# Simulate an authenticate() call on a different socket.
credentials = auth._build_credentials_tuple(
'DEFAULT', 'admin', db_user, db_pwd, {})
c._cache_credentials('test', credentials, connect=False)
# Cause a network error on the actual socket.
pool = get_pool(c)
socket_info = one(pool.sockets)
socket_info.sock.close()
# SocketInfo.check_auth logs in with the new credential, but gets a
# socket.error. Should be reraised as AutoReconnect.
self.assertRaises(AutoReconnect, c.test.collection.find_one)
# No semaphore leak, the pool is allowed to make a new socket.
c.test.collection.find_one()
开发者ID:gregbanks,项目名称:mongo-python-driver,代码行数:25,代码来源:test_client.py
示例11: test_failover
def test_failover(self):
nthreads = 10
client = connected(self.mock_client(localThresholdMS=0.001))
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
# Our chosen mongos goes down.
client.kill_host('a:1')
# Trigger failover to higher-latency nodes. AutoReconnect should be
# raised at most once in each thread.
passed = []
def f():
try:
client.db.command('ismaster')
except AutoReconnect:
# Second attempt succeeds.
client.db.command('ismaster')
passed.append(True)
threads = [threading.Thread(target=f) for _ in range(nthreads)]
for t in threads:
t.start()
for t in threads:
t.join()
self.assertEqual(nthreads, len(passed))
# Down host removed from list.
self.assertEqual(2, len(client.nodes))
开发者ID:BlazeMediaGroup,项目名称:mongo-python-driver,代码行数:32,代码来源:test_mongos_load_balancing.py
示例12: test_load_balancing
def test_load_balancing(self):
# Although the server selection JSON tests already prove that
# select_servers works for sharded topologies, here we do an end-to-end
# test of discovering servers' round trip times and configuring
# localThresholdMS.
client = connected(self.mock_client())
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
# Prohibited for topology type Sharded.
with self.assertRaises(InvalidOperation):
client.address
topology = client._topology
self.assertEqual(TOPOLOGY_TYPE.Sharded,
topology.description.topology_type)
# a and b are within the 15-ms latency window, see self.mock_client().
self.assertEqual(set([('a', 1), ('b', 2)]),
writable_addresses(topology))
client.mock_rtts['a:1'] = 0.040
# Discover only b is within latency window.
wait_until(lambda: set([('b', 2)]) == writable_addresses(topology),
'discover server "a" is too far')
开发者ID:BlazeMediaGroup,项目名称:mongo-python-driver,代码行数:25,代码来源:test_mongos_load_balancing.py
示例13: test_local_threshold
def test_local_threshold(self):
client = connected(self.mock_client(localThresholdMS=30))
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
topology = client._topology
# All are within a 30-ms latency window, see self.mock_client().
self.assertEqual(set([('a', 1), ('b', 2), ('c', 3)]),
writable_addresses(topology))
开发者ID:BlazeMediaGroup,项目名称:mongo-python-driver,代码行数:8,代码来源:test_mongos_load_balancing.py
示例14: test_connect_with_internal_ips
def test_connect_with_internal_ips(self):
# Client is passed an IP it can reach, 'a:1', but the RS config
# only contains unreachable IPs like 'internal-ip'. PYTHON-608.
with self.assertRaises(AutoReconnect) as context:
connected(MockClient(
standalones=[],
members=['a:1'],
mongoses=[],
ismaster_hosts=['internal-ip:27017'],
host='a:1',
replicaSet='rs',
serverSelectionTimeoutMS=100))
self.assertEqual(
"Could not reach any servers in [('internal-ip', 27017)]."
" Replica set is configured with internal hostnames or IPs?",
str(context.exception))
开发者ID:BiosPsucheZoe,项目名称:mongo-python-driver,代码行数:17,代码来源:test_replica_set_client.py
示例15: test_validation_with_system_ca_certs
def test_validation_with_system_ca_certs(self):
# Expects the server to be running with the server.pem, ca.pem
# and crl.pem provided in mongodb and the server tests eg:
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
# --sslCRLFile=/path/to/pymongo/test/certificates/crl.pem
# --sslWeakCertificateValidation
#
# Also requires an /etc/hosts entry where "server" is resolvable
if not CERT_SSL:
raise SkipTest("No mongod available over SSL with certs")
if not SERVER_IS_RESOLVABLE:
raise SkipTest("No hosts entry for 'server'. Cannot validate "
"hostname in the certificate")
if sys.platform == "win32":
raise SkipTest("Can't test system ca certs on Windows.")
if sys.version_info < (2, 7, 9):
raise SkipTest("Can't load system CA certificates.")
# Tell OpenSSL where CA certificates live.
os.environ['SSL_CERT_FILE'] = CA_PEM
try:
with self.assertRaises(ConnectionFailure):
# Server cert is verified but hostname matching fails
connected(MongoClient(pair,
ssl=True,
serverSelectionTimeoutMS=100))
# Server cert is verified. Disable hostname matching.
connected(MongoClient(pair,
ssl=True,
ssl_match_hostname=False,
serverSelectionTimeoutMS=100))
# Server cert and hostname are verified.
connected(MongoClient('server',
ssl=True,
serverSelectionTimeoutMS=100))
# Server cert and hostname are verified.
connected(
MongoClient(
'mongodb://server/?ssl=true&serverSelectionTimeoutMS=100'))
finally:
os.environ.pop('SSL_CERT_FILE')
开发者ID:BlazeMediaGroup,项目名称:mongo-python-driver,代码行数:50,代码来源:test_ssl.py
示例16: get_client
def get_client():
# Attempt a direct connection to each node until one succeeds. Using a
# non-PRIMARY read preference allows us to use the node even if it's a
# secondary.
for i, node in enumerate(nodes.keys()):
try:
return connected(
pymongo.MongoClient(
node,
read_preference=ReadPreference.PRIMARY_PREFERRED))
except pymongo.errors.ConnectionFailure:
if i == len(nodes) - 1:
raise
开发者ID:BiosPsucheZoe,项目名称:mongo-python-driver,代码行数:13,代码来源:ha_tools.py
示例17: test_exhaust_network_error
def test_exhaust_network_error(self):
# When doing an exhaust query, the socket stays checked out on success
# but must be checked in on error to avoid semaphore leaks.
client = rs_or_single_client(maxPoolSize=1)
collection = client.pymongo_test.test
pool = get_pool(client)
pool._check_interval_seconds = None # Never check.
# Ensure a socket.
connected(client)
# Cause a network error.
sock_info = one(pool.sockets)
sock_info.sock.close()
cursor = collection.find(cursor_type=CursorType.EXHAUST)
with self.assertRaises(ConnectionFailure):
next(cursor)
self.assertTrue(sock_info.closed)
# The semaphore was decremented despite the error.
self.assertTrue(pool._socket_semaphore.acquire(blocking=False))
开发者ID:gregbanks,项目名称:mongo-python-driver,代码行数:22,代码来源:test_client.py
示例18: test_socket_timeout_ms_validation
def test_socket_timeout_ms_validation(self):
c = rs_or_single_client(socketTimeoutMS=10 * 1000)
self.assertEqual(10, get_pool(c).opts.socket_timeout)
c = connected(rs_or_single_client(socketTimeoutMS=None))
self.assertEqual(None, get_pool(c).opts.socket_timeout)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS=0)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS=-1)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS=1e10)
self.assertRaises(ValueError, rs_or_single_client, socketTimeoutMS="foo")
开发者ID:rychipman,项目名称:mongo-python-driver,代码行数:14,代码来源:test_client.py
示例19: test_reconnect
def test_reconnect(self):
nthreads = 10
client = connected(self.mock_client())
# connected() ensures we've contacted at least one mongos. Wait for
# all of them.
wait_until(lambda: len(client.nodes) == 3, 'connect to all mongoses')
# Trigger reconnect.
client.close()
do_simple_op(client, nthreads)
wait_until(lambda: len(client.nodes) == 3,
'reconnect to all mongoses')
开发者ID:BlazeMediaGroup,项目名称:mongo-python-driver,代码行数:14,代码来源:test_mongos_load_balancing.py
示例20: test_cert_ssl_validation_hostname_matching
def test_cert_ssl_validation_hostname_matching(self):
# Expects the server to be running with the server.pem, ca.pem
# and crl.pem provided in mongodb and the server tests eg:
#
# --sslPEMKeyFile=/path/to/pymongo/test/certificates/server.pem
# --sslCAFile=/path/to/pymongo/test/certificates/ca.pem
# --sslCRLFile=/path/to/pymongo/test/certificates/crl.pem
if not CERT_SSL:
raise SkipTest("No mongod available over SSL with certs")
response = ssl_client.admin.command('ismaster')
with self.assertRaises(ConnectionFailure):
connected(MongoClient(pair,
ssl=True,
ssl_certfile=CLIENT_PEM,
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100))
connected(MongoClient(pair,
ssl=True,
ssl_certfile=CLIENT_PEM,
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=CA_PEM,
ssl_match_hostname=False,
serverSelectionTimeoutMS=100))
if 'setName' in response:
with self.assertRaises(ConnectionFailure):
connected(MongoClient(pair,
replicaSet=response['setName'],
ssl=True,
ssl_certfile=CLIENT_PEM,
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=CA_PEM,
serverSelectionTimeoutMS=100))
connected(MongoClient(pair,
replicaSet=response['setName'],
ssl=True,
ssl_certfile=CLIENT_PEM,
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=CA_PEM,
ssl_match_hostname=False,
serverSelectionTimeoutMS=100))
开发者ID:llvtt,项目名称:mongo-python-driver,代码行数:47,代码来源:test_ssl.py
注:本文中的test.utils.connected函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论