| 1 |
import unittest |
| 2 |
|
| 3 |
from threading import Thread, Barrier |
| 4 |
from unittest import TestCase |
| 5 |
|
| 6 |
from test.support import threading_helper |
| 7 |
|
| 8 |
|
| 9 |
@threading_helper.requires_working_threading() |
| 10 |
class TestSet(TestCase): |
| 11 |
def test_repr_clear(self): |
| 12 |
"""Test repr() of a set while another thread is calling clear()""" |
| 13 |
NUM_ITERS = 10 |
| 14 |
NUM_REPR_THREADS = 10 |
| 15 |
barrier = Barrier(NUM_REPR_THREADS + 1) |
| 16 |
s = {1, 2, 3, 4, 5, 6, 7, 8} |
| 17 |
|
| 18 |
def clear_set(): |
| 19 |
barrier.wait() |
| 20 |
s.clear() |
| 21 |
|
| 22 |
def repr_set(): |
| 23 |
barrier.wait() |
| 24 |
set_reprs.append(repr(s)) |
| 25 |
|
| 26 |
for _ in range(NUM_ITERS): |
| 27 |
set_reprs = [] |
| 28 |
threads = [Thread(target=clear_set)] |
| 29 |
for _ in range(NUM_REPR_THREADS): |
| 30 |
threads.append(Thread(target=repr_set)) |
| 31 |
for t in threads: |
| 32 |
t.start() |
| 33 |
for t in threads: |
| 34 |
t.join() |
| 35 |
|
| 36 |
for set_repr in set_reprs: |
| 37 |
self.assertIn(set_repr, ("set()", "{1, 2, 3, 4, 5, 6, 7, 8}")) |
| 38 |
|
| 39 |
|
| 40 |
if __name__ == "__main__": |
| 41 |
unittest.main() |