Ejemplo n.º 1
0
def test_read_json_object_from_a_socket_with_an_complete_json_object_and_incomplete_json_object_must_return_only_one_json_string():
    json_string = '{"a":1}{"a":1'
    socket = MockedStreamReader([json_string])
    stream = JsonStream(socket)

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() == '{"a":1}'

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() is None
Ejemplo n.º 2
0
def test_read_json_object_from_a_socket_with_one_json_string_bigger_than_the_buffer_size_must_return_one_json_string():
    json_string = '{"a":1}'

    socket = MockedStreamReader([json_string])
    stream = JsonStream(socket, buffer_size=len(json_string) - 2)

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() == json_string

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() is None
Ejemplo n.º 3
0
def test_read_json_object_from_a_socket_without_data_return_None():
    socket = MockedStreamReader('')
    stream = JsonStream(socket)

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() is None
Ejemplo n.º 4
0
def test_read_json_object_twice_from_a_socket_with_two_json_object_must_return_two_json_string():
    json1 = '{"a":1}'
    json2 = '{"b":2}'
    socket = MockedStreamReader([json1 + json2])
    stream = JsonStream(socket)

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() == json1

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() == json2

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() is None