@@ -1747,7 +1747,7 @@ def create_event_loop(self):
17471747 return asyncio .SelectorEventLoop (selectors .SelectSelector ())
17481748
17491749
1750- def noop ():
1750+ def noop (* args ):
17511751 pass
17521752
17531753
@@ -1797,50 +1797,52 @@ def test_handle_weakref(self):
17971797 h = asyncio .Handle (lambda : None , (), self .loop )
17981798 wd ['h' ] = h # Would fail without __weakref__ slot.
17991799
1800- def test_repr (self ):
1800+ def test_handle_repr (self ):
18011801 # simple function
18021802 h = asyncio .Handle (noop , (), self .loop )
18031803 src = test_utils .get_function_source (noop )
18041804 self .assertEqual (repr (h ),
1805- 'Handle( noop at %s:%s, ()) ' % src )
1805+ '< Handle noop() at %s:%s> ' % src )
18061806
18071807 # cancelled handle
18081808 h .cancel ()
18091809 self .assertEqual (repr (h ),
1810- 'Handle( noop at %s:%s, ())<cancelled >' % src )
1810+ '< Handle cancelled noop() at %s:%s>' % src )
18111811
18121812 # decorated function
18131813 cb = asyncio .coroutine (noop )
18141814 h = asyncio .Handle (cb , (), self .loop )
18151815 self .assertEqual (repr (h ),
1816- 'Handle( noop at %s:%s, ()) ' % src )
1816+ '< Handle noop() at %s:%s> ' % src )
18171817
18181818 # partial function
1819- cb = functools .partial (noop )
1820- h = asyncio .Handle (cb , (), self .loop )
1819+ cb = functools .partial (noop , 1 , 2 )
1820+ h = asyncio .Handle (cb , (3 , ), self .loop )
18211821 filename , lineno = src
1822- regex = (r'^Handle\(functools.partial\('
1823- r'<function noop .*>\) at %s:%s, '
1824- r'\(\)\)$' % (re .escape (filename ), lineno ))
1822+ regex = (r'^<Handle noop\(1, 2\)\(3\) at %s:%s>$'
1823+ % (re .escape (filename ), lineno ))
18251824 self .assertRegex (repr (h ), regex )
18261825
18271826 # partial method
18281827 if sys .version_info >= (3 , 4 ):
1829- method = HandleTests .test_repr
1828+ method = HandleTests .test_handle_repr
18301829 cb = functools .partialmethod (method )
18311830 src = test_utils .get_function_source (method )
18321831 h = asyncio .Handle (cb , (), self .loop )
18331832
18341833 filename , lineno = src
1835- regex = (r'^Handle\(functools.partialmethod\('
1836- r'<function HandleTests.test_repr .*>, , \) at %s:%s, '
1837- r'\(\)\)$' % (re .escape (filename ), lineno ))
1834+ cb_regex = r'<function HandleTests.test_handle_repr .*>'
1835+ cb_regex = (r'functools.partialmethod\(%s, , \)\(\)' % cb_regex )
1836+ regex = (r'^<Handle %s at %s:%s>$'
1837+ % (cb_regex , re .escape (filename ), lineno ))
18381838 self .assertRegex (repr (h ), regex )
18391839
18401840
1841-
18421841class TimerTests (unittest .TestCase ):
18431842
1843+ def setUp (self ):
1844+ self .loop = mock .Mock ()
1845+
18441846 def test_hash (self ):
18451847 when = time .monotonic ()
18461848 h = asyncio .TimerHandle (when , lambda : False , (),
@@ -1858,29 +1860,37 @@ def callback(*args):
18581860 self .assertIs (h ._args , args )
18591861 self .assertFalse (h ._cancelled )
18601862
1861- r = repr (h )
1862- self .assertTrue (r .endswith ('())' ))
1863-
1863+ # cancel
18641864 h .cancel ()
18651865 self .assertTrue (h ._cancelled )
18661866
1867- r = repr (h )
1868- self .assertTrue (r .endswith ('())<cancelled>' ), r )
18691867
1868+ # when cannot be None
18701869 self .assertRaises (AssertionError ,
18711870 asyncio .TimerHandle , None , callback , args ,
1872- mock . Mock () )
1871+ self . loop )
18731872
1874- def test_timer_comparison (self ):
1875- loop = mock .Mock ()
1873+ def test_timer_repr (self ):
1874+ # simple function
1875+ h = asyncio .TimerHandle (123 , noop , (), self .loop )
1876+ src = test_utils .get_function_source (noop )
1877+ self .assertEqual (repr (h ),
1878+ '<TimerHandle when=123 noop() at %s:%s>' % src )
18761879
1880+ # cancelled handle
1881+ h .cancel ()
1882+ self .assertEqual (repr (h ),
1883+ '<TimerHandle cancelled when=123 noop() at %s:%s>'
1884+ % src )
1885+
1886+ def test_timer_comparison (self ):
18771887 def callback (* args ):
18781888 return args
18791889
18801890 when = time .monotonic ()
18811891
1882- h1 = asyncio .TimerHandle (when , callback , (), loop )
1883- h2 = asyncio .TimerHandle (when , callback , (), loop )
1892+ h1 = asyncio .TimerHandle (when , callback , (), self . loop )
1893+ h2 = asyncio .TimerHandle (when , callback , (), self . loop )
18841894 # TODO: Use assertLess etc.
18851895 self .assertFalse (h1 < h2 )
18861896 self .assertFalse (h2 < h1 )
@@ -1896,8 +1906,8 @@ def callback(*args):
18961906 h2 .cancel ()
18971907 self .assertFalse (h1 == h2 )
18981908
1899- h1 = asyncio .TimerHandle (when , callback , (), loop )
1900- h2 = asyncio .TimerHandle (when + 10.0 , callback , (), loop )
1909+ h1 = asyncio .TimerHandle (when , callback , (), self . loop )
1910+ h2 = asyncio .TimerHandle (when + 10.0 , callback , (), self . loop )
19011911 self .assertTrue (h1 < h2 )
19021912 self .assertFalse (h2 < h1 )
19031913 self .assertTrue (h1 <= h2 )
@@ -1909,7 +1919,7 @@ def callback(*args):
19091919 self .assertFalse (h1 == h2 )
19101920 self .assertTrue (h1 != h2 )
19111921
1912- h3 = asyncio .Handle (callback , (), loop )
1922+ h3 = asyncio .Handle (callback , (), self . loop )
19131923 self .assertIs (NotImplemented , h1 .__eq__ (h3 ))
19141924 self .assertIs (NotImplemented , h1 .__ne__ (h3 ))
19151925
0 commit comments