Skip to content

Commit 0b72f88

Browse files
committed
PYTHON-1871 ChangeStreams must honor batchSize
1 parent aefd02a commit 0b72f88

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

pymongo/change_stream.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ def _change_stream_options(self):
112112

113113
def _command_options(self):
114114
"""Return the options dict for the aggregation command."""
115-
options = {'cursor': {}}
115+
options = {}
116116
if self._max_await_time_ms is not None:
117117
options["maxAwaitTimeMS"] = self._max_await_time_ms
118+
if self._batch_size is not None:
119+
options["batchSize"] = self._batch_size
118120
return options
119121

120122
def _aggregation_pipeline(self):

test/test_change_stream.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,34 @@ def test_try_next_runs_one_getmore(self):
139139
self.assertEqual(listener.started_command_names(), ["getMore"])
140140
self.assertIsNone(stream.try_next())
141141

142+
def test_batch_size_is_honored(self):
143+
listener = EventListener()
144+
client = rs_or_single_client(event_listeners=[listener])
145+
# Connect to the cluster.
146+
client.admin.command('ping')
147+
listener.results.clear()
148+
# ChangeStreams only read majority committed data so use w:majority.
149+
coll = self.watched_collection().with_options(
150+
write_concern=WriteConcern("majority"))
151+
coll.drop()
152+
# Create the watched collection before starting the change stream to
153+
# skip any "create" events.
154+
coll.insert_one({'_id': 1})
155+
self.addCleanup(coll.drop)
156+
# Expected batchSize.
157+
expected = {'batchSize': 23}
158+
with self.change_stream_with_client(
159+
client, max_await_time_ms=250, batch_size=23) as stream:
160+
# Confirm that batchSize is honored for initial batch.
161+
cmd = listener.results['started'][0].command
162+
self.assertEqual(cmd['cursor'], expected)
163+
listener.results.clear()
164+
# Confirm that batchSize is honored by getMores.
165+
self.assertIsNone(stream.try_next())
166+
cmd = listener.results['started'][0].command
167+
key = next(iter(expected))
168+
self.assertEqual(expected[key], cmd[key])
169+
142170

143171
class TestClusterChangeStream(IntegrationTest, ChangeStreamTryNextMixin):
144172

0 commit comments

Comments
 (0)