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_policies.py 32KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test code for policies.
  5. """
  6. import builtins
  7. from io import StringIO
  8. from zope.interface import Interface, implementedBy, implementer
  9. from twisted.internet import address, defer, protocol, reactor, task
  10. from twisted.protocols import policies
  11. from twisted.test.proto_helpers import StringTransport, StringTransportWithDisconnection
  12. from twisted.trial import unittest
  13. class SimpleProtocol(protocol.Protocol):
  14. connected = disconnected = 0
  15. buffer = b""
  16. def __init__(self):
  17. self.dConnected = defer.Deferred()
  18. self.dDisconnected = defer.Deferred()
  19. def connectionMade(self):
  20. self.connected = 1
  21. self.dConnected.callback("")
  22. def connectionLost(self, reason):
  23. self.disconnected = 1
  24. self.dDisconnected.callback("")
  25. def dataReceived(self, data):
  26. self.buffer += data
  27. class SillyFactory(protocol.ClientFactory):
  28. def __init__(self, p):
  29. self.p = p
  30. def buildProtocol(self, addr):
  31. return self.p
  32. class EchoProtocol(protocol.Protocol):
  33. paused = False
  34. def pauseProducing(self):
  35. self.paused = True
  36. def resumeProducing(self):
  37. self.paused = False
  38. def stopProducing(self):
  39. pass
  40. def dataReceived(self, data):
  41. self.transport.write(data)
  42. class Server(protocol.ServerFactory):
  43. """
  44. A simple server factory using L{EchoProtocol}.
  45. """
  46. protocol = EchoProtocol
  47. class TestableThrottlingFactory(policies.ThrottlingFactory):
  48. """
  49. L{policies.ThrottlingFactory} using a L{task.Clock} for tests.
  50. """
  51. def __init__(self, clock, *args, **kwargs):
  52. """
  53. @param clock: object providing a callLater method that can be used
  54. for tests.
  55. @type clock: C{task.Clock} or alike.
  56. """
  57. policies.ThrottlingFactory.__init__(self, *args, **kwargs)
  58. self.clock = clock
  59. def callLater(self, period, func):
  60. """
  61. Forward to the testable clock.
  62. """
  63. return self.clock.callLater(period, func)
  64. class TestableTimeoutFactory(policies.TimeoutFactory):
  65. """
  66. L{policies.TimeoutFactory} using a L{task.Clock} for tests.
  67. """
  68. def __init__(self, clock, *args, **kwargs):
  69. """
  70. @param clock: object providing a callLater method that can be used
  71. for tests.
  72. @type clock: C{task.Clock} or alike.
  73. """
  74. policies.TimeoutFactory.__init__(self, *args, **kwargs)
  75. self.clock = clock
  76. def callLater(self, period, func):
  77. """
  78. Forward to the testable clock.
  79. """
  80. return self.clock.callLater(period, func)
  81. class WrapperTests(unittest.TestCase):
  82. """
  83. Tests for L{WrappingFactory} and L{ProtocolWrapper}.
  84. """
  85. def test_protocolFactoryAttribute(self):
  86. """
  87. Make sure protocol.factory is the wrapped factory, not the wrapping
  88. factory.
  89. """
  90. f = Server()
  91. wf = policies.WrappingFactory(f)
  92. p = wf.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 35))
  93. self.assertIs(p.wrappedProtocol.factory, f)
  94. def test_transportInterfaces(self):
  95. """
  96. The transport wrapper passed to the wrapped protocol's
  97. C{makeConnection} provides the same interfaces as are provided by the
  98. original transport.
  99. """
  100. class IStubTransport(Interface):
  101. pass
  102. @implementer(IStubTransport)
  103. class StubTransport:
  104. pass
  105. # Looking up what ProtocolWrapper implements also mutates the class.
  106. # It adds __implemented__ and __providedBy__ attributes to it. These
  107. # prevent __getattr__ from causing the IStubTransport.providedBy call
  108. # below from returning True. If, by accident, nothing else causes
  109. # these attributes to be added to ProtocolWrapper, the test will pass,
  110. # but the interface will only be provided until something does trigger
  111. # their addition. So we just trigger it right now to be sure.
  112. implementedBy(policies.ProtocolWrapper)
  113. proto = protocol.Protocol()
  114. wrapper = policies.ProtocolWrapper(policies.WrappingFactory(None), proto)
  115. wrapper.makeConnection(StubTransport())
  116. self.assertTrue(IStubTransport.providedBy(proto.transport))
  117. def test_factoryLogPrefix(self):
  118. """
  119. L{WrappingFactory.logPrefix} is customized to mention both the original
  120. factory and the wrapping factory.
  121. """
  122. server = Server()
  123. factory = policies.WrappingFactory(server)
  124. self.assertEqual("Server (WrappingFactory)", factory.logPrefix())
  125. def test_factoryLogPrefixFallback(self):
  126. """
  127. If the wrapped factory doesn't have a L{logPrefix} method,
  128. L{WrappingFactory.logPrefix} falls back to the factory class name.
  129. """
  130. class NoFactory:
  131. pass
  132. server = NoFactory()
  133. factory = policies.WrappingFactory(server)
  134. self.assertEqual("NoFactory (WrappingFactory)", factory.logPrefix())
  135. def test_protocolLogPrefix(self):
  136. """
  137. L{ProtocolWrapper.logPrefix} is customized to mention both the original
  138. protocol and the wrapper.
  139. """
  140. server = Server()
  141. factory = policies.WrappingFactory(server)
  142. protocol = factory.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 35))
  143. self.assertEqual("EchoProtocol (ProtocolWrapper)", protocol.logPrefix())
  144. def test_protocolLogPrefixFallback(self):
  145. """
  146. If the wrapped protocol doesn't have a L{logPrefix} method,
  147. L{ProtocolWrapper.logPrefix} falls back to the protocol class name.
  148. """
  149. class NoProtocol:
  150. pass
  151. server = Server()
  152. server.protocol = NoProtocol
  153. factory = policies.WrappingFactory(server)
  154. protocol = factory.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 35))
  155. self.assertEqual("NoProtocol (ProtocolWrapper)", protocol.logPrefix())
  156. def _getWrapper(self):
  157. """
  158. Return L{policies.ProtocolWrapper} that has been connected to a
  159. L{StringTransport}.
  160. """
  161. wrapper = policies.ProtocolWrapper(
  162. policies.WrappingFactory(Server()), protocol.Protocol()
  163. )
  164. transport = StringTransport()
  165. wrapper.makeConnection(transport)
  166. return wrapper
  167. def test_getHost(self):
  168. """
  169. L{policies.ProtocolWrapper.getHost} calls C{getHost} on the underlying
  170. transport.
  171. """
  172. wrapper = self._getWrapper()
  173. self.assertEqual(wrapper.getHost(), wrapper.transport.getHost())
  174. def test_getPeer(self):
  175. """
  176. L{policies.ProtocolWrapper.getPeer} calls C{getPeer} on the underlying
  177. transport.
  178. """
  179. wrapper = self._getWrapper()
  180. self.assertEqual(wrapper.getPeer(), wrapper.transport.getPeer())
  181. def test_registerProducer(self):
  182. """
  183. L{policies.ProtocolWrapper.registerProducer} calls C{registerProducer}
  184. on the underlying transport.
  185. """
  186. wrapper = self._getWrapper()
  187. producer = object()
  188. wrapper.registerProducer(producer, True)
  189. self.assertIs(wrapper.transport.producer, producer)
  190. self.assertTrue(wrapper.transport.streaming)
  191. def test_unregisterProducer(self):
  192. """
  193. L{policies.ProtocolWrapper.unregisterProducer} calls
  194. C{unregisterProducer} on the underlying transport.
  195. """
  196. wrapper = self._getWrapper()
  197. producer = object()
  198. wrapper.registerProducer(producer, True)
  199. wrapper.unregisterProducer()
  200. self.assertIsNone(wrapper.transport.producer)
  201. self.assertIsNone(wrapper.transport.streaming)
  202. def test_stopConsuming(self):
  203. """
  204. L{policies.ProtocolWrapper.stopConsuming} calls C{stopConsuming} on
  205. the underlying transport.
  206. """
  207. wrapper = self._getWrapper()
  208. result = []
  209. wrapper.transport.stopConsuming = lambda: result.append(True)
  210. wrapper.stopConsuming()
  211. self.assertEqual(result, [True])
  212. def test_startedConnecting(self):
  213. """
  214. L{policies.WrappingFactory.startedConnecting} calls
  215. C{startedConnecting} on the underlying factory.
  216. """
  217. result = []
  218. class Factory:
  219. def startedConnecting(self, connector):
  220. result.append(connector)
  221. wrapper = policies.WrappingFactory(Factory())
  222. connector = object()
  223. wrapper.startedConnecting(connector)
  224. self.assertEqual(result, [connector])
  225. def test_clientConnectionLost(self):
  226. """
  227. L{policies.WrappingFactory.clientConnectionLost} calls
  228. C{clientConnectionLost} on the underlying factory.
  229. """
  230. result = []
  231. class Factory:
  232. def clientConnectionLost(self, connector, reason):
  233. result.append((connector, reason))
  234. wrapper = policies.WrappingFactory(Factory())
  235. connector = object()
  236. reason = object()
  237. wrapper.clientConnectionLost(connector, reason)
  238. self.assertEqual(result, [(connector, reason)])
  239. def test_clientConnectionFailed(self):
  240. """
  241. L{policies.WrappingFactory.clientConnectionFailed} calls
  242. C{clientConnectionFailed} on the underlying factory.
  243. """
  244. result = []
  245. class Factory:
  246. def clientConnectionFailed(self, connector, reason):
  247. result.append((connector, reason))
  248. wrapper = policies.WrappingFactory(Factory())
  249. connector = object()
  250. reason = object()
  251. wrapper.clientConnectionFailed(connector, reason)
  252. self.assertEqual(result, [(connector, reason)])
  253. def test_breakReferenceCycle(self):
  254. """
  255. L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to
  256. C{None} in order to break reference cycle between wrapper and wrapped
  257. protocols.
  258. :return:
  259. """
  260. wrapper = policies.ProtocolWrapper(
  261. policies.WrappingFactory(Server()), protocol.Protocol()
  262. )
  263. transport = StringTransportWithDisconnection()
  264. transport.protocol = wrapper
  265. wrapper.makeConnection(transport)
  266. self.assertIsNotNone(wrapper.wrappedProtocol)
  267. transport.loseConnection()
  268. self.assertIsNone(wrapper.wrappedProtocol)
  269. class WrappingFactory(policies.WrappingFactory):
  270. def protocol(self, f, p):
  271. return p
  272. def startFactory(self):
  273. policies.WrappingFactory.startFactory(self)
  274. self.deferred.callback(None)
  275. class ThrottlingTests(unittest.TestCase):
  276. """
  277. Tests for L{policies.ThrottlingFactory}.
  278. """
  279. def test_limit(self):
  280. """
  281. Full test using a custom server limiting number of connections.
  282. FIXME: https://twistedmatrix.com/trac/ticket/10012
  283. This is a flaky test.
  284. """
  285. server = Server()
  286. c1, c2, c3, c4 = (SimpleProtocol() for i in range(4))
  287. tServer = policies.ThrottlingFactory(server, 2)
  288. wrapTServer = WrappingFactory(tServer)
  289. wrapTServer.deferred = defer.Deferred()
  290. # Start listening
  291. p = reactor.listenTCP(0, wrapTServer, interface="127.0.0.1")
  292. n = p.getHost().port
  293. def _connect123(results):
  294. reactor.connectTCP("127.0.0.1", n, SillyFactory(c1))
  295. c1.dConnected.addCallback(
  296. lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c2))
  297. )
  298. c2.dConnected.addCallback(
  299. lambda r: reactor.connectTCP("127.0.0.1", n, SillyFactory(c3))
  300. )
  301. return c3.dDisconnected
  302. def _check123(results):
  303. self.assertEqual([c.connected for c in (c1, c2, c3)], [1, 1, 1])
  304. self.assertEqual([c.disconnected for c in (c1, c2, c3)], [0, 0, 1])
  305. self.assertEqual(len(tServer.protocols.keys()), 2)
  306. return results
  307. def _lose1(results):
  308. # disconnect one protocol and now another should be able to connect
  309. c1.transport.loseConnection()
  310. return c1.dDisconnected
  311. def _connect4(results):
  312. reactor.connectTCP("127.0.0.1", n, SillyFactory(c4))
  313. return c4.dConnected
  314. def _check4(results):
  315. self.assertEqual(c4.connected, 1)
  316. self.assertEqual(c4.disconnected, 0)
  317. return results
  318. def _cleanup(results):
  319. for c in c2, c4:
  320. c.transport.loseConnection()
  321. return defer.DeferredList(
  322. [
  323. defer.maybeDeferred(p.stopListening),
  324. c2.dDisconnected,
  325. c4.dDisconnected,
  326. ]
  327. )
  328. wrapTServer.deferred.addCallback(_connect123)
  329. wrapTServer.deferred.addCallback(_check123)
  330. wrapTServer.deferred.addCallback(_lose1)
  331. wrapTServer.deferred.addCallback(_connect4)
  332. wrapTServer.deferred.addCallback(_check4)
  333. wrapTServer.deferred.addCallback(_cleanup)
  334. return wrapTServer.deferred
  335. def test_writeSequence(self):
  336. """
  337. L{ThrottlingProtocol.writeSequence} is called on the underlying factory.
  338. """
  339. server = Server()
  340. tServer = TestableThrottlingFactory(task.Clock(), server)
  341. protocol = tServer.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 0))
  342. transport = StringTransportWithDisconnection()
  343. transport.protocol = protocol
  344. protocol.makeConnection(transport)
  345. protocol.writeSequence([b"bytes"] * 4)
  346. self.assertEqual(transport.value(), b"bytesbytesbytesbytes")
  347. self.assertEqual(tServer.writtenThisSecond, 20)
  348. def test_writeLimit(self):
  349. """
  350. Check the writeLimit parameter: write data, and check for the pause
  351. status.
  352. """
  353. server = Server()
  354. tServer = TestableThrottlingFactory(task.Clock(), server, writeLimit=10)
  355. port = tServer.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 0))
  356. tr = StringTransportWithDisconnection()
  357. tr.protocol = port
  358. port.makeConnection(tr)
  359. port.producer = port.wrappedProtocol
  360. port.dataReceived(b"0123456789")
  361. port.dataReceived(b"abcdefghij")
  362. self.assertEqual(tr.value(), b"0123456789abcdefghij")
  363. self.assertEqual(tServer.writtenThisSecond, 20)
  364. self.assertFalse(port.wrappedProtocol.paused)
  365. # at this point server should've written 20 bytes, 10 bytes
  366. # above the limit so writing should be paused around 1 second
  367. # from 'now', and resumed a second after that
  368. tServer.clock.advance(1.05)
  369. self.assertEqual(tServer.writtenThisSecond, 0)
  370. self.assertTrue(port.wrappedProtocol.paused)
  371. tServer.clock.advance(1.05)
  372. self.assertEqual(tServer.writtenThisSecond, 0)
  373. self.assertFalse(port.wrappedProtocol.paused)
  374. def test_readLimit(self):
  375. """
  376. Check the readLimit parameter: read data and check for the pause
  377. status.
  378. """
  379. server = Server()
  380. tServer = TestableThrottlingFactory(task.Clock(), server, readLimit=10)
  381. port = tServer.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 0))
  382. tr = StringTransportWithDisconnection()
  383. tr.protocol = port
  384. port.makeConnection(tr)
  385. port.dataReceived(b"0123456789")
  386. port.dataReceived(b"abcdefghij")
  387. self.assertEqual(tr.value(), b"0123456789abcdefghij")
  388. self.assertEqual(tServer.readThisSecond, 20)
  389. tServer.clock.advance(1.05)
  390. self.assertEqual(tServer.readThisSecond, 0)
  391. self.assertEqual(tr.producerState, "paused")
  392. tServer.clock.advance(1.05)
  393. self.assertEqual(tServer.readThisSecond, 0)
  394. self.assertEqual(tr.producerState, "producing")
  395. tr.clear()
  396. port.dataReceived(b"0123456789")
  397. port.dataReceived(b"abcdefghij")
  398. self.assertEqual(tr.value(), b"0123456789abcdefghij")
  399. self.assertEqual(tServer.readThisSecond, 20)
  400. tServer.clock.advance(1.05)
  401. self.assertEqual(tServer.readThisSecond, 0)
  402. self.assertEqual(tr.producerState, "paused")
  403. tServer.clock.advance(1.05)
  404. self.assertEqual(tServer.readThisSecond, 0)
  405. self.assertEqual(tr.producerState, "producing")
  406. class TimeoutProtocolTests(unittest.TestCase):
  407. """
  408. Tests for L{policies.TimeoutProtocol}.
  409. """
  410. def getProtocolAndClock(self):
  411. """
  412. Helper to set up an already connected protocol to be tested.
  413. @return: A new protocol with its attached clock.
  414. @rtype: Tuple of (L{policies.TimeoutProtocol}, L{task.Clock})
  415. """
  416. clock = task.Clock()
  417. wrappedFactory = protocol.ServerFactory()
  418. wrappedFactory.protocol = SimpleProtocol
  419. factory = TestableTimeoutFactory(clock, wrappedFactory, None)
  420. proto = factory.buildProtocol(address.IPv4Address("TCP", "127.0.0.1", 12345))
  421. transport = StringTransportWithDisconnection()
  422. transport.protocol = proto
  423. proto.makeConnection(transport)
  424. return (proto, clock)
  425. def test_cancelTimeout(self):
  426. """
  427. Will cancel the ongoing timeout.
  428. """
  429. sut, clock = self.getProtocolAndClock()
  430. sut.setTimeout(3)
  431. # Check some pre-execution state.
  432. self.assertIsNotNone(sut.timeoutCall)
  433. self.assertFalse(sut.wrappedProtocol.disconnected)
  434. clock.advance(1)
  435. sut.cancelTimeout()
  436. self.assertIsNone(sut.timeoutCall)
  437. # After timeout should have pass, nothing happens and the transport
  438. # is still connected.
  439. clock.advance(3)
  440. self.assertFalse(sut.wrappedProtocol.disconnected)
  441. def test_cancelTimeoutNoTimeout(self):
  442. """
  443. Does nothing if no timeout is already set.
  444. """
  445. sut, clock = self.getProtocolAndClock()
  446. self.assertIsNone(sut.timeoutCall)
  447. sut.cancelTimeout()
  448. # Protocol is still connected.
  449. self.assertFalse(sut.wrappedProtocol.disconnected)
  450. def test_cancelTimeoutAlreadyCalled(self):
  451. """
  452. Does nothing if no timeout is already reached.
  453. """
  454. sut, clock = self.getProtocolAndClock()
  455. wrappedProto = sut.wrappedProtocol
  456. sut.setTimeout(3)
  457. # Trigger the timeout call.
  458. clock.advance(3)
  459. self.assertTrue(wrappedProto.disconnected)
  460. # No error is raised when trying to cancel it.
  461. sut.cancelTimeout()
  462. def test_cancelTimeoutAlreadyCancelled(self):
  463. """
  464. Does nothing if the timeout is cancelled from another part.
  465. Ex from another thread.
  466. """
  467. sut, clock = self.getProtocolAndClock()
  468. sut.setTimeout(3)
  469. # Manually cancel this
  470. sut.timeoutCall.cancel()
  471. # No error is raised when trying to cancel it.
  472. sut.cancelTimeout()
  473. # The connection state is not touched.
  474. self.assertFalse(sut.wrappedProtocol.disconnected)
  475. class TimeoutFactoryTests(unittest.TestCase):
  476. """
  477. Tests for L{policies.TimeoutFactory}.
  478. """
  479. def setUp(self):
  480. """
  481. Create a testable, deterministic clock, and a set of
  482. server factory/protocol/transport.
  483. """
  484. self.clock = task.Clock()
  485. wrappedFactory = protocol.ServerFactory()
  486. wrappedFactory.protocol = SimpleProtocol
  487. self.factory = TestableTimeoutFactory(self.clock, wrappedFactory, 3)
  488. self.proto = self.factory.buildProtocol(
  489. address.IPv4Address("TCP", "127.0.0.1", 12345)
  490. )
  491. self.transport = StringTransportWithDisconnection()
  492. self.transport.protocol = self.proto
  493. self.proto.makeConnection(self.transport)
  494. self.wrappedProto = self.proto.wrappedProtocol
  495. def test_timeout(self):
  496. """
  497. Make sure that when a TimeoutFactory accepts a connection, it will
  498. time out that connection if no data is read or written within the
  499. timeout period.
  500. """
  501. # Let almost 3 time units pass
  502. self.clock.pump([0.0, 0.5, 1.0, 1.0, 0.4])
  503. self.assertFalse(self.wrappedProto.disconnected)
  504. # Now let the timer elapse
  505. self.clock.pump([0.0, 0.2])
  506. self.assertTrue(self.wrappedProto.disconnected)
  507. def test_sendAvoidsTimeout(self):
  508. """
  509. Make sure that writing data to a transport from a protocol
  510. constructed by a TimeoutFactory resets the timeout countdown.
  511. """
  512. # Let half the countdown period elapse
  513. self.clock.pump([0.0, 0.5, 1.0])
  514. self.assertFalse(self.wrappedProto.disconnected)
  515. # Send some data (self.proto is the /real/ proto's transport, so this
  516. # is the write that gets called)
  517. self.proto.write(b"bytes bytes bytes")
  518. # More time passes, putting us past the original timeout
  519. self.clock.pump([0.0, 1.0, 1.0])
  520. self.assertFalse(self.wrappedProto.disconnected)
  521. # Make sure writeSequence delays timeout as well
  522. self.proto.writeSequence([b"bytes"] * 3)
  523. # Tick tock
  524. self.clock.pump([0.0, 1.0, 1.0])
  525. self.assertFalse(self.wrappedProto.disconnected)
  526. # Don't write anything more, just let the timeout expire
  527. self.clock.pump([0.0, 2.0])
  528. self.assertTrue(self.wrappedProto.disconnected)
  529. def test_receiveAvoidsTimeout(self):
  530. """
  531. Make sure that receiving data also resets the timeout countdown.
  532. """
  533. # Let half the countdown period elapse
  534. self.clock.pump([0.0, 1.0, 0.5])
  535. self.assertFalse(self.wrappedProto.disconnected)
  536. # Some bytes arrive, they should reset the counter
  537. self.proto.dataReceived(b"bytes bytes bytes")
  538. # We pass the original timeout
  539. self.clock.pump([0.0, 1.0, 1.0])
  540. self.assertFalse(self.wrappedProto.disconnected)
  541. # Nothing more arrives though, the new timeout deadline is passed,
  542. # the connection should be dropped.
  543. self.clock.pump([0.0, 1.0, 1.0])
  544. self.assertTrue(self.wrappedProto.disconnected)
  545. class TimeoutTester(protocol.Protocol, policies.TimeoutMixin):
  546. """
  547. A testable protocol with timeout facility.
  548. @ivar timedOut: set to C{True} if a timeout has been detected.
  549. @type timedOut: C{bool}
  550. """
  551. timeOut = 3
  552. timedOut = False
  553. def __init__(self, clock):
  554. """
  555. Initialize the protocol with a C{task.Clock} object.
  556. """
  557. self.clock = clock
  558. def connectionMade(self):
  559. """
  560. Upon connection, set the timeout.
  561. """
  562. self.setTimeout(self.timeOut)
  563. def dataReceived(self, data):
  564. """
  565. Reset the timeout on data.
  566. """
  567. self.resetTimeout()
  568. protocol.Protocol.dataReceived(self, data)
  569. def connectionLost(self, reason=None):
  570. """
  571. On connection lost, cancel all timeout operations.
  572. """
  573. self.setTimeout(None)
  574. def timeoutConnection(self):
  575. """
  576. Flags the timedOut variable to indicate the timeout of the connection.
  577. """
  578. self.timedOut = True
  579. def callLater(self, timeout, func, *args, **kwargs):
  580. """
  581. Override callLater to use the deterministic clock.
  582. """
  583. return self.clock.callLater(timeout, func, *args, **kwargs)
  584. class TimeoutMixinTests(unittest.TestCase):
  585. """
  586. Tests for L{policies.TimeoutMixin}.
  587. """
  588. def setUp(self):
  589. """
  590. Create a testable, deterministic clock and a C{TimeoutTester} instance.
  591. """
  592. self.clock = task.Clock()
  593. self.proto = TimeoutTester(self.clock)
  594. def test_overriddenCallLater(self):
  595. """
  596. Test that the callLater of the clock is used instead of
  597. L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
  598. """
  599. self.proto.setTimeout(10)
  600. self.assertEqual(len(self.clock.calls), 1)
  601. def test_timeout(self):
  602. """
  603. Check that the protocol does timeout at the time specified by its
  604. C{timeOut} attribute.
  605. """
  606. self.proto.makeConnection(StringTransport())
  607. # timeOut value is 3
  608. self.clock.pump([0, 0.5, 1.0, 1.0])
  609. self.assertFalse(self.proto.timedOut)
  610. self.clock.pump([0, 1.0])
  611. self.assertTrue(self.proto.timedOut)
  612. def test_noTimeout(self):
  613. """
  614. Check that receiving data is delaying the timeout of the connection.
  615. """
  616. self.proto.makeConnection(StringTransport())
  617. self.clock.pump([0, 0.5, 1.0, 1.0])
  618. self.assertFalse(self.proto.timedOut)
  619. self.proto.dataReceived(b"hello there")
  620. self.clock.pump([0, 1.0, 1.0, 0.5])
  621. self.assertFalse(self.proto.timedOut)
  622. self.clock.pump([0, 1.0])
  623. self.assertTrue(self.proto.timedOut)
  624. def test_resetTimeout(self):
  625. """
  626. Check that setting a new value for timeout cancel the previous value
  627. and install a new timeout.
  628. """
  629. self.proto.timeOut = None
  630. self.proto.makeConnection(StringTransport())
  631. self.proto.setTimeout(1)
  632. self.assertEqual(self.proto.timeOut, 1)
  633. self.clock.pump([0, 0.9])
  634. self.assertFalse(self.proto.timedOut)
  635. self.clock.pump([0, 0.2])
  636. self.assertTrue(self.proto.timedOut)
  637. def test_cancelTimeout(self):
  638. """
  639. Setting the timeout to L{None} cancel any timeout operations.
  640. """
  641. self.proto.timeOut = 5
  642. self.proto.makeConnection(StringTransport())
  643. self.proto.setTimeout(None)
  644. self.assertIsNone(self.proto.timeOut)
  645. self.clock.pump([0, 5, 5, 5])
  646. self.assertFalse(self.proto.timedOut)
  647. def test_setTimeoutReturn(self):
  648. """
  649. setTimeout should return the value of the previous timeout.
  650. """
  651. self.proto.timeOut = 5
  652. self.assertEqual(self.proto.setTimeout(10), 5)
  653. self.assertEqual(self.proto.setTimeout(None), 10)
  654. self.assertIsNone(self.proto.setTimeout(1))
  655. self.assertEqual(self.proto.timeOut, 1)
  656. # Clean up the DelayedCall
  657. self.proto.setTimeout(None)
  658. def test_setTimeoutCancleAlreadyCancelled(self):
  659. """
  660. When the timeout was already cancelled from an external place,
  661. calling setTimeout with C{None} to explicitly cancel it will clean
  662. up the timeout without raising any exception.
  663. """
  664. self.proto.setTimeout(3)
  665. # We trigger an external cancelling of that timeout, for example
  666. # when the reactor is stopped.
  667. self.clock.getDelayedCalls()[0].cancel()
  668. self.assertIsNotNone(self.proto.timeOut)
  669. self.proto.setTimeout(None)
  670. self.assertIsNone(self.proto.timeOut)
  671. class LimitTotalConnectionsFactoryTests(unittest.TestCase):
  672. """Tests for policies.LimitTotalConnectionsFactory"""
  673. def testConnectionCounting(self):
  674. # Make a basic factory
  675. factory = policies.LimitTotalConnectionsFactory()
  676. factory.protocol = protocol.Protocol
  677. # connectionCount starts at zero
  678. self.assertEqual(0, factory.connectionCount)
  679. # connectionCount increments as connections are made
  680. p1 = factory.buildProtocol(None)
  681. self.assertEqual(1, factory.connectionCount)
  682. p2 = factory.buildProtocol(None)
  683. self.assertEqual(2, factory.connectionCount)
  684. # and decrements as they are lost
  685. p1.connectionLost(None)
  686. self.assertEqual(1, factory.connectionCount)
  687. p2.connectionLost(None)
  688. self.assertEqual(0, factory.connectionCount)
  689. def testConnectionLimiting(self):
  690. # Make a basic factory with a connection limit of 1
  691. factory = policies.LimitTotalConnectionsFactory()
  692. factory.protocol = protocol.Protocol
  693. factory.connectionLimit = 1
  694. # Make a connection
  695. p = factory.buildProtocol(None)
  696. self.assertIsNotNone(p)
  697. self.assertEqual(1, factory.connectionCount)
  698. # Try to make a second connection, which will exceed the connection
  699. # limit. This should return None, because overflowProtocol is None.
  700. self.assertIsNone(factory.buildProtocol(None))
  701. self.assertEqual(1, factory.connectionCount)
  702. # Define an overflow protocol
  703. class OverflowProtocol(protocol.Protocol):
  704. def connectionMade(self):
  705. factory.overflowed = True
  706. factory.overflowProtocol = OverflowProtocol
  707. factory.overflowed = False
  708. # Try to make a second connection again, now that we have an overflow
  709. # protocol. Note that overflow connections count towards the connection
  710. # count.
  711. op = factory.buildProtocol(None)
  712. op.makeConnection(None) # to trigger connectionMade
  713. self.assertTrue(factory.overflowed)
  714. self.assertEqual(2, factory.connectionCount)
  715. # Close the connections.
  716. p.connectionLost(None)
  717. self.assertEqual(1, factory.connectionCount)
  718. op.connectionLost(None)
  719. self.assertEqual(0, factory.connectionCount)
  720. class WriteSequenceEchoProtocol(EchoProtocol):
  721. def dataReceived(self, bytes):
  722. if bytes.find(b"vector!") != -1:
  723. self.transport.writeSequence([bytes])
  724. else:
  725. EchoProtocol.dataReceived(self, bytes)
  726. class TestLoggingFactory(policies.TrafficLoggingFactory):
  727. openFile = None
  728. def open(self, name):
  729. assert self.openFile is None, "open() called too many times"
  730. self.openFile = StringIO()
  731. return self.openFile
  732. class LoggingFactoryTests(unittest.TestCase):
  733. """
  734. Tests for L{policies.TrafficLoggingFactory}.
  735. """
  736. def test_thingsGetLogged(self):
  737. """
  738. Check the output produced by L{policies.TrafficLoggingFactory}.
  739. """
  740. wrappedFactory = Server()
  741. wrappedFactory.protocol = WriteSequenceEchoProtocol
  742. t = StringTransportWithDisconnection()
  743. f = TestLoggingFactory(wrappedFactory, "test")
  744. p = f.buildProtocol(("1.2.3.4", 5678))
  745. t.protocol = p
  746. p.makeConnection(t)
  747. v = f.openFile.getvalue()
  748. self.assertIn("*", v)
  749. self.assertFalse(t.value())
  750. p.dataReceived(b"here are some bytes")
  751. v = f.openFile.getvalue()
  752. self.assertIn("C 1: {!r}".format(b"here are some bytes"), v)
  753. self.assertIn("S 1: {!r}".format(b"here are some bytes"), v)
  754. self.assertEqual(t.value(), b"here are some bytes")
  755. t.clear()
  756. p.dataReceived(b"prepare for vector! to the extreme")
  757. v = f.openFile.getvalue()
  758. self.assertIn("SV 1: {!r}".format([b"prepare for vector! to the extreme"]), v)
  759. self.assertEqual(t.value(), b"prepare for vector! to the extreme")
  760. p.loseConnection()
  761. v = f.openFile.getvalue()
  762. self.assertIn("ConnectionDone", v)
  763. def test_counter(self):
  764. """
  765. Test counter management with the resetCounter method.
  766. """
  767. wrappedFactory = Server()
  768. f = TestLoggingFactory(wrappedFactory, "test")
  769. self.assertEqual(f._counter, 0)
  770. f.buildProtocol(("1.2.3.4", 5678))
  771. self.assertEqual(f._counter, 1)
  772. # Reset log file
  773. f.openFile = None
  774. f.buildProtocol(("1.2.3.4", 5679))
  775. self.assertEqual(f._counter, 2)
  776. f.resetCounter()
  777. self.assertEqual(f._counter, 0)
  778. def test_loggingFactoryOpensLogfileAutomatically(self):
  779. """
  780. When the L{policies.TrafficLoggingFactory} builds a protocol, it
  781. automatically opens a unique log file for that protocol and attaches
  782. the logfile to the built protocol.
  783. """
  784. open_calls = []
  785. open_rvalues = []
  786. def mocked_open(*args, **kwargs):
  787. """
  788. Mock for the open call to prevent actually opening a log file.
  789. """
  790. open_calls.append((args, kwargs))
  791. io = StringIO()
  792. io.name = args[0]
  793. open_rvalues.append(io)
  794. return io
  795. self.patch(builtins, "open", mocked_open)
  796. wrappedFactory = protocol.ServerFactory()
  797. wrappedFactory.protocol = SimpleProtocol
  798. factory = policies.TrafficLoggingFactory(wrappedFactory, "test")
  799. first_proto = factory.buildProtocol(
  800. address.IPv4Address("TCP", "127.0.0.1", 12345)
  801. )
  802. second_proto = factory.buildProtocol(
  803. address.IPv4Address("TCP", "127.0.0.1", 12346)
  804. )
  805. # We expect open to be called twice, with the files passed to the
  806. # protocols.
  807. first_call = (("test-1", "w"), {})
  808. second_call = (("test-2", "w"), {})
  809. self.assertEqual([first_call, second_call], open_calls)
  810. self.assertEqual([first_proto.logfile, second_proto.logfile], open_rvalues)