| 1 |
import threading |
| 2 |
from test.support import threading_helper |
| 3 |
from unittest import TestCase |
| 4 |
|
| 5 |
|
| 6 |
def run_in_threads(targets): |
| 7 |
"""Run `targets` in separate threads""" |
| 8 |
threads = [ |
| 9 |
threading.Thread(target=target) |
| 10 |
for target in targets |
| 11 |
] |
| 12 |
for thread in threads: |
| 13 |
thread.start() |
| 14 |
for thread in threads: |
| 15 |
thread.join() |
| 16 |
|
| 17 |
|
| 18 |
@threading_helper.requires_working_threading() |
| 19 |
class TestSlots(TestCase): |
| 20 |
|
| 21 |
def test_object(self): |
| 22 |
class Spam: |
| 23 |
__slots__ = [ |
| 24 |
"eggs", |
| 25 |
] |
| 26 |
|
| 27 |
def __init__(self, initial_value): |
| 28 |
self.eggs = initial_value |
| 29 |
|
| 30 |
spam = Spam(0) |
| 31 |
iters = 20_000 |
| 32 |
|
| 33 |
def writer(): |
| 34 |
for _ in range(iters): |
| 35 |
spam.eggs += 1 |
| 36 |
|
| 37 |
def reader(): |
| 38 |
for _ in range(iters): |
| 39 |
eggs = spam.eggs |
| 40 |
assert type(eggs) is int |
| 41 |
assert 0 <= eggs <= iters |
| 42 |
|
| 43 |
run_in_threads([writer, reader, reader, reader]) |