def test_simple(self): p = Pipe() # default constructor self.assertTrue(isinstance(p, io.RawIOBase)) self.assertFalse(p.closed) try: p.fileno() self.fail("fileno should raise IOError") except IOError: pass self.assertFalse(p.isatty()) self.assertTrue(p.readable()) self.assertFalse(p.seekable()) self.assertTrue(p.writable()) # now for our custom attributes self.assertTrue(p.readblocking()) self.assertTrue(p.writeblocking()) self.assertTrue(p.canwrite() == io.DEFAULT_BUFFER_SIZE) self.assertFalse(p.canread()) # now try a quick read and write test data = b"The quick brown fox jumped over the lazy dog" wlen = p.write(data) self.assertTrue(wlen == len(data)) self.assertTrue(p.canwrite() == io.DEFAULT_BUFFER_SIZE - len(data)) self.assertTrue(p.canread()) self.assertTrue(p.read(3) == data[:3]) self.assertTrue(p.canwrite() == io.DEFAULT_BUFFER_SIZE - len(data) + 3) self.assertTrue(p.canread()) # now deal with EOF conditions p.write_eof() try: p.write(b"extra") self.fail("write past EOF") except IOError: pass try: p.canwrite() self.fail("canwrite called past EOF") except IOError: pass self.assertTrue(p.canread(), "But can still read") self.assertFalse(p.closed) self.assertTrue(p.readall() == data[3:]) self.assertTrue(p.canread(), "Can still read") self.assertTrue(p.read(3) == b'') self.assertTrue(len(p.read()) == 0) self.assertTrue(len(p.readall()) == 0) p.close() self.assertTrue(p.closed) try: p.canread() self.fail("canread called on closed pipe") except IOError: pass
def test_wblocking(self): p = Pipe(timeout=15, bsize=10) t = threading.Thread(target=self.rrunner, args=(p, )) p.write(b"1234567890") data = b"extra" t.start() try: # should block until the other thread reads wresult = p.write(bytearray(data)) # and should then write at most 1 byte self.assertTrue(wresult == 1, repr(wresult)) except IOError as e: self.fail("Timeout on mutlithreaded pipe; %s" % str(e)) t = threading.Thread(target=self.rallrunner, args=(p, )) t.start() try: # should block until all data has been read logging.debug("flush waiting...") p.flush() logging.debug("flush complete") self.assertTrue(p.canwrite() == 10, "empty after flush") except IOError as e: self.fail("flush timeout on mutlithreaded pipe; %s" % str(e)) # put the other thread out of its misery p.write_eof() logging.debug("eof written, joining rallrunner") t.join()
def test_wblocking(self): p = Pipe(timeout=15, bsize=10) t = threading.Thread(target=self.rrunner, args=(p,)) p.write(b"1234567890") data = b"extra" t.start() try: # should block until the other thread reads wresult = p.write(bytearray(data)) # and should then write at most 1 byte self.assertTrue(wresult == 1, repr(wresult)) except IOError as e: self.fail("Timeout on mutlithreaded pipe; %s" % str(e)) t = threading.Thread(target=self.rallrunner, args=(p,)) t.start() try: # should block until all data has been read logging.debug("flush waiting...") p.flush() logging.debug("flush complete") self.assertTrue(p.canwrite() == 10, "empty after flush") except IOError as e: self.fail("flush timeout on mutlithreaded pipe; %s" % str(e)) # put the other thread out of its misery p.write_eof() logging.debug("eof written, joining rallrunner") t.join()
def test_rdetect(self): p = Pipe(timeout=15, bsize=10) rflag = threading.Event() # set a read event p.set_rflag(rflag) self.assertFalse(rflag.is_set()) t = threading.Thread(target=self.rrunner, args=(p, )) t.start() # the runner will issue a read call, should trigger the event rflag.wait(5.0) self.assertTrue(rflag.is_set()) # write 10 bytes, thread should terminate p.write(b"1234567890") t.join() # one byte read, write another byte p.write(b"A") # buffer should now be full at this point... self.assertFalse(p.canwrite()) self.assertFalse(rflag.is_set()) # the next call to read should set the flag again p.read(1) self.assertTrue(rflag.is_set())
def test_rdetect(self): p = Pipe(timeout=15, bsize=10) rflag = threading.Event() # set a read event p.set_rflag(rflag) self.assertFalse(rflag.is_set()) t = threading.Thread(target=self.rrunner, args=(p,)) t.start() # the runner will issue a read call, should trigger the event rflag.wait(5.0) self.assertTrue(rflag.is_set()) # write 10 bytes, thread should terminate p.write(b"1234567890") t.join() # one byte read, write another byte p.write(b"A") # buffer should now be full at this point... self.assertFalse(p.canwrite()) self.assertFalse(rflag.is_set()) # the next call to read should set the flag again p.read(1) self.assertTrue(rflag.is_set())