@@ -191,54 +191,42 @@ def test_union(self):
191191
192192 @threading_helper .requires_working_threading ()
193193 def test_no_value_overwrite_race_condition (self ):
194- """Test that concurrent access to missing keys doesn't overwrite values."""
195- # Use a factory that returns unique objects so we can detect overwrites
196194 call_count = 0
197195
198196 def unique_factory ():
199197 nonlocal call_count
200198 call_count += 1
201- # Return a unique object that identifies this call
202199 return f"value_{ call_count } _{ threading .get_ident ()} "
203200
204201 d = defaultdict (unique_factory )
205202 results = {}
206203
207204 def worker (thread_id ):
208- # Multiple threads access the same missing key
209205 for _ in range (5 ):
210206 value = d ['shared_key' ]
211207 if 'shared_key' not in results :
212208 results ['shared_key' ] = value
213- # Small delay to increase chance of race conditions
214209 time .sleep (0.001 )
215210
216- # Start multiple threads
217211 threads = []
218212 for i in range (3 ):
219213 t = threading .Thread (target = worker , args = (i ,))
220214 threads .append (t )
221215 t .start ()
222216
223- # Wait for all threads to complete
224217 for t in threads :
225218 t .join ()
226219
227- # Key should exist in the dictionary
228220 self .assertIn ('shared_key' , d )
229221
230- # All threads should see the same value (no overwrites occurred)
231222 final_value = d ['shared_key' ]
232223 self .assertEqual (results ['shared_key' ], final_value )
233224
234- # The value should be from the first successful factory call
235225 self .assertTrue (final_value .startswith ('value_' ))
236226
237- # Factory should only be called once (since key only inserted once)
238227 self .assertEqual (call_count , 1 )
239228
240229 def test_factory_called_only_when_key_missing (self ):
241- """Test that factory is only called when key is truly missing."""
242230 factory_calls = []
243231
244232 def tracked_factory ():
@@ -247,17 +235,14 @@ def tracked_factory():
247235
248236 d = defaultdict (tracked_factory )
249237
250- # First access should call factory
251238 value1 = d ['key' ]
252239 self .assertEqual (value1 , [1 , 2 , 3 ])
253240 initial_call_count = len (factory_calls )
254241
255- # Multiple subsequent accesses should not call factory
256242 for _ in range (10 ):
257243 value = d ['key' ]
258244 self .assertEqual (value , [1 , 2 , 3 ])
259245
260- # Factory call count should not have increased
261246 self .assertEqual (len (factory_calls ), initial_call_count )
262247
263248
0 commit comments