async def test_unbound_conduit(app, client, serial_mock, transport_mock):
    conduit = communication.SparkConduit(app)

    # test pre-bind behavior
    assert not conduit.connected
    with pytest.raises(exceptions.NotConnected):
        await conduit.write('stuff')
async def test_tcp_connection(app, client, mocker, tcp_create_connection_mock):
    app['config']['device_serial'] = None
    app['config']['device_host'] = 'enterprise'
    spock = communication.SparkConduit(app)

    await spock.startup(app)
    await asyncio.sleep(0.01)

    assert spock.connected
    assert tcp_create_connection_mock.call_count == 1
async def test_connect_error(app, client, interval_mock, tcp_create_connection_mock):
    app['config']['device_serial'] = None
    app['config']['device_host'] = 'enterprise'
    tcp_create_connection_mock.side_effect = ConnectionRefusedError

    spock = communication.SparkConduit(app)
    await spock.startup(app)

    await asyncio.sleep(0.01)
    assert not spock.connected
    await spock.shutdown(app)

    assert tcp_create_connection_mock.call_count > 1
async def test_mdns_repeat(app, client, mocker, tcp_create_connection_mock, discovery_resp):
    app['config']['device_serial'] = None
    app['config']['device_host'] = None
    app['config']['discovery'] = 'wifi'
    app['config']['mdns_host'] = 'mdns_error_host'
    mocker.patch(TESTED + '.DISCOVER_INTERVAL_S', 0.001)

    spock = communication.SparkConduit(app)

    await spock.startup(app)
    await asyncio.sleep(0.1)

    assert not spock.connected
    assert tcp_create_connection_mock.call_count == 0
async def test_reconnect(app, client, interval_mock, tcp_create_connection_mock):
    app['config']['device_serial'] = None
    app['config']['device_host'] = 'enterprise'

    spock = communication.SparkConduit(app)
    await spock.startup(app)

    await asyncio.sleep(0.01)
    assert tcp_create_connection_mock.call_count == 1

    spock._protocol.connection_lost(ConnectionError('boo!'))
    await asyncio.sleep(0.01)
    assert tcp_create_connection_mock.call_count == 2

    spock._protocol.connection_lost(None)
    await asyncio.sleep(0.01)
    assert tcp_create_connection_mock.call_count == 3
async def test_discover_retry(discover_all_app,
                              client,
                              mocker,
                              tcp_create_connection_mock,
                              serial_create_connection_mock,
                              interval_mock):
    ser_mock = mocker.patch(TESTED + '.discover_serial', CoroutineMock(return_value=None))
    tcp_mock = mocker.patch(TESTED + '.discover_tcp', CoroutineMock(return_value=None))
    exit_discovery_mock = mocker.patch(TESTED + '.exit_discovery')

    spock = communication.SparkConduit(discover_all_app)

    await spock.startup(discover_all_app)
    await asyncio.sleep(0.1)

    assert not spock.connected
    assert ser_mock.call_count > 1
    assert tcp_mock.call_count > 1
    assert exit_discovery_mock.call_count >= 1