Exemplo n.º 1
0
    def test_prompt_cancellation(self):
        r, w = test_utils.socketpair()
        r.setblocking(False)
        f = self.loop.sock_recv(r, 1)
        ov = getattr(f, 'ov', None)
        self.assertTrue(ov is None or ov.pending)

        def main():
            try:
                self.loop.call_soon(f.cancel)
                yield from f
            except futures.CancelledError:
                res = 'cancelled'
            else:
                res = None
            finally:
                self.loop.stop()
            return res

        start = time.monotonic()
        t = tasks.Task(main(), timeout=1, loop=self.loop)
        self.loop.run_forever()
        elapsed = time.monotonic() - start

        self.assertLess(elapsed, 0.1)
        self.assertEqual(t.result(), 'cancelled')
        self.assertRaises(futures.CancelledError, f.result)
        self.assertTrue(ov is None or not ov.pending)
        self.loop.stop_serving(r)

        r.close()
        w.close()
Exemplo n.º 2
0
    def test_reader_callback(self):
        r, w = test_utils.socketpair()
        bytes_read = []

        def reader():
            try:
                data = r.recv(1024)
            except BlockingIOError:
                # Spurious readiness notifications are possible
                # at least on Linux -- see man select.
                return
            if data:
                bytes_read.append(data)
            else:
                self.assertTrue(self.loop.remove_reader(r.fileno()))
                r.close()

        self.loop.add_reader(r.fileno(), reader)
        self.loop.call_soon(w.send, b'abc')
        test_utils.run_briefly(self.loop)
        self.loop.call_soon(w.send, b'def')
        self.loop.call_soon(w.close)
        self.loop.call_soon(self.loop.stop)
        self.loop.run_forever()
        self.assertEqual(b''.join(bytes_read), b'abcdef')
Exemplo n.º 3
0
    def test_writer_callback(self):
        r, w = test_utils.socketpair()
        w.setblocking(False)
        self.loop.add_writer(w.fileno(), w.send, b'x'*(256*1024))
        test_utils.run_briefly(self.loop)

        def remove_writer():
            self.assertTrue(self.loop.remove_writer(w.fileno()))

        self.loop.call_soon(remove_writer)
        self.loop.call_soon(self.loop.stop)
        self.loop.run_forever()
        w.close()
        data = r.recv(256*1024)
        r.close()
        self.assertGreaterEqual(len(data), 200)
Exemplo n.º 4
0
    def test_write_pipe_disconnect_on_close(self):
        rsock, wsock = test_utils.socketpair()
        rsock.setblocking(False)
        pipeobj = io.open(wsock.detach(), 'wb', 1024)

        proto = MyWritePipeProto(loop=self.loop)
        connect = self.loop.connect_write_pipe(lambda: proto, pipeobj)
        transport, p = self.loop.run_until_complete(connect)
        self.assertIs(p, proto)
        self.assertIs(transport, proto.transport)
        self.assertEqual('CONNECTED', proto.state)

        transport.write(b'1')
        data = self.loop.run_until_complete(self.loop.sock_recv(rsock, 1024))
        self.assertEqual(b'1', data)

        rsock.close()

        self.loop.run_until_complete(proto.done)
        self.assertEqual('CLOSED', proto.state)
Exemplo n.º 5
0
def test_write_pipe_disconnect_on_close(loop):
    rsock, wsock = test_utils.socketpair()
    rsock.setblocking(False)

    pipeobj = io.open(wsock.detach(), 'wb', 1024)

    proto = MyWritePipeProto(loop=loop)
    connect = loop.connect_write_pipe(lambda: proto, pipeobj)
    transport, p = loop.run_until_complete(connect)
    assert p is proto
    assert transport is proto.transport
    assert 'CONNECTED' is proto.state

    transport.write(b'1')
    data = loop.run_until_complete(loop.sock_recv(rsock, 1024))
    assert b'1' == data

    rsock.close()

    loop.run_until_complete(proto.done)
    assert 'CLOSED' == proto.state
Exemplo n.º 6
0
    def test_write_pipe_disconnect_on_close(self):
        rsock, wsock = test_utils.socketpair()
        rsock.setblocking(False)

        pipeobj = io.open(wsock.detach(), 'wb', 1024)

        proto = MyWritePipeProto(loop=self.loop)
        connect = self.loop.connect_write_pipe(lambda: proto, pipeobj)
        transport, p = self.loop.run_until_complete(connect)
        self.assertIs(p, proto)
        self.assertIs(transport, proto.transport)
        self.assertEqual('CONNECTED', proto.state)

        transport.write(b'1')
        data = self.loop.run_until_complete(self.loop.sock_recv(rsock, 1024))
        self.assertEqual(b'1', data)

        rsock.close()

        self.loop.run_until_complete(proto.done)
        self.assertEqual('CLOSED', proto.state)
Exemplo n.º 7
0
 def make_socketpair(self):
     rd, wr = socketpair()
     self.addCleanup(rd.close)
     self.addCleanup(wr.close)
     return rd, wr