Skip to content

Commit

Permalink
Don't track is_online flag ourselves
Browse files Browse the repository at this point in the history
Just ask pyspotify if we're LOGGED_IN when needed.
  • Loading branch information
jodal committed Aug 10, 2015
1 parent 5dc57ae commit 4d3cd01
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 30 deletions.
10 changes: 2 additions & 8 deletions mopidy_spotify/backend.py
Expand Up @@ -28,7 +28,6 @@ class SpotifyBackend(pykka.ThreadingActor, backend.Backend):
_logged_in = threading.Event()
_logged_out = threading.Event()
_logged_out.set()
_online = threading.Event()

def __init__(self, config, audio):
super(SpotifyBackend, self).__init__()
Expand Down Expand Up @@ -80,8 +79,7 @@ def _get_session(self, config):
session.on(
spotify.SessionEvent.CONNECTION_STATE_UPDATED,
on_connection_state_changed,
self._logged_in, self._logged_out, self._online,
backend_actor_proxy)
self._logged_in, self._logged_out, backend_actor_proxy)
session.on(
spotify.SessionEvent.PLAY_TOKEN_LOST,
on_play_token_lost, backend_actor_proxy)
Expand Down Expand Up @@ -127,28 +125,24 @@ def on_play_token_lost(self):


def on_connection_state_changed(
session, logged_in_event, logged_out_event, online_event, backend):
session, logged_in_event, logged_out_event, backend):

# Called from the pyspotify event loop, and not in an actor context.
if session.connection.state is spotify.ConnectionState.LOGGED_OUT:
logger.debug('Logged out of Spotify')
logged_in_event.clear()
logged_out_event.set()
online_event.clear()
elif session.connection.state is spotify.ConnectionState.LOGGED_IN:
logger.info('Logged in to Spotify in online mode')
logged_in_event.set()
logged_out_event.clear()
online_event.set()
backend.on_logged_in()
elif session.connection.state is spotify.ConnectionState.DISCONNECTED:
logger.info('Disconnected from Spotify')
online_event.clear()
elif session.connection.state is spotify.ConnectionState.OFFLINE:
logger.info('Logged in to Spotify in offline mode')
logged_in_event.set()
logged_out_event.clear()
online_event.clear()


def on_play_token_lost(session, backend):
Expand Down
1 change: 0 additions & 1 deletion mopidy_spotify/library.py
Expand Up @@ -33,5 +33,4 @@ def lookup(self, uri):
def search(self, query=None, uris=None, exact=False):
return search.search(
self._config, self._backend._session,
self._backend._online.is_set(),
query, uris, exact)
6 changes: 4 additions & 2 deletions mopidy_spotify/search.py
Expand Up @@ -5,13 +5,15 @@

from mopidy import models

import spotify

from mopidy_spotify import lookup, translator


logger = logging.getLogger(__name__)


def search(config, session, is_online, query=None, uris=None, exact=False):
def search(config, session, query=None, uris=None, exact=False):
# TODO Respect `uris` argument
# TODO Support `exact` search

Expand All @@ -30,7 +32,7 @@ def search(config, session, is_online, query=None, uris=None, exact=False):
uri = 'spotify:search:%s' % urllib.quote(sp_query.encode('utf-8'))
logger.info('Searching Spotify for: %s', sp_query)

if not is_online:
if session.connection.state is not spotify.ConnectionState.LOGGED_IN:
logger.info('Spotify search aborted: Spotify is offline')
return models.SearchResult(uri=uri)

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Expand Up @@ -206,6 +206,7 @@ def sp_search_mock(sp_album_mock, sp_artist_mock, sp_track_mock):
@pytest.fixture
def session_mock():
sp_session_mock = mock.Mock(spec=spotify.Session)
sp_session_mock.connection.state = spotify.ConnectionState.LOGGED_IN
sp_session_mock.playlist_container = []
return sp_session_mock

Expand Down
21 changes: 4 additions & 17 deletions tests/test_backend.py
Expand Up @@ -112,7 +112,6 @@ def test_on_start_adds_connection_state_changed_handler_to_session(
backend.on_connection_state_changed,
backend.SpotifyBackend._logged_in,
backend.SpotifyBackend._logged_out,
backend.SpotifyBackend._online,
mock.ANY)
in session.on.call_args_list)

Expand Down Expand Up @@ -164,35 +163,29 @@ def test_on_connection_state_changed_when_logged_out(spotify_mock, caplog):
session_mock.connection.state = spotify_mock.ConnectionState.LOGGED_OUT
logged_in_event = threading.Event()
logged_out_event = threading.Event()
online_event = threading.Event()
actor_mock = mock.Mock(spec=backend.SpotifyBackend)

backend.on_connection_state_changed(
session_mock, logged_in_event, logged_out_event, online_event,
actor_mock)
session_mock, logged_in_event, logged_out_event, actor_mock)

assert 'Logged out of Spotify' in caplog.text()
assert not logged_in_event.is_set()
assert logged_out_event.is_set()
assert not online_event.is_set()


def test_on_connection_state_changed_when_logged_in(spotify_mock, caplog):
session_mock = spotify_mock.Session.return_value
session_mock.connection.state = spotify_mock.ConnectionState.LOGGED_IN
logged_in_event = threading.Event()
logged_out_event = threading.Event()
online_event = threading.Event()
actor_mock = mock.Mock(spec=backend.SpotifyBackend)

backend.on_connection_state_changed(
session_mock, logged_in_event, logged_out_event, online_event,
actor_mock)
session_mock, logged_in_event, logged_out_event, actor_mock)

assert 'Logged in to Spotify in online mode' in caplog.text()
assert logged_in_event.is_set()
assert not logged_out_event.is_set()
assert online_event.is_set()
actor_mock.on_logged_in.assert_called_once_with()


Expand All @@ -201,33 +194,27 @@ def test_on_connection_state_changed_when_disconnected(spotify_mock, caplog):
session_mock.connection.state = spotify_mock.ConnectionState.DISCONNECTED
logged_in_event = threading.Event()
logged_out_event = threading.Event()
online_event = threading.Event()
actor_mock = mock.Mock(spec=backend.SpotifyBackend)

backend.on_connection_state_changed(
session_mock, logged_in_event, logged_out_event, online_event,
actor_mock)
session_mock, logged_in_event, logged_out_event, actor_mock)

assert 'Disconnected from Spotify' in caplog.text()
assert not online_event.is_set()


def test_on_connection_state_changed_when_offline(spotify_mock, caplog):
session_mock = spotify_mock.Session.return_value
session_mock.connection.state = spotify_mock.ConnectionState.OFFLINE
logged_in_event = threading.Event()
logged_out_event = threading.Event()
online_event = threading.Event()
actor_mock = mock.Mock(spec=backend.SpotifyBackend)

backend.on_connection_state_changed(
session_mock, logged_in_event, logged_out_event, online_event,
actor_mock)
session_mock, logged_in_event, logged_out_event, actor_mock)

assert 'Logged in to Spotify in offline mode' in caplog.text()
assert logged_in_event.is_set()
assert not logged_out_event.is_set()
assert not online_event.is_set()


def test_on_logged_in_event_activates_private_session(
Expand Down
6 changes: 4 additions & 2 deletions tests/test_search.py
Expand Up @@ -2,6 +2,8 @@

from mopidy import models

import spotify


def test_search_with_no_query_returns_nothing(provider, caplog):
result = provider.search()
Expand Down Expand Up @@ -51,8 +53,8 @@ def test_search_by_multiple_uris(session_mock, sp_track_mock, provider):
assert track.bitrate == 160


def test_search_when_offline_returns_nothing(provider, caplog):
provider._backend._online.is_set.return_value = False
def test_search_when_offline_returns_nothing(session_mock, provider, caplog):
session_mock.connection.state = spotify.ConnectionState.OFFLINE

result = provider.search({'any': ['ABBA']})

Expand Down

0 comments on commit 4d3cd01

Please sign in to comment.