Exemplo n.º 1
0
def test_select_timeout():
    h = vanilla.Hub()

    s1, r1 = h.pipe()
    s2, r2 = h.pipe()
    check_s, check_r = h.pipe()

    pytest.raises(vanilla.Timeout, h.select, [s1, r2], timeout=0)

    @h.spawn
    def _():
        h.sleep(20)
        check_s.send(r1.recv())

    pytest.raises(vanilla.Timeout, h.select, [s1, r2], timeout=10)

    ch, item = h.select([s1, r2], timeout=20)
    assert ch == s1
    s1.send(20)
    assert check_r.recv() == 20

    @h.spawn
    def _():
        h.sleep(20)
        s2.send(10)
        check_s.send('done')

    pytest.raises(vanilla.Timeout, h.select, [s1, r2], timeout=10)

    ch, item = h.select([s1, r2], timeout=20)
    assert ch == r2
    assert item == 10
    assert check_r.recv() == 'done'
Exemplo n.º 2
0
    def test_post_chunked(self):
        print
        print
        h = vanilla.Hub()

        serve = h.http.listen()

        @h.spawn
        def _():
            conn = serve.recv()
            for request in conn:
                sender, recver = h.pipe()
                request.reply(vanilla.http.Status(200), {}, recver)
                for data in request.body:
                    sender.send(data)
                sender.close()

        uri = 'http://localhost:%s' % serve.port
        conn = h.http.connect(uri)

        sender, recver = h.pipe()

        @h.spawn
        def _():
            for i in xrange(3):
                sender.send(str(i))
                h.sleep(10)
            sender.close()

        response = conn.post('/', data=recver).recv()

        for data in response.body:
            print data
Exemplo n.º 3
0
    def test_trigger(self):
        h = vanilla.Hub()

        check = h.pipe()

        @h.trigger
        def go():
            check.send(1)

        h.sleep(1)
        gc.collect()

        go.trigger()
        assert check.recv() == 1

        pipe = go.middle

        h.sleep(1)
        del go
        gc.collect()
        h.sleep(1)
        gc.collect()
        h.sleep(1)

        assert pipe.recver() is None
        assert pipe.sender() is None
Exemplo n.º 4
0
def test_timeout():
    h = vanilla.Hub()

    sender, recver = h.pipe()
    check_sender, check_recver = h.pipe()

    pytest.raises(vanilla.Timeout, sender.send, 12, timeout=0)
    pytest.raises(vanilla.Timeout, recver.recv, timeout=0)
    pytest.raises(vanilla.Timeout, sender.send, 12, timeout=0)

    @h.spawn
    def _():
        h.sleep(20)
        check_sender.send(recver.recv())

    pytest.raises(vanilla.Timeout, sender.send, 12, timeout=10)
    sender.send(12, timeout=20)
    assert check_recver.recv() == 12

    @h.spawn
    def _():
        h.sleep(20)
        sender.send(12)

    pytest.raises(vanilla.Timeout, recver.recv, timeout=10)
    assert recver.recv(timeout=20) == 12
Exemplo n.º 5
0
def test_pipe():
    h = vanilla.Hub()
    sender, recver = h.thread.pipe()
    sender.send(1)
    sender.send(2)
    assert recver.recv() == 1
    assert recver.recv() == 2
Exemplo n.º 6
0
 def test_onclose(self):
     h = vanilla.Hub()
     p1 = h.pipe()
     p2 = h.queue(1)
     p1.onclose(p2.send, 'Toby')
     p1.close()
     assert p2.recv() == 'Toby'
Exemplo n.º 7
0
    def test_get_body(self):
        h = vanilla.Hub()

        serve = h.http.listen()

        @h.spawn
        def _():
            conn = serve.recv()
            for request in conn:
                request.reply(vanilla.http.Status(200), {}, request.path)

        uri = 'http://localhost:%s' % serve.port
        conn = h.http.connect(uri)

        response = conn.get('/')
        response = response.recv()
        assert response.status.code == 200
        assert response.consume() == '/'
        assert response.headers['Date']

        response = conn.get('/toby').recv()
        assert response.status.code == 200
        assert response.consume() == '/toby'
        h.stop()
        assert not h.registered
Exemplo n.º 8
0
 def test_read_close(self):
     h = vanilla.Hub()
     sender, recver = h.io.pipe()
     recver.close()
     pytest.raises(vanilla.Closed, sender.send, '123')
     pytest.raises(vanilla.Closed, recver.recv)
     assert not h.registered
Exemplo n.º 9
0
    def test_get_chunked(self):
        h = vanilla.Hub()

        serve = h.http.listen()

        @h.spawn
        def _():
            conn = serve.recv()
            for request in conn:
                sender, recver = h.pipe()
                request.reply(vanilla.http.Status(200), {}, recver)

                for i in xrange(3):
                    h.sleep(10)
                    sender.send(str(i))

                if len(request.path) > 1:
                    sender.send(request.path[1:])

                sender.close()

        uri = 'http://localhost:%s' % serve.port
        conn = h.http.connect(uri)

        response = conn.get('/').recv()
        assert response.status.code == 200
        assert list(response.body) == ['0', '1', '2']

        response = conn.get('/peace').recv()
        assert response.status.code == 200
        assert list(response.body) == ['0', '1', '2', 'peace']
        h.stop()
        assert not h.registered
Exemplo n.º 10
0
    def test_stream(self):
        h = vanilla.Hub()

        sender, recver = h.pipe()
        recver = vanilla.message.Stream(recver)

        @h.spawn
        def _():
            sender.send('foo')
            sender.send('123')
            sender.send('456')
            sender.send('TobyTobyToby')
            sender.send('foo\n')
            sender.send('bar\nend.')
            sender.close()

        assert recver.recv() == 'foo'
        assert recver.recv_n(2) == '12'
        assert recver.recv_n(2) == '34'
        assert recver.recv_partition('y') == '56Tob'
        assert recver.recv_partition('y') == 'Tob'
        assert recver.recv_line() == 'Tobyfoo'
        assert recver.recv_line() == 'bar'
        assert recver.recv() == 'end.'
        pytest.raises(vanilla.Closed, recver.recv_n, 2)
Exemplo n.º 11
0
 def test_pipe(self):
     h = vanilla.Hub()
     sender, recver = h.io.pipe()
     pytest.raises(vanilla.Timeout, recver.recv, timeout=0)
     sender.send('123')
     assert recver.recv() == '123'
     h.stop()
Exemplo n.º 12
0
    def test_broadcast(self):
        h = vanilla.Hub()

        b = h.broadcast()
        check = h.queue(10)

        b.onempty(check.send, 'empty')

        def subscriber(s, name):
            for item in s:
                check.send((name, item))

        s1 = b.subscribe()
        s2 = b.subscribe()
        h.spawn(subscriber, s1, 's1')
        h.spawn(subscriber, s2, 's2')
        h.sleep(1)

        b.send(1)
        assert check.recv() == ('s1', 1)
        assert check.recv() == ('s2', 1)

        b.send(2)
        assert check.recv() == ('s1', 2)
        assert check.recv() == ('s2', 2)

        s1.close()
        b.send(3)
        assert check.recv() == ('s2', 3)
        pytest.raises(vanilla.Timeout, check.recv, timeout=0)

        s2.close()
        assert check.recv() == 'empty'
Exemplo n.º 13
0
def test_select():
    h = vanilla.Hub()

    s1, r1 = h.pipe()
    s2, r2 = h.pipe()
    check_s, check_r = h.pipe()

    @h.spawn
    def _():
        check_s.send(r1.recv())

    @h.spawn
    def _():
        s2.send(10)
        check_s.send('done')

    ch, item = h.select([s1, r2])
    assert ch == s1
    s1.send(20)

    ch, item = h.select([s1, r2])
    assert ch == r2
    assert item == 10

    assert check_r.recv() == 20
    assert check_r.recv() == 'done'
Exemplo n.º 14
0
def test_stream():
    h = vanilla.Hub()

    print
    print

    @stream(h)
    def counter(out):
        for i in xrange(10):
            print "s", i
            out.send(i)

    """
    @counter.map
    def double(n):
        return n * 2


    print double.recv()
    print double.recv()
    print double.recv()
    """

    ch = counter.pipe()
    print ch.recv()
    print ch.recv()

    print
    print
Exemplo n.º 15
0
def test_abandoned_recver():
    h = vanilla.Hub()

    check_sender, check_recver = h.pipe()

    # test abondoned after pause
    sender, recver = h.pipe()

    @h.spawn
    def _():
        pytest.raises(vanilla.Abandoned, recver.recv)
        check_sender.send('done')

    # sleep so the spawn runs and the recv pauses
    h.sleep(1)
    del sender
    gc.collect()
    assert check_recver.recv() == 'done'

    # test abondoned before pause
    sender, recver = h.pipe()

    @h.spawn
    def _():
        pytest.raises(vanilla.Abandoned, recver.recv)
        check_sender.send('done')

    del sender
    gc.collect()
    assert check_recver.recv() == 'done'
Exemplo n.º 16
0
    def test_basic(self):
        h = vanilla.Hub()

        child = h.process.execv(
            ['/usr/bin/env', 'grep', '--line-buffered', 'foo'])
        assert child.check_liveness()
        pytest.raises(vanilla.Timeout, child.done.recv, timeout=0)

        child.stdin.send('foo1\n')
        assert child.stdout.recv() == 'foo1\n'
        child.stdin.send('bar1\n')
        child.stdin.send('foo2\n')
        assert child.stdout.recv() == 'foo2\n'

        child.terminate()
        child.done.recv()
        assert not child.check_liveness()

        # spawn a new process to ensure the reaper watch is rebooted
        h.sleep(1)
        child = h.process.execv(
            ['/usr/bin/env', 'grep', '--line-buffered', 'foo'])
        assert child.check_liveness()
        pytest.raises(vanilla.Timeout, child.done.recv, timeout=0)

        child.stdin.send('foo1\n')
        assert child.stdout.recv() == 'foo1\n'
        child.stdin.send('bar1\n')
        child.stdin.send('foo2\n')
        assert child.stdout.recv() == 'foo2\n'

        child.terminate()
        child.done.recv()
        assert not child.check_liveness()
Exemplo n.º 17
0
 def test_signal(self):
     h = vanilla.Hub()
     child = h.process.execv(
         ['/usr/bin/env', 'grep', '--line-buffered', 'foo'])
     child.signal(signal.SIGTERM)
     child.done.recv()
     assert not child.check_liveness()
Exemplo n.º 18
0
    def test_overlap(self):
        h = vanilla.Hub()

        serve = h.http.listen()

        @h.spawn
        def _():
            conn = serve.recv()
            for request in conn:
                t = request.path[1:]
                h.sleep(int(t))
                request.reply(vanilla.http.Status(200), {}, t)

        uri = 'http://localhost:%s' % serve.port
        conn = h.http.connect(uri)

        q = h.queue(10)

        def go(t):
            r = conn.get('/' + str(t)).recv()
            q.send(int(r.consume()))

        h.spawn(go, 50)
        h.spawn(go, 20)

        assert q.recv() == 50
        assert q.recv() == 20
        h.stop()
Exemplo n.º 19
0
 def test_consume(self):
     h = vanilla.Hub()
     p = h.pipe()
     check = h.pipe()
     p.consume(lambda x: check.send(x))
     p.send(1)
     assert check.recv() == 1
Exemplo n.º 20
0
 def test_pipe_pipe(self):
     """check we can pipe io.pipes"""
     h = vanilla.Hub()
     p1 = h.io.pipe()
     p2 = h.pipe()
     p1.pipe(p2)
     p1.send('123')
     assert p2.recv() == '123'
Exemplo n.º 21
0
    def test_stop(self):
        h = vanilla.Hub()

        @h.spawn
        def _():
            h.sleep(20)

        h.stop()
Exemplo n.º 22
0
 def test_post_form_encoded(self, scheme):
     h = vanilla.Hub()
     data = {'k1': 'v1', 'k2': 'v2'}
     conn = h.http.connect('%s://httpbin.org' % scheme)
     response = conn.post('/post', data=data).recv()
     assert response.status.code == 200
     body = response.consume()
     assert json.loads(body)['form'] == data
Exemplo n.º 23
0
 def test_unexecutable(self):
     h = vanilla.Hub()
     child = h.process.execv(['foobar123'])
     child.done.recv()
     assert child.exitcode == 127
     child = h.process.execv(['/dev/null'])
     child.done.recv()
     assert child.exitcode == 126
Exemplo n.º 24
0
 def test_pipefu(self):
     h = vanilla.Hub()
     p = h.pipe() \
         .map(lambda x: x*2) \
         .pipe(h.pipe()) \
         .map(lambda x: x+'.')
     h.spawn(p.send, 'foo')
     assert p.recv() == 'foofoo.'
Exemplo n.º 25
0
    def test_exception(self):
        h = vanilla.Hub()

        @h.serialize
        def go():
            raise AssertionError('foo')

        pytest.raises(AssertionError, go)
Exemplo n.º 26
0
 def test_abandoned_recver_before_wait(self, primitive, a):
     h = vanilla.Hub()
     sender, recver = getattr(h, primitive)(*a)
     check = h.pipe()
     h.spawn(self.assert_abandoned_recver, recver, check)
     del sender
     gc.collect()
     assert check.recv() == 'done'
Exemplo n.º 27
0
    def test_post(self, scheme):
        h = vanilla.Hub()

        conn = h.http.connect('%s://httpbin.org' % scheme)

        response = conn.post('/post', data='toby').recv()
        assert response.status.code == 200
        body = response.consume()
        assert json.loads(body)['data'] == 'toby'
Exemplo n.º 28
0
def test_buffer():
    h = vanilla.Hub()

    sender, recver = buffer(h, 2)

    sender.send(1)
    sender.send(2)

    assert recver.recv() == 1
    assert recver.recv() == 2
Exemplo n.º 29
0
 def test_read_final(self):
     h = vanilla.Hub()
     sender, recver = h.io.pipe()
     sender.send('123')
     sender.close()
     pytest.raises(vanilla.Closed, sender.send, '123')
     assert recver.recv() == '123'
     pytest.raises(vanilla.Closed, recver.recv)
     h.sleep(1)
     assert not h.registered
Exemplo n.º 30
0
    def test_api(self):
        h = vanilla.Hub()
        p1 = h.io.pipe()
        p2 = h.io.pipe()

        p1.pipe(p2)
        p3 = p2.map(lambda x: int(x) * 2)

        p1.send('3')
        assert p3.recv() == 6