Beispiel #1
0
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)
Beispiel #2
0
def first_evlp():
  try:
    yield sleep(1)
    yield req()
    yield launch(second)
  finally:
    eventloop.halt()
Beispiel #3
0
def first_evlp():
    try:
        yield sleep(1)
        yield req()
        yield launch(second)
    finally:
        eventloop.halt()
Beispiel #4
0
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)
Beispiel #5
0
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']
Beispiel #6
0
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
Beispiel #8
0
 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
Beispiel #9
0
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)
Beispiel #10
0
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)
Beispiel #11
0
    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
Beispiel #12
0
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)
Beispiel #13
0
    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
Beispiel #14
0
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)
Beispiel #15
0
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
Beispiel #16
0
def get_conn(cb, conn):
    cb(conn)
    while not conn.is_closed():
        yield sleep(1)
Beispiel #17
0
def req():
    yield sleep(1)
Beispiel #18
0
def print_every_two_seconds():
    for i in xrange(5):
        print("2")
        yield sleep(2)
Beispiel #19
0
def print_every_second():
    for i in xrange(5):
        print("1")
        yield sleep(1)
Beispiel #20
0
def foo(x, z=1):
    yield sleep(1)
    print x
Beispiel #21
0
def fail():
    raise Exception("whoo")
    yield sleep(1)
Beispiel #22
0
def get_conn(cb, conn):
    cb(conn)
    while not conn.is_closed():
        yield sleep(1)
Beispiel #23
0
def yielding_oroutine(x, z=1):
    yield sleep(1)
    print(x)
Beispiel #24
0
def first_evlp():
    yield sleep(1)
    yield req()
    yield launch(second)  # won't crash
Beispiel #25
0
 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)
Beispiel #26
0
def test_sleep():
    t = time.time()
    yield util.sleep(0.01)
    dt = time.time() - t
    assert 0.005 < dt < 0.015
Beispiel #27
0
 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)
Beispiel #28
0
def req():
    yield sleep(1)
Beispiel #29
0
def foo(x, z=1):
    yield sleep(1)
    print x
Beispiel #30
0
def print_every_two_seconds():
    for i in xrange(5):
        print "2"
        yield sleep(2)
    eventloop.halt()
Beispiel #31
0
def fail():
    raise Exception("whoo")
    yield sleep(1)
Beispiel #32
0
def first_evlp():
    yield sleep(1)
    yield req()
    yield launch(second)  # won't crash
Beispiel #33
0
def yielding_oroutine(x, z=1):
    yield sleep(1)
    print x
Beispiel #34
0
def fail():
    raise Exception("whoo")
    # make this a generator function, to demonstrate exceptions work properly in this context
    yield sleep(1)