Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c877658

Browse files
Issue #20556: Used specific assert methods in threading tests.
2 parents 04bc5b9 + 8c0f0c5 commit c877658

3 files changed

Lines changed: 22 additions & 23 deletions

File tree

Lib/test/test_dummy_thread.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def setUp(self):
2424

2525
def test_initlock(self):
2626
#Make sure locks start locked
27-
self.assertTrue(not self.lock.locked(),
27+
self.assertFalse(self.lock.locked(),
2828
"Lock object is not initialized unlocked.")
2929

3030
def test_release(self):
3131
# Test self.lock.release()
3232
self.lock.acquire()
3333
self.lock.release()
34-
self.assertTrue(not self.lock.locked(),
34+
self.assertFalse(self.lock.locked(),
3535
"Lock object did not release properly.")
3636

3737
def test_improper_release(self):
@@ -46,7 +46,7 @@ def test_cond_acquire_success(self):
4646
def test_cond_acquire_fail(self):
4747
#Test acquiring locked lock returns False
4848
self.lock.acquire(0)
49-
self.assertTrue(not self.lock.acquire(0),
49+
self.assertFalse(self.lock.acquire(0),
5050
"Conditional acquiring of a locked lock incorrectly "
5151
"succeeded.")
5252

@@ -58,9 +58,9 @@ def test_uncond_acquire_success(self):
5858

5959
def test_uncond_acquire_return_val(self):
6060
#Make sure that an unconditional locking returns True.
61-
self.assertTrue(self.lock.acquire(1) is True,
61+
self.assertIs(self.lock.acquire(1), True,
6262
"Unconditional locking did not return True.")
63-
self.assertTrue(self.lock.acquire() is True)
63+
self.assertIs(self.lock.acquire(), True)
6464

6565
def test_uncond_acquire_blocking(self):
6666
#Make sure that unconditional acquiring of a locked lock blocks.
@@ -80,7 +80,7 @@ def delay_unlock(to_unlock, delay):
8080
end_time = int(time.time())
8181
if support.verbose:
8282
print("done")
83-
self.assertTrue((end_time - start_time) >= DELAY,
83+
self.assertGreaterEqual(end_time - start_time, DELAY,
8484
"Blocking by unconditional acquiring failed.")
8585

8686
class MiscTests(unittest.TestCase):
@@ -94,7 +94,7 @@ def test_ident(self):
9494
#Test sanity of _thread.get_ident()
9595
self.assertIsInstance(_thread.get_ident(), int,
9696
"_thread.get_ident() returned a non-integer")
97-
self.assertTrue(_thread.get_ident() != 0,
97+
self.assertNotEqual(_thread.get_ident(), 0,
9898
"_thread.get_ident() returned 0")
9999

100100
def test_LockType(self):
@@ -164,7 +164,7 @@ def queue_mark(queue, delay):
164164
time.sleep(DELAY)
165165
if support.verbose:
166166
print('done')
167-
self.assertTrue(testing_queue.qsize() == thread_count,
167+
self.assertEqual(testing_queue.qsize(), thread_count,
168168
"Not all %s threads executed properly after %s sec." %
169169
(thread_count, DELAY))
170170

Lib/test/test_threading.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ def run(self):
5959
self.nrunning.inc()
6060
if verbose:
6161
print(self.nrunning.get(), 'tasks are running')
62-
self.testcase.assertTrue(self.nrunning.get() <= 3)
62+
self.testcase.assertLessEqual(self.nrunning.get(), 3)
6363

6464
time.sleep(delay)
6565
if verbose:
6666
print('task', self.name, 'done')
6767

6868
with self.mutex:
6969
self.nrunning.dec()
70-
self.testcase.assertTrue(self.nrunning.get() >= 0)
70+
self.testcase.assertGreaterEqual(self.nrunning.get(), 0)
7171
if verbose:
7272
print('%s is finished. %d tasks are running' %
7373
(self.name, self.nrunning.get()))
@@ -101,34 +101,33 @@ def test_various_ops(self):
101101
for i in range(NUMTASKS):
102102
t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
103103
threads.append(t)
104-
self.assertEqual(t.ident, None)
105-
self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t)))
104+
self.assertIsNone(t.ident)
105+
self.assertRegex(repr(t), r'^<TestThread\(.*, initial\)>$')
106106
t.start()
107107

108108
if verbose:
109109
print('waiting for all tasks to complete')
110110
for t in threads:
111111
t.join()
112-
self.assertTrue(not t.is_alive())
112+
self.assertFalse(t.is_alive())
113113
self.assertNotEqual(t.ident, 0)
114-
self.assertFalse(t.ident is None)
115-
self.assertTrue(re.match('<TestThread\(.*, stopped -?\d+\)>',
116-
repr(t)))
114+
self.assertIsNotNone(t.ident)
115+
self.assertRegex(repr(t), r'^<TestThread\(.*, stopped -?\d+\)>$')
117116
if verbose:
118117
print('all tasks done')
119118
self.assertEqual(numrunning.get(), 0)
120119

121120
def test_ident_of_no_threading_threads(self):
122121
# The ident still must work for the main thread and dummy threads.
123-
self.assertFalse(threading.currentThread().ident is None)
122+
self.assertIsNotNone(threading.currentThread().ident)
124123
def f():
125124
ident.append(threading.currentThread().ident)
126125
done.set()
127126
done = threading.Event()
128127
ident = []
129128
_thread.start_new_thread(f, ())
130129
done.wait()
131-
self.assertFalse(ident[0] is None)
130+
self.assertIsNotNone(ident[0])
132131
# Kill the "immortal" _DummyThread
133132
del threading._active[ident[0]]
134133

@@ -245,7 +244,7 @@ def run(self):
245244
self.assertTrue(ret)
246245
if verbose:
247246
print(" verifying worker hasn't exited")
248-
self.assertTrue(not t.finished)
247+
self.assertFalse(t.finished)
249248
if verbose:
250249
print(" attempting to raise asynch exception in worker")
251250
result = set_async_exc(ctypes.c_long(t.id), exception)
@@ -416,9 +415,9 @@ def test_old_threading_api(self):
416415

417416
def test_repr_daemon(self):
418417
t = threading.Thread()
419-
self.assertFalse('daemon' in repr(t))
418+
self.assertNotIn('daemon', repr(t))
420419
t.daemon = True
421-
self.assertTrue('daemon' in repr(t))
420+
self.assertIn('daemon', repr(t))
422421

423422
def test_deamon_param(self):
424423
t = threading.Thread()
@@ -570,7 +569,7 @@ def f():
570569
tstate_lock.release()
571570
self.assertFalse(t.is_alive())
572571
# And verify the thread disposed of _tstate_lock.
573-
self.assertTrue(t._tstate_lock is None)
572+
self.assertIsNone(t._tstate_lock)
574573

575574
def test_repr_stopped(self):
576575
# Verify that "stopped" shows up in repr(Thread) appropriately.

Lib/test/test_threading_local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class X:
189189
wr = weakref.ref(x)
190190
del x
191191
gc.collect()
192-
self.assertIs(wr(), None)
192+
self.assertIsNone(wr())
193193

194194

195195
class ThreadLocalTest(unittest.TestCase, BaseLocalTest):

0 commit comments

Comments
 (0)