@@ -135,39 +135,36 @@ def test_iteration(self):
135135 with self .assertRaises (StopIteration ):
136136 next (change_stream )
137137
138+ def _test_next_blocks (self , change_stream ):
139+ inserted_doc = {'_id' : ObjectId ()}
140+ changes = []
141+ t = threading .Thread (
142+ target = lambda : changes .append (change_stream .next ()))
143+ t .start ()
144+ # Sleep for a bit to prove that the call to next() blocks.
145+ time .sleep (1 )
146+ self .assertTrue (t .is_alive ())
147+ self .assertFalse (changes )
148+ self .coll .insert_one (inserted_doc )
149+ # Join with large timeout to give the server time to return the change,
150+ # in particular for shard clusters.
151+ t .join (30 )
152+ self .assertFalse (t .is_alive ())
153+ self .assertEqual (1 , len (changes ))
154+ self .assertEqual (changes [0 ]['operationType' ], 'insert' )
155+ self .assertEqual (changes [0 ]['fullDocument' ], inserted_doc )
156+
138157 def test_next_blocks (self ):
139158 """Test that next blocks until a change is readable"""
140- inserted_doc = {'_id' : ObjectId ()}
141159 # Use a short await time to speed up the test.
142160 with self .coll .watch (max_await_time_ms = 250 ) as change_stream :
143- changes = []
144- t = threading .Thread (
145- target = lambda : changes .append (change_stream .next ()))
146- t .start ()
147- self .coll .insert_one (inserted_doc )
148- time .sleep (1 )
149- t .join (3 )
150- self .assertFalse (t .is_alive ())
151- self .assertEqual (1 , len (changes ))
152- self .assertEqual (changes [0 ]['operationType' ], 'insert' )
153- self .assertEqual (changes [0 ]['fullDocument' ], inserted_doc )
161+ self ._test_next_blocks (change_stream )
154162
155163 def test_aggregate_cursor_blocks (self ):
156164 """Test that an aggregate cursor blocks until a change is readable."""
157- inserted_doc = {'_id' : ObjectId ()}
158165 with self .coll .aggregate ([{'$changeStream' : {}}],
159166 maxAwaitTimeMS = 250 ) as change_stream :
160- changes = []
161- t = threading .Thread (
162- target = lambda : changes .append (change_stream .next ()))
163- t .start ()
164- self .coll .insert_one (inserted_doc )
165- time .sleep (1 )
166- t .join (3 )
167- self .assertFalse (t .is_alive ())
168- self .assertEqual (1 , len (changes ))
169- self .assertEqual (changes [0 ]['operationType' ], 'insert' )
170- self .assertEqual (changes [0 ]['fullDocument' ], inserted_doc )
167+ self ._test_next_blocks (change_stream )
171168
172169 def test_concurrent_close (self ):
173170 """Ensure a ChangeStream can be closed from another thread."""
@@ -188,7 +185,7 @@ def test_update_resume_token(self):
188185 """ChangeStream must continuously track the last seen resumeToken."""
189186 with self .coll .watch () as change_stream :
190187 self .assertIsNone (change_stream ._resume_token )
191- for i in range (10 ):
188+ for i in range (3 ):
192189 self .coll .insert_one ({})
193190 change = next (change_stream )
194191 self .assertEqual (change ['_id' ], change_stream ._resume_token )
0 commit comments