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_task.py 39KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.internet.task}.
  5. """
  6. from __future__ import division, absolute_import
  7. from twisted.trial import unittest
  8. from twisted.internet import interfaces, task, reactor, defer, error
  9. from twisted.internet.main import installReactor
  10. from twisted.internet.test.modulehelpers import NoReactor
  11. # Be compatible with any jerks who used our private stuff
  12. Clock = task.Clock
  13. from twisted.python import failure
  14. class TestableLoopingCall(task.LoopingCall):
  15. def __init__(self, clock, *a, **kw):
  16. super(TestableLoopingCall, self).__init__(*a, **kw)
  17. self.clock = clock
  18. class TestException(Exception):
  19. pass
  20. class ClockTests(unittest.TestCase):
  21. """
  22. Test the non-wallclock based clock implementation.
  23. """
  24. def testSeconds(self):
  25. """
  26. Test that the C{seconds} method of the fake clock returns fake time.
  27. """
  28. c = task.Clock()
  29. self.assertEqual(c.seconds(), 0)
  30. def testCallLater(self):
  31. """
  32. Test that calls can be scheduled for later with the fake clock and
  33. hands back an L{IDelayedCall}.
  34. """
  35. c = task.Clock()
  36. call = c.callLater(1, lambda a, b: None, 1, b=2)
  37. self.assertTrue(interfaces.IDelayedCall.providedBy(call))
  38. self.assertEqual(call.getTime(), 1)
  39. self.assertTrue(call.active())
  40. def testCallLaterCancelled(self):
  41. """
  42. Test that calls can be cancelled.
  43. """
  44. c = task.Clock()
  45. call = c.callLater(1, lambda a, b: None, 1, b=2)
  46. call.cancel()
  47. self.assertFalse(call.active())
  48. def test_callLaterOrdering(self):
  49. """
  50. Test that the DelayedCall returned is not one previously
  51. created.
  52. """
  53. c = task.Clock()
  54. call1 = c.callLater(10, lambda a, b: None, 1, b=2)
  55. call2 = c.callLater(1, lambda a, b: None, 3, b=4)
  56. self.assertFalse(call1 is call2)
  57. def testAdvance(self):
  58. """
  59. Test that advancing the clock will fire some calls.
  60. """
  61. events = []
  62. c = task.Clock()
  63. call = c.callLater(2, lambda: events.append(None))
  64. c.advance(1)
  65. self.assertEqual(events, [])
  66. c.advance(1)
  67. self.assertEqual(events, [None])
  68. self.assertFalse(call.active())
  69. def testAdvanceCancel(self):
  70. """
  71. Test attempting to cancel the call in a callback.
  72. AlreadyCalled should be raised, not for example a ValueError from
  73. removing the call from Clock.calls. This requires call.called to be
  74. set before the callback is called.
  75. """
  76. c = task.Clock()
  77. def cb():
  78. self.assertRaises(error.AlreadyCalled, call.cancel)
  79. call = c.callLater(1, cb)
  80. c.advance(1)
  81. def testCallLaterDelayed(self):
  82. """
  83. Test that calls can be delayed.
  84. """
  85. events = []
  86. c = task.Clock()
  87. call = c.callLater(1, lambda a, b: events.append((a, b)), 1, b=2)
  88. call.delay(1)
  89. self.assertEqual(call.getTime(), 2)
  90. c.advance(1.5)
  91. self.assertEqual(events, [])
  92. c.advance(1.0)
  93. self.assertEqual(events, [(1, 2)])
  94. def testCallLaterResetLater(self):
  95. """
  96. Test that calls can have their time reset to a later time.
  97. """
  98. events = []
  99. c = task.Clock()
  100. call = c.callLater(2, lambda a, b: events.append((a, b)), 1, b=2)
  101. c.advance(1)
  102. call.reset(3)
  103. self.assertEqual(call.getTime(), 4)
  104. c.advance(2)
  105. self.assertEqual(events, [])
  106. c.advance(1)
  107. self.assertEqual(events, [(1, 2)])
  108. def testCallLaterResetSooner(self):
  109. """
  110. Test that calls can have their time reset to an earlier time.
  111. """
  112. events = []
  113. c = task.Clock()
  114. call = c.callLater(4, lambda a, b: events.append((a, b)), 1, b=2)
  115. call.reset(3)
  116. self.assertEqual(call.getTime(), 3)
  117. c.advance(3)
  118. self.assertEqual(events, [(1, 2)])
  119. def test_getDelayedCalls(self):
  120. """
  121. Test that we can get a list of all delayed calls
  122. """
  123. c = task.Clock()
  124. call = c.callLater(1, lambda x: None)
  125. call2 = c.callLater(2, lambda x: None)
  126. calls = c.getDelayedCalls()
  127. self.assertEqual(set([call, call2]), set(calls))
  128. def test_getDelayedCallsEmpty(self):
  129. """
  130. Test that we get an empty list from getDelayedCalls on a newly
  131. constructed Clock.
  132. """
  133. c = task.Clock()
  134. self.assertEqual(c.getDelayedCalls(), [])
  135. def test_providesIReactorTime(self):
  136. c = task.Clock()
  137. self.assertTrue(interfaces.IReactorTime.providedBy(c),
  138. "Clock does not provide IReactorTime")
  139. def test_callLaterKeepsCallsOrdered(self):
  140. """
  141. The order of calls scheduled by L{task.Clock.callLater} is honored when
  142. adding a new call via calling L{task.Clock.callLater} again.
  143. For example, if L{task.Clock.callLater} is invoked with a callable "A"
  144. and a time t0, and then the L{IDelayedCall} which results from that is
  145. C{reset} to a later time t2 which is greater than t0, and I{then}
  146. L{task.Clock.callLater} is invoked again with a callable "B", and time
  147. t1 which is less than t2 but greater than t0, "B" will be invoked before
  148. "A".
  149. """
  150. result = []
  151. expected = [('b', 2.0), ('a', 3.0)]
  152. clock = task.Clock()
  153. logtime = lambda n: result.append((n, clock.seconds()))
  154. call_a = clock.callLater(1.0, logtime, "a")
  155. call_a.reset(3.0)
  156. clock.callLater(2.0, logtime, "b")
  157. clock.pump([1]*3)
  158. self.assertEqual(result, expected)
  159. def test_callLaterResetKeepsCallsOrdered(self):
  160. """
  161. The order of calls scheduled by L{task.Clock.callLater} is honored when
  162. re-scheduling an existing call via L{IDelayedCall.reset} on the result
  163. of a previous call to C{callLater}.
  164. For example, if L{task.Clock.callLater} is invoked with a callable "A"
  165. and a time t0, and then L{task.Clock.callLater} is invoked again with a
  166. callable "B", and time t1 greater than t0, and finally the
  167. L{IDelayedCall} for "A" is C{reset} to a later time, t2, which is
  168. greater than t1, "B" will be invoked before "A".
  169. """
  170. result = []
  171. expected = [('b', 2.0), ('a', 3.0)]
  172. clock = task.Clock()
  173. logtime = lambda n: result.append((n, clock.seconds()))
  174. call_a = clock.callLater(1.0, logtime, "a")
  175. clock.callLater(2.0, logtime, "b")
  176. call_a.reset(3.0)
  177. clock.pump([1]*3)
  178. self.assertEqual(result, expected)
  179. def test_callLaterResetInsideCallKeepsCallsOrdered(self):
  180. """
  181. The order of calls scheduled by L{task.Clock.callLater} is honored when
  182. re-scheduling an existing call via L{IDelayedCall.reset} on the result
  183. of a previous call to C{callLater}, even when that call to C{reset}
  184. occurs within the callable scheduled by C{callLater} itself.
  185. """
  186. result = []
  187. expected = [('c', 3.0), ('b', 4.0)]
  188. clock = task.Clock()
  189. logtime = lambda n: result.append((n, clock.seconds()))
  190. call_b = clock.callLater(2.0, logtime, "b")
  191. def a():
  192. call_b.reset(3.0)
  193. clock.callLater(1.0, a)
  194. clock.callLater(3.0, logtime, "c")
  195. clock.pump([0.5] * 10)
  196. self.assertEqual(result, expected)
  197. class LoopTests(unittest.TestCase):
  198. """
  199. Tests for L{task.LoopingCall} based on a fake L{IReactorTime}
  200. implementation.
  201. """
  202. def test_defaultClock(self):
  203. """
  204. L{LoopingCall}'s default clock should be the reactor.
  205. """
  206. call = task.LoopingCall(lambda: None)
  207. self.assertEqual(call.clock, reactor)
  208. def test_callbackTimeSkips(self):
  209. """
  210. When more time than the defined interval passes during the execution
  211. of a callback, L{LoopingCall} should schedule the next call for the
  212. next interval which is still in the future.
  213. """
  214. times = []
  215. callDuration = None
  216. clock = task.Clock()
  217. def aCallback():
  218. times.append(clock.seconds())
  219. clock.advance(callDuration)
  220. call = task.LoopingCall(aCallback)
  221. call.clock = clock
  222. # Start a LoopingCall with a 0.5 second increment, and immediately call
  223. # the callable.
  224. callDuration = 2
  225. call.start(0.5)
  226. # Verify that the callable was called, and since it was immediate, with
  227. # no skips.
  228. self.assertEqual(times, [0])
  229. # The callback should have advanced the clock by the callDuration.
  230. self.assertEqual(clock.seconds(), callDuration)
  231. # An iteration should have occurred at 2, but since 2 is the present
  232. # and not the future, it is skipped.
  233. clock.advance(0)
  234. self.assertEqual(times, [0])
  235. # 2.5 is in the future, and is not skipped.
  236. callDuration = 1
  237. clock.advance(0.5)
  238. self.assertEqual(times, [0, 2.5])
  239. self.assertEqual(clock.seconds(), 3.5)
  240. # Another iteration should have occurred, but it is again the
  241. # present and not the future, so it is skipped as well.
  242. clock.advance(0)
  243. self.assertEqual(times, [0, 2.5])
  244. # 4 is in the future, and is not skipped.
  245. callDuration = 0
  246. clock.advance(0.5)
  247. self.assertEqual(times, [0, 2.5, 4])
  248. self.assertEqual(clock.seconds(), 4)
  249. def test_reactorTimeSkips(self):
  250. """
  251. When more time than the defined interval passes between when
  252. L{LoopingCall} schedules itself to run again and when it actually
  253. runs again, it should schedule the next call for the next interval
  254. which is still in the future.
  255. """
  256. times = []
  257. clock = task.Clock()
  258. def aCallback():
  259. times.append(clock.seconds())
  260. # Start a LoopingCall that tracks the time passed, with a 0.5 second
  261. # increment.
  262. call = task.LoopingCall(aCallback)
  263. call.clock = clock
  264. call.start(0.5)
  265. # Initially, no time should have passed!
  266. self.assertEqual(times, [0])
  267. # Advance the clock by 2 seconds (2 seconds should have passed)
  268. clock.advance(2)
  269. self.assertEqual(times, [0, 2])
  270. # Advance the clock by 1 second (3 total should have passed)
  271. clock.advance(1)
  272. self.assertEqual(times, [0, 2, 3])
  273. # Advance the clock by 0 seconds (this should have no effect!)
  274. clock.advance(0)
  275. self.assertEqual(times, [0, 2, 3])
  276. def test_reactorTimeCountSkips(self):
  277. """
  278. When L{LoopingCall} schedules itself to run again, if more than the
  279. specified interval has passed, it should schedule the next call for the
  280. next interval which is still in the future. If it was created
  281. using L{LoopingCall.withCount}, a positional argument will be
  282. inserted at the beginning of the argument list, indicating the number
  283. of calls that should have been made.
  284. """
  285. times = []
  286. clock = task.Clock()
  287. def aCallback(numCalls):
  288. times.append((clock.seconds(), numCalls))
  289. # Start a LoopingCall that tracks the time passed, and the number of
  290. # skips, with a 0.5 second increment.
  291. call = task.LoopingCall.withCount(aCallback)
  292. call.clock = clock
  293. INTERVAL = 0.5
  294. REALISTIC_DELAY = 0.01
  295. call.start(INTERVAL)
  296. # Initially, no seconds should have passed, and one calls should have
  297. # been made.
  298. self.assertEqual(times, [(0, 1)])
  299. # After the interval (plus a small delay, to account for the time that
  300. # the reactor takes to wake up and process the LoopingCall), we should
  301. # still have only made one call.
  302. clock.advance(INTERVAL + REALISTIC_DELAY)
  303. self.assertEqual(times, [(0, 1), (INTERVAL + REALISTIC_DELAY, 1)])
  304. # After advancing the clock by three intervals (plus a small delay to
  305. # account for the reactor), we should have skipped two calls; one less
  306. # than the number of intervals which have completely elapsed. Along
  307. # with the call we did actually make, the final number of calls is 3.
  308. clock.advance((3 * INTERVAL) + REALISTIC_DELAY)
  309. self.assertEqual(times,
  310. [(0, 1), (INTERVAL + REALISTIC_DELAY, 1),
  311. ((4 * INTERVAL) + (2 * REALISTIC_DELAY), 3)])
  312. # Advancing the clock by 0 seconds should not cause any changes!
  313. clock.advance(0)
  314. self.assertEqual(times,
  315. [(0, 1), (INTERVAL + REALISTIC_DELAY, 1),
  316. ((4 * INTERVAL) + (2 * REALISTIC_DELAY), 3)])
  317. def test_countLengthyIntervalCounts(self):
  318. """
  319. L{LoopingCall.withCount} counts only calls that were expected to be
  320. made. So, if more than one, but less than two intervals pass between
  321. invocations, it won't increase the count above 1. For example, a
  322. L{LoopingCall} with interval T expects to be invoked at T, 2T, 3T, etc.
  323. However, the reactor takes some time to get around to calling it, so in
  324. practice it will be called at T+something, 2T+something, 3T+something;
  325. and due to other things going on in the reactor, "something" is
  326. variable. It won't increase the count unless "something" is greater
  327. than T. So if the L{LoopingCall} is invoked at T, 2.75T, and 3T,
  328. the count has not increased, even though the distance between
  329. invocation 1 and invocation 2 is 1.75T.
  330. """
  331. times = []
  332. clock = task.Clock()
  333. def aCallback(count):
  334. times.append((clock.seconds(), count))
  335. # Start a LoopingCall that tracks the time passed, and the number of
  336. # calls, with a 0.5 second increment.
  337. call = task.LoopingCall.withCount(aCallback)
  338. call.clock = clock
  339. INTERVAL = 0.5
  340. REALISTIC_DELAY = 0.01
  341. call.start(INTERVAL)
  342. self.assertEqual(times.pop(), (0, 1))
  343. # About one interval... So far, so good
  344. clock.advance(INTERVAL + REALISTIC_DELAY)
  345. self.assertEqual(times.pop(), (INTERVAL + REALISTIC_DELAY, 1))
  346. # Oh no, something delayed us for a while.
  347. clock.advance(INTERVAL * 1.75)
  348. self.assertEqual(times.pop(), ((2.75 * INTERVAL) + REALISTIC_DELAY, 1))
  349. # Back on track! We got invoked when we expected this time.
  350. clock.advance(INTERVAL * 0.25)
  351. self.assertEqual(times.pop(), ((3.0 * INTERVAL) + REALISTIC_DELAY, 1))
  352. def test_withCountFloatingPointBoundary(self):
  353. """
  354. L{task.LoopingCall.withCount} should never invoke its callable with a
  355. zero. Specifically, if a L{task.LoopingCall} created with C{withCount}
  356. has its L{start <task.LoopingCall.start>} method invoked with a
  357. floating-point number which introduces decimal inaccuracy when
  358. multiplied or divided, such as "0.1", L{task.LoopingCall} will never
  359. invoke its callable with 0. Also, the sum of all the values passed to
  360. its callable as the "count" will be an integer, the number of intervals
  361. that have elapsed.
  362. This is a regression test for a particularly tricky case to implement.
  363. """
  364. clock = task.Clock()
  365. accumulator = []
  366. call = task.LoopingCall.withCount(accumulator.append)
  367. call.clock = clock
  368. # 'count': the number of ticks within the time span, the number of
  369. # calls that should be made. this should be a value which causes
  370. # floating-point inaccuracy as the denominator for the timespan.
  371. count = 10
  372. # 'timespan': the amount of virtual time that the test will take, in
  373. # seconds, as a floating point number
  374. timespan = 1.0
  375. # 'interval': the amount of time for one actual call.
  376. interval = timespan / count
  377. call.start(interval, now=False)
  378. for x in range(count):
  379. clock.advance(interval)
  380. # There is still an epsilon of inaccuracy here; 0.1 is not quite
  381. # exactly 1/10 in binary, so we need to push our clock over the
  382. # threshold.
  383. epsilon = timespan - sum([interval] * count)
  384. clock.advance(epsilon)
  385. secondsValue = clock.seconds()
  386. # The following two assertions are here to ensure that if the values of
  387. # count, timespan, and interval are changed, that the test remains
  388. # valid. First, the "epsilon" value here measures the floating-point
  389. # inaccuracy in question, and so if it doesn't exist then we are not
  390. # triggering an interesting condition.
  391. self.assertTrue(abs(epsilon) > 0.0,
  392. "{0} should be greater than zero"
  393. .format(epsilon))
  394. # Secondly, task.Clock should behave in such a way that once we have
  395. # advanced to this point, it has reached or exceeded the timespan.
  396. self.assertTrue(secondsValue >= timespan,
  397. "{0} should be greater than or equal to {1}"
  398. .format(secondsValue, timespan))
  399. self.assertEqual(sum(accumulator), count)
  400. self.assertNotIn(0, accumulator)
  401. def test_withCountIntervalZero(self):
  402. """
  403. L{task.LoopingCall.withCount} with interval set to 0
  404. will call the countCallable 1.
  405. """
  406. clock = task.Clock()
  407. accumulator = []
  408. def foo(cnt):
  409. accumulator.append(cnt)
  410. if len(accumulator) > 4:
  411. loop.stop()
  412. loop = task.LoopingCall.withCount(foo)
  413. loop.clock = clock
  414. deferred = loop.start(0, now=False)
  415. clock.advance(0)
  416. self.successResultOf(deferred)
  417. self.assertEqual([1, 1, 1, 1, 1], accumulator)
  418. def test_withCountIntervalZeroDelay(self):
  419. """
  420. L{task.LoopingCall.withCount} with interval set to 0 and a delayed
  421. call during the loop run will still call the countCallable 1 as if
  422. no delay occurred.
  423. """
  424. clock = task.Clock()
  425. deferred = defer.Deferred()
  426. accumulator = []
  427. def foo(cnt):
  428. accumulator.append(cnt)
  429. if len(accumulator) == 2:
  430. return deferred
  431. if len(accumulator) > 4:
  432. loop.stop()
  433. loop = task.LoopingCall.withCount(foo)
  434. loop.clock = clock
  435. loop.start(0, now=False)
  436. clock.advance(0)
  437. # Loop will block at the third call.
  438. self.assertEqual([1, 1], accumulator)
  439. # Even if more time pass, the loops is not
  440. # advanced.
  441. clock.advance(2)
  442. self.assertEqual([1, 1], accumulator)
  443. # Once the waiting call got a result the loop continues without
  444. # observing any delay in countCallable.
  445. deferred.callback(None)
  446. clock.advance(0)
  447. self.assertEqual([1, 1, 1, 1, 1], accumulator)
  448. def test_withCountIntervalZeroDelayThenNonZeroInterval(self):
  449. """
  450. L{task.LoopingCall.withCount} with interval set to 0 will still keep
  451. the time when last called so when the interval is reset.
  452. """
  453. clock = task.Clock()
  454. deferred = defer.Deferred()
  455. accumulator = []
  456. def foo(cnt):
  457. accumulator.append(cnt)
  458. if len(accumulator) == 2:
  459. return deferred
  460. loop = task.LoopingCall.withCount(foo)
  461. loop.clock = clock
  462. loop.start(0, now=False)
  463. # Even if a lot of time pass, loop will block at the third call.
  464. clock.advance(10)
  465. self.assertEqual([1, 1], accumulator)
  466. # When a new interval is set, once the waiting call got a result the
  467. # loop continues with the new interval.
  468. loop.interval = 2
  469. deferred.callback(None)
  470. # It will count skipped steps since the last loop call.
  471. clock.advance(7)
  472. self.assertEqual([1, 1, 3], accumulator)
  473. clock.advance(2)
  474. self.assertEqual([1, 1, 3, 1], accumulator)
  475. clock.advance(4)
  476. self.assertEqual([1, 1, 3, 1, 2], accumulator)
  477. def testBasicFunction(self):
  478. # Arrange to have time advanced enough so that our function is
  479. # called a few times.
  480. # Only need to go to 2.5 to get 3 calls, since the first call
  481. # happens before any time has elapsed.
  482. timings = [0.05, 0.1, 0.1]
  483. clock = task.Clock()
  484. L = []
  485. def foo(a, b, c=None, d=None):
  486. L.append((a, b, c, d))
  487. lc = TestableLoopingCall(clock, foo, "a", "b", d="d")
  488. D = lc.start(0.1)
  489. theResult = []
  490. def saveResult(result):
  491. theResult.append(result)
  492. D.addCallback(saveResult)
  493. clock.pump(timings)
  494. self.assertEqual(len(L), 3,
  495. "got %d iterations, not 3" % (len(L),))
  496. for (a, b, c, d) in L:
  497. self.assertEqual(a, "a")
  498. self.assertEqual(b, "b")
  499. self.assertIsNone(c)
  500. self.assertEqual(d, "d")
  501. lc.stop()
  502. self.assertIs(theResult[0], lc)
  503. # Make sure it isn't planning to do anything further.
  504. self.assertFalse(clock.calls)
  505. def testDelayedStart(self):
  506. timings = [0.05, 0.1, 0.1]
  507. clock = task.Clock()
  508. L = []
  509. lc = TestableLoopingCall(clock, L.append, None)
  510. d = lc.start(0.1, now=False)
  511. theResult = []
  512. def saveResult(result):
  513. theResult.append(result)
  514. d.addCallback(saveResult)
  515. clock.pump(timings)
  516. self.assertEqual(len(L), 2,
  517. "got %d iterations, not 2" % (len(L),))
  518. lc.stop()
  519. self.assertIs(theResult[0], lc)
  520. self.assertFalse(clock.calls)
  521. def testBadDelay(self):
  522. lc = task.LoopingCall(lambda: None)
  523. self.assertRaises(ValueError, lc.start, -1)
  524. # Make sure that LoopingCall.stop() prevents any subsequent calls.
  525. def _stoppingTest(self, delay):
  526. ran = []
  527. def foo():
  528. ran.append(None)
  529. clock = task.Clock()
  530. lc = TestableLoopingCall(clock, foo)
  531. lc.start(delay, now=False)
  532. lc.stop()
  533. self.assertFalse(ran)
  534. self.assertFalse(clock.calls)
  535. def testStopAtOnce(self):
  536. return self._stoppingTest(0)
  537. def testStoppingBeforeDelayedStart(self):
  538. return self._stoppingTest(10)
  539. def test_reset(self):
  540. """
  541. Test that L{LoopingCall} can be reset.
  542. """
  543. ran = []
  544. def foo():
  545. ran.append(None)
  546. c = task.Clock()
  547. lc = TestableLoopingCall(c, foo)
  548. lc.start(2, now=False)
  549. c.advance(1)
  550. lc.reset()
  551. c.advance(1)
  552. self.assertEqual(ran, [])
  553. c.advance(1)
  554. self.assertEqual(ran, [None])
  555. def test_reprFunction(self):
  556. """
  557. L{LoopingCall.__repr__} includes the wrapped function's name.
  558. """
  559. self.assertEqual(repr(task.LoopingCall(installReactor, 1, key=2)),
  560. "LoopingCall<None>(installReactor, *(1,), **{'key': 2})")
  561. def test_reprMethod(self):
  562. """
  563. L{LoopingCall.__repr__} includes the wrapped method's full name.
  564. """
  565. self.assertEqual(
  566. repr(task.LoopingCall(TestableLoopingCall.__init__)),
  567. "LoopingCall<None>(TestableLoopingCall.__init__, *(), **{})")
  568. def test_deferredDeprecation(self):
  569. """
  570. L{LoopingCall.deferred} is deprecated.
  571. """
  572. loop = task.LoopingCall(lambda: None)
  573. loop.deferred
  574. message = (
  575. 'twisted.internet.task.LoopingCall.deferred was deprecated in '
  576. 'Twisted 16.0.0; '
  577. 'please use the deferred returned by start() instead'
  578. )
  579. warnings = self.flushWarnings([self.test_deferredDeprecation])
  580. self.assertEqual(1, len(warnings))
  581. self.assertEqual(DeprecationWarning, warnings[0]['category'])
  582. self.assertEqual(message, warnings[0]['message'])
  583. class ReactorLoopTests(unittest.TestCase):
  584. # Slightly inferior tests which exercise interactions with an actual
  585. # reactor.
  586. def testFailure(self):
  587. def foo(x):
  588. raise TestException(x)
  589. lc = task.LoopingCall(foo, "bar")
  590. return self.assertFailure(lc.start(0.1), TestException)
  591. def testFailAndStop(self):
  592. def foo(x):
  593. lc.stop()
  594. raise TestException(x)
  595. lc = task.LoopingCall(foo, "bar")
  596. return self.assertFailure(lc.start(0.1), TestException)
  597. def testEveryIteration(self):
  598. ran = []
  599. def foo():
  600. ran.append(None)
  601. if len(ran) > 5:
  602. lc.stop()
  603. lc = task.LoopingCall(foo)
  604. d = lc.start(0)
  605. def stopped(ign):
  606. self.assertEqual(len(ran), 6)
  607. return d.addCallback(stopped)
  608. def testStopAtOnceLater(self):
  609. # Ensure that even when LoopingCall.stop() is called from a
  610. # reactor callback, it still prevents any subsequent calls.
  611. d = defer.Deferred()
  612. def foo():
  613. d.errback(failure.DefaultException(
  614. "This task also should never get called."))
  615. self._lc = task.LoopingCall(foo)
  616. self._lc.start(1, now=False)
  617. reactor.callLater(0, self._callback_for_testStopAtOnceLater, d)
  618. return d
  619. def _callback_for_testStopAtOnceLater(self, d):
  620. self._lc.stop()
  621. reactor.callLater(0, d.callback, "success")
  622. def testWaitDeferred(self):
  623. # Tests if the callable isn't scheduled again before the returned
  624. # deferred has fired.
  625. timings = [0.2, 0.8]
  626. clock = task.Clock()
  627. def foo():
  628. d = defer.Deferred()
  629. d.addCallback(lambda _: lc.stop())
  630. clock.callLater(1, d.callback, None)
  631. return d
  632. lc = TestableLoopingCall(clock, foo)
  633. lc.start(0.2)
  634. clock.pump(timings)
  635. self.assertFalse(clock.calls)
  636. def testFailurePropagation(self):
  637. # Tests if the failure of the errback of the deferred returned by the
  638. # callable is propagated to the lc errback.
  639. #
  640. # To make sure this test does not hang trial when LoopingCall does not
  641. # wait for the callable's deferred, it also checks there are no
  642. # calls in the clock's callLater queue.
  643. timings = [0.3]
  644. clock = task.Clock()
  645. def foo():
  646. d = defer.Deferred()
  647. clock.callLater(0.3, d.errback, TestException())
  648. return d
  649. lc = TestableLoopingCall(clock, foo)
  650. d = lc.start(1)
  651. self.assertFailure(d, TestException)
  652. clock.pump(timings)
  653. self.assertFalse(clock.calls)
  654. return d
  655. def test_deferredWithCount(self):
  656. """
  657. In the case that the function passed to L{LoopingCall.withCount}
  658. returns a deferred, which does not fire before the next interval
  659. elapses, the function should not be run again. And if a function call
  660. is skipped in this fashion, the appropriate count should be
  661. provided.
  662. """
  663. testClock = task.Clock()
  664. d = defer.Deferred()
  665. deferredCounts = []
  666. def countTracker(possibleCount):
  667. # Keep a list of call counts
  668. deferredCounts.append(possibleCount)
  669. # Return a deferred, but only on the first request
  670. if len(deferredCounts) == 1:
  671. return d
  672. else:
  673. return None
  674. # Start a looping call for our countTracker function
  675. # Set the increment to 0.2, and do not call the function on startup.
  676. lc = task.LoopingCall.withCount(countTracker)
  677. lc.clock = testClock
  678. d = lc.start(0.2, now=False)
  679. # Confirm that nothing has happened yet.
  680. self.assertEqual(deferredCounts, [])
  681. # Advance the clock by 0.2 and then 0.4;
  682. testClock.pump([0.2, 0.4])
  683. # We should now have exactly one count (of 1 call)
  684. self.assertEqual(len(deferredCounts), 1)
  685. # Fire the deferred, and advance the clock by another 0.2
  686. d.callback(None)
  687. testClock.pump([0.2])
  688. # We should now have exactly 2 counts...
  689. self.assertEqual(len(deferredCounts), 2)
  690. # The first count should be 1 (one call)
  691. # The second count should be 3 (calls were missed at about 0.6 and 0.8)
  692. self.assertEqual(deferredCounts, [1, 3])
  693. class DeferLaterTests(unittest.TestCase):
  694. """
  695. Tests for L{task.deferLater}.
  696. """
  697. def test_callback(self):
  698. """
  699. The L{Deferred} returned by L{task.deferLater} is called back after
  700. the specified delay with the result of the function passed in.
  701. """
  702. results = []
  703. flag = object()
  704. def callable(foo, bar):
  705. results.append((foo, bar))
  706. return flag
  707. clock = task.Clock()
  708. d = task.deferLater(clock, 3, callable, 'foo', bar='bar')
  709. d.addCallback(self.assertIs, flag)
  710. clock.advance(2)
  711. self.assertEqual(results, [])
  712. clock.advance(1)
  713. self.assertEqual(results, [('foo', 'bar')])
  714. return d
  715. def test_errback(self):
  716. """
  717. The L{Deferred} returned by L{task.deferLater} is errbacked if the
  718. supplied function raises an exception.
  719. """
  720. def callable():
  721. raise TestException()
  722. clock = task.Clock()
  723. d = task.deferLater(clock, 1, callable)
  724. clock.advance(1)
  725. return self.assertFailure(d, TestException)
  726. def test_cancel(self):
  727. """
  728. The L{Deferred} returned by L{task.deferLater} can be
  729. cancelled to prevent the call from actually being performed.
  730. """
  731. called = []
  732. clock = task.Clock()
  733. d = task.deferLater(clock, 1, called.append, None)
  734. d.cancel()
  735. def cbCancelled(ignored):
  736. # Make sure there are no calls outstanding.
  737. self.assertEqual([], clock.getDelayedCalls())
  738. # And make sure the call didn't somehow happen already.
  739. self.assertFalse(called)
  740. self.assertFailure(d, defer.CancelledError)
  741. d.addCallback(cbCancelled)
  742. return d
  743. def test_noCallback(self):
  744. """
  745. The L{Deferred} returned by L{task.deferLater} fires with C{None}
  746. when no callback function is passed.
  747. """
  748. clock = task.Clock()
  749. d = task.deferLater(clock, 2.0)
  750. self.assertNoResult(d)
  751. clock.advance(2.0)
  752. self.assertIs(None, self.successResultOf(d))
  753. class _FakeReactor(object):
  754. def __init__(self):
  755. self._running = False
  756. self._clock = task.Clock()
  757. self.callLater = self._clock.callLater
  758. self.seconds = self._clock.seconds
  759. self.getDelayedCalls = self._clock.getDelayedCalls
  760. self._whenRunning = []
  761. self._shutdownTriggers = {'before': [], 'during': []}
  762. def callWhenRunning(self, callable, *args, **kwargs):
  763. if self._whenRunning is None:
  764. callable(*args, **kwargs)
  765. else:
  766. self._whenRunning.append((callable, args, kwargs))
  767. def addSystemEventTrigger(self, phase, event, callable, *args):
  768. assert phase in ('before', 'during')
  769. assert event == 'shutdown'
  770. self._shutdownTriggers[phase].append((callable, args))
  771. def run(self):
  772. """
  773. Call timed events until there are no more or the reactor is stopped.
  774. @raise RuntimeError: When no timed events are left and the reactor is
  775. still running.
  776. """
  777. self._running = True
  778. whenRunning = self._whenRunning
  779. self._whenRunning = None
  780. for callable, args, kwargs in whenRunning:
  781. callable(*args, **kwargs)
  782. while self._running:
  783. calls = self.getDelayedCalls()
  784. if not calls:
  785. raise RuntimeError("No DelayedCalls left")
  786. self._clock.advance(calls[0].getTime() - self.seconds())
  787. shutdownTriggers = self._shutdownTriggers
  788. self._shutdownTriggers = None
  789. for (trigger, args) in shutdownTriggers['before'] + shutdownTriggers['during']:
  790. trigger(*args)
  791. def stop(self):
  792. """
  793. Stop the reactor.
  794. """
  795. if not self._running:
  796. raise error.ReactorNotRunning()
  797. self._running = False
  798. class ReactTests(unittest.SynchronousTestCase):
  799. """
  800. Tests for L{twisted.internet.task.react}.
  801. """
  802. def test_runsUntilAsyncCallback(self):
  803. """
  804. L{task.react} runs the reactor until the L{Deferred} returned by the
  805. function it is passed is called back, then stops it.
  806. """
  807. timePassed = []
  808. def main(reactor):
  809. finished = defer.Deferred()
  810. reactor.callLater(1, timePassed.append, True)
  811. reactor.callLater(2, finished.callback, None)
  812. return finished
  813. r = _FakeReactor()
  814. exitError = self.assertRaises(
  815. SystemExit, task.react, main, _reactor=r)
  816. self.assertEqual(0, exitError.code)
  817. self.assertEqual(timePassed, [True])
  818. self.assertEqual(r.seconds(), 2)
  819. def test_runsUntilSyncCallback(self):
  820. """
  821. L{task.react} returns quickly if the L{Deferred} returned by the
  822. function it is passed has already been called back at the time it is
  823. returned.
  824. """
  825. def main(reactor):
  826. return defer.succeed(None)
  827. r = _FakeReactor()
  828. exitError = self.assertRaises(
  829. SystemExit, task.react, main, _reactor=r)
  830. self.assertEqual(0, exitError.code)
  831. self.assertEqual(r.seconds(), 0)
  832. def test_runsUntilAsyncErrback(self):
  833. """
  834. L{task.react} runs the reactor until the L{defer.Deferred} returned by
  835. the function it is passed is errbacked, then it stops the reactor and
  836. reports the error.
  837. """
  838. class ExpectedException(Exception):
  839. pass
  840. def main(reactor):
  841. finished = defer.Deferred()
  842. reactor.callLater(1, finished.errback, ExpectedException())
  843. return finished
  844. r = _FakeReactor()
  845. exitError = self.assertRaises(
  846. SystemExit, task.react, main, _reactor=r)
  847. self.assertEqual(1, exitError.code)
  848. errors = self.flushLoggedErrors(ExpectedException)
  849. self.assertEqual(len(errors), 1)
  850. def test_runsUntilSyncErrback(self):
  851. """
  852. L{task.react} returns quickly if the L{defer.Deferred} returned by the
  853. function it is passed has already been errbacked at the time it is
  854. returned.
  855. """
  856. class ExpectedException(Exception):
  857. pass
  858. def main(reactor):
  859. return defer.fail(ExpectedException())
  860. r = _FakeReactor()
  861. exitError = self.assertRaises(
  862. SystemExit, task.react, main, _reactor=r)
  863. self.assertEqual(1, exitError.code)
  864. self.assertEqual(r.seconds(), 0)
  865. errors = self.flushLoggedErrors(ExpectedException)
  866. self.assertEqual(len(errors), 1)
  867. def test_singleStopCallback(self):
  868. """
  869. L{task.react} doesn't try to stop the reactor if the L{defer.Deferred}
  870. the function it is passed is called back after the reactor has already
  871. been stopped.
  872. """
  873. def main(reactor):
  874. reactor.callLater(1, reactor.stop)
  875. finished = defer.Deferred()
  876. reactor.addSystemEventTrigger(
  877. 'during', 'shutdown', finished.callback, None)
  878. return finished
  879. r = _FakeReactor()
  880. exitError = self.assertRaises(
  881. SystemExit, task.react, main, _reactor=r)
  882. self.assertEqual(r.seconds(), 1)
  883. self.assertEqual(0, exitError.code)
  884. def test_singleStopErrback(self):
  885. """
  886. L{task.react} doesn't try to stop the reactor if the L{defer.Deferred}
  887. the function it is passed is errbacked after the reactor has already
  888. been stopped.
  889. """
  890. class ExpectedException(Exception):
  891. pass
  892. def main(reactor):
  893. reactor.callLater(1, reactor.stop)
  894. finished = defer.Deferred()
  895. reactor.addSystemEventTrigger(
  896. 'during', 'shutdown', finished.errback, ExpectedException())
  897. return finished
  898. r = _FakeReactor()
  899. exitError = self.assertRaises(
  900. SystemExit, task.react, main, _reactor=r)
  901. self.assertEqual(1, exitError.code)
  902. self.assertEqual(r.seconds(), 1)
  903. errors = self.flushLoggedErrors(ExpectedException)
  904. self.assertEqual(len(errors), 1)
  905. def test_arguments(self):
  906. """
  907. L{task.react} passes the elements of the list it is passed as
  908. positional arguments to the function it is passed.
  909. """
  910. args = []
  911. def main(reactor, x, y, z):
  912. args.extend((x, y, z))
  913. return defer.succeed(None)
  914. r = _FakeReactor()
  915. exitError = self.assertRaises(
  916. SystemExit, task.react, main, [1, 2, 3], _reactor=r)
  917. self.assertEqual(0, exitError.code)
  918. self.assertEqual(args, [1, 2, 3])
  919. def test_defaultReactor(self):
  920. """
  921. L{twisted.internet.reactor} is used if no reactor argument is passed to
  922. L{task.react}.
  923. """
  924. def main(reactor):
  925. self.passedReactor = reactor
  926. return defer.succeed(None)
  927. reactor = _FakeReactor()
  928. with NoReactor():
  929. installReactor(reactor)
  930. exitError = self.assertRaises(SystemExit, task.react, main, [])
  931. self.assertEqual(0, exitError.code)
  932. self.assertIs(reactor, self.passedReactor)
  933. def test_exitWithDefinedCode(self):
  934. """
  935. L{task.react} forwards the exit code specified by the C{SystemExit}
  936. error returned by the passed function, if any.
  937. """
  938. def main(reactor):
  939. return defer.fail(SystemExit(23))
  940. r = _FakeReactor()
  941. exitError = self.assertRaises(
  942. SystemExit, task.react, main, [], _reactor=r)
  943. self.assertEqual(23, exitError.code)
  944. def test_synchronousStop(self):
  945. """
  946. L{task.react} handles when the reactor is stopped just before the
  947. returned L{Deferred} fires.
  948. """
  949. def main(reactor):
  950. d = defer.Deferred()
  951. def stop():
  952. reactor.stop()
  953. d.callback(None)
  954. reactor.callWhenRunning(stop)
  955. return d
  956. r = _FakeReactor()
  957. exitError = self.assertRaises(
  958. SystemExit, task.react, main, [], _reactor=r)
  959. self.assertEqual(0, exitError.code)
  960. def test_asynchronousStop(self):
  961. """
  962. L{task.react} handles when the reactor is stopped and the
  963. returned L{Deferred} doesn't fire.
  964. """
  965. def main(reactor):
  966. reactor.callLater(1, reactor.stop)
  967. return defer.Deferred()
  968. r = _FakeReactor()
  969. exitError = self.assertRaises(
  970. SystemExit, task.react, main, [], _reactor=r)
  971. self.assertEqual(0, exitError.code)