ÿØÿà JFIF    ÿÛ „ !.%+&8&+/1555$;@;4?.451 4,$,44444444444414444444444444444444444444444444444444ÿÀ  á á" ÿÄ     ÿÄ ?    !1AQaq"2‘¡±ÁðBRbrÑá#‚’¢²3S CñÿÄ   ÿÄ !    !1QAa‘2ÿÚ   ? 5˜Z¯V¦cø)›t/? z¨±>Õ5€¶‹Á¤·¼z¼Ü¬+ñ®v¤¨_ˆR­BFn©—˜ý®ç̝P8gýt·ÉSTŦˆìät?þé¼íìN/Þa)ì–í6ô… Ï¿øÃj´¿KÇü]ÿ ªô¹-eKànëÕHTx}ýSÜ›ÿ ”7Ø×&µ<¦  ¥ÑO¶[Ù¯ä¨ÞÃÿ PZ-¬;#õ|•oaÿ ©CìÞz3˜öː/¤­ñTûIØ}š^ mÓ%ªxˆ¥ÉŸu=Z+ISe¿45™¼u;ú&WØ÷€æßQ™®{|íx*TC“#ZŠìZ§²‹ 6pv…³¿¡äª*áZÐ%ÒOáˆo"x«OHk w±æ+¬V(kMúŸ5Vö«$ ÁrÏbàb57/luR ¸ÑÛj Òµì`Мq­û žICÀÊ•©4€Âcà¨Ï€O´<èÐ:›ù(Ë^L8þ‘ÍÌ#¸Ð_Ì©ÙK(Öz 4¬û+¸;ü’V’84‘¬ÃŽ:[â‡ÔÌáõp¢~§ªlæ£ö{®G>J¼"°‡7¯ÆÉèßû ‹É‹§ÁòÃýâßî ^ƾÙõ‹×óH#«LP½ïX=xÑÍ$|W?•~• îëÔ©ª‹ {ÝT…Kÿ ”hûâá)J*ö˜–ÔU;iÇ€/ ÆþjóZ\ýwØ=Ìm ºèËL9 ýèÆð/¨’¥öo=nË.%Îì ŽÕ¯È|{Oj²ƒE6e/ßdÄõ²Ìâ1O®ò×TsəԸhOMýíMˆ¿¼H˜l²,7Â¥#MF/Úf°Ö½± ¸–dr‹NýÊ íjqx{œÉ ä-È ¦ øÄër¨q°ð †nцýÑÄÆ’mä…n<0È™;ÁÝá¯ÁZƒ7FÀmì­ É&9ˆîéi¶ùN§Y• ÃZãAâ?•‡©‰ , ó¾IŸŠc1 4â&y­&pŠ­6;M À 0¹qç»p.á …ŸÅáK@%6·y6ƒ‰3?”úºŽ‰éX5ªPT §µ!=Mž«Ú½‹ÅgÂSâÉaþÓoö–¯ÁÔìR>5éÿ üs¶ÆUcÌ kÇR ]ÿ ù¬¼«VŽ;Â|‡~¢¦”ÏŰæ {L™Õ°Óv¹ò¸írޡעCÃ!íVÕ {¶»sŒNPg/ "uÕbkm²“$ďå¿é¹§°½æz¯6 †s¿!s–wÚÝ“™Œ °.ûj>·+™Òa…©Œ&rÝÎtÛë긪Ît’LAVp%c Úý[ÄzJ¾ÇàXXç@˜ó<êL]·T˜¾¥1Ó©V‡g´æ½¦Ý@¹óø!_@´ÞâSÁ —S3™•& ]@JHÚý©ZŽ €×æÔr»Áf!‡yÞ4Mv*èÓã_{‘åóUuљØ«Oïé*®EvÑ Œ÷‡U \"㪒ÍK+À 4“M¡ï:0¥5í!'<@î´”>Ç»&Z–ïCCV˜Ì5Šo&îhè.žû |ÓK©h$s6KìŒëã)¹hI¦GïOåóI;ììü#É$Š0…Ææ¥TØ.5­¾gn´ “ÂÖ\:hœ89G)J@„}œ:’Ò{/Š"¦_Æ×7Æ3VÇŠÊa]ÚŒÙ€Ä–=®uÁßâACZƒ§§£ Qnâ:«,×{tyø¬iÛcœÜÄ€H½ÄÍCk´÷šß .W'b¤Íåh]÷€=,Žv×cÚEÚHXJX¶îo¨FÒtèöŸ>ªª6[J®Fµ£sGÁeqõfe\íjÒÐïÄÐGˆe1Ø‹.Ø”‘Ëuø Y­ˆÜ ŽG|zùªüMpDnQWÄ”%JŠ™)â*p@Örš«ÕT2Ð%ˆG#ª„ ·¤!°ŸOTÂT¸aÚ%4&h™LµšØüÐ.F¿²ÐÞ_Ç‚¾ÅÃaÜ÷09Æ q€öy˜v‡85õN÷]¬äѼóS{°_MެúÔ#°Ç¸0åÞè2ëôPcvÆw9®ií1Ä8F™˜à‰´+‰Ik1òÝ7“Ñ×ÒsÝ\x‚h`ÞÑ`ó"|µEcý£n˜h`}GÞ !±ù²Ápü²ß6 0ïi󜵩SÈÇ7˜-ÕURO˜¦´f$ªž-Í6(œ}<„ éc øs]ŽŽ„*—¾ ìdŽ„)méª\¿êÎIg¾ØÞ~I#C/¼¼´EÁÈŽi8“©õådô·>euä ƒ'Ê×लR1ÉJE1ÐAát`t;ÇР%Ý<‡¥„ÍÆ`×Oyó)õiI€ñQaŸ4Ûù\áàaÃÔ¹HÃu¹*k€¦<„e S‡&õÏ B!ŽhüÞ`yj}mªf×\¿ Ç~æ­9‡û\՞Ǖg²1Žû5V7 !àöšm° c`ܬøÇìµÒ'P"?…´Ö,"§^•õލsÔ)6˜sæéÍR¼ ò|Sl”‹7 nPW Gòú÷½§O¯‡„l¡kSÞŒr½PÊ@æ¢pŽ-mÿ #Ÿ˜Àº¶Áä¦;ïÔæ$1££`“Õ>„—·ž)ßð³ñ#Ï Ô$¶œ‰ÊE‹À;÷º ¯«P:Ñ”8–IÊtpÞ3ª“>ê“þës4ò2OÏÕ­±zô†Õ§‰.÷ä¸;¿˜“'œ›žª}«Œ{ª±Ì 9ÔóÞÕ‡0 $íWV3Üì¬ —@kÝ4@¿r¼±½¬™›?øØæ´'Áé®CË3-g$˜ö‡×auÚi´Žp/êÛ æF›Ú2v‹ã¿¿,nB1̨ƃqÞa5͝@&Æû“él÷ \C²½UÍc ¯k×¢U ÖéQå™—-r wô ÞÏ<Ò=&=ÿ Ôê Òêˈt,i—;LîÜ á¸*ÚÃ1$êL•LÍ <É)ýÐà’ ;F™{ƒ™˜€&'}‚ãÄK`¡ÞT@I;®žZóè‚s’7®°›+§O­Åq©é»²9<Ô J ¼9O’HL»Ùïì¸rk¼Ž_ý‘TŸu[²ßÚŒ·ü÷B%¯E ŸÔX5êO´ Ç•€’I0 ÉJX` ñ¹õ%;µŸD‘«´€àwÒ™U ûئžÖö\×®×´8 ½‡ºÐÆÓ§?Àkmœ=;d5*@-ì0F Rªýš[Ü6âö̃ڸr*KA9· u*µæ£?U¸Âêí†8@¦X4 e-ò„0s{ HâUpU?¼mñRa°®a%Ð'tÉ×’\¾ÊÉ]t›h>·(Ë@R¼¡Ãt h}’O÷au<+nT…Ö…MӐ??Óe95 q>í/;&JSû °¯ÊéÞ øƒ*Ã2½Ài&:nôUl=¾¿5eˆ3”ñc|Ú2V”>„»&eE;«ÚäC p¢Û úy 9š[ŒÌx¼擼A&DåÒ¯ˆ¤ÀÌ;"˜ ÏQä¸åhÊ}Ûq«Û0WžÒ|»€ø®öCm5•\ÇÀ§Pe3£]0ÃàLDÉ‰1øªxjgwT‚÷¿LΨK‹›ùs—xˆÜ±µ kæ¸f‰‰ÜGk/LÛØ6d9ò¶ùA{ƒA3š/¬D¬khÓk‰`˜"㯒r¿±Óã jx‡°e}<Ñø\3y:'À•/h½Í€Ç4~g ?Û(¼]v‘ªlKÎâ~?O‚W%{Ì:“'©úNq¾›úo(X’¥¯ˆ nFê{Ç€ü?º'ë ø‹ì Þ09ŒÌç9Æ —ËC`j@ÓÄ(+a‹un¸#ÂꟋ{K`‘ÑÍÍ'à´»/Û,KW;Þ4²þð ï Nm|~fGÏ(…³Ã)«1ö­Õ ¥‡¨©ƒÃ™ü-s=à=U66Ï«Ýc蓦W¹íž®›nÔ%êÇìŒ<#Ü×84ån®Ð ÒåOC` ñânÑs‡¢ç 1õ%Îhì½Ã½® e:ݼUZo™`  ÅZŸŒÊ«ê1ÏÄo$q¹Þ€©ˆhÐÉä¯ñ[!…Ú˜àJ:x2$Íß&PåT£6ç— ‡Í*4Ýšçjÿ ‰É nófÐ ó(L5C•åÆ\rMÒ@ò }y-W}™üýVù—ú¢=Ù”c®‘< M ž ´Phr ¦©TD ‘ù.$´÷O‡‘V2Æò.=IUŒ=ž‡â¬i™aþÓåÙ?òUø'ØÖ•.~* šTŒ!•-×áºTâ®ä#õü'´ eýlYÅÓeÕKÂrT"CÚ@u!Óxƒ{š3€}1¿(r}%«nËamjÑ%ÑNEò v ˜à  σöK³,*º.àzù¨™Ó ÚçâU¦*¿ 9{%Ö¹ njûdaXöb) kÛÆ±ûÓ\°M7ˆÂ=û›ç¿Ã‚­V»Cg–8ÙêE- j)k$º`Ã-ùEýeBÆÇ]c¡°ñty&Òd0nõ'¡W+ƒ*|–øµFa\GQªEAÔp5\Ǽ·¼Ç8·õ -â§Ú[ ‡ uZeÖ 3}×d'+¹:ð+K†Û®s!Ï$úe€<Û”x)1»a­¡LC]¸µík…ÚàA»AYº{†ªS[¦5HÒ7ù --,ísòDØ€èk ÞÀîÜ ò@â( ËNˆë›4ô½•/¦o‡€Û7 ê•ÆêòðÜy'Án½µ á˜ݦ ndeo…[ì¶Ê,¥R³Ä=À±—–ß;£™´ñSâ*g§”ïaið‘Jå~™ÓÞ ß³Õ¢»8x埒²52>AÊb&-÷\7´éÄù€T˜,w;3{ï˜k…à¹ÄqÀ«œ{€\ ˆ¾[´¨јr &Úé„Ívˆ±8†¿]|¬ņ4I×pÞS1ÈÖz‰#Ìv‡G!YNògñ:màTz¢Ý1ô©^O=~ë|5Bã™ç•¼µõ•bÆ@úÕS¬ÈŒ#¬zünrŸ û” Z²•èðV"ÁHÚý©wÝ €7¼Ìu1hÑa3Éä û f$o¿É ™Ú›ÝçnpÒ3äÌ3†Í§,Äï]$‰/pê †«À¼¸e9­Æê_C]žƒ·ý·frÁN«, E=›Çq -‰öŒ:aÏ¿±í&£Í:-} 84‘ÿ eƒQÑeëSsuiA ³g㟥ú£?ÿ ʼn*”“÷aühe:ÊWa@ÒÞk±eØ] F Ô—r.åä˜ @ö¥ªZoÐýYL·¥S²G/‡ñ <~*ZÆ´è>JlòàÛÆ½ÿ 窘ìGN¢:I®KšJp/`íIÁÀõ#Ä-€ö­šµŒoF4|ÆQØÆ@Ì|£Ô…¢À{9˜è½Üó›€ôYÒÎYsið;ís¤€à²ˆ‚4qÉVŒI$ ‰"° æµ8cXGjœˏ¡Aâý•ËÜ¢ûï e·çLx']á"oÅÎê3¯Ç—¹”ó0nå‚âg{Œñ> S´˜îè°g238‚ãköÝfÚd´6Ò€;ò÷±¢™¼›º ¢Æ'¥Ðx'e¬ç ]bÈÆV¢ó‹kýBO ðÊâ$Ÿ!×T 3Mýמ žìٍàÌü‘8÷€àæØ8æ©6‰©L´«…oãpð„~Çk‰!ñ;‹”ÛžÍ àž±z Ÿôû øŸÝužÏ;ÿ #|u6™Þ¬ÚˆÐõA4¶â|ôl|Ê2ŽÇ¤ÝÅÇY.<#Aí.k§hóF‚”Y; M½Ö4hŸ4&›­¿tès´%FìL¥£Ãk‰ÇT¤haÁ¤ÚxfÉ`ÑìË›>i 3t‚:,–+^÷´–{Û–Nxi"x‘Ûg î¨>¥Õ܁ùZH,2Û“:8xÊ¢Çí9.É-Ìâã-=çjwµS˜dütžçwýGòú®®ûº_ˆýx$–¡ãøO EÚÛÏ÷R„×w+3£Á£öUMyR²¹âŒ°š›¸Ñãò9§Ó_Dl+Ùßc›úšGÅÌc†Ž!Ko=¶.‘Îÿ c²(2®V mª.ÿ ¹B›¹å ù„öŸSV>™ü¯$y:G¢Z×àøúdî¹û­·ýÇ´:•c LÍõi_‹ö+ÎæGÊè>OŠ•äž´§Þ{X}¨1ÚTc›»Qþ•êô°t¿OP?eæ~É{5]•ÙR£r5†nZ\ã@ &îJõ ¾àC°þV>fé¥/ü5ñÊIº_é5 ;e­h<@ Ä&æÃëE%;X,ÒãÆÞ`Oò¦kŸm#˜!ÀyÄ¢| óLšò¥Ä` ¶R=|ÈCâh5ò3DˆïF†ðÒ#ÅìÛœ?¸yhBãœí ZxßÎÄhºRK„`Þödvײ™ÀÈÑÒgŒuY w³%†ƒÓzõ ÖÏp‚dH®¦A´ù§»ÓÇMæ~)ˆð‡û:ù&Ä •vGD´À n ݇¼Ö8Fö óáà£~Ë¥x`oK|Ä?fxiØü%pìR>éò+Û±éÎ>núlFŤ'tq8LZÏvÃ?„¡ß±È⽆¯³íü@x|PöUäèØã¡ð‚ŒAìÏ"vÍwóŸÍ{ ý0.z È•Ö{,N¡£¡ŸKÕÙž>Ýœþ ÍÀ°<×EA!Å‚D™IúOÍ¡>ôG}Â` ÍßkÜL™Ž Þð™ {IøF²¹òQ3&!ÃÂÞz.d&Ï-sH¸,Ôõ˜ŽP€ 77ˆÝ¼ÊëÜw =cÕ Ú,ØÐ5ÎYÐ)ì´öœgŒ[¤ßv㙑8心>h]§µháYš£²ºÑ.{Ï7Sð•?´~×SÃKýJÛ˜ ™Íäiúu<µX¶1õ^kâçIÑ£sZ4h>j*ÔšD:4­¿_ ÷¸ Õxæÿ ¸?Mù _•­ÊÐ ä ÷ý ÑwL œ­ïnTkÛUÍN©ë:¦fV ¶ÜÔÜMªÅâA½–¿R×TXš-%iTÊT•‡Ù‚JôϐZxWÑè‰f‰òG º ×Õû2aZ7OU3[“×AT–ÞŒ…-‘¤”Ì ì&(ˆ¿­•ƒkï’:ðY¦W‘ Å)“†‘˜³Åtcø˜ñTÂwÚÇ4|üLÇªí–v- qˆèU qPE.†â‘˜µ Æ,ÐÅs]8¾„oúÑ i>ÜxxÈó)ƒ ´æÁâØ$À‰vžŸf$Ž |ãw;ÀÁIJ»b` {¦Ó¤Ú$©YÀ‘n@Óïž«9J¼êG m¤ ܯ¹ÌW4€ÐÒÅÛ‡#褕Ÿn-?í|с¥÷Ú¹¬'´ÞÜ9ÓK `hê£SÄSà?7—Wí_´…óB›»:=Ãïq`<8ñÓŒÑlú2d¬ê³£hÖ[l|$vÝro~'R®‰§°ñmY ͧäP |PUª¹·:3Œ[Û{Xÿ ºâ@‚W–Äé u‚ ¯´*=íή.pûÒdt @G‰¬ s¸ ëÉücr ÞæÑ¨Ê@>¤¢Ö±. Þ'¯°ÌME[YéïĵÂCå½ Ué©Áû'Ê9%eÔðNU”ë‘ÌsD3/®+UI˜9h.WC”빓$#:pz:YÓ ¿xž* ³$Í +$kñAŠ‹†¢ Uê>¸)_š¬÷©ßAÂÔb9ÇU ¯¾á•9¯ÏÏ÷O÷¼¼Fähal1‰3Ì[Ïr•´UCksNÐ] R‘¸¥H+§Šé†c©vÖÞ0iÓ76s†î!§=ß ¼~Ô'°Ãmäoäš³ªøi1úÉ)³yV8 CLÄØÁ‘WYïi€H6ÖÑiámø^ÈY´°Ñ7¥Û*—Ñ©L«Qƒï—Ùrÿ ›£Ð*š¸ˆL©ˆ$ˆ ÷¾D§9È®«qbqC)–ˆïv´çñsÑVT­Ø, <àïºÀO«Jý·õ àfPìð .wFšir´þ’2_Y *Æ€x\« ì€9š@ Ž|F⇥ˆkZ@hÖÄ0t¿-<“‹qµ¾*ZL¤Ú)&BJpÓF5=$„at*Zš$’ÑtdûÝRI1 2މ$€$I$#‰SÞ’Hë¬ï;Á$¡t$’`<(ñÇt)$‡Ð.Êf¢X’Kt=Éé$‚ˆªè¢oÝëòI%Rgcª÷ŠyI%¡‰ÿ !ñ)´õ $¤ Ô’IIGÿÙ"""Tests for lock.py""" import unittest from unittest import mock import re import asyncio STR_RGX_REPR = ( r'^<(?P.*?) object at (?P
.*?)' r'\[(?P' r'(set|unset|locked|unlocked)(, value:\d)?(, waiters:\d+)?' r')\]>\Z' ) RGX_REPR = re.compile(STR_RGX_REPR) def tearDownModule(): asyncio.set_event_loop_policy(None) class LockTests(unittest.IsolatedAsyncioTestCase): async def test_ctor_loop(self): loop = mock.Mock() with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=loop) self.assertIs(lock._loop, loop) with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) self.assertIs(lock._loop, asyncio.get_running_loop()) async def test_ctor_noloop(self): lock = asyncio.Lock() self.assertIs(lock._loop, asyncio.get_running_loop()) async def test_repr(self): with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) self.assertTrue(repr(lock).endswith('[unlocked]>')) self.assertTrue(RGX_REPR.match(repr(lock))) await lock.acquire() self.assertTrue(repr(lock).endswith('[locked]>')) self.assertTrue(RGX_REPR.match(repr(lock))) async def test_lock(self): lock = asyncio.Lock() with self.assertRaisesRegex( TypeError, "object Lock can't be used in 'await' expression" ): await lock self.assertFalse(lock.locked()) async def test_lock_by_with_statement(self): primitives = [ asyncio.Lock(), asyncio.Condition(), asyncio.Semaphore(), asyncio.BoundedSemaphore(), ] for lock in primitives: await asyncio.sleep(0.01) self.assertFalse(lock.locked()) with self.assertRaisesRegex( TypeError, r"object \w+ can't be used in 'await' expression" ): with await lock: pass self.assertFalse(lock.locked()) async def test_acquire(self): lock = asyncio.Lock() result = [] self.assertTrue(await lock.acquire()) async def c1(result): if await lock.acquire(): result.append(1) return True async def c2(result): if await lock.acquire(): result.append(2) return True async def c3(result): if await lock.acquire(): result.append(3) return True t1 = asyncio.create_task(c1(result)) t2 = asyncio.create_task(c2(result)) await asyncio.sleep(0) self.assertEqual([], result) lock.release() await asyncio.sleep(0) self.assertEqual([1], result) await asyncio.sleep(0) self.assertEqual([1], result) t3 = asyncio.create_task(c3(result)) lock.release() await asyncio.sleep(0) self.assertEqual([1, 2], result) lock.release() await asyncio.sleep(0) self.assertEqual([1, 2, 3], result) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) self.assertTrue(t3.done()) self.assertTrue(t3.result()) async def test_acquire_cancel(self): with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) self.assertTrue(await lock.acquire()) task = asyncio.create_task(lock.acquire()) asyncio.get_running_loop().call_soon(task.cancel) with self.assertRaises(asyncio.CancelledError): await task self.assertFalse(lock._waiters) async def test_cancel_race(self): # Several tasks: # - A acquires the lock # - B is blocked in acquire() # - C is blocked in acquire() # # Now, concurrently: # - B is cancelled # - A releases the lock # # If B's waiter is marked cancelled but not yet removed from # _waiters, A's release() call will crash when trying to set # B's waiter; instead, it should move on to C's waiter. # Setup: A has the lock, b and c are waiting. with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) async def lockit(name, blocker): await lock.acquire() try: if blocker is not None: await blocker finally: lock.release() fa = asyncio.get_running_loop().create_future() ta = asyncio.create_task(lockit('A', fa)) await asyncio.sleep(0) self.assertTrue(lock.locked()) tb = asyncio.create_task(lockit('B', None)) await asyncio.sleep(0) self.assertEqual(len(lock._waiters), 1) tc = asyncio.create_task(lockit('C', None)) await asyncio.sleep(0) self.assertEqual(len(lock._waiters), 2) # Create the race and check. # Without the fix this failed at the last assert. fa.set_result(None) tb.cancel() self.assertTrue(lock._waiters[0].cancelled()) await asyncio.sleep(0) self.assertFalse(lock.locked()) self.assertTrue(ta.done()) self.assertTrue(tb.cancelled()) await tc async def test_cancel_release_race(self): # Issue 32734 # Acquire 4 locks, cancel second, release first # and 2 locks are taken at once. loop = asyncio.get_running_loop() with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=loop) lock_count = 0 call_count = 0 async def lockit(): nonlocal lock_count nonlocal call_count call_count += 1 await lock.acquire() lock_count += 1 def trigger(): t1.cancel() lock.release() await lock.acquire() t1 = asyncio.create_task(lockit()) t2 = asyncio.create_task(lockit()) t3 = asyncio.create_task(lockit()) # Start scheduled tasks await asyncio.sleep(0) loop.call_soon(trigger) with self.assertRaises(asyncio.CancelledError): # Wait for cancellation await t1 # Make sure only one lock was taken self.assertEqual(lock_count, 1) # While 3 calls were made to lockit() self.assertEqual(call_count, 3) self.assertTrue(t1.cancelled() and t2.done()) # Cleanup the task that is stuck on acquire. t3.cancel() await asyncio.sleep(0) self.assertTrue(t3.cancelled()) async def test_finished_waiter_cancelled(self): with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) await lock.acquire() self.assertTrue(lock.locked()) tb = asyncio.create_task(lock.acquire()) await asyncio.sleep(0) self.assertEqual(len(lock._waiters), 1) # Create a second waiter, wake up the first, and cancel it. # Without the fix, the second was not woken up. tc = asyncio.create_task(lock.acquire()) tb.cancel() lock.release() await asyncio.sleep(0) self.assertTrue(lock.locked()) self.assertTrue(tb.cancelled()) # Cleanup await tc async def test_release_not_acquired(self): with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) self.assertRaises(RuntimeError, lock.release) async def test_release_no_waiters(self): with self.assertWarns(DeprecationWarning): lock = asyncio.Lock(loop=asyncio.get_running_loop()) await lock.acquire() self.assertTrue(lock.locked()) lock.release() self.assertFalse(lock.locked()) async def test_context_manager(self): lock = asyncio.Lock() self.assertFalse(lock.locked()) async with lock: self.assertTrue(lock.locked()) self.assertFalse(lock.locked()) class EventTests(unittest.IsolatedAsyncioTestCase): async def test_ctor_loop(self): loop = mock.Mock() with self.assertWarns(DeprecationWarning): ev = asyncio.Event(loop=loop) self.assertIs(ev._loop, loop) with self.assertWarns(DeprecationWarning): ev = asyncio.Event(loop=asyncio.get_running_loop()) self.assertIs(ev._loop, asyncio.get_running_loop()) async def test_ctor_noloop(self): asyncio.set_event_loop(asyncio.get_running_loop()) ev = asyncio.Event() self.assertIs(ev._loop, asyncio.get_running_loop()) async def test_repr(self): with self.assertWarns(DeprecationWarning): ev = asyncio.Event(loop=asyncio.get_running_loop()) self.assertTrue(repr(ev).endswith('[unset]>')) match = RGX_REPR.match(repr(ev)) self.assertEqual(match.group('extras'), 'unset') ev.set() self.assertTrue(repr(ev).endswith('[set]>')) self.assertTrue(RGX_REPR.match(repr(ev))) ev._waiters.append(mock.Mock()) self.assertTrue('waiters:1' in repr(ev)) self.assertTrue(RGX_REPR.match(repr(ev))) async def test_wait(self): with self.assertWarns(DeprecationWarning): ev = asyncio.Event(loop=asyncio.get_running_loop()) self.assertFalse(ev.is_set()) result = [] async def c1(result): if await ev.wait(): result.append(1) async def c2(result): if await ev.wait(): result.append(2) async def c3(result): if await ev.wait(): result.append(3) t1 = asyncio.create_task(c1(result)) t2 = asyncio.create_task(c2(result)) await asyncio.sleep(0) self.assertEqual([], result) t3 = asyncio.create_task(c3(result)) ev.set() await asyncio.sleep(0) self.assertEqual([3, 1, 2], result) self.assertTrue(t1.done()) self.assertIsNone(t1.result()) self.assertTrue(t2.done()) self.assertIsNone(t2.result()) self.assertTrue(t3.done()) self.assertIsNone(t3.result()) async def test_wait_on_set(self): ev = asyncio.Event() ev.set() res = await ev.wait() self.assertTrue(res) async def test_wait_cancel(self): ev = asyncio.Event() wait = asyncio.create_task(ev.wait()) asyncio.get_running_loop().call_soon(wait.cancel) with self.assertRaises(asyncio.CancelledError): await wait self.assertFalse(ev._waiters) async def test_clear(self): ev = asyncio.Event() self.assertFalse(ev.is_set()) ev.set() self.assertTrue(ev.is_set()) ev.clear() self.assertFalse(ev.is_set()) async def test_clear_with_waiters(self): ev = asyncio.Event() result = [] async def c1(result): if await ev.wait(): result.append(1) return True t = asyncio.create_task(c1(result)) await asyncio.sleep(0) self.assertEqual([], result) ev.set() ev.clear() self.assertFalse(ev.is_set()) ev.set() ev.set() self.assertEqual(1, len(ev._waiters)) await asyncio.sleep(0) self.assertEqual([1], result) self.assertEqual(0, len(ev._waiters)) self.assertTrue(t.done()) self.assertTrue(t.result()) class ConditionTests(unittest.IsolatedAsyncioTestCase): async def test_ctor_loop(self): loop = mock.Mock() with self.assertWarns(DeprecationWarning): cond = asyncio.Condition(loop=loop) self.assertIs(cond._loop, loop) cond = asyncio.Condition(loop=asyncio.get_running_loop()) self.assertIs(cond._loop, asyncio.get_running_loop()) async def test_ctor_noloop(self): cond = asyncio.Condition() self.assertIs(cond._loop, asyncio.get_running_loop()) async def test_wait(self): cond = asyncio.Condition() result = [] async def c1(result): await cond.acquire() if await cond.wait(): result.append(1) return True async def c2(result): await cond.acquire() if await cond.wait(): result.append(2) return True async def c3(result): await cond.acquire() if await cond.wait(): result.append(3) return True t1 = asyncio.create_task(c1(result)) t2 = asyncio.create_task(c2(result)) t3 = asyncio.create_task(c3(result)) await asyncio.sleep(0) self.assertEqual([], result) self.assertFalse(cond.locked()) self.assertTrue(await cond.acquire()) cond.notify() await asyncio.sleep(0) self.assertEqual([], result) self.assertTrue(cond.locked()) cond.release() await asyncio.sleep(0) self.assertEqual([1], result) self.assertTrue(cond.locked()) cond.notify(2) await asyncio.sleep(0) self.assertEqual([1], result) self.assertTrue(cond.locked()) cond.release() await asyncio.sleep(0) self.assertEqual([1, 2], result) self.assertTrue(cond.locked()) cond.release() await asyncio.sleep(0) self.assertEqual([1, 2, 3], result) self.assertTrue(cond.locked()) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) self.assertTrue(t3.done()) self.assertTrue(t3.result()) async def test_wait_cancel(self): cond = asyncio.Condition() await cond.acquire() wait = asyncio.create_task(cond.wait()) asyncio.get_running_loop().call_soon(wait.cancel) with self.assertRaises(asyncio.CancelledError): await wait self.assertFalse(cond._waiters) self.assertTrue(cond.locked()) async def test_wait_cancel_contested(self): cond = asyncio.Condition() await cond.acquire() self.assertTrue(cond.locked()) wait_task = asyncio.create_task(cond.wait()) await asyncio.sleep(0) self.assertFalse(cond.locked()) # Notify, but contest the lock before cancelling await cond.acquire() self.assertTrue(cond.locked()) cond.notify() asyncio.get_running_loop().call_soon(wait_task.cancel) asyncio.get_running_loop().call_soon(cond.release) try: await wait_task except asyncio.CancelledError: # Should not happen, since no cancellation points pass self.assertTrue(cond.locked()) async def test_wait_cancel_after_notify(self): # See bpo-32841 with self.assertWarns(DeprecationWarning): cond = asyncio.Condition(loop=asyncio.get_running_loop()) waited = False async def wait_on_cond(): nonlocal waited async with cond: waited = True # Make sure this area was reached await cond.wait() waiter = asyncio.create_task(wait_on_cond()) await asyncio.sleep(0) # Start waiting await cond.acquire() cond.notify() await asyncio.sleep(0) # Get to acquire() waiter.cancel() await asyncio.sleep(0) # Activate cancellation cond.release() await asyncio.sleep(0) # Cancellation should occur self.assertTrue(waiter.cancelled()) self.assertTrue(waited) async def test_wait_unacquired(self): cond = asyncio.Condition() with self.assertRaises(RuntimeError): await cond.wait() async def test_wait_for(self): cond = asyncio.Condition() presult = False def predicate(): return presult result = [] async def c1(result): await cond.acquire() if await cond.wait_for(predicate): result.append(1) cond.release() return True t = asyncio.create_task(c1(result)) await asyncio.sleep(0) self.assertEqual([], result) await cond.acquire() cond.notify() cond.release() await asyncio.sleep(0) self.assertEqual([], result) presult = True await cond.acquire() cond.notify() cond.release() await asyncio.sleep(0) self.assertEqual([1], result) self.assertTrue(t.done()) self.assertTrue(t.result()) async def test_wait_for_unacquired(self): cond = asyncio.Condition() # predicate can return true immediately res = await cond.wait_for(lambda: [1, 2, 3]) self.assertEqual([1, 2, 3], res) with self.assertRaises(RuntimeError): await cond.wait_for(lambda: False) async def test_notify(self): cond = asyncio.Condition() result = [] async def c1(result): await cond.acquire() if await cond.wait(): result.append(1) cond.release() return True async def c2(result): await cond.acquire() if await cond.wait(): result.append(2) cond.release() return True async def c3(result): await cond.acquire() if await cond.wait(): result.append(3) cond.release() return True t1 = asyncio.create_task(c1(result)) t2 = asyncio.create_task(c2(result)) t3 = asyncio.create_task(c3(result)) await asyncio.sleep(0) self.assertEqual([], result) await cond.acquire() cond.notify(1) cond.release() await asyncio.sleep(0) self.assertEqual([1], result) await cond.acquire() cond.notify(1) cond.notify(2048) cond.release() await asyncio.sleep(0) self.assertEqual([1, 2, 3], result) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) self.assertTrue(t3.done()) self.assertTrue(t3.result()) async def test_notify_all(self): cond = asyncio.Condition() result = [] async def c1(result): await cond.acquire() if await cond.wait(): result.append(1) cond.release() return True async def c2(result): await cond.acquire() if await cond.wait(): result.append(2) cond.release() return True t1 = asyncio.create_task(c1(result)) t2 = asyncio.create_task(c2(result)) await asyncio.sleep(0) self.assertEqual([], result) await cond.acquire() cond.notify_all() cond.release() await asyncio.sleep(0) self.assertEqual([1, 2], result) self.assertTrue(t1.done()) self.assertTrue(t1.result()) self.assertTrue(t2.done()) self.assertTrue(t2.result()) def test_notify_unacquired(self): cond = asyncio.Condition() self.assertRaises(RuntimeError, cond.notify) def test_notify_all_unacquired(self): cond = asyncio.Condition() self.assertRaises(RuntimeError, cond.notify_all) async def test_repr(self): cond = asyncio.Condition() self.assertTrue('unlocked' in repr(cond)) self.assertTrue(RGX_REPR.match(repr(cond))) await cond.acquire() self.assertTrue('locked' in repr(cond)) cond._waiters.append(mock.Mock()) self.assertTrue('waiters:1' in repr(cond)) self.assertTrue(RGX_REPR.match(repr(cond))) cond._waiters.append(mock.Mock()) self.assertTrue('waiters:2' in repr(cond)) self.assertTrue(RGX_REPR.match(repr(cond))) async def test_context_manager(self): cond = asyncio.Condition() self.assertFalse(cond.locked()) async with cond: self.assertTrue(cond.locked()) self.assertFalse(cond.locked()) async def test_explicit_lock(self): async def f(lock=None, cond=None): if lock is None: lock = asyncio.Lock() if cond is None: cond = asyncio.Condition(lock) self.assertIs(cond._lock, lock) self.assertFalse(lock.locked()) self.assertFalse(cond.locked()) async with cond: self.assertTrue(lock.locked()) self.assertTrue(cond.locked()) self.assertFalse(lock.locked()) self.assertFalse(cond.locked()) async with lock: self.assertTrue(lock.locked()) self.assertTrue(cond.locked()) self.assertFalse(lock.locked()) self.assertFalse(cond.locked()) # All should work in the same way. await f() await f(asyncio.Lock()) lock = asyncio.Lock() await f(lock, asyncio.Condition(lock)) async def test_ambiguous_loops(self): loop = asyncio.new_event_loop() self.addCleanup(loop.close) async def wrong_loop_in_lock(): with self.assertWarns(DeprecationWarning): asyncio.Lock(loop=loop) lock = asyncio.Lock() lock._loop = loop # use private API for testing async with lock: # acquired immediately via the fast-path # without interaction with any event loop. # cond.acquire() will trigger waiting on the lock # and it will discover the event loop mismatch. with self.assertRaisesRegex( ValueError, "loop argument must agree with lock", ): asyncio.Condition(lock) async def wrong_loop_in_cond(): # Same analogy here with the condition's loop. lock = asyncio.Lock() async with lock: with self.assertRaisesRegex( ValueError, "loop argument must agree with lock" ): with self.assertWarns(DeprecationWarning): asyncio.Condition(lock, loop=loop) await wrong_loop_in_lock() await wrong_loop_in_cond() async def test_timeout_in_block(self): condition = asyncio.Condition() async with condition: with self.assertRaises(asyncio.TimeoutError): await asyncio.wait_for(condition.wait(), timeout=0.5) class SemaphoreTests(unittest.IsolatedAsyncioTestCase): async def test_ctor_loop(self): loop = mock.Mock() with self.assertWarns(DeprecationWarning): sem = asyncio.Semaphore(loop=loop) self.assertIs(sem._loop, loop) with self.assertWarns(DeprecationWarning): sem = asyncio.Semaphore(loop=asyncio.get_running_loop()) self.assertIs(sem._loop, asyncio.get_running_loop()) async def test_ctor_noloop(self): sem = asyncio.Semaphore() self.assertIs(sem._loop, asyncio.get_running_loop()) async def test_initial_value_zero(self): with self.assertWarns(DeprecationWarning): sem = asyncio.Semaphore(0, loop=asyncio.get_running_loop()) self.assertTrue(sem.locked()) async def test_repr(self): sem = asyncio.Semaphore() self.assertTrue(repr(sem).endswith('[unlocked, value:1]>')) self.assertTrue(RGX_REPR.match(repr(sem))) await sem.acquire() self.assertTrue(repr(sem).endswith('[locked]>')) self.assertTrue('waiters' not in repr(sem)) self.assertTrue(RGX_REPR.match(repr(sem))) sem._waiters.append(mock.Mock()) self.assertTrue('waiters:1' in repr(sem)) self.assertTrue(RGX_REPR.match(repr(sem))) sem._waiters.append(mock.Mock()) self.assertTrue('waiters:2' in repr(sem)) self.assertTrue(RGX_REPR.match(repr(sem))) async def test_semaphore(self): sem = asyncio.Semaphore() self.assertEqual(1, sem._value) with self.assertRaisesRegex( TypeError, "object Semaphore can't be used in 'await' expression", ): await sem self.assertFalse(sem.locked()) self.assertEqual(1, sem._value) def test_semaphore_value(self): self.assertRaises(ValueError, asyncio.Semaphore, -1) async def test_acquire(self): sem = asyncio.Semaphore(3) result = [] self.assertTrue(await sem.acquire()) self.assertTrue(await sem.acquire()) self.assertFalse(sem.locked()) async def c1(result): await sem.acquire() result.append(1) return True async def c2(result): await sem.acquire() result.append(2) return True async def c3(result): await sem.acquire() result.append(3) return True async def c4(result): await sem.acquire() result.append(4) return True t1 = asyncio.create_task(c1(result)) t2 = asyncio.create_task(c2(result)) t3 = asyncio.create_task(c3(result)) await asyncio.sleep(0) self.assertEqual([1], result) self.assertTrue(sem.locked()) self.assertEqual(2, len(sem._waiters)) self.assertEqual(0, sem._value) t4 = asyncio.create_task(c4(result)) sem.release() sem.release() self.assertEqual(2, sem._value) await asyncio.sleep(0) self.assertEqual(0, sem._value) self.assertEqual(3, len(result)) self.assertTrue(sem.locked()) self.assertEqual(1, len(sem._waiters)) self.assertEqual(0, sem._value) self.assertTrue(t1.done()) self.assertTrue(t1.result()) race_tasks = [t2, t3, t4] done_tasks = [t for t in race_tasks if t.done() and t.result()] self.assertTrue(2, len(done_tasks)) # cleanup locked semaphore sem.release() await asyncio.gather(*race_tasks) async def test_acquire_cancel(self): sem = asyncio.Semaphore() await sem.acquire() acquire = asyncio.create_task(sem.acquire()) asyncio.get_running_loop().call_soon(acquire.cancel) with self.assertRaises(asyncio.CancelledError): await acquire self.assertTrue((not sem._waiters) or all(waiter.done() for waiter in sem._waiters)) async def test_acquire_cancel_before_awoken(self): sem = asyncio.Semaphore(value=0) t1 = asyncio.create_task(sem.acquire()) t2 = asyncio.create_task(sem.acquire()) t3 = asyncio.create_task(sem.acquire()) t4 = asyncio.create_task(sem.acquire()) await asyncio.sleep(0) t1.cancel() t2.cancel() sem.release() await asyncio.sleep(0) num_done = sum(t.done() for t in [t3, t4]) self.assertEqual(num_done, 1) self.assertTrue(t3.done()) self.assertFalse(t4.done()) t3.cancel() t4.cancel() await asyncio.sleep(0) async def test_acquire_hang(self): sem = asyncio.Semaphore(value=0) t1 = asyncio.create_task(sem.acquire()) t2 = asyncio.create_task(sem.acquire()) await asyncio.sleep(0) t1.cancel() sem.release() await asyncio.sleep(0) self.assertTrue(sem.locked()) self.assertTrue(t2.done()) async def test_release_not_acquired(self): with self.assertWarns(DeprecationWarning): sem = asyncio.BoundedSemaphore(loop=asyncio.get_running_loop()) self.assertRaises(ValueError, sem.release) async def test_release_no_waiters(self): sem = asyncio.Semaphore() await sem.acquire() self.assertTrue(sem.locked()) sem.release() self.assertFalse(sem.locked()) async def test_acquire_fifo_order(self): sem = asyncio.Semaphore(1) result = [] async def coro(tag): await sem.acquire() result.append(f'{tag}_1') await asyncio.sleep(0.01) sem.release() await sem.acquire() result.append(f'{tag}_2') await asyncio.sleep(0.01) sem.release() t1 = asyncio.create_task(coro('c1')) t2 = asyncio.create_task(coro('c2')) t3 = asyncio.create_task(coro('c3')) await asyncio.gather(t1, t2, t3) self.assertEqual( ['c1_1', 'c2_1', 'c3_1', 'c1_2', 'c2_2', 'c3_2'], result ) if __name__ == '__main__': unittest.main()