Esempio n. 1
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
Esempio n. 2
0
def test_read_json_object_from_a_socket_with_an_incomplete_json_object_must_return_None():
    json_string = '{"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() is None
Esempio n. 3
0
def test_read_json_object_twice_from_a_socket_with_one_json_object_must_return_only_one_json_string(
):
    json_string = '{"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() == json_string

    future = asyncio.ensure_future(stream.read_json_object())
    asyncio.get_event_loop().run_until_complete(future)
    assert future.result() is None
Esempio n. 4
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
Esempio n. 5
0
 async def callback(stream_reader, _):
     stream = JsonStream(stream_reader)
     while True:
         json_str = await stream.read_json_object()
         if json_str is None:
             break
         await self.queue.put(json_str)
Esempio n. 6
0
 async def __anext__(self):
     reader, writer = self.db
     stream = JsonStream(reader)
     try:
         json_str = await stream.read_json_object()
         report = self.report_model.get_type().deserialize(
             self.report_model.from_json(json_str))
         print(report, " ", json_str)
         return report
     except asyncio.TimeoutError:
         return None
Esempio n. 7
0
 async def callback(stream_reader, _):
     stream = JsonStream(stream_reader)
     count = 0  # If 10 times in a row we don't have a full message we stop
     while True:
         json_str = await stream.read_json_object()
         if json_str is None:
             if count > 10:
                 break
             count += 1
             continue
         count = 0
         await self.queue.put(json_str)