Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_internet.py 45KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for lots of functionality provided by L{twisted.internet}.
  5. """
  6. import os
  7. import sys
  8. import time
  9. from unittest import skipIf
  10. from twisted.internet import abstract, base, defer, error, interfaces, protocol, reactor
  11. from twisted.internet.defer import Deferred, passthru
  12. from twisted.internet.tcp import Connector
  13. from twisted.python import util
  14. from twisted.trial.unittest import TestCase
  15. try:
  16. from twisted.internet import ssl as _ssl
  17. except ImportError:
  18. ssl = None
  19. else:
  20. ssl = _ssl
  21. if ssl and not ssl.supported:
  22. ssl = None
  23. class ThreePhaseEventTests(TestCase):
  24. """
  25. Tests for the private implementation helpers for system event triggers.
  26. """
  27. def setUp(self):
  28. """
  29. Create a trigger, an argument, and an event to be used by tests.
  30. """
  31. self.trigger = lambda x: None
  32. self.arg = object()
  33. self.event = base._ThreePhaseEvent()
  34. def test_addInvalidPhase(self):
  35. """
  36. L{_ThreePhaseEvent.addTrigger} should raise L{KeyError} when called
  37. with an invalid phase.
  38. """
  39. self.assertRaises(
  40. KeyError, self.event.addTrigger, "xxx", self.trigger, self.arg
  41. )
  42. def test_addBeforeTrigger(self):
  43. """
  44. L{_ThreePhaseEvent.addTrigger} should accept C{'before'} as a phase, a
  45. callable, and some arguments and add the callable with the arguments to
  46. the before list.
  47. """
  48. self.event.addTrigger("before", self.trigger, self.arg)
  49. self.assertEqual(self.event.before, [(self.trigger, (self.arg,), {})])
  50. def test_addDuringTrigger(self):
  51. """
  52. L{_ThreePhaseEvent.addTrigger} should accept C{'during'} as a phase, a
  53. callable, and some arguments and add the callable with the arguments to
  54. the during list.
  55. """
  56. self.event.addTrigger("during", self.trigger, self.arg)
  57. self.assertEqual(self.event.during, [(self.trigger, (self.arg,), {})])
  58. def test_addAfterTrigger(self):
  59. """
  60. L{_ThreePhaseEvent.addTrigger} should accept C{'after'} as a phase, a
  61. callable, and some arguments and add the callable with the arguments to
  62. the after list.
  63. """
  64. self.event.addTrigger("after", self.trigger, self.arg)
  65. self.assertEqual(self.event.after, [(self.trigger, (self.arg,), {})])
  66. def test_removeTrigger(self):
  67. """
  68. L{_ThreePhaseEvent.removeTrigger} should accept an opaque object
  69. previously returned by L{_ThreePhaseEvent.addTrigger} and remove the
  70. associated trigger.
  71. """
  72. handle = self.event.addTrigger("before", self.trigger, self.arg)
  73. self.event.removeTrigger(handle)
  74. self.assertEqual(self.event.before, [])
  75. def test_removeNonexistentTrigger(self):
  76. """
  77. L{_ThreePhaseEvent.removeTrigger} should raise L{ValueError} when given
  78. an object not previously returned by L{_ThreePhaseEvent.addTrigger}.
  79. """
  80. self.assertRaises(ValueError, self.event.removeTrigger, object())
  81. def test_removeRemovedTrigger(self):
  82. """
  83. L{_ThreePhaseEvent.removeTrigger} should raise L{ValueError} the second
  84. time it is called with an object returned by
  85. L{_ThreePhaseEvent.addTrigger}.
  86. """
  87. handle = self.event.addTrigger("before", self.trigger, self.arg)
  88. self.event.removeTrigger(handle)
  89. self.assertRaises(ValueError, self.event.removeTrigger, handle)
  90. def test_removeAlmostValidTrigger(self):
  91. """
  92. L{_ThreePhaseEvent.removeTrigger} should raise L{ValueError} if it is
  93. given a trigger handle which resembles a valid trigger handle aside
  94. from its phase being incorrect.
  95. """
  96. self.assertRaises(
  97. KeyError, self.event.removeTrigger, ("xxx", self.trigger, (self.arg,), {})
  98. )
  99. def test_fireEvent(self):
  100. """
  101. L{_ThreePhaseEvent.fireEvent} should call I{before}, I{during}, and
  102. I{after} phase triggers in that order.
  103. """
  104. events = []
  105. self.event.addTrigger("after", events.append, ("first", "after"))
  106. self.event.addTrigger("during", events.append, ("first", "during"))
  107. self.event.addTrigger("before", events.append, ("first", "before"))
  108. self.event.addTrigger("before", events.append, ("second", "before"))
  109. self.event.addTrigger("during", events.append, ("second", "during"))
  110. self.event.addTrigger("after", events.append, ("second", "after"))
  111. self.assertEqual(events, [])
  112. self.event.fireEvent()
  113. self.assertEqual(
  114. events,
  115. [
  116. ("first", "before"),
  117. ("second", "before"),
  118. ("first", "during"),
  119. ("second", "during"),
  120. ("first", "after"),
  121. ("second", "after"),
  122. ],
  123. )
  124. def test_asynchronousBefore(self):
  125. """
  126. L{_ThreePhaseEvent.fireEvent} should wait for any L{Deferred} returned
  127. by a I{before} phase trigger before proceeding to I{during} events.
  128. """
  129. events = []
  130. beforeResult = Deferred()
  131. self.event.addTrigger("before", lambda: beforeResult)
  132. self.event.addTrigger("during", events.append, "during")
  133. self.event.addTrigger("after", events.append, "after")
  134. self.assertEqual(events, [])
  135. self.event.fireEvent()
  136. self.assertEqual(events, [])
  137. beforeResult.callback(None)
  138. self.assertEqual(events, ["during", "after"])
  139. def test_beforeTriggerException(self):
  140. """
  141. If a before-phase trigger raises a synchronous exception, it should be
  142. logged and the remaining triggers should be run.
  143. """
  144. events = []
  145. class DummyException(Exception):
  146. pass
  147. def raisingTrigger():
  148. raise DummyException()
  149. self.event.addTrigger("before", raisingTrigger)
  150. self.event.addTrigger("before", events.append, "before")
  151. self.event.addTrigger("during", events.append, "during")
  152. self.event.fireEvent()
  153. self.assertEqual(events, ["before", "during"])
  154. errors = self.flushLoggedErrors(DummyException)
  155. self.assertEqual(len(errors), 1)
  156. def test_duringTriggerException(self):
  157. """
  158. If a during-phase trigger raises a synchronous exception, it should be
  159. logged and the remaining triggers should be run.
  160. """
  161. events = []
  162. class DummyException(Exception):
  163. pass
  164. def raisingTrigger():
  165. raise DummyException()
  166. self.event.addTrigger("during", raisingTrigger)
  167. self.event.addTrigger("during", events.append, "during")
  168. self.event.addTrigger("after", events.append, "after")
  169. self.event.fireEvent()
  170. self.assertEqual(events, ["during", "after"])
  171. errors = self.flushLoggedErrors(DummyException)
  172. self.assertEqual(len(errors), 1)
  173. def test_synchronousRemoveAlreadyExecutedBefore(self):
  174. """
  175. If a before-phase trigger tries to remove another before-phase trigger
  176. which has already run, a warning should be emitted.
  177. """
  178. events = []
  179. def removeTrigger():
  180. self.event.removeTrigger(beforeHandle)
  181. beforeHandle = self.event.addTrigger(
  182. "before", events.append, ("first", "before")
  183. )
  184. self.event.addTrigger("before", removeTrigger)
  185. self.event.addTrigger("before", events.append, ("second", "before"))
  186. self.assertWarns(
  187. DeprecationWarning,
  188. "Removing already-fired system event triggers will raise an "
  189. "exception in a future version of Twisted.",
  190. __file__,
  191. self.event.fireEvent,
  192. )
  193. self.assertEqual(events, [("first", "before"), ("second", "before")])
  194. def test_synchronousRemovePendingBefore(self):
  195. """
  196. If a before-phase trigger removes another before-phase trigger which
  197. has not yet run, the removed trigger should not be run.
  198. """
  199. events = []
  200. self.event.addTrigger("before", lambda: self.event.removeTrigger(beforeHandle))
  201. beforeHandle = self.event.addTrigger(
  202. "before", events.append, ("first", "before")
  203. )
  204. self.event.addTrigger("before", events.append, ("second", "before"))
  205. self.event.fireEvent()
  206. self.assertEqual(events, [("second", "before")])
  207. def test_synchronousBeforeRemovesDuring(self):
  208. """
  209. If a before-phase trigger removes a during-phase trigger, the
  210. during-phase trigger should not be run.
  211. """
  212. events = []
  213. self.event.addTrigger("before", lambda: self.event.removeTrigger(duringHandle))
  214. duringHandle = self.event.addTrigger("during", events.append, "during")
  215. self.event.addTrigger("after", events.append, "after")
  216. self.event.fireEvent()
  217. self.assertEqual(events, ["after"])
  218. def test_asynchronousBeforeRemovesDuring(self):
  219. """
  220. If a before-phase trigger returns a L{Deferred} and later removes a
  221. during-phase trigger before the L{Deferred} fires, the during-phase
  222. trigger should not be run.
  223. """
  224. events = []
  225. beforeResult = Deferred()
  226. self.event.addTrigger("before", lambda: beforeResult)
  227. duringHandle = self.event.addTrigger("during", events.append, "during")
  228. self.event.addTrigger("after", events.append, "after")
  229. self.event.fireEvent()
  230. self.event.removeTrigger(duringHandle)
  231. beforeResult.callback(None)
  232. self.assertEqual(events, ["after"])
  233. def test_synchronousBeforeRemovesConspicuouslySimilarDuring(self):
  234. """
  235. If a before-phase trigger removes a during-phase trigger which is
  236. identical to an already-executed before-phase trigger aside from their
  237. phases, no warning should be emitted and the during-phase trigger
  238. should not be run.
  239. """
  240. events = []
  241. def trigger():
  242. events.append("trigger")
  243. self.event.addTrigger("before", trigger)
  244. self.event.addTrigger("before", lambda: self.event.removeTrigger(duringTrigger))
  245. duringTrigger = self.event.addTrigger("during", trigger)
  246. self.event.fireEvent()
  247. self.assertEqual(events, ["trigger"])
  248. def test_synchronousRemovePendingDuring(self):
  249. """
  250. If a during-phase trigger removes another during-phase trigger which
  251. has not yet run, the removed trigger should not be run.
  252. """
  253. events = []
  254. self.event.addTrigger("during", lambda: self.event.removeTrigger(duringHandle))
  255. duringHandle = self.event.addTrigger(
  256. "during", events.append, ("first", "during")
  257. )
  258. self.event.addTrigger("during", events.append, ("second", "during"))
  259. self.event.fireEvent()
  260. self.assertEqual(events, [("second", "during")])
  261. def test_triggersRunOnce(self):
  262. """
  263. A trigger should only be called on the first call to
  264. L{_ThreePhaseEvent.fireEvent}.
  265. """
  266. events = []
  267. self.event.addTrigger("before", events.append, "before")
  268. self.event.addTrigger("during", events.append, "during")
  269. self.event.addTrigger("after", events.append, "after")
  270. self.event.fireEvent()
  271. self.event.fireEvent()
  272. self.assertEqual(events, ["before", "during", "after"])
  273. def test_finishedBeforeTriggersCleared(self):
  274. """
  275. The temporary list L{_ThreePhaseEvent.finishedBefore} should be emptied
  276. and the state reset to C{'BASE'} before the first during-phase trigger
  277. executes.
  278. """
  279. events = []
  280. def duringTrigger():
  281. events.append("during")
  282. self.assertEqual(self.event.finishedBefore, [])
  283. self.assertEqual(self.event.state, "BASE")
  284. self.event.addTrigger("before", events.append, "before")
  285. self.event.addTrigger("during", duringTrigger)
  286. self.event.fireEvent()
  287. self.assertEqual(events, ["before", "during"])
  288. class SystemEventTests(TestCase):
  289. """
  290. Tests for the reactor's implementation of the C{fireSystemEvent},
  291. C{addSystemEventTrigger}, and C{removeSystemEventTrigger} methods of the
  292. L{IReactorCore} interface.
  293. @ivar triggers: A list of the handles to triggers which have been added to
  294. the reactor.
  295. """
  296. def setUp(self):
  297. """
  298. Create an empty list in which to store trigger handles.
  299. """
  300. self.triggers = []
  301. def tearDown(self):
  302. """
  303. Remove all remaining triggers from the reactor.
  304. """
  305. while self.triggers:
  306. trigger = self.triggers.pop()
  307. try:
  308. reactor.removeSystemEventTrigger(trigger)
  309. except (ValueError, KeyError):
  310. pass
  311. def addTrigger(self, event, phase, func):
  312. """
  313. Add a trigger to the reactor and remember it in C{self.triggers}.
  314. """
  315. t = reactor.addSystemEventTrigger(event, phase, func)
  316. self.triggers.append(t)
  317. return t
  318. def removeTrigger(self, trigger):
  319. """
  320. Remove a trigger by its handle from the reactor and from
  321. C{self.triggers}.
  322. """
  323. reactor.removeSystemEventTrigger(trigger)
  324. self.triggers.remove(trigger)
  325. def _addSystemEventTriggerTest(self, phase):
  326. eventType = "test"
  327. events = []
  328. def trigger():
  329. events.append(None)
  330. self.addTrigger(phase, eventType, trigger)
  331. self.assertEqual(events, [])
  332. reactor.fireSystemEvent(eventType)
  333. self.assertEqual(events, [None])
  334. def test_beforePhase(self):
  335. """
  336. L{IReactorCore.addSystemEventTrigger} should accept the C{'before'}
  337. phase and not call the given object until the right event is fired.
  338. """
  339. self._addSystemEventTriggerTest("before")
  340. def test_duringPhase(self):
  341. """
  342. L{IReactorCore.addSystemEventTrigger} should accept the C{'during'}
  343. phase and not call the given object until the right event is fired.
  344. """
  345. self._addSystemEventTriggerTest("during")
  346. def test_afterPhase(self):
  347. """
  348. L{IReactorCore.addSystemEventTrigger} should accept the C{'after'}
  349. phase and not call the given object until the right event is fired.
  350. """
  351. self._addSystemEventTriggerTest("after")
  352. def test_unknownPhase(self):
  353. """
  354. L{IReactorCore.addSystemEventTrigger} should reject phases other than
  355. C{'before'}, C{'during'}, or C{'after'}.
  356. """
  357. eventType = "test"
  358. self.assertRaises(KeyError, self.addTrigger, "xxx", eventType, lambda: None)
  359. def test_beforePreceedsDuring(self):
  360. """
  361. L{IReactorCore.addSystemEventTrigger} should call triggers added to the
  362. C{'before'} phase before it calls triggers added to the C{'during'}
  363. phase.
  364. """
  365. eventType = "test"
  366. events = []
  367. def beforeTrigger():
  368. events.append("before")
  369. def duringTrigger():
  370. events.append("during")
  371. self.addTrigger("before", eventType, beforeTrigger)
  372. self.addTrigger("during", eventType, duringTrigger)
  373. self.assertEqual(events, [])
  374. reactor.fireSystemEvent(eventType)
  375. self.assertEqual(events, ["before", "during"])
  376. def test_duringPreceedsAfter(self):
  377. """
  378. L{IReactorCore.addSystemEventTrigger} should call triggers added to the
  379. C{'during'} phase before it calls triggers added to the C{'after'}
  380. phase.
  381. """
  382. eventType = "test"
  383. events = []
  384. def duringTrigger():
  385. events.append("during")
  386. def afterTrigger():
  387. events.append("after")
  388. self.addTrigger("during", eventType, duringTrigger)
  389. self.addTrigger("after", eventType, afterTrigger)
  390. self.assertEqual(events, [])
  391. reactor.fireSystemEvent(eventType)
  392. self.assertEqual(events, ["during", "after"])
  393. def test_beforeReturnsDeferred(self):
  394. """
  395. If a trigger added to the C{'before'} phase of an event returns a
  396. L{Deferred}, the C{'during'} phase should be delayed until it is called
  397. back.
  398. """
  399. triggerDeferred = Deferred()
  400. eventType = "test"
  401. events = []
  402. def beforeTrigger():
  403. return triggerDeferred
  404. def duringTrigger():
  405. events.append("during")
  406. self.addTrigger("before", eventType, beforeTrigger)
  407. self.addTrigger("during", eventType, duringTrigger)
  408. self.assertEqual(events, [])
  409. reactor.fireSystemEvent(eventType)
  410. self.assertEqual(events, [])
  411. triggerDeferred.callback(None)
  412. self.assertEqual(events, ["during"])
  413. def test_multipleBeforeReturnDeferred(self):
  414. """
  415. If more than one trigger added to the C{'before'} phase of an event
  416. return L{Deferred}s, the C{'during'} phase should be delayed until they
  417. are all called back.
  418. """
  419. firstDeferred = Deferred()
  420. secondDeferred = Deferred()
  421. eventType = "test"
  422. events = []
  423. def firstBeforeTrigger():
  424. return firstDeferred
  425. def secondBeforeTrigger():
  426. return secondDeferred
  427. def duringTrigger():
  428. events.append("during")
  429. self.addTrigger("before", eventType, firstBeforeTrigger)
  430. self.addTrigger("before", eventType, secondBeforeTrigger)
  431. self.addTrigger("during", eventType, duringTrigger)
  432. self.assertEqual(events, [])
  433. reactor.fireSystemEvent(eventType)
  434. self.assertEqual(events, [])
  435. firstDeferred.callback(None)
  436. self.assertEqual(events, [])
  437. secondDeferred.callback(None)
  438. self.assertEqual(events, ["during"])
  439. def test_subsequentBeforeTriggerFiresPriorBeforeDeferred(self):
  440. """
  441. If a trigger added to the C{'before'} phase of an event calls back a
  442. L{Deferred} returned by an earlier trigger in the C{'before'} phase of
  443. the same event, the remaining C{'before'} triggers for that event
  444. should be run and any further L{Deferred}s waited on before proceeding
  445. to the C{'during'} events.
  446. """
  447. eventType = "test"
  448. events = []
  449. firstDeferred = Deferred()
  450. secondDeferred = Deferred()
  451. def firstBeforeTrigger():
  452. return firstDeferred
  453. def secondBeforeTrigger():
  454. firstDeferred.callback(None)
  455. def thirdBeforeTrigger():
  456. events.append("before")
  457. return secondDeferred
  458. def duringTrigger():
  459. events.append("during")
  460. self.addTrigger("before", eventType, firstBeforeTrigger)
  461. self.addTrigger("before", eventType, secondBeforeTrigger)
  462. self.addTrigger("before", eventType, thirdBeforeTrigger)
  463. self.addTrigger("during", eventType, duringTrigger)
  464. self.assertEqual(events, [])
  465. reactor.fireSystemEvent(eventType)
  466. self.assertEqual(events, ["before"])
  467. secondDeferred.callback(None)
  468. self.assertEqual(events, ["before", "during"])
  469. def test_removeSystemEventTrigger(self):
  470. """
  471. A trigger removed with L{IReactorCore.removeSystemEventTrigger} should
  472. not be called when the event fires.
  473. """
  474. eventType = "test"
  475. events = []
  476. def firstBeforeTrigger():
  477. events.append("first")
  478. def secondBeforeTrigger():
  479. events.append("second")
  480. self.addTrigger("before", eventType, firstBeforeTrigger)
  481. self.removeTrigger(self.addTrigger("before", eventType, secondBeforeTrigger))
  482. self.assertEqual(events, [])
  483. reactor.fireSystemEvent(eventType)
  484. self.assertEqual(events, ["first"])
  485. def test_removeNonExistentSystemEventTrigger(self):
  486. """
  487. Passing an object to L{IReactorCore.removeSystemEventTrigger} which was
  488. not returned by a previous call to
  489. L{IReactorCore.addSystemEventTrigger} or which has already been passed
  490. to C{removeSystemEventTrigger} should result in L{TypeError},
  491. L{KeyError}, or L{ValueError} being raised.
  492. """
  493. b = self.addTrigger("during", "test", lambda: None)
  494. self.removeTrigger(b)
  495. self.assertRaises(TypeError, reactor.removeSystemEventTrigger, None)
  496. self.assertRaises(ValueError, reactor.removeSystemEventTrigger, b)
  497. self.assertRaises(
  498. KeyError, reactor.removeSystemEventTrigger, (b[0], ("xxx",) + b[1][1:])
  499. )
  500. def test_interactionBetweenDifferentEvents(self):
  501. """
  502. L{IReactorCore.fireSystemEvent} should behave the same way for a
  503. particular system event regardless of whether Deferreds are being
  504. waited on for a different system event.
  505. """
  506. events = []
  507. firstEvent = "first-event"
  508. firstDeferred = Deferred()
  509. def beforeFirstEvent():
  510. events.append(("before", "first"))
  511. return firstDeferred
  512. def afterFirstEvent():
  513. events.append(("after", "first"))
  514. secondEvent = "second-event"
  515. secondDeferred = Deferred()
  516. def beforeSecondEvent():
  517. events.append(("before", "second"))
  518. return secondDeferred
  519. def afterSecondEvent():
  520. events.append(("after", "second"))
  521. self.addTrigger("before", firstEvent, beforeFirstEvent)
  522. self.addTrigger("after", firstEvent, afterFirstEvent)
  523. self.addTrigger("before", secondEvent, beforeSecondEvent)
  524. self.addTrigger("after", secondEvent, afterSecondEvent)
  525. self.assertEqual(events, [])
  526. # After this, firstEvent should be stuck before 'during' waiting for
  527. # firstDeferred.
  528. reactor.fireSystemEvent(firstEvent)
  529. self.assertEqual(events, [("before", "first")])
  530. # After this, secondEvent should be stuck before 'during' waiting for
  531. # secondDeferred.
  532. reactor.fireSystemEvent(secondEvent)
  533. self.assertEqual(events, [("before", "first"), ("before", "second")])
  534. # After this, firstEvent should have finished completely, but
  535. # secondEvent should be at the same place.
  536. firstDeferred.callback(None)
  537. self.assertEqual(
  538. events, [("before", "first"), ("before", "second"), ("after", "first")]
  539. )
  540. # After this, secondEvent should have finished completely.
  541. secondDeferred.callback(None)
  542. self.assertEqual(
  543. events,
  544. [
  545. ("before", "first"),
  546. ("before", "second"),
  547. ("after", "first"),
  548. ("after", "second"),
  549. ],
  550. )
  551. class TimeTests(TestCase):
  552. """
  553. Tests for the IReactorTime part of the reactor.
  554. """
  555. def test_seconds(self):
  556. """
  557. L{twisted.internet.reactor.seconds} should return something
  558. like a number.
  559. 1. This test specifically does not assert any relation to the
  560. "system time" as returned by L{time.time} or
  561. L{twisted.python.runtime.seconds}, because at some point we
  562. may find a better option for scheduling calls than
  563. wallclock-time.
  564. 2. This test *also* does not assert anything about the type of
  565. the result, because operations may not return ints or
  566. floats: For example, datetime-datetime == timedelta(0).
  567. """
  568. now = reactor.seconds()
  569. self.assertEqual(now - now + now, now)
  570. def test_callLaterUsesReactorSecondsInDelayedCall(self):
  571. """
  572. L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
  573. should use the reactor's seconds factory
  574. to produce the time at which the DelayedCall will be called.
  575. """
  576. oseconds = reactor.seconds
  577. reactor.seconds = lambda: 100
  578. try:
  579. call = reactor.callLater(5, lambda: None)
  580. self.assertEqual(call.getTime(), 105)
  581. finally:
  582. reactor.seconds = oseconds
  583. call.cancel()
  584. def test_callLaterUsesReactorSecondsAsDelayedCallSecondsFactory(self):
  585. """
  586. L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
  587. should propagate its own seconds factory
  588. to the DelayedCall to use as its own seconds factory.
  589. """
  590. oseconds = reactor.seconds
  591. reactor.seconds = lambda: 100
  592. try:
  593. call = reactor.callLater(5, lambda: None)
  594. self.assertEqual(call.seconds(), 100)
  595. finally:
  596. reactor.seconds = oseconds
  597. call.cancel()
  598. def test_callLater(self):
  599. """
  600. Test that a DelayedCall really calls the function it is
  601. supposed to call.
  602. """
  603. d = Deferred()
  604. reactor.callLater(0, d.callback, None)
  605. d.addCallback(self.assertEqual, None)
  606. return d
  607. def test_callLaterReset(self):
  608. """
  609. A L{DelayedCall} that is reset will be scheduled at the new time.
  610. """
  611. call = reactor.callLater(2, passthru, passthru)
  612. self.addCleanup(call.cancel)
  613. origTime = call.time
  614. call.reset(1)
  615. self.assertNotEqual(call.time, origTime)
  616. def test_cancelDelayedCall(self):
  617. """
  618. Test that when a DelayedCall is cancelled it does not run.
  619. """
  620. called = []
  621. def function():
  622. called.append(None)
  623. call = reactor.callLater(0, function)
  624. call.cancel()
  625. # Schedule a call in two "iterations" to check to make sure that the
  626. # above call never ran.
  627. d = Deferred()
  628. def check():
  629. try:
  630. self.assertEqual(called, [])
  631. except BaseException:
  632. d.errback()
  633. else:
  634. d.callback(None)
  635. reactor.callLater(0, reactor.callLater, 0, check)
  636. return d
  637. def test_cancelCancelledDelayedCall(self):
  638. """
  639. Test that cancelling a DelayedCall which has already been cancelled
  640. raises the appropriate exception.
  641. """
  642. call = reactor.callLater(0, lambda: None)
  643. call.cancel()
  644. self.assertRaises(error.AlreadyCancelled, call.cancel)
  645. def test_cancelCalledDelayedCallSynchronous(self):
  646. """
  647. Test that cancelling a DelayedCall in the DelayedCall's function as
  648. that function is being invoked by the DelayedCall raises the
  649. appropriate exception.
  650. """
  651. d = Deferred()
  652. def later():
  653. try:
  654. self.assertRaises(error.AlreadyCalled, call.cancel)
  655. except BaseException:
  656. d.errback()
  657. else:
  658. d.callback(None)
  659. call = reactor.callLater(0, later)
  660. return d
  661. def test_cancelCalledDelayedCallAsynchronous(self):
  662. """
  663. Test that cancelling a DelayedCall after it has run its function
  664. raises the appropriate exception.
  665. """
  666. d = Deferred()
  667. def check():
  668. try:
  669. self.assertRaises(error.AlreadyCalled, call.cancel)
  670. except BaseException:
  671. d.errback()
  672. else:
  673. d.callback(None)
  674. def later():
  675. reactor.callLater(0, check)
  676. call = reactor.callLater(0, later)
  677. return d
  678. def testCallLaterTime(self):
  679. d = reactor.callLater(10, lambda: None)
  680. try:
  681. self.assertTrue(d.getTime() - (time.time() + 10) < 1)
  682. finally:
  683. d.cancel()
  684. def testDelayedCallStringification(self):
  685. # Mostly just make sure str() isn't going to raise anything for
  686. # DelayedCalls within reason.
  687. dc = reactor.callLater(0, lambda x, y: None, "x", y=10)
  688. str(dc)
  689. dc.reset(5)
  690. str(dc)
  691. dc.cancel()
  692. str(dc)
  693. dc = reactor.callLater(
  694. 0, lambda: None, x=[({"hello": "world"}, 10j), reactor], *range(10)
  695. )
  696. str(dc)
  697. dc.cancel()
  698. str(dc)
  699. def calledBack(ignored):
  700. str(dc)
  701. d = Deferred().addCallback(calledBack)
  702. dc = reactor.callLater(0, d.callback, None)
  703. str(dc)
  704. return d
  705. def testDelayedCallSecondsOverride(self):
  706. """
  707. Test that the C{seconds} argument to DelayedCall gets used instead of
  708. the default timing function, if it is not None.
  709. """
  710. def seconds():
  711. return 10
  712. dc = base.DelayedCall(
  713. 5, lambda: None, (), {}, lambda dc: None, lambda dc: None, seconds
  714. )
  715. self.assertEqual(dc.getTime(), 5)
  716. dc.reset(3)
  717. self.assertEqual(dc.getTime(), 13)
  718. class CallFromThreadStopsAndWakeUpTests(TestCase):
  719. @skipIf(
  720. not interfaces.IReactorThreads(reactor, None),
  721. "Nothing to wake up for without thread support",
  722. )
  723. def testWakeUp(self):
  724. # Make sure other threads can wake up the reactor
  725. d = Deferred()
  726. def wake():
  727. time.sleep(0.1)
  728. # callFromThread will call wakeUp for us
  729. reactor.callFromThread(d.callback, None)
  730. reactor.callInThread(wake)
  731. return d
  732. def _stopCallFromThreadCallback(self):
  733. self.stopped = True
  734. def _callFromThreadCallback(self, d):
  735. reactor.callFromThread(self._callFromThreadCallback2, d)
  736. reactor.callLater(0, self._stopCallFromThreadCallback)
  737. def _callFromThreadCallback2(self, d):
  738. try:
  739. self.assertTrue(self.stopped)
  740. except BaseException:
  741. # Send the error to the deferred
  742. d.errback()
  743. else:
  744. d.callback(None)
  745. def testCallFromThreadStops(self):
  746. """
  747. Ensure that callFromThread from inside a callFromThread
  748. callback doesn't sit in an infinite loop and lets other
  749. things happen too.
  750. """
  751. self.stopped = False
  752. d = defer.Deferred()
  753. reactor.callFromThread(self._callFromThreadCallback, d)
  754. return d
  755. class DelayedTests(TestCase):
  756. def setUp(self):
  757. self.finished = 0
  758. self.counter = 0
  759. self.timers = {}
  760. self.deferred = defer.Deferred()
  761. def tearDown(self):
  762. for t in self.timers.values():
  763. t.cancel()
  764. def checkTimers(self):
  765. l1 = self.timers.values()
  766. l2 = list(reactor.getDelayedCalls())
  767. # There should be at least the calls we put in. There may be other
  768. # calls that are none of our business and that we should ignore,
  769. # though.
  770. missing = []
  771. for dc in l1:
  772. if dc not in l2:
  773. missing.append(dc)
  774. if missing:
  775. self.finished = 1
  776. self.assertFalse(
  777. missing,
  778. "Should have been missing no calls, instead "
  779. + "was missing "
  780. + repr(missing),
  781. )
  782. def callback(self, tag):
  783. del self.timers[tag]
  784. self.checkTimers()
  785. def addCallback(self, tag):
  786. self.callback(tag)
  787. self.addTimer(15, self.callback)
  788. def done(self, tag):
  789. self.finished = 1
  790. self.callback(tag)
  791. self.deferred.callback(None)
  792. def addTimer(self, when, callback):
  793. self.timers[self.counter] = reactor.callLater(
  794. when * 0.01, callback, self.counter
  795. )
  796. self.counter += 1
  797. self.checkTimers()
  798. def testGetDelayedCalls(self):
  799. if not hasattr(reactor, "getDelayedCalls"):
  800. return
  801. # This is not a race because we don't do anything which might call
  802. # the reactor until we have all the timers set up. If we did, this
  803. # test might fail on slow systems.
  804. self.checkTimers()
  805. self.addTimer(35, self.done)
  806. self.addTimer(20, self.callback)
  807. self.addTimer(30, self.callback)
  808. which = self.counter
  809. self.addTimer(29, self.callback)
  810. self.addTimer(25, self.addCallback)
  811. self.addTimer(26, self.callback)
  812. self.timers[which].cancel()
  813. del self.timers[which]
  814. self.checkTimers()
  815. self.deferred.addCallback(lambda x: self.checkTimers())
  816. return self.deferred
  817. def test_active(self):
  818. """
  819. L{IDelayedCall.active} returns False once the call has run.
  820. """
  821. dcall = reactor.callLater(0.01, self.deferred.callback, True)
  822. self.assertTrue(dcall.active())
  823. def checkDeferredCall(success):
  824. self.assertFalse(dcall.active())
  825. return success
  826. self.deferred.addCallback(checkDeferredCall)
  827. return self.deferred
  828. resolve_helper = """
  829. import %(reactor)s
  830. %(reactor)s.install()
  831. from twisted.internet import reactor
  832. class Foo:
  833. def __init__(self):
  834. reactor.callWhenRunning(self.start)
  835. self.timer = reactor.callLater(3, self.failed)
  836. def start(self):
  837. reactor.resolve('localhost').addBoth(self.done)
  838. def done(self, res):
  839. print('done', res)
  840. reactor.stop()
  841. def failed(self):
  842. print('failed')
  843. self.timer = None
  844. reactor.stop()
  845. f = Foo()
  846. reactor.run()
  847. """
  848. class ChildResolveProtocol(protocol.ProcessProtocol):
  849. def __init__(self, onCompletion):
  850. self.onCompletion = onCompletion
  851. def connectionMade(self):
  852. self.output = []
  853. self.error = []
  854. def outReceived(self, out):
  855. self.output.append(out)
  856. def errReceived(self, err):
  857. self.error.append(err)
  858. def processEnded(self, reason):
  859. self.onCompletion.callback((reason, self.output, self.error))
  860. self.onCompletion = None
  861. @skipIf(
  862. not interfaces.IReactorProcess(reactor, None),
  863. "cannot run test: reactor doesn't support IReactorProcess",
  864. )
  865. class ResolveTests(TestCase):
  866. def testChildResolve(self):
  867. # I've seen problems with reactor.run under gtk2reactor. Spawn a
  868. # child which just does reactor.resolve after the reactor has
  869. # started, fail if it does not complete in a timely fashion.
  870. helperPath = os.path.abspath(self.mktemp())
  871. with open(helperPath, "w") as helperFile:
  872. # Eeueuuggg
  873. reactorName = reactor.__module__
  874. helperFile.write(resolve_helper % {"reactor": reactorName})
  875. env = os.environ.copy()
  876. env["PYTHONPATH"] = os.pathsep.join(sys.path)
  877. helperDeferred = Deferred()
  878. helperProto = ChildResolveProtocol(helperDeferred)
  879. reactor.spawnProcess(
  880. helperProto, sys.executable, ("python", "-u", helperPath), env
  881. )
  882. def cbFinished(result):
  883. (reason, output, error) = result
  884. # If the output is "done 127.0.0.1\n" we don't really care what
  885. # else happened.
  886. output = b"".join(output)
  887. expected_output = b"done 127.0.0.1" + os.linesep.encode("ascii")
  888. if output != expected_output:
  889. self.fail(
  890. (
  891. "The child process failed to produce "
  892. "the desired results:\n"
  893. " Reason for termination was: {!r}\n"
  894. " Output stream was: {!r}\n"
  895. " Error stream was: {!r}\n"
  896. ).format(reason.getErrorMessage(), output, b"".join(error))
  897. )
  898. helperDeferred.addCallback(cbFinished)
  899. return helperDeferred
  900. @skipIf(
  901. not interfaces.IReactorThreads(reactor, None),
  902. "Nothing to test without thread support",
  903. )
  904. class CallFromThreadTests(TestCase):
  905. """
  906. Task scheduling from threads tests.
  907. """
  908. def setUp(self):
  909. self.counter = 0
  910. self.deferred = Deferred()
  911. def schedule(self, *args, **kwargs):
  912. """
  913. Override in subclasses.
  914. """
  915. reactor.callFromThread(*args, **kwargs)
  916. def test_lotsOfThreadsAreScheduledCorrectly(self):
  917. """
  918. L{IReactorThreads.callFromThread} can be used to schedule a large
  919. number of calls in the reactor thread.
  920. """
  921. def addAndMaybeFinish():
  922. self.counter += 1
  923. if self.counter == 100:
  924. self.deferred.callback(True)
  925. for i in range(100):
  926. self.schedule(addAndMaybeFinish)
  927. return self.deferred
  928. def test_threadsAreRunInScheduledOrder(self):
  929. """
  930. Callbacks should be invoked in the order they were scheduled.
  931. """
  932. order = []
  933. def check(_):
  934. self.assertEqual(order, [1, 2, 3])
  935. self.deferred.addCallback(check)
  936. self.schedule(order.append, 1)
  937. self.schedule(order.append, 2)
  938. self.schedule(order.append, 3)
  939. self.schedule(reactor.callFromThread, self.deferred.callback, None)
  940. return self.deferred
  941. def test_scheduledThreadsNotRunUntilReactorRuns(self):
  942. """
  943. Scheduled tasks should not be run until the reactor starts running.
  944. """
  945. def incAndFinish():
  946. self.counter = 1
  947. self.deferred.callback(True)
  948. self.schedule(incAndFinish)
  949. # Callback shouldn't have fired yet.
  950. self.assertEqual(self.counter, 0)
  951. return self.deferred
  952. class MyProtocol(protocol.Protocol):
  953. """
  954. Sample protocol.
  955. """
  956. class MyFactory(protocol.Factory):
  957. """
  958. Sample factory.
  959. """
  960. protocol = MyProtocol
  961. class ProtocolTests(TestCase):
  962. def testFactory(self):
  963. factory = MyFactory()
  964. protocol = factory.buildProtocol(None)
  965. self.assertEqual(protocol.factory, factory)
  966. self.assertIsInstance(protocol, factory.protocol)
  967. class DummyProducer:
  968. """
  969. Very uninteresting producer implementation used by tests to ensure the
  970. right methods are called by the consumer with which it is registered.
  971. @type events: C{list} of C{str}
  972. @ivar events: The producer/consumer related events which have happened to
  973. this producer. Strings in this list may be C{'resume'}, C{'stop'}, or
  974. C{'pause'}. Elements are added as they occur.
  975. """
  976. def __init__(self):
  977. self.events = []
  978. def resumeProducing(self):
  979. self.events.append("resume")
  980. def stopProducing(self):
  981. self.events.append("stop")
  982. def pauseProducing(self):
  983. self.events.append("pause")
  984. class SillyDescriptor(abstract.FileDescriptor):
  985. """
  986. A descriptor whose data buffer gets filled very fast.
  987. Useful for testing FileDescriptor's IConsumer interface, since
  988. the data buffer fills as soon as at least four characters are
  989. written to it, and gets emptied in a single doWrite() cycle.
  990. """
  991. bufferSize = 3
  992. connected = True
  993. def writeSomeData(self, data):
  994. """
  995. Always write all data.
  996. """
  997. return len(data)
  998. def startWriting(self):
  999. """
  1000. Do nothing: bypass the reactor.
  1001. """
  1002. stopWriting = startWriting
  1003. class ReentrantProducer(DummyProducer):
  1004. """
  1005. Similar to L{DummyProducer}, but with a resumeProducing method which calls
  1006. back into an L{IConsumer} method of the consumer against which it is
  1007. registered.
  1008. @ivar consumer: The consumer with which this producer has been or will
  1009. be registered.
  1010. @ivar methodName: The name of the method to call on the consumer inside
  1011. C{resumeProducing}.
  1012. @ivar methodArgs: The arguments to pass to the consumer method invoked in
  1013. C{resumeProducing}.
  1014. """
  1015. def __init__(self, consumer, methodName, *methodArgs):
  1016. super().__init__()
  1017. self.consumer = consumer
  1018. self.methodName = methodName
  1019. self.methodArgs = methodArgs
  1020. def resumeProducing(self):
  1021. super().resumeProducing()
  1022. getattr(self.consumer, self.methodName)(*self.methodArgs)
  1023. class ProducerTests(TestCase):
  1024. """
  1025. Test abstract.FileDescriptor's consumer interface.
  1026. """
  1027. def test_doubleProducer(self):
  1028. """
  1029. Verify that registering a non-streaming producer invokes its
  1030. resumeProducing() method and that you can only register one producer
  1031. at a time.
  1032. """
  1033. fd = abstract.FileDescriptor()
  1034. fd.connected = 1
  1035. dp = DummyProducer()
  1036. fd.registerProducer(dp, 0)
  1037. self.assertEqual(dp.events, ["resume"])
  1038. self.assertRaises(RuntimeError, fd.registerProducer, DummyProducer(), 0)
  1039. def test_unconnectedFileDescriptor(self):
  1040. """
  1041. Verify that registering a producer when the connection has already
  1042. been closed invokes its stopProducing() method.
  1043. """
  1044. fd = abstract.FileDescriptor()
  1045. fd.disconnected = 1
  1046. dp = DummyProducer()
  1047. fd.registerProducer(dp, 0)
  1048. self.assertEqual(dp.events, ["stop"])
  1049. def _dontPausePullConsumerTest(self, methodName):
  1050. """
  1051. Pull consumers don't get their C{pauseProducing} method called if the
  1052. descriptor buffer fills up.
  1053. @param _methodName: Either 'write', or 'writeSequence', indicating
  1054. which transport method to write data to.
  1055. """
  1056. descriptor = SillyDescriptor()
  1057. producer = DummyProducer()
  1058. descriptor.registerProducer(producer, streaming=False)
  1059. self.assertEqual(producer.events, ["resume"])
  1060. del producer.events[:]
  1061. # Fill up the descriptor's write buffer so we can observe whether or
  1062. # not it pauses its producer in that case.
  1063. if methodName == "writeSequence":
  1064. descriptor.writeSequence([b"1", b"2", b"3", b"4"])
  1065. else:
  1066. descriptor.write(b"1234")
  1067. self.assertEqual(producer.events, [])
  1068. def test_dontPausePullConsumerOnWrite(self):
  1069. """
  1070. Verify that FileDescriptor does not call producer.pauseProducing() on a
  1071. non-streaming pull producer in response to a L{IConsumer.write} call
  1072. which results in a full write buffer. Issue #2286.
  1073. """
  1074. return self._dontPausePullConsumerTest("write")
  1075. def test_dontPausePullConsumerOnWriteSequence(self):
  1076. """
  1077. Like L{test_dontPausePullConsumerOnWrite}, but for a call to
  1078. C{writeSequence} rather than L{IConsumer.write}.
  1079. C{writeSequence} is not part of L{IConsumer}, but
  1080. L{abstract.FileDescriptor} has supported consumery behavior in response
  1081. to calls to L{writeSequence} forever.
  1082. """
  1083. return self._dontPausePullConsumerTest("writeSequence")
  1084. def _reentrantStreamingProducerTest(self, methodName):
  1085. descriptor = SillyDescriptor()
  1086. if methodName == "writeSequence":
  1087. data = [b"s", b"p", b"am"]
  1088. else:
  1089. data = b"spam"
  1090. producer = ReentrantProducer(descriptor, methodName, data)
  1091. descriptor.registerProducer(producer, streaming=True)
  1092. # Start things off by filling up the descriptor's buffer so it will
  1093. # pause its producer.
  1094. getattr(descriptor, methodName)(data)
  1095. # Sanity check - make sure that worked.
  1096. self.assertEqual(producer.events, ["pause"])
  1097. del producer.events[:]
  1098. # After one call to doWrite, the buffer has been emptied so the
  1099. # FileDescriptor should resume its producer. That will result in an
  1100. # immediate call to FileDescriptor.write which will again fill the
  1101. # buffer and result in the producer being paused.
  1102. descriptor.doWrite()
  1103. self.assertEqual(producer.events, ["resume", "pause"])
  1104. del producer.events[:]
  1105. # After a second call to doWrite, the exact same thing should have
  1106. # happened. Prior to the bugfix for which this test was written,
  1107. # FileDescriptor would have incorrectly believed its producer was
  1108. # already resumed (it was paused) and so not resume it again.
  1109. descriptor.doWrite()
  1110. self.assertEqual(producer.events, ["resume", "pause"])
  1111. def test_reentrantStreamingProducerUsingWrite(self):
  1112. """
  1113. Verify that FileDescriptor tracks producer's paused state correctly.
  1114. Issue #811, fixed in revision r12857.
  1115. """
  1116. return self._reentrantStreamingProducerTest("write")
  1117. def test_reentrantStreamingProducerUsingWriteSequence(self):
  1118. """
  1119. Like L{test_reentrantStreamingProducerUsingWrite}, but for calls to
  1120. C{writeSequence}.
  1121. C{writeSequence} is B{not} part of L{IConsumer}, however
  1122. C{abstract.FileDescriptor} has supported consumery behavior in response
  1123. to calls to C{writeSequence} forever.
  1124. """
  1125. return self._reentrantStreamingProducerTest("writeSequence")
  1126. class PortStringificationTests(TestCase):
  1127. @skipIf(not interfaces.IReactorTCP(reactor, None), "IReactorTCP is needed")
  1128. def testTCP(self):
  1129. p = reactor.listenTCP(0, protocol.ServerFactory())
  1130. portNo = p.getHost().port
  1131. self.assertNotEqual(
  1132. str(p).find(str(portNo)), -1, "%d not found in %s" % (portNo, p)
  1133. )
  1134. return p.stopListening()
  1135. @skipIf(not interfaces.IReactorUDP(reactor, None), "IReactorUDP is needed")
  1136. def testUDP(self):
  1137. p = reactor.listenUDP(0, protocol.DatagramProtocol())
  1138. portNo = p.getHost().port
  1139. self.assertNotEqual(
  1140. str(p).find(str(portNo)), -1, "%d not found in %s" % (portNo, p)
  1141. )
  1142. return p.stopListening()
  1143. @skipIf(not interfaces.IReactorSSL(reactor, None), "IReactorSSL is needed")
  1144. @skipIf(not ssl, "SSL support is missing")
  1145. def testSSL(self, ssl=ssl):
  1146. pem = util.sibpath(__file__, "server.pem")
  1147. p = reactor.listenSSL(
  1148. 0, protocol.ServerFactory(), ssl.DefaultOpenSSLContextFactory(pem, pem)
  1149. )
  1150. portNo = p.getHost().port
  1151. self.assertNotEqual(
  1152. str(p).find(str(portNo)), -1, "%d not found in %s" % (portNo, p)
  1153. )
  1154. return p.stopListening()
  1155. class ConnectorReprTests(TestCase):
  1156. def test_tcp_repr(self):
  1157. c = Connector("localhost", 666, object(), 0, object())
  1158. expect = "<twisted.internet.tcp.Connector instance at 0x%x " "disconnected %s>"
  1159. expect = expect % (id(c), c.getDestination())
  1160. self.assertEqual(repr(c), expect)