async def test_send_receive(server_port, messages): async with create_conn_pair(server_port, 0.01) as conn_pair: for send_msg in messages: for local_conn, remote_conn in itertools.permutations(conn_pair): await local_conn.send(send_msg) rcv_msg = await remote_conn.receive() assert json.equals(send_msg, rcv_msg)
async def _client_loop(self): changes = aio.Queue() def on_change(): remote_data = (self._conn.remote_data if isinstance( self._conn.remote_data, dict) else {}) changes.put_nowait(remote_data.get(self._name)) try: with self._conn.register_change_cb(on_change): on_change() while True: remote_data = await changes.get() if json.equals(remote_data, self._remote_data): continue self._remote_data = remote_data self._change_cbs.notify() except Exception as e: mlog.error("client loop error: %s", e, exc_info=e) finally: self.close() self._receive_queue.close()
async def test_flush_local_data(server_port): async with create_conn_pair(server_port, 100) as conn_pair: local_conn, remote_conn = conn_pair change_queue = aio.Queue() remote_conn.register_change_cb(lambda: change_queue.put_nowait(None)) local_conn.set_local_data([1]) await asyncio.sleep(0.1) assert change_queue.empty() assert remote_conn.remote_data is None local_conn.set_local_data([1, 2]) await asyncio.sleep(0.1) assert change_queue.empty() assert remote_conn.remote_data is None await local_conn.flush_local_data() await asyncio.wait_for(change_queue.get(), 0.1) assert json.equals(remote_conn.remote_data, local_conn.local_data)
async def test_set_local_data(server_port, data): async with create_conn_pair(server_port, 0) as conn_pair: change_queues = [] for conn in conn_pair: assert conn.local_data is None assert conn.remote_data is None change_queue = aio.Queue() conn.register_change_cb(lambda: change_queue.put_nowait(None)) change_queues.append(change_queue) for i in data: for local, remote in itertools.permutations( zip(conn_pair, change_queues)): local_conn, _ = local remote_conn, change_queue = remote local_conn.set_local_data(i) assert local_conn.local_data == i await change_queue.get() assert json.equals(remote_conn.remote_data, local_conn.local_data)
def test_flatten(data, result): x = list(json.flatten(data)) assert json.equals(x, result)
def test_clone_example(): x = {'a': [1, 2, 3]} y = json.clone(x) assert x is not y assert x['a'] is not y['a'] assert json.equals(x, y)
def test_equals_example(): assert json.equals(0, 0.0) is True assert json.equals({'a': 1, 'b': 2}, {'b': 2, 'a': 1}) is True assert json.equals(1, True) is False
def test_equals(params, is_equal): for a, b in itertools.combinations(params, 2): assert json.equals(a, b) == is_equal
def test_remove(data, path, result): x = json.remove(data, path) assert json.equals(x, result)
def test_set_(data, path, value, result): x = json.set_(data, path, value) assert json.equals(x, result)
def test_get(data, path, default, result): x = json.get(data, path, default) assert json.equals(x, result)