MCPcopy Create free account
hub / github.com/python/cpython / test_threads

Method test_threads

Lib/test/test_io/test_bufferedio.py:862–906  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

860 @threading_helper.requires_working_threading()
861 @support.requires_resource('cpu')
862 def test_threads(self):
863 try:
864 # Write out many bytes from many threads and test they were
865 # all flushed.
866 N = 1000
867 contents = bytes(range(256)) * N
868 sizes = cycle([1, 19])
869 n = 0
870 queue = deque()
871 while n < len(contents):
872 size = next(sizes)
873 queue.append(contents[n:n+size])
874 n += size
875 del contents
876 # We use a real file object because it allows us to
877 # exercise situations where the GIL is released before
878 # writing the buffer to the raw streams. This is in addition
879 # to concurrency issues due to switching threads in the middle
880 # of Python code.
881 with self.open(os_helper.TESTFN, self.write_mode, buffering=0) as raw:
882 bufio = self.tp(raw, 8)
883 errors = []
884 def f():
885 try:
886 while True:
887 try:
888 s = queue.popleft()
889 except IndexError:
890 return
891 bufio.write(s)
892 except Exception as e:
893 errors.append(e)
894 raise
895 threads = [threading.Thread(target=f) for x in range(20)]
896 with threading_helper.start_threads(threads):
897 time.sleep(0.02) # yield
898 self.assertFalse(errors,
899 "the following exceptions were caught: %r" % errors)
900 bufio.close()
901 with self.open(os_helper.TESTFN, "rb") as f:
902 s = f.read()
903 for i in range(256):
904 self.assertEqual(s.count(bytes([i])), N)
905 finally:
906 os_helper.unlink(os_helper.TESTFN)
907
908 def test_misbehaved_io(self):
909 rawio = self.MisbehavedRawIO()

Callers

nothing calls this directly

Calls 9

assertFalseMethod · 0.80
appendMethod · 0.45
openMethod · 0.45
sleepMethod · 0.45
closeMethod · 0.45
readMethod · 0.45
assertEqualMethod · 0.45
countMethod · 0.45
unlinkMethod · 0.45

Tested by

no test coverage detected