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_tpfile.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. from twisted.trial import unittest
  4. from twisted.protocols import loopback
  5. from twisted.protocols import basic
  6. from twisted.internet import protocol, abstract
  7. from io import BytesIO
  8. class BufferingServer(protocol.Protocol):
  9. buffer = b''
  10. def dataReceived(self, data):
  11. self.buffer += data
  12. class FileSendingClient(protocol.Protocol):
  13. def __init__(self, f):
  14. self.f = f
  15. def connectionMade(self):
  16. s = basic.FileSender()
  17. d = s.beginFileTransfer(self.f, self.transport, lambda x: x)
  18. d.addCallback(lambda r: self.transport.loseConnection())
  19. class FileSenderTests(unittest.TestCase):
  20. def testSendingFile(self):
  21. testStr = b'xyz' * 100 + b'abc' * 100 + b'123' * 100
  22. s = BufferingServer()
  23. c = FileSendingClient(BytesIO(testStr))
  24. d = loopback.loopbackTCP(s, c)
  25. d.addCallback(lambda x : self.assertEqual(s.buffer, testStr))
  26. return d
  27. def testSendingEmptyFile(self):
  28. fileSender = basic.FileSender()
  29. consumer = abstract.FileDescriptor()
  30. consumer.connected = 1
  31. emptyFile = BytesIO(b'')
  32. d = fileSender.beginFileTransfer(emptyFile, consumer, lambda x: x)
  33. # The producer will be immediately exhausted, and so immediately
  34. # unregistered
  35. self.assertIsNone(consumer.producer)
  36. # Which means the Deferred from FileSender should have been called
  37. self.assertTrue(d.called,
  38. 'producer unregistered with deferred being called')