Ejemplo n.º 1
0
 def test_read_buffer_size(self):
     got = []
     def callback(data):
         got.append(data)
     loop = self.make_event_loop()
     sock1, sock2 = stream.socketpair()
     reader = stream.FDReader(loop, sock1, callback, lambda: None,
                              get_buffer_size=lambda: 2)
     os.write(sock2.fileno(), "hello")
     loop.once_safely()
     self.assertEquals(got, ["he"])
     loop.once_safely()
     self.assertEquals(got, ["he", "ll"])
     loop.once_safely()
     self.assertEquals(got, ["he", "ll", "o"])
Ejemplo n.º 2
0
 def test_end_of_stream_multiple_reads(self):
     got = []
     def callback(data):
         got.append(data)
     def eof_callback():
         got.append("EOF")
     loop = self.make_event_loop()
     pipe_read, pipe_write = stream.socketpair()
     reader = stream.FDReader(loop, pipe_read, callback, eof_callback,
                              get_buffer_size=lambda: 2)
     self.assertTrue(loop.will_block())
     os.write(pipe_write.fileno(), "hello world")
     del pipe_write
     loop.once_safely()
     self.assertEquals(got, ["he", "llo world", "EOF"])
     self.assertTrue(not loop.is_listening())
Ejemplo n.º 3
0
 def test_aborting_reading(self):
     got = []
     loop = self.make_event_loop()
     pipe_read, pipe_write = stream.socketpair()
     os.write(pipe_write.fileno(), "hello world")
     reader = stream.FDReader(loop, pipe_read,
                              lambda data: got.append(data),
                              lambda: got.append("EOF"))
     del pipe_read
     # Calling finish() stops the reader from reading any more.
     reader.finish()
     self.assertFalse(loop.is_listening())
     self.assertEquals(got, ["EOF"])
     # The reader should drop its FD so the pipe should now be broken.
     try:
         os.write(pipe_write.fileno(), "hello world")
     except OSError, exn:
         self.assertEquals(exn.errno, errno.EPIPE)
Ejemplo n.º 4
0
 def test_reading_with_econnreset(self):
     # If A has written data when B has not read and B has closed
     # its socket, when A tries to read it will get ECONNRESET.
     got = []
     def callback(data):
         got.append(data)
     def eof_callback():
         got.append("EOF")
     loop = self.make_event_loop()
     sock1, sock2 = stream.socketpair()
     reader = stream.FDReader(loop, sock1, callback, eof_callback)
     self.assertTrue(loop.will_block())
     # Ordering is significant here.  If we close sock2 first,
     # writing to sock1 will simply raise EPIPE.
     os.write(sock1.fileno(), "this data gets dropped but causes ECONNRESET")
     del sock2
     loop.once_safely()
     self.assertEquals(got, ["EOF"])
Ejemplo n.º 5
0
 def test_end_of_stream(self):
     got = []
     def callback(data):
         got.append(data)
     def eof_callback():
         got.append("EOF")
     loop = self.make_event_loop()
     # Unix domain sockets report EOF from read() but pipes will
     # return an error from poll() instead.
     pipe_read, pipe_write = stream.socketpair()
     reader = stream.FDReader(loop, pipe_read, callback, eof_callback)
     self.assertTrue(loop.will_block())
     os.write(pipe_write.fileno(), "hello world")
     self.assertTrue(not loop.will_block())
     loop.once_safely()
     self.assertEquals(got, ["hello world"])
     del pipe_write
     loop.once_safely()
     self.assertEquals(got, ["hello world", "EOF"])
     self.assertTrue(not loop.is_listening())
Ejemplo n.º 6
0
 def test_reader_flow_control(self):
     # Checks whether the buffer size of the reader can be changed.
     got = []
     def callback(data):
         got.append(data)
     loop = self.make_event_loop()
     sock1, sock2 = stream.socketpair()
     read_size = 2
     reader = stream.FDReader(loop, sock1, callback, lambda: None,
                              get_buffer_size=lambda: read_size)
     os.write(sock2.fileno(), "hello world")
     loop.once_safely()
     self.assertEquals(got, ["he"])
     # Setting the read buffer size to zero disables reading.
     read_size = 0
     reader.update_buffer_size()
     self.assertTrue(loop.will_block())
     # But we can set the read buffer back to a non-zero size.
     read_size = 6
     reader.update_buffer_size()
     loop.once_safely()
     self.assertEquals(got, ["he", "llo wo"])
Ejemplo n.º 7
0
 def test_sockets(self):
     sock1, sock2 = stream.socketpair()
     for sock in (sock1, sock2):
         self.assertTrue(stream.fd_is_readable(sock))
         self.assertTrue(stream.fd_is_writable(sock))