11import unittest
2-
32from threading import Thread , Barrier
4- from unittest import TestCase
5-
63from test .support import threading_helper
74
85
9- @threading_helper .requires_working_threading ()
10- class TestSet (TestCase ):
6+ class TestSetRepr (unittest .TestCase ):
117 def test_repr_clear (self ):
128 """Test repr() of a set while another thread is calling clear()"""
139 NUM_ITERS = 10
@@ -36,19 +32,20 @@ def repr_set():
3632 for set_repr in set_reprs :
3733 self .assertIn (set_repr , ("set()" , "{1, 2, 3, 4, 5, 6, 7, 8}" ))
3834
35+
36+ class RaceTestBase :
3937 def test_contains_mutate (self ):
4038 """Test set contains operation combined with mutation."""
4139 barrier = Barrier (2 , timeout = 2 )
4240 s = set ()
4341 done = False
4442
45- NUM_ITEMS = 200
46- NUM_LOOPS = 200
43+ NUM_LOOPS = 1000
4744
4845 def read_set ():
4946 barrier .wait ()
5047 while not done :
51- for i in range (NUM_ITEMS ):
48+ for i in range (self . SET_SIZE ):
5249 item = i >> 1
5350 result = item in s
5451
@@ -57,9 +54,9 @@ def mutate_set():
5754 barrier .wait ()
5855 for i in range (NUM_LOOPS ):
5956 s .clear ()
60- for j in range (NUM_ITEMS ):
57+ for j in range (self . SET_SIZE ):
6158 s .add (j )
62- for j in range (NUM_ITEMS ):
59+ for j in range (self . SET_SIZE ):
6360 s .discard (j )
6461 # executes the set_swap_bodies() function
6562 s .__iand__ (set (k for k in range (10 , 20 )))
@@ -112,10 +109,10 @@ def test_contains_hash_mutate(self):
112109 """Test set contains operation with mutating hash method."""
113110 barrier = Barrier (2 , timeout = 2 )
114111
115- NUM_ITEMS = 20 # should be larger than PySet_MINSIZE
116112 NUM_LOOPS = 1_000
113+ SET_SIZE = self .SET_SIZE
117114
118- s = set (range (NUM_ITEMS ))
115+ s = set (range (SET_SIZE ))
119116
120117 class Key :
121118 def __init__ (self ):
@@ -130,7 +127,7 @@ def __hash__(self):
130127 if self .count % 2 == 0 :
131128 s .clear ()
132129 else :
133- s .update (range (NUM_ITEMS ))
130+ s .update (range (SET_SIZE ))
134131 return hash (self .value )
135132
136133 def __eq__ (self , other ):
@@ -154,5 +151,15 @@ def read_set():
154151 t .join ()
155152
156153
154+ @threading_helper .requires_working_threading ()
155+ class SmallSetTest (RaceTestBase , unittest .TestCase ):
156+ SET_SIZE = 6 # smaller than PySet_MINSIZE
157+
158+
159+ @threading_helper .requires_working_threading ()
160+ class LargeSetTest (RaceTestBase , unittest .TestCase ):
161+ SET_SIZE = 20 # larger than PySet_MINSIZE
162+
163+
157164if __name__ == "__main__" :
158165 unittest .main ()
0 commit comments