MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / test_exhaust

Method test_exhaust

test/test_collection.py:1780–1840  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1778 self.db.test.find(no_cursor_timeout=False).to_list()
1779
1780 def test_exhaust(self):
1781 if is_mongos(self.db.client):
1782 with self.assertRaises(InvalidOperation):
1783 next(self.db.test.find(cursor_type=CursorType.EXHAUST))
1784 return
1785
1786 # Limit is incompatible with exhaust.
1787 with self.assertRaises(InvalidOperation):
1788 next(self.db.test.find(cursor_type=CursorType.EXHAUST, limit=5))
1789 cur = self.db.test.find(cursor_type=CursorType.EXHAUST)
1790 with self.assertRaises(InvalidOperation):
1791 cur.limit(5)
1792 cur.next()
1793 cur = self.db.test.find(limit=5)
1794 with self.assertRaises(InvalidOperation):
1795 cur.add_option(64)
1796 cur = self.db.test.find()
1797 cur.add_option(64)
1798 with self.assertRaises(InvalidOperation):
1799 cur.limit(5)
1800
1801 self.db.drop_collection("test")
1802 # Insert enough documents to require more than one batch
1803 self.db.test.insert_many([{"i": i} for i in range(150)])
1804
1805 client = self.rs_or_single_client(maxPoolSize=1)
1806 pool = get_pool(client)
1807
1808 # Make sure the socket is returned after exhaustion.
1809 cur = client[self.db.name].test.find(cursor_type=CursorType.EXHAUST)
1810 next(cur)
1811 self.assertEqual(0, len(pool.conns))
1812 for _ in cur:
1813 pass
1814 self.assertEqual(1, len(pool.conns))
1815
1816 # Same as previous but don't call next()
1817 for _ in client[self.db.name].test.find(cursor_type=CursorType.EXHAUST):
1818 pass
1819 self.assertEqual(1, len(pool.conns))
1820
1821 # If the Cursor instance is discarded before being completely iterated
1822 # and the socket has pending data (more_to_come=True) we have to close
1823 # and discard the socket.
1824 cur = client[self.db.name].test.find(cursor_type=CursorType.EXHAUST, batch_size=2)
1825 if client_context.version.at_least(4, 2):
1826 # On 4.2+ we use OP_MSG which only sets more_to_come=True after the
1827 # first getMore.
1828 for _ in range(3):
1829 next(cur)
1830 else:
1831 next(cur)
1832 self.assertEqual(0, len(pool.conns))
1833 # if sys.platform.startswith("java") or "PyPy" in sys.version:
1834 # # Don't wait for GC or use gc.collect(), it's unreliable.
1835 cur.close()
1836 cur = None
1837 # Wait until the background thread returns the socket.

Callers

nothing calls this directly

Calls 13

is_mongosFunction · 0.90
nextFunction · 0.90
get_poolFunction · 0.90
wait_untilFunction · 0.85
rs_or_single_clientMethod · 0.80
at_leastMethod · 0.80
findMethod · 0.45
limitMethod · 0.45
nextMethod · 0.45
add_optionMethod · 0.45
drop_collectionMethod · 0.45
insert_manyMethod · 0.45

Tested by

no test coverage detected