Skip to content

Commit d351827

Browse files
Issue #20556: Used specific assert methods in threading tests.
1 parent aac1dd0 commit d351827

3 files changed

Lines changed: 19 additions & 19 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 test_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 test_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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def run(self):
5151
self.nrunning.inc()
5252
if verbose:
5353
print self.nrunning.get(), 'tasks are running'
54-
self.testcase.assertTrue(self.nrunning.get() <= 3)
54+
self.testcase.assertLessEqual(self.nrunning.get(), 3)
5555

5656
time.sleep(delay)
5757
if verbose:
5858
print 'task', self.name, 'done'
5959

6060
with self.mutex:
6161
self.nrunning.dec()
62-
self.testcase.assertTrue(self.nrunning.get() >= 0)
62+
self.testcase.assertGreaterEqual(self.nrunning.get(), 0)
6363
if verbose:
6464
print '%s is finished. %d tasks are running' % (
6565
self.name, self.nrunning.get())
@@ -92,33 +92,33 @@ def test_various_ops(self):
9292
for i in range(NUMTASKS):
9393
t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
9494
threads.append(t)
95-
self.assertEqual(t.ident, None)
96-
self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t)))
95+
self.assertIsNone(t.ident)
96+
self.assertRegexpMatches(repr(t), r'^<TestThread\(.*, initial\)>$')
9797
t.start()
9898

9999
if verbose:
100100
print 'waiting for all tasks to complete'
101101
for t in threads:
102102
t.join(NUMTASKS)
103-
self.assertTrue(not t.is_alive())
103+
self.assertFalse(t.is_alive())
104104
self.assertNotEqual(t.ident, 0)
105-
self.assertFalse(t.ident is None)
106-
self.assertTrue(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
105+
self.assertIsNotNone(t.ident)
106+
self.assertRegexpMatches(repr(t), r'^<TestThread\(.*, \w+ -?\d+\)>$')
107107
if verbose:
108108
print 'all tasks done'
109109
self.assertEqual(numrunning.get(), 0)
110110

111111
def test_ident_of_no_threading_threads(self):
112112
# The ident still must work for the main thread and dummy threads.
113-
self.assertFalse(threading.currentThread().ident is None)
113+
self.assertIsNotNone(threading.currentThread().ident)
114114
def f():
115115
ident.append(threading.currentThread().ident)
116116
done.set()
117117
done = threading.Event()
118118
ident = []
119119
thread.start_new_thread(f, ())
120120
done.wait()
121-
self.assertFalse(ident[0] is None)
121+
self.assertIsNotNone(ident[0])
122122
# Kill the "immortal" _DummyThread
123123
del threading._active[ident[0]]
124124

@@ -236,7 +236,7 @@ def run(self):
236236
self.assertTrue(ret)
237237
if verbose:
238238
print " verifying worker hasn't exited"
239-
self.assertTrue(not t.finished)
239+
self.assertFalse(t.finished)
240240
if verbose:
241241
print " attempting to raise asynch exception in worker"
242242
result = set_async_exc(ctypes.c_long(t.id), exception)

Lib/test/test_threading_local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class X:
196196
wr = weakref.ref(x)
197197
del x
198198
gc.collect()
199-
self.assertIs(wr(), None)
199+
self.assertIsNone(wr())
200200

201201
class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest):
202202
_local = _threading_local.local

0 commit comments

Comments
 (0)