def test_discovery_timeout_randomizer(s_endp, client): """Check that discovery timeout randomizer falls within limits""" net_man = NetworkManager(s_endp, client) assert net_man.discovery_timeout in range( int(NetworkManager.DISCOVERY_TIMEOUT_S * (1 - NetworkManager.DISCOVERY_TIMEOUT_RAND_FACTOR)), int(NetworkManager.DISCOVERY_TIMEOUT_S * (1 + NetworkManager.DISCOVERY_TIMEOUT_RAND_FACTOR)))
def test_client_transmission(s_endp, client): """Check end to end client communication""" net_man = NetworkManager(s_endp, client, discovery_timeout=0.1, randomize_timeout=False) net_man.search() client.connected() net_man.send('Test', 4) s_endp.assert_called_once_with('Test', 4)
def test_server_transmission(s_endp, client, server): """Check end to end server communication""" net_man = NetworkManager(s_endp, client, server, discovery_timeout=0.1, randomize_timeout=False) net_man.search() time.sleep(net_man.discovery_timeout + 0.05) server.connected() net_man.send('Test', 4) s_endp.assert_called_once_with('Test', 4)
def test_client_multi_endpoint_transmission(m_endp, client): """Check end to end client communication to multiple endpoints""" net_man = NetworkManager(m_endp, client, discovery_timeout=0.1, randomize_timeout=False) net_man.search() client.connected() net_man.send('Test', 4) for endpoint in m_endp: endpoint.assert_called_once_with('Test', 4)
def test_client_invalid_endpoint_init(client): """Check that initialization with an invalid endpoint fails""" with pytest.raises(AttributeError): NetworkManager(None, client, discovery_timeout=0.1, randomize_timeout=False)
def test_invalid_client_init(s_endp): """Ensure that initialization with an invalid client fails""" with pytest.raises(AttributeError): NetworkManager(s_endp, None)
def nm_client_server(s_endp, client, server): """Create a client-server network manager""" return NetworkManager(s_endp, client=client, server=server, discovery_timeout=0.1, randomize_timeout=False)
def nm_client_only(s_endp, client): """Create a client only network manager""" return NetworkManager(s_endp, client, discovery_timeout=0.1, randomize_timeout=False)
def test_send_during_search(s_endp, client): net_man = NetworkManager(s_endp, client, discovery_timeout=0.1, randomize_timeout=False) net_man.search() with pytest.raises(SystemError): net_man.send('Test', 4)