ÿØÿà JFIF    ÿÛ „  ( %"1!%)+...383,7(-.+  -+++--++++---+-+-----+---------------+---+-++7-----ÿÀ  ß â" ÿÄ     ÿÄ H    !1AQaq"‘¡2B±ÁÑð#R“Ò Tbr‚²á3csƒ’ÂñDS¢³$CÿÄ   ÿÄ %  !1AQa"23‘ÿÚ   ? ôÿ ¨pŸªáÿ —åYõõ\?àÒü©ŠÄï¨pŸªáÿ —åYõõ\?àÓü©ŠÄá 0Ÿªáÿ Ÿå[úƒ ú®ði~TÁbqÐ8OÕpÿ ƒOò¤Oè`–RÂáœá™êi€ßÉ< FtŸI“öÌ8úDf´°å}“¾œ6  öFá°y¥jñÇh†ˆ¢ã/ÃÐ:ªcÈ "Y¡ðÑl>ÿ ”ÏËte:qž\oäŠe÷󲍷˜HT4&ÿ ÓÐü6ö®¿øþßèô Ÿ•7Ñi’•j|“ñì>b…þS?*Óôÿ ÓÐü*h¥£ír¶ü UãS炟[AÐaè[ûª•õ&õj?†Éö+EzP—WeÒírJFt ‘BŒ†Ï‡%#tE Øz ¥OÛ«!1›üä±Í™%ºÍãö]°î(–:@<‹ŒÊö×òÆt¦ãº+‡¦%ÌÁ²h´OƒJŒtMÜ>ÀÜÊw3Y´•牋4ǍýʏTì>œú=Íwhyë,¾Ôò×õ¿ßÊa»«þˆѪQ|%6ž™A õ%:øj<>É—ÿ Å_ˆCbõ¥š±ý¯Ýƒï…¶|RëócÍf溪“t.СøTÿ *Ä¿-{†çàczůŽ_–^XþŒ±miB[X±d 1,é”zEù»& î9gœf™9Ð'.;—™i}!ôšåîqêÛ٤ёý£½ÆA–àôe"A$˝Úsäÿ ÷Û #°xŸëí(l »ý3—¥5m! rt`†0~'j2(]S¦¦kv,ÚÇ l¦øJA£Šƒ J3E8ÙiŽ:cÉžúeZ°€¯\®kÖ(79«Ž:¯X”¾³Š&¡* ….‰Ž(ÜíŸ2¥ª‡×Hi²TF¤ò[¨íÈRëÉ䢍mgÑ.Ÿ<öäS0í„ǹÁU´f#Vß;Õ–…P@3ío<ä-±»Ž.L|kªÀê›fÂ6@»eu‚|ÓaÞÆŸ…¨ááå>åŠ?cKü6ùTÍÆ”†sĤÚ;H2RÚ†õ\Ö·Ÿn'¾ ñ#ºI¤Å´%çÁ­‚â7›‹qT3Iï¨ÖÚ5I7Ë!ÅOóŸ¶øÝñØôת¦$Tcö‘[«Ö³šÒ';Aþ ¸èíg A2Z"i¸vdÄ÷.iõ®§)¿]¤À†–‡É&ä{V¶iŽ”.Ó×Õÿ û?h¬Mt–íª[ÿ Ñÿ ÌV(í}=ibÔ¡›¥¢±b Lô¥‡piη_Z<‡z§èŒ)iÖwiÇ 2hÙ3·=’d÷8éŽ1¦¸c¤µ€7›7Ø ð\á)} ¹fËí›pAÃL%âc2 í§æQz¿;T8sæ°qø)QFMð‰XŒÂ±N¢aF¨…8¯!U  Z©RÊ ÖPVÄÀÍin™Ì-GˆªÅËŠ›•zË}º±ŽÍFò¹}Uw×#ä5B¤{î}Ð<ÙD é©¤&‡ïDbàÁôMÁ." ¤‡ú*õ'VŽ|¼´Úgllº¼klz[Æüï÷Aób‡Eÿ dÑ»Xx9ÃÜ£ÁT/`¼¸vI±Ýµ·Ë‚“G³þ*Ÿû´r|*}<¨îºœ @¦mÄ’M¹”.œ«Y–|6ÏU¤jç¥ÕÞqO ˜kDÆÁ¨5ÿ š;ÐЦ¦€GÙk \ –Þ=â¼=SͧµªS°ÚÍpÜãQűÀõ¬?ÃÁ1Ñ•õZà?hóœ€ L¦l{Y*K˜Ù›zc˜–ˆâ ø+¾ ­-Ök¥%ùEÜA'}ˆ><ÊIè“bpÍ/qÞâvoX€w,\úªò6Z[XdÒæ­@Ö—€$òJí#é>'°Ú ôª˜<)4ryÙ£|óAÅn5žêŸyÒäMÝ2{"}‰–¤l÷ûWX\l¾Á¸góÉOÔ /óñB¤f¸çñ[.P˜ZsÊË*ßT܈§QN¢’¡¨§V¼(Üù*eÕ“”5T¨‹Âê¥FŒã½Dü[8'Ò¥a…Ú¶k7a *•›¼'Ò·\8¨ª\@\õ¢¦íq+DÙrmÎ…_ªæ»ŠÓœ¡¯’Ré9MÅ×D™lælffc+ŒÑ,ý™ÿ ¯þǤ=Å’Á7µ÷ÚÛ/“Ü€ñýã¼àí¾ÕÑ+ƒ,uµMâÀÄbm:ÒÎPæ{˜Gz[ƒ¯«® KHà`ߨŠéí¯P8Aq.C‰ à€kòpj´kN¶qô€…Õ,ÜNŠª-­{Zö’æû44‰sŽè‰îVíRœÕm" 6?³D9¡ÇTíÅꋇ`4«¸ÝÁô ï’ýorqКÇZ«x4Žâéþuïf¹µö[P ,Q£éaX±`PÉÍZ ¸äYúg üAx ’6Lê‚xÝÓ*äQ  Ï’¨hÍ =²,6ï#rÃ<¯–£»ƒ‹,–ê•€ aÛsñ'%Æ"®ÛüìBᝠHÚ3ß°©$“XnœÖ’î2ËTeûìxîß ¦å¿çÉ ðK§þ{‘t‚Ϋ¬jéîZ[ ”š7L¥4VÚCE×]m¤Øy”ä4-dz£œ§¸x.*ãÊÊ b÷•h:©‡¦s`BTÁRû¾g⻩‹jø sF¢àJøFl‘È•Xᓁà~*j¯ +(ÚÕ6-£¯÷GŠØy‚<Ç’.F‹Hœw(+)ÜÜâÈzÄäT§FߘãÏ;DmVœ3Àu@mÚüXÝü•3B¨òÌÁÛ<·ÃÜ z,Ì@õÅ·d2]ü8s÷IôÞ¯^Ç9¢u„~ëAŸï4«M? K]­ÅàPl@s_ p:°¬ZR”´›JC[CS.h‹ƒïËœ«Æ]–÷ó‚wR×k7X‰k›‘´ù¦=¡«‰¨¨Â')—71ó’c‡Ðúµ `é.{§p¹ój\Ž{1h{o±Ý=áUÊïGÖŒõ–-BÄm+AZX¶¡ ïHðæ¥JmÙ;…䡟ˆ¦ ° äšiÉg«$üMk5¤L“’çÊvïâï ,=f“"íἊ5ô¬x6{ɏžID0e¸vçmi'︧ºð9$ò¹÷*£’9ÿ ²TÔ…×>JV¥}Œ}$p[bÔ®*[jzS*8 ”·T›Í–ñUîƒwo$áè=LT™ç—~ô·¤ÈÚ$榍q‰„+´kFm)ž‹©i–ËqÞŠ‰à¶ü( ‚•§ •°ò·‡#5ª•µÊ﯅¡X¨šÁ*F#TXJÊ ušJVÍ&=iÄs1‚3•'fý§5Ñ<=[íÞ­ PÚ;ѱÌ_~Ä££8rÞ ²w;’hDT°>ÈG¬8Á²ÚzŽ®ò®qZcqJêäÞ-ö[ܘbň±çb“ж31²n×iƒðÕ;1¶þÉ ªX‰,ßqÏ$>•î íZ¥Z 1{ç൵+ƒÕµ¥°T$§K]á»Ûï*·¤tMI’ÂZbŽÕiÒ˜}bÓ0£ª5›¨ [5Ž^ÝœWøÂÝh° ¢OWun£¤5 a2Z.G2³YL]jåtì”ä ÁÓ‘%"©<Ôúʰsº UZvä‡ÄiÆÒM .÷V·™ø#kèýiíÌ–ª)µT[)BˆõÑ xB¾B€ÖT¨.¥~ð@VĶr#¸ü*åZNDŽH;âi ],©£öØpù(šºãö¼T.uCê•4@ÿ GÕÛ)Cx›®0ø#:ÏðFÒbR\(€€Ä®fã4Þ‰Fä¯HXƒÅ,†öEÑÔÜ]Öv²?tLÃvBY£ú6Êu5ÅAQ³1‘’¬x–HŒÐ‡ ^ ¸KwJôÖŽ5×CÚ¨vÜ«/B0$×k°=ðbÇ(Ï)w±A†Á† 11Í=èQšµ626ŒÜ/`G«µ<}—-Ö7KEHÈÉðóȤmݱû±·ø«Snmá=“䫚mݱŸ¡¶~ó·“äUóJæúòB|E LêŽy´jDÔ$G¢þÐñ7óR8ýÒ…Ç› WVe#·Ÿ p·Fx~•ݤF÷0Èÿ K¯æS<6’¡WШ; ´ÿ ¥Êø\Òuî†åÝ–VNœkÒ7oòX¨Á­Ø÷FÎÑä±g÷ÿ M~Çî=p,X´ ÝÌÚÅ‹’ÃjÖ.ØöÏñ qïQ¤ÓZE†° =6·]܈ s¸>v•Ž^Ý\wq9r‰Î\¸¡kURÒ$­*‹Nq?Þª*!sŠÆ:TU_u±T+øX¡ ®¹¡,ÄâÃBTsÜ$Ø›4m椴zÜK]’’›Pƒ @€#â˜`é¹=I‡fiV•Ôî“nRm+µFPOhÍ0B£ €+¬5c v•:P'ÒyÎ ‰V~‚Ó†ÖuókDoh$å\*ö%Ю=£«…aȼ½÷Û.-½VŒŠ¼'lyî±1¬3ó#ÞE¿ÔS¤gV£m›=§\û"—WU¤ÚǼÿ ÂnÁGŒÃ ‚õN D³õNÚíŒÕ;HôyÄÈ©P¹Ä{:?R‘Ô¨âF÷ø£bÅó® JS|‚R÷ivýáâ€Æé¡è³´IئÑT!§˜•ت‚¬â@q€wnïCWÄ@JU€ê¯m6]Ï:£âx'+ÒðXvÓ¦Úm=–´7œ $ì“B£~p%ÕŸUþ« N@¼üï~w˜ñø5®—'Ôe»¤5ã//€ž~‰Tþ›Å7•#¤× Íö pÄ$ùeåì*«ÓŠEØWEÈsßg ¦ûvžSsLpºÊW–âµEWöˬH; ™!CYõZ ÃÄf æ#1W. \uWâ\,\Çf j’<qTbên›Î[vxx£ë 'ö¨1›˜ÀM¼Pÿ H)ƒêêŒA7s,|F“ 꺸k³9Ìö*ç®;Ö!Ö$Eiž•¹ÒÚ†ýóéÝû¾ÕS®ó$’NÝäŸz¤5r¦ãÄÃD÷Üø!°ø‡Ô&@m™Ì^Ãä­d q5Lnÿ N;.6½·N|#ä"1Nƒx“ã<3('&ñßt  ~ªu”1Tb㫨9ê–›–bìd$ߣ=#ÕãÒmU¯eí$EFù5ýYô櫨æì™Ç—±ssM]·á¿0ÕåJRÓªîiƒ+O58ÖñªŠÒx" \µâá¨i’¤i —Ö ” M+M¤ë9‚‰A¦°Qõ¾ßøK~¼Ã‘g…Ö´~÷Ï[3GUœÒ½#…kàÔ®Ò”‰³·dWV‰IP‰Ú8u¹”E ÖqLj¾êÕCBš{A^Âß;–¨`¯¬ìö ˼ ×tìø.tƐm*n¨y4o&Àx¥n¦×î‡aupáÛj8¿m›è¶ã!o½;ß0y^ý×^EÑ¿ÒjzŒ­)vÚÑnÄL …^ªô× ‡—‚3k Îý­hï]içå–îÏ*÷ñþ»Ô CÒjøjÍznˆ´ ¹#b'Fô‹ ‰v¥'’à'T´ƒHýÍ%M‰ ƒ&ÆÇŒï1 ‘ –Þ ‰i¬s žR-Ÿ kЬá¬7:þ 0ŒÅÒÕ/aÙ¬ÃÝ#Úøœ ©aiVc‰. ¹¦ãµ” ›Yg¦›ÆÎýº°f³7ƒhá·¸­}&D9¡ÂsÉÙÞèŠõØàC™¨ñbFC|´Ü(ŸƒÚÒ-%»'a Ì¿)ËÇn¿úÿ ÞŽX…4ÊÅH^ôΑí@ù¹Eh¶“L8Çjù ¼ÎåVªóR©Ï5uà V4lZß®=€xÖŸ–ÑÈ ÷”¨°¾__yM1tÉ?uÆþIkÄgæ@þ[¢†°XÃJ£j·:nkÅ¢u ‘}âGzö­/IµèЬ¼48q¦F°ŽR¼=ûì{´¯RýicS ÕÛ íNtÍÙï£,w4rêì®»~x(©Uñ§#Ñ&œÕ¤>ÎåÍÓ9’Ö{9eV­[Öjâ²ãu]˜å2›qÑšÕJç0€sÄ|Êëè0튔bÁ>“{×_F`Ø©ºê:µä,v¤ðfc1±"«ÔÍän1#=· Âøv~H½ÐßA¾¿Ü€Óš]Õ; I¾÷ç‚Qi†î¹9ywÔKG˜áñ zQY—§ÃÕZ07§X‚ Áh;ÁM)iÌCH-¯T‘ë|A0{Ò½LÚ–TâÖkÜ’dÀ“rmm»”جPF³ÖcbE§T€ÒxKºû’Ó®7±²(\4ŽÃ¸Uu@j™yĵ;³µ!Á¢b.W¤=mõ´êµK k ¸K^ÜÛ#p*Ü14qkZç5ïë †°5Ï%ÍÛ<Õ¤×Ô¥ê†C Õ´¼ú$ƒÖ“”]Ù¬qÞÚ[4©ý!ûÏ—Áb쳐XµA¬â~`›Çr¸8ìùÝ䫦<>ä÷«?xs´ÇÑ /á;¹øüÊÈÙà{"@Žïzâ¬[âß‚ U_<ÇŸ½4èN˜ú61®qŠu ¦þF£»äJ_ˆÙÎ~ ÞAã–݄ϗrŠD;xTž‘ô`É«…suãO`?³à™ô Lý#Íc5öoæØ‚y´´÷«ZR§<&JÇ+éâô´€i!Àˆ0æAoàðLèÖ-2ŸõW.’t^–(KÁmHµV@xÜÇy®Ñø­â^:Ú3w· 7½¹°ñ¸â¹®:',«Mœ—n­Á+Ãbš LÈ‘ÄnRÓÅœ%¦²‰¨ùQ:¤f‚ "PÕtô¸…cæl…&˜Ú˜Ôkv‹ž+vŠ,=¢v­6—Xy*¥t£«<™:“aîϲ=¦6rO]XI¿Œ÷¤zÚ­›¶ 6÷”w\d ü~v®ˆÌk«^m<ÿ ¢‰Õ\)ùºŽ;… lîÙÅEŠ®cѾ@vnMÏ,¼“ñ•ŽBxðÃzãÇç%3ˆ"}Ù•Åî> BÉú;Ò]V+P˜F_´ßé> Øše|ï‡ÄOmFæÇ ãqÞ$/xÐx­z`ï9"œÜij‚!7.\Td…9M‡•iŽ‹¾‘50ÞŽn¥ß4ÉôO ¹*í^QêËÜÇÌ8=ާs‰'ÂëÙ«á%Pú[O †ÅP¯Vsް.‰,kc¶ ¬A9n˜XÎ-ÞšN["¹QÕ‰ƒMýÁߺXJæÍaLj¾×Ãmã¾ãÚ uñÒþåQô¦¥ /ÄUx:‚ÍÜ’ Đ©ØÝ3V¨‰ÕnÐ6ó*óúK­«…c ¯U òhsý­jóÔj#,ímŒRµ«lbïUTŒÑ8†Ä0œÏr`ð¡¬É Ї ë"À² ™ 6¥ f¶ ¢ÚoܱԷ-<Àî)†a¶ž'Ú»¨TXqØæ¶÷YÄHy˜9ÈIW­YÀuMFë ºÏ’AqÌ4·/Ú †ô'i$øä­=Ä Ý|öK×40è|È6p‘0§)o¥ctî§H+CA-“ xØ|ÐXАç l8íºð3Ø:³¤¬KX¯UÿÙ # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Tests for L{twisted.application} and its interaction with L{twisted.persisted.sob}. """ from __future__ import absolute_import, division import copy import os import pickle try: import asyncio except ImportError: asyncio = None from twisted.application import service, internet, app, reactors from twisted.internet import interfaces, defer, protocol, reactor from twisted.persisted import sob from twisted.plugins import twisted_reactors from twisted.protocols import wire, basic from twisted.python import usage from twisted.python.compat import NativeStringIO from twisted.python.runtime import platformType from twisted.python.test.modules_helpers import TwistedModulesMixin from twisted.test.proto_helpers import MemoryReactor from twisted.trial import unittest class Dummy: processName=None class ServiceTests(unittest.TestCase): def testName(self): s = service.Service() s.setName("hello") self.assertEqual(s.name, "hello") def testParent(self): s = service.Service() p = service.MultiService() s.setServiceParent(p) self.assertEqual(list(p), [s]) self.assertEqual(s.parent, p) def testApplicationAsParent(self): s = service.Service() p = service.Application("") s.setServiceParent(p) self.assertEqual(list(service.IServiceCollection(p)), [s]) self.assertEqual(s.parent, service.IServiceCollection(p)) def testNamedChild(self): s = service.Service() p = service.MultiService() s.setName("hello") s.setServiceParent(p) self.assertEqual(list(p), [s]) self.assertEqual(s.parent, p) self.assertEqual(p.getServiceNamed("hello"), s) def testDoublyNamedChild(self): s = service.Service() p = service.MultiService() s.setName("hello") s.setServiceParent(p) self.assertRaises(RuntimeError, s.setName, "lala") def testDuplicateNamedChild(self): s = service.Service() p = service.MultiService() s.setName("hello") s.setServiceParent(p) s = service.Service() s.setName("hello") self.assertRaises(RuntimeError, s.setServiceParent, p) def testDisowning(self): s = service.Service() p = service.MultiService() s.setServiceParent(p) self.assertEqual(list(p), [s]) self.assertEqual(s.parent, p) s.disownServiceParent() self.assertEqual(list(p), []) self.assertIsNone(s.parent) def testRunning(self): s = service.Service() self.assertFalse(s.running) s.startService() self.assertTrue(s.running) s.stopService() self.assertFalse(s.running) def testRunningChildren1(self): s = service.Service() p = service.MultiService() s.setServiceParent(p) self.assertFalse(s.running) self.assertFalse(p.running) p.startService() self.assertTrue(s.running) self.assertTrue(p.running) p.stopService() self.assertFalse(s.running) self.assertFalse(p.running) def testRunningChildren2(self): s = service.Service() def checkRunning(): self.assertTrue(s.running) t = service.Service() t.stopService = checkRunning t.startService = checkRunning p = service.MultiService() s.setServiceParent(p) t.setServiceParent(p) p.startService() p.stopService() def testAddingIntoRunning(self): p = service.MultiService() p.startService() s = service.Service() self.assertFalse(s.running) s.setServiceParent(p) self.assertTrue(s.running) s.disownServiceParent() self.assertFalse(s.running) def testPrivileged(self): s = service.Service() def pss(): s.privilegedStarted = 1 s.privilegedStartService = pss s1 = service.Service() p = service.MultiService() s.setServiceParent(p) s1.setServiceParent(p) p.privilegedStartService() self.assertTrue(s.privilegedStarted) def testCopying(self): s = service.Service() s.startService() s1 = copy.copy(s) self.assertFalse(s1.running) self.assertTrue(s.running) if hasattr(os, "getuid"): curuid = os.getuid() curgid = os.getgid() else: curuid = curgid = 0 class ProcessTests(unittest.TestCase): def testID(self): p = service.Process(5, 6) self.assertEqual(p.uid, 5) self.assertEqual(p.gid, 6) def testDefaults(self): p = service.Process(5) self.assertEqual(p.uid, 5) self.assertIsNone(p.gid) p = service.Process(gid=5) self.assertIsNone(p.uid) self.assertEqual(p.gid, 5) p = service.Process() self.assertIsNone(p.uid) self.assertIsNone(p.gid) def testProcessName(self): p = service.Process() self.assertIsNone(p.processName) p.processName = 'hello' self.assertEqual(p.processName, 'hello') class InterfacesTests(unittest.TestCase): def testService(self): self.assertTrue(service.IService.providedBy(service.Service())) def testMultiService(self): self.assertTrue(service.IService.providedBy(service.MultiService())) self.assertTrue(service.IServiceCollection.providedBy( service.MultiService())) def testProcess(self): self.assertTrue(service.IProcess.providedBy(service.Process())) class ApplicationTests(unittest.TestCase): def testConstructor(self): service.Application("hello") service.Application("hello", 5) service.Application("hello", 5, 6) def testProcessComponent(self): a = service.Application("hello") self.assertIsNone(service.IProcess(a).uid) self.assertIsNone(service.IProcess(a).gid) a = service.Application("hello", 5) self.assertEqual(service.IProcess(a).uid, 5) self.assertIsNone(service.IProcess(a).gid) a = service.Application("hello", 5, 6) self.assertEqual(service.IProcess(a).uid, 5) self.assertEqual(service.IProcess(a).gid, 6) def testServiceComponent(self): a = service.Application("hello") self.assertIs(service.IService(a), service.IServiceCollection(a)) self.assertEqual(service.IService(a).name, "hello") self.assertIsNone(service.IService(a).parent) def testPersistableComponent(self): a = service.Application("hello") p = sob.IPersistable(a) self.assertEqual(p.style, 'pickle') self.assertEqual(p.name, 'hello') self.assertIs(p.original, a) class LoadingTests(unittest.TestCase): def test_simpleStoreAndLoad(self): a = service.Application("hello") p = sob.IPersistable(a) for style in 'source pickle'.split(): p.setStyle(style) p.save() a1 = service.loadApplication("hello.ta"+style[0], style) self.assertEqual(service.IService(a1).name, "hello") with open("hello.tac", 'w') as f: f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = service.loadApplication("hello.tac", 'python') self.assertEqual(service.IService(a1).name, "hello") class AppSupportTests(unittest.TestCase): def testPassphrase(self): self.assertIsNone(app.getPassphrase(0)) def testLoadApplication(self): """ Test loading an application file in different dump format. """ a = service.Application("hello") baseconfig = {'file': None, 'source': None, 'python':None} for style in 'source pickle'.split(): config = baseconfig.copy() config[{'pickle': 'file'}.get(style, style)] = 'helloapplication' sob.IPersistable(a).setStyle(style) sob.IPersistable(a).save(filename='helloapplication') a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello") config = baseconfig.copy() config['python'] = 'helloapplication' with open("helloapplication", 'w') as f: f.writelines([ "from twisted.application import service\n", "application = service.Application('hello')\n", ]) a1 = app.getApplication(config, None) self.assertEqual(service.IService(a1).name, "hello") def test_convertStyle(self): appl = service.Application("lala") for instyle in 'source pickle'.split(): for outstyle in 'source pickle'.split(): sob.IPersistable(appl).setStyle(instyle) sob.IPersistable(appl).save(filename="converttest") app.convertStyle("converttest", instyle, None, "converttest.out", outstyle, 0) appl2 = service.loadApplication("converttest.out", outstyle) self.assertEqual(service.IService(appl2).name, "lala") def test_startApplication(self): appl = service.Application("lala") app.startApplication(appl, 0) self.assertTrue(service.IService(appl).running) class Foo(basic.LineReceiver): def connectionMade(self): self.transport.write(b'lalala\r\n') def lineReceived(self, line): self.factory.line = line self.transport.loseConnection() def connectionLost(self, reason): self.factory.d.callback(self.factory.line) class DummyApp: processName = None def addService(self, service): self.services[service.name] = service def removeService(self, service): del self.services[service.name] class TimerTarget: def __init__(self): self.l = [] def append(self, what): self.l.append(what) class TestEcho(wire.Echo): def connectionLost(self, reason): self.d.callback(True) class InternetTests(unittest.TestCase): def testTCP(self): s = service.MultiService() s.startService() factory = protocol.ServerFactory() factory.protocol = TestEcho TestEcho.d = defer.Deferred() t = internet.TCPServer(0, factory) t.setServiceParent(s) num = t._port.getHost().port factory = protocol.ClientFactory() factory.d = defer.Deferred() factory.protocol = Foo factory.line = None internet.TCPClient('127.0.0.1', num, factory).setServiceParent(s) factory.d.addCallback(self.assertEqual, b'lalala') factory.d.addCallback(lambda x : s.stopService()) factory.d.addCallback(lambda x : TestEcho.d) return factory.d def test_UDP(self): """ Test L{internet.UDPServer} with a random port: starting the service should give it valid port, and stopService should free it so that we can start a server on the same port again. """ if not interfaces.IReactorUDP(reactor, None): raise unittest.SkipTest("This reactor does not support UDP sockets") p = protocol.DatagramProtocol() t = internet.UDPServer(0, p) t.startService() num = t._port.getHost().port self.assertNotEqual(num, 0) def onStop(ignored): t = internet.UDPServer(num, p) t.startService() return t.stopService() return defer.maybeDeferred(t.stopService).addCallback(onStop) def testPrivileged(self): factory = protocol.ServerFactory() factory.protocol = TestEcho TestEcho.d = defer.Deferred() t = internet.TCPServer(0, factory) t.privileged = 1 t.privilegedStartService() num = t._port.getHost().port factory = protocol.ClientFactory() factory.d = defer.Deferred() factory.protocol = Foo factory.line = None c = internet.TCPClient('127.0.0.1', num, factory) c.startService() factory.d.addCallback(self.assertEqual, b'lalala') factory.d.addCallback(lambda x : c.stopService()) factory.d.addCallback(lambda x : t.stopService()) factory.d.addCallback(lambda x : TestEcho.d) return factory.d def testConnectionGettingRefused(self): factory = protocol.ServerFactory() factory.protocol = wire.Echo t = internet.TCPServer(0, factory) t.startService() num = t._port.getHost().port t.stopService() d = defer.Deferred() factory = protocol.ClientFactory() factory.clientConnectionFailed = lambda *args: d.callback(None) c = internet.TCPClient('127.0.0.1', num, factory) c.startService() return d def testUNIX(self): # FIXME: This test is far too dense. It needs comments. # -- spiv, 2004-11-07 s = service.MultiService() s.startService() factory = protocol.ServerFactory() factory.protocol = TestEcho TestEcho.d = defer.Deferred() t = internet.UNIXServer('echo.skt', factory) t.setServiceParent(s) factory = protocol.ClientFactory() factory.protocol = Foo factory.d = defer.Deferred() factory.line = None internet.UNIXClient('echo.skt', factory).setServiceParent(s) factory.d.addCallback(self.assertEqual, b'lalala') factory.d.addCallback(lambda x : s.stopService()) factory.d.addCallback(lambda x : TestEcho.d) factory.d.addCallback(self._cbTestUnix, factory, s) return factory.d def _cbTestUnix(self, ignored, factory, s): TestEcho.d = defer.Deferred() factory.line = None factory.d = defer.Deferred() s.startService() factory.d.addCallback(self.assertEqual, b'lalala') factory.d.addCallback(lambda x : s.stopService()) factory.d.addCallback(lambda x : TestEcho.d) return factory.d def testVolatile(self): factory = protocol.ServerFactory() factory.protocol = wire.Echo t = internet.UNIXServer('echo.skt', factory) t.startService() self.failIfIdentical(t._port, None) t1 = copy.copy(t) self.assertIsNone(t1._port) t.stopService() self.assertIsNone(t._port) self.assertFalse(t.running) factory = protocol.ClientFactory() factory.protocol = wire.Echo t = internet.UNIXClient('echo.skt', factory) t.startService() self.failIfIdentical(t._connection, None) t1 = copy.copy(t) self.assertIsNone(t1._connection) t.stopService() self.assertIsNone(t._connection) self.assertFalse(t.running) def testStoppingServer(self): factory = protocol.ServerFactory() factory.protocol = wire.Echo t = internet.UNIXServer('echo.skt', factory) t.startService() t.stopService() self.assertFalse(t.running) factory = protocol.ClientFactory() d = defer.Deferred() factory.clientConnectionFailed = lambda *args: d.callback(None) reactor.connectUNIX('echo.skt', factory) return d if not interfaces.IReactorUNIX(reactor, None): _skipMsg = "This reactor does not support UNIX domain sockets" testUNIX.skip = _skipMsg testVolatile.skip = _skipMsg testStoppingServer.skip = _skipMsg def testPickledTimer(self): target = TimerTarget() t0 = internet.TimerService(1, target.append, "hello") t0.startService() s = pickle.dumps(t0) t0.stopService() t = pickle.loads(s) self.assertFalse(t.running) def testBrokenTimer(self): d = defer.Deferred() t = internet.TimerService(1, lambda: 1 // 0) oldFailed = t._failed def _failed(why): oldFailed(why) d.callback(None) t._failed = _failed t.startService() d.addCallback(lambda x : t.stopService) d.addCallback(lambda x : self.assertEqual( [ZeroDivisionError], [o.value.__class__ for o in self.flushLoggedErrors(ZeroDivisionError)])) return d def test_everythingThere(self): """ L{twisted.application.internet} dynamically defines a set of L{service.Service} subclasses that in general have corresponding reactor.listenXXX or reactor.connectXXX calls. """ trans = 'TCP UNIX SSL UDP UNIXDatagram Multicast'.split() for tran in trans[:]: if not getattr(interfaces, "IReactor" + tran)(reactor, None): trans.remove(tran) for tran in trans: for side in 'Server Client'.split(): if tran == "Multicast" and side == "Client": continue if tran == "UDP" and side == "Client": continue self.assertTrue(hasattr(internet, tran + side)) method = getattr(internet, tran + side).method prefix = {'Server': 'listen', 'Client': 'connect'}[side] self.assertTrue(hasattr(reactor, prefix + method) or (prefix == "connect" and method == "UDP")) o = getattr(internet, tran + side)() self.assertEqual(service.IService(o), o) def test_importAll(self): """ L{twisted.application.internet} dynamically defines L{service.Service} subclasses. This test ensures that the subclasses exposed by C{__all__} are valid attributes of the module. """ for cls in internet.__all__: self.assertTrue( hasattr(internet, cls), '%s not importable from twisted.application.internet' % (cls,)) def test_reactorParametrizationInServer(self): """ L{internet._AbstractServer} supports a C{reactor} keyword argument that can be used to parametrize the reactor used to listen for connections. """ reactor = MemoryReactor() factory = object() t = internet.TCPServer(1234, factory, reactor=reactor) t.startService() self.assertEqual(reactor.tcpServers.pop()[:2], (1234, factory)) def test_reactorParametrizationInClient(self): """ L{internet._AbstractClient} supports a C{reactor} keyword arguments that can be used to parametrize the reactor used to create new client connections. """ reactor = MemoryReactor() factory = protocol.ClientFactory() t = internet.TCPClient('127.0.0.1', 1234, factory, reactor=reactor) t.startService() self.assertEqual( reactor.tcpClients.pop()[:3], ('127.0.0.1', 1234, factory)) def test_reactorParametrizationInServerMultipleStart(self): """ Like L{test_reactorParametrizationInServer}, but stop and restart the service and check that the given reactor is still used. """ reactor = MemoryReactor() factory = protocol.Factory() t = internet.TCPServer(1234, factory, reactor=reactor) t.startService() self.assertEqual(reactor.tcpServers.pop()[:2], (1234, factory)) t.stopService() t.startService() self.assertEqual(reactor.tcpServers.pop()[:2], (1234, factory)) def test_reactorParametrizationInClientMultipleStart(self): """ Like L{test_reactorParametrizationInClient}, but stop and restart the service and check that the given reactor is still used. """ reactor = MemoryReactor() factory = protocol.ClientFactory() t = internet.TCPClient('127.0.0.1', 1234, factory, reactor=reactor) t.startService() self.assertEqual( reactor.tcpClients.pop()[:3], ('127.0.0.1', 1234, factory)) t.stopService() t.startService() self.assertEqual( reactor.tcpClients.pop()[:3], ('127.0.0.1', 1234, factory)) class TimerBasicTests(unittest.TestCase): def testTimerRuns(self): d = defer.Deferred() self.t = internet.TimerService(1, d.callback, 'hello') self.t.startService() d.addCallback(self.assertEqual, 'hello') d.addCallback(lambda x : self.t.stopService()) d.addCallback(lambda x : self.assertFalse(self.t.running)) return d def tearDown(self): return self.t.stopService() def testTimerRestart(self): # restart the same TimerService d1 = defer.Deferred() d2 = defer.Deferred() work = [(d2, "bar"), (d1, "foo")] def trigger(): d, arg = work.pop() d.callback(arg) self.t = internet.TimerService(1, trigger) self.t.startService() def onFirstResult(result): self.assertEqual(result, 'foo') return self.t.stopService() def onFirstStop(ignored): self.assertFalse(self.t.running) self.t.startService() return d2 def onSecondResult(result): self.assertEqual(result, 'bar') self.t.stopService() d1.addCallback(onFirstResult) d1.addCallback(onFirstStop) d1.addCallback(onSecondResult) return d1 def testTimerLoops(self): l = [] def trigger(data, number, d): l.append(data) if len(l) == number: d.callback(l) d = defer.Deferred() self.t = internet.TimerService(0.01, trigger, "hello", 10, d) self.t.startService() d.addCallback(self.assertEqual, ['hello'] * 10) d.addCallback(lambda x : self.t.stopService()) return d class FakeReactor(reactors.Reactor): """ A fake reactor with a hooked install method. """ def __init__(self, install, *args, **kwargs): """ @param install: any callable that will be used as install method. @type install: C{callable} """ reactors.Reactor.__init__(self, *args, **kwargs) self.install = install class PluggableReactorTests(TwistedModulesMixin, unittest.TestCase): """ Tests for the reactor discovery/inspection APIs. """ def setUp(self): """ Override the L{reactors.getPlugins} function, normally bound to L{twisted.plugin.getPlugins}, in order to control which L{IReactorInstaller} plugins are seen as available. C{self.pluginResults} can be customized and will be used as the result of calls to C{reactors.getPlugins}. """ self.pluginCalls = [] self.pluginResults = [] self.originalFunction = reactors.getPlugins reactors.getPlugins = self._getPlugins def tearDown(self): """ Restore the original L{reactors.getPlugins}. """ reactors.getPlugins = self.originalFunction def _getPlugins(self, interface, package=None): """ Stand-in for the real getPlugins method which records its arguments and returns a fixed result. """ self.pluginCalls.append((interface, package)) return list(self.pluginResults) def test_getPluginReactorTypes(self): """ Test that reactor plugins are returned from L{getReactorTypes} """ name = 'fakereactortest' package = __name__ + '.fakereactor' description = 'description' self.pluginResults = [reactors.Reactor(name, package, description)] reactorTypes = reactors.getReactorTypes() self.assertEqual( self.pluginCalls, [(reactors.IReactorInstaller, None)]) for r in reactorTypes: if r.shortName == name: self.assertEqual(r.description, description) break else: self.fail("Reactor plugin not present in getReactorTypes() result") def test_reactorInstallation(self): """ Test that L{reactors.Reactor.install} loads the correct module and calls its install attribute. """ installed = [] def install(): installed.append(True) fakeReactor = FakeReactor(install, 'fakereactortest', __name__, 'described') modules = {'fakereactortest': fakeReactor} self.replaceSysModules(modules) installer = reactors.Reactor('fakereactor', 'fakereactortest', 'described') installer.install() self.assertEqual(installed, [True]) def test_installReactor(self): """ Test that the L{reactors.installReactor} function correctly installs the specified reactor. """ installed = [] def install(): installed.append(True) name = 'fakereactortest' package = __name__ description = 'description' self.pluginResults = [FakeReactor(install, name, package, description)] reactors.installReactor(name) self.assertEqual(installed, [True]) def test_installReactorReturnsReactor(self): """ Test that the L{reactors.installReactor} function correctly returns the installed reactor. """ reactor = object() def install(): from twisted import internet self.patch(internet, 'reactor', reactor) name = 'fakereactortest' package = __name__ description = 'description' self.pluginResults = [FakeReactor(install, name, package, description)] installed = reactors.installReactor(name) self.assertIs(installed, reactor) def test_installReactorMultiplePlugins(self): """ Test that the L{reactors.installReactor} function correctly installs the specified reactor when there are multiple reactor plugins. """ installed = [] def install(): installed.append(True) name = 'fakereactortest' package = __name__ description = 'description' fakeReactor = FakeReactor(install, name, package, description) otherReactor = FakeReactor(lambda: None, "otherreactor", package, description) self.pluginResults = [otherReactor, fakeReactor] reactors.installReactor(name) self.assertEqual(installed, [True]) def test_installNonExistentReactor(self): """ Test that L{reactors.installReactor} raises L{reactors.NoSuchReactor} when asked to install a reactor which it cannot find. """ self.pluginResults = [] self.assertRaises( reactors.NoSuchReactor, reactors.installReactor, 'somereactor') def test_installNotAvailableReactor(self): """ Test that L{reactors.installReactor} raises an exception when asked to install a reactor which doesn't work in this environment. """ def install(): raise ImportError("Missing foo bar") name = 'fakereactortest' package = __name__ description = 'description' self.pluginResults = [FakeReactor(install, name, package, description)] self.assertRaises(ImportError, reactors.installReactor, name) def test_reactorSelectionMixin(self): """ Test that the reactor selected is installed as soon as possible, ie when the option is parsed. """ executed = [] INSTALL_EVENT = 'reactor installed' SUBCOMMAND_EVENT = 'subcommands loaded' class ReactorSelectionOptions(usage.Options, app.ReactorSelectionMixin): def subCommands(self): executed.append(SUBCOMMAND_EVENT) return [('subcommand', None, lambda: self, 'test subcommand')] subCommands = property(subCommands) def install(): executed.append(INSTALL_EVENT) self.pluginResults = [ FakeReactor(install, 'fakereactortest', __name__, 'described') ] options = ReactorSelectionOptions() options.parseOptions(['--reactor', 'fakereactortest', 'subcommand']) self.assertEqual(executed[0], INSTALL_EVENT) self.assertEqual(executed.count(INSTALL_EVENT), 1) self.assertEqual(options["reactor"], "fakereactortest") def test_reactorSelectionMixinNonExistent(self): """ Test that the usage mixin exits when trying to use a non existent reactor (the name not matching to any reactor), giving an error message. """ class ReactorSelectionOptions(usage.Options, app.ReactorSelectionMixin): pass self.pluginResults = [] options = ReactorSelectionOptions() options.messageOutput = NativeStringIO() e = self.assertRaises(usage.UsageError, options.parseOptions, ['--reactor', 'fakereactortest', 'subcommand']) self.assertIn("fakereactortest", e.args[0]) self.assertIn("help-reactors", e.args[0]) def test_reactorSelectionMixinNotAvailable(self): """ Test that the usage mixin exits when trying to use a reactor not available (the reactor raises an error at installation), giving an error message. """ class ReactorSelectionOptions(usage.Options, app.ReactorSelectionMixin): pass message = "Missing foo bar" def install(): raise ImportError(message) name = 'fakereactortest' package = __name__ description = 'description' self.pluginResults = [FakeReactor(install, name, package, description)] options = ReactorSelectionOptions() options.messageOutput = NativeStringIO() e = self.assertRaises(usage.UsageError, options.parseOptions, ['--reactor', 'fakereactortest', 'subcommand']) self.assertIn(message, e.args[0]) self.assertIn("help-reactors", e.args[0]) class HelpReactorsTests(unittest.TestCase): """ --help-reactors lists the available reactors """ def setUp(self): """ Get the text from --help-reactors """ self.options = app.ReactorSelectionMixin() self.options.messageOutput = NativeStringIO() self.assertRaises(SystemExit, self.options.opt_help_reactors) self.message = self.options.messageOutput.getvalue() def test_lacksAsyncIO(self): """ --help-reactors should NOT display the asyncio reactor on Python < 3.4 """ self.assertIn(twisted_reactors.asyncio.description, self.message) self.assertIn("!" + twisted_reactors.asyncio.shortName, self.message) if asyncio: test_lacksAsyncIO.skip = "Not applicable, asyncio is available" def test_hasAsyncIO(self): """ --help-reactors should display the asyncio reactor on Python >= 3.4 """ self.assertIn(twisted_reactors.asyncio.description, self.message) self.assertNotIn( "!" + twisted_reactors.asyncio.shortName, self.message) if not asyncio: test_hasAsyncIO.skip = "asyncio library not available" def test_iocpWin32(self): """ --help-reactors should display the iocp reactor on Windows """ self.assertIn(twisted_reactors.iocp.description, self.message) self.assertNotIn("!" + twisted_reactors.iocp.shortName, self.message) if platformType != "win32": test_iocpWin32.skip = "Test only applicable on Windows" def test_iocpNotWin32(self): """ --help-reactors should NOT display the iocp reactor on Windows """ self.assertIn(twisted_reactors.iocp.description, self.message) self.assertIn("!" + twisted_reactors.iocp.shortName, self.message) if platformType == "win32": test_iocpNotWin32.skip = "Test only applicable on Windows" def test_onlySupportedReactors(self): """ --help-reactors with only supported reactors """ def getReactorTypes(): yield twisted_reactors.default options = app.ReactorSelectionMixin() options._getReactorTypes = getReactorTypes options.messageOutput = NativeStringIO() self.assertRaises(SystemExit, options.opt_help_reactors) message = options.messageOutput.getvalue() self.assertNotIn("reactors not available", message)