def test_client(): addr = '127.0.0.1' port = 5555 with network_server_running(addr, port): yield sleep(0.1) with network_client() as client: msg = 'ok' yield client.connect(addr, port) yield client.write(msg + EOL) result = yield client.read_until(EOL) result = result.decode('utf-8') assert result == 'you said: ' + msg + EOL yield sleep(0.1)
def first_evlp(): try: yield sleep(1) yield req() yield launch(second) finally: eventloop.halt()
def test_add(): cb = Callback() calls = [] assert cb.future._callbacks == [] cb.add(calls.append) assert len(cb.future._callbacks) == 1 pytest.raises(TypeError, cb.add, False) assert len(cb.future._callbacks) == 1 assert calls == [] cb('ok') yield sleep(0) assert calls == ['ok'] cb.add(calls.append) yield sleep(0) assert calls == ['ok', 'ok'] pytest.raises(TypeError, cb.add, False)
def test_defer(): cb = defer('ok') assert isinstance(cb, Callback) assert cb.result == 'ok' calls = [] cb.add(calls.append) yield sleep(0) assert calls == ['ok']
def test_client(): addr = '127.0.0.1' port = 5556 with http_server_running(port): yield sleep(0.1) with http_client() as client: yield client.connect(addr, port) r = yield client.request('/') assert r.code == '200', r.code # should this be an int?
def test_read_some_delay(self): data = 'ok' self.conn.timeout = 0.2 def populate(): self.buffer = data self.stack_conn.read_cb(None) eventloop.queue_task(0.1, populate) r = yield self.conn.read_some() self.assertEqual(r, data) self.assertEqual(self.stack_conn.resume_called, 1) yield sleep(0.2) # ensure timeout has expired
def test_read_delay(self): data = 'ok' self.buffer = 'o' self.conn.timeout = 0.2 def populate(): self.buffer += 'k' self.stack_conn.read_cb(None) eventloop.queue_task(0.1, populate) r = yield self.conn.read(2) assert r == data assert self.stack_conn.resume_called == 1 yield sleep(0.2) # ensure timeout has expired
def _subproc_wrapper(port, target, *args, **kwargs): try: client = Client() while True: try: yield client.connect('127.0.0.1', port) break except Exception, e: print "failed to connect to monocle multiprocess parent on port", port, type(e), str(e) yield sleep(1) chan = SocketChannel(client) yield target(chan, *args, **kwargs)
def _subproc_wrapper(port, target, *args, **kwargs): try: client = Client() while True: try: yield client.connect('127.0.0.1', port) break except Exception, e: print type(e), str(e) yield sleep(1) chan = SocketChannel(client) yield target(chan, *args, **kwargs)
def _subproc_wrapper(port, target, *args, **kwargs): try: client = Client() while True: try: yield client.connect('127.0.0.1', port) break except Exception, e: print "failed to connect to monocle multiprocess parent on port", port, type( e), str(e) yield sleep(1) chan = SocketChannel(client) yield target(chan, *args, **kwargs)
def test_lots_of_messages(): add_service(Service(handle_echo, port=8000)) try: client = Client() yield client.connect('localhost', 8000) t = time.time() for x in range(1000): msg = "hello, world #%s!" % x yield client.write(msg + '\r\n') echo_result = yield client.read_until("\r\n") echo_result = echo_result.decode('utf-8') assert echo_result.strip() == "you said: %s" % msg, echo_result print('10000 loops in %.2fs' % (time.time() - t)) finally: client.close() yield sleep(0.1)
def test_large_requests(): addr = '127.0.0.1' port = 5557 recv_body = [] @_o def _handler(conn): recv_body.append(conn.body) data = 'Hello, World!' headers = http.HttpHeaders() headers.add('Content-Length', len(data)) headers.add('Content-Type', 'text/plain') headers.add('Connection', 'close') yield Return(200, headers, data) with http_server_running(port, handler=_handler): yield sleep(0.1) with http_client() as client: yield client.connect(addr, port) send_body = 'x' * payload_multiplier r = yield client.request('/', method='POST', body='x' * payload_multiplier) assert recv_body and recv_body[0] == send_body
def get_conn(cb, conn): cb(conn) while not conn.is_closed(): yield sleep(1)
def req(): yield sleep(1)
def print_every_two_seconds(): for i in xrange(5): print("2") yield sleep(2)
def print_every_second(): for i in xrange(5): print("1") yield sleep(1)
def foo(x, z=1): yield sleep(1) print x
def fail(): raise Exception("whoo") yield sleep(1)
def yielding_oroutine(x, z=1): yield sleep(1) print(x)
def first_evlp(): yield sleep(1) yield req() yield launch(second) # won't crash
def test_simple(self): t = time.time() yield util.sleep(0.01) dt = time.time() - t self.assertTrue(dt > 0.005 and dt < 0.015)
def test_sleep(): t = time.time() yield util.sleep(0.01) dt = time.time() - t assert 0.005 < dt < 0.015
def print_every_two_seconds(): for i in xrange(5): print "2" yield sleep(2) eventloop.halt()
def yielding_oroutine(x, z=1): yield sleep(1) print x
def fail(): raise Exception("whoo") # make this a generator function, to demonstrate exceptions work properly in this context yield sleep(1)