def test_basic_send(self): i = IOWorker() i.send("foo") self.assertTrue(i._ready_to_send) self.assertEqual(i.send_buf, "foo") i._consume_send_buf(3) self.assertFalse(i._ready_to_send)
def send(self, data): if threading.current_thread( ).name != "MainThread" and threading.current_thread( ).name != "BackgroundIOThread": raise RuntimeError("Wrong thread: %s" % threading.current_thread()) """ send data from the client side. fire and forget. """ return IOWorker.send(self, data)
def test_not_sent_until_permitted(self): i = DeferredIOWorker(IOWorker()) i.set_receive_handler(self.call_later) i.block() i.send("foo") self.assertFalse(i._io_worker._ready_to_send) self.assertFalse(i._send_queue.empty()) i.unblock() self.assertTrue(i._send_queue.empty()) i._io_worker._consume_send_buf(3) self.assertFalse(i._io_worker._ready_to_send)
def test_basic_receive(self): i = IOWorker() self.data = None def d(worker): self.data = worker.peek_receive_buf() i.set_receive_handler(d) i._push_receive_data("bar") self.assertEqual(self.data, "bar") # d does not consume the data i._push_receive_data("hepp") self.assertEqual(self.data, "barhepp")
def test_receive_consume(self): i = IOWorker() self.data = None def consume(worker): self.data = worker.peek_receive_buf() worker.consume_receive_buf(len(self.data)) i.set_receive_handler(consume) i._push_receive_data("bar") self.assertEqual(self.data, "bar") # data has been consumed i._push_receive_data("hepp") self.assertEqual(self.data, "hepp")
def test_receive_consume(self): i = DeferredIOWorker(IOWorker()) i.set_receive_handler(self.call_later) self.data = None def consume(worker): self.data = worker.peek_receive_buf() worker.consume_receive_buf(len(self.data)) i.set_receive_handler(consume) i.block() i._io_worker._push_receive_data("bar") self.assertEqual(self.data, None) i.unblock() self.assertEqual(self.data, "bar") # data has been consumed i._io_worker._push_receive_data("hepp") self.assertEqual(self.data, "hepp")
def test_not_received_until_permitted(self): i = DeferredIOWorker(IOWorker()) i.set_receive_handler(self.call_later) i.block() self.data = None def d(worker): self.data = worker.peek_receive_buf() i.set_receive_handler(d) i._io_worker._push_receive_data("bar") self.assertEqual(self.data, None) i.unblock() self.assertEqual(self.data, "bar") # Now if unblocked, should go through immediately # Note: d does not consume the data i._io_worker._push_receive_data("hepp") self.assertEqual(self.data, "barhepp")
def __init__(self, socket, on_close): IOWorker.__init__(self) self.socket = socket # (on_close factory method hides details of the Select loop) self.on_close = on_close
def send(self, data): """ send data from the client side. fire and forget. """ return IOWorker.send(self, data)
def __init__(self, socket, on_close): IOWorker.__init__(self) self.socket = socket self.closed = False # (on_close factory method hides details of the Select loop) self.on_close = on_close
def close(self): """ Register this socket to be closed. fire and forget """ # (don't close until Select loop is ready) IOWorker.close(self) # on_close is a function not a method self.on_close(self)
def send(self, data): if threading.current_thread().name != "MainThread" and threading.current_thread().name != "BackgroundIOThread": raise RuntimeError("Wrong thread: %s" % threading.current_thread()) """ send data from the client side. fire and forget. """ return IOWorker.send(self, data)