Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Demonstrate that get_reader and get_writer can defer.

  • Loading branch information...
commit 9d70905eb6f3e128d3d89e0358e4a0811bf92673 1 parent b13e7d6
Gavin Panella authored

Showing 1 changed file with 52 additions and 1 deletion. Show diff stats Hide diff stats

  1. +52 1 tftp/test/test_protocol.py
53 tftp/test/test_protocol.py
... ... @@ -1,7 +1,7 @@
1 1 '''
2 2 @author: shylent
3 3 '''
4   -from tftp.backend import FilesystemSynchronousBackend
  4 +from tftp.backend import FilesystemSynchronousBackend, IReader, IWriter
5 5 from tftp.bootstrap import RemoteOriginWriteSession, RemoteOriginReadSession
6 6 from tftp.datagram import (WRQDatagram, TFTPDatagramFactory, split_opcode,
7 7 ERR_ILLEGAL_OP, RRQDatagram, ERR_ACCESS_VIOLATION, ERR_FILE_EXISTS,
@@ -192,3 +192,54 @@ def cb(ign):
192 192 def tearDown(self):
193 193 self.tftp.transport.stopListening()
194 194 self.client.transport.stopListening()
  195 +
  196 +
  197 +class FilesystemAsyncBackend(FilesystemSynchronousBackend):
  198 +
  199 + def __init__(self, base_path, clock):
  200 + super(FilesystemAsyncBackend, self).__init__(
  201 + base_path, can_read=True, can_write=True)
  202 + self.clock = clock
  203 +
  204 + def get_reader(self, file_name):
  205 + reader = super(FilesystemAsyncBackend, self).get_reader(file_name)
  206 + d = Deferred()
  207 + self.clock.callLater(0, d.callback, reader)
  208 + return d
  209 +
  210 + def get_writer(self, file_name):
  211 + writer = super(FilesystemAsyncBackend, self).get_writer(file_name)
  212 + d = Deferred()
  213 + self.clock.callLater(0, d.callback, writer)
  214 + return d
  215 +
  216 +
  217 +class SuccessfulAsyncDispatch(unittest.TestCase):
  218 +
  219 + def setUp(self):
  220 + self.clock = Clock()
  221 + self.tmp_dir_path = tempfile.mkdtemp()
  222 + with FilePath(self.tmp_dir_path).child('nonempty').open('w') as fd:
  223 + fd.write('Something uninteresting')
  224 + self.backend = FilesystemAsyncBackend(self.tmp_dir_path, self.clock)
  225 + self.tftp = TFTP(self.backend, self.clock)
  226 +
  227 + def test_get_reader_can_defer(self):
  228 + rrq_datagram = RRQDatagram('nonempty', 'NetASCiI', {})
  229 + rrq_addr = ('127.0.0.1', 1069)
  230 + rrq_mode = "octet"
  231 + d = self.tftp._startSession(rrq_datagram, rrq_addr, rrq_mode)
  232 + self.assertFalse(d.called)
  233 + self.clock.advance(1)
  234 + self.assertTrue(d.called)
  235 + self.assertTrue(IReader.providedBy(d.result.backend))
  236 +
  237 + def test_get_writer_can_defer(self):
  238 + wrq_datagram = WRQDatagram('foobar', 'NetASCiI', {})
  239 + wrq_addr = ('127.0.0.1', 1069)
  240 + wrq_mode = "octet"
  241 + d = self.tftp._startSession(wrq_datagram, wrq_addr, wrq_mode)
  242 + self.assertFalse(d.called)
  243 + self.clock.advance(1)
  244 + self.assertTrue(d.called)
  245 + self.assertTrue(IWriter.providedBy(d.result.backend))

0 comments on commit 9d70905

Please sign in to comment.
Something went wrong with that request. Please try again.