Store each client's play queue in the database
The queue lived in memory and was shared by all clients, so a restart or a finished sync cleared it. Every client now has its own queue in the new queue and queue_song tables (added to the undeployed migration 000006). The music endpoints return the queue ID in the X-Queue-Id header and expect it back. A request without an ID, or with an ended one, starts a new queue. /music/reset ends the queue and returns a new ID, and a sync ends all running queues. Ended queues are kept as history. The deprecated legacy routes without the /api/v1 prefix are removed.
This commit is contained in:
1 parent
b5dc42b654
commit
a705d7b476
28 files changed
+1103
-320
No files matched your search
+180
-44
@@ -33,30 +33,55 @@ func addSoundtrackWithSongs(t *testing.T, store *fakedb.Store, musicDir, name st
|
||||
return st
|
||||
}
|
||||
|
||||
// newQueue starts a queue for the tests.
|
||||
func newQueue(t *testing.T, m *MusicService) uuid.UUID {
|
||||
t.Helper()
|
||||
id, err := m.ResolveQueue(context.Background(), "")
|
||||
require.NoError(t, err)
|
||||
return id
|
||||
}
|
||||
|
||||
// queueSong fetches a random song and adds it to the queue.
|
||||
func queueSong(t *testing.T, m *MusicService, queueID uuid.UUID) string {
|
||||
t.Helper()
|
||||
path, err := m.RandomSong(context.Background(), queueID)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, m.AddLatestToQueue(context.Background(), queueID))
|
||||
return path
|
||||
}
|
||||
|
||||
func TestMusicServiceEmptyQueue(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
ctx := context.Background()
|
||||
q := newQueue(t, m)
|
||||
|
||||
_, err := m.NextSong()
|
||||
_, err := m.NextSong(ctx, q)
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
_, err = m.PreviousSong()
|
||||
_, err = m.PreviousSong(ctx, q)
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
_, err = m.Song(0)
|
||||
_, err = m.Song(ctx, q, 0)
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
assert.Equal(t, SongInfo{}, m.SongInfo(context.Background()))
|
||||
assert.Empty(t, m.PlayedSongs(context.Background()))
|
||||
assert.NoError(t, m.AddLatestPlayed(context.Background()))
|
||||
assert.NoError(t, m.SetPlayed(context.Background(), -1))
|
||||
info, err := m.SongInfo(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, SongInfo{}, info)
|
||||
played, err := m.PlayedSongs(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, played)
|
||||
assert.NotNil(t, played, "an empty queue is [] in JSON")
|
||||
assert.NoError(t, m.AddLatestPlayed(ctx, q))
|
||||
assert.NoError(t, m.SetPlayed(ctx, q, -1))
|
||||
}
|
||||
|
||||
func TestMusicServiceNoSoundtracks(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
ctx := context.Background()
|
||||
q := newQueue(t, m)
|
||||
|
||||
_, err := m.RandomSong(ctx)
|
||||
_, err := m.RandomSong(ctx, q)
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
_, err = m.RandomSongLowChance(ctx)
|
||||
_, err = m.RandomSongLowChance(ctx, q)
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
_, err = m.RandomSongClassic(ctx)
|
||||
_, err = m.RandomSongClassic(ctx, q)
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
}
|
||||
|
||||
@@ -65,7 +90,7 @@ func TestMusicServiceSoundtracksWithoutSongs(t *testing.T) {
|
||||
store.AddSoundtrack(repository.Soundtrack{SoundtrackName: "Empty"})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
|
||||
_, err := m.RandomSong(context.Background())
|
||||
_, err := m.RandomSong(context.Background(), newQueue(t, m))
|
||||
assert.ErrorIs(t, err, ErrNoSongs)
|
||||
}
|
||||
|
||||
@@ -75,70 +100,182 @@ func TestMusicServiceQueue(t *testing.T) {
|
||||
addSoundtrackWithSongs(t, store, musicDir, "Game A", 0, "Song 1", "Song 2")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
q := newQueue(t, m)
|
||||
|
||||
first, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
second, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
first := queueSong(t, m, q)
|
||||
second := queueSong(t, m, q)
|
||||
|
||||
info := m.SongInfo(ctx)
|
||||
info, err := m.SongInfo(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Game A", info.Soundtrack)
|
||||
soundtrack, _ := store.Soundtrack("Game A")
|
||||
assert.Equal(t, soundtrack.Uuid, info.SoundtrackUuid)
|
||||
assert.Contains(t, []string{first, second}, songPathByUUID(store, info.SongUuid))
|
||||
assert.Equal(t, second, songPathByUUID(store, info.SongUuid))
|
||||
assert.Equal(t, 1, info.SongNo)
|
||||
assert.True(t, info.CurrentlyPlaying)
|
||||
|
||||
path, err := m.PreviousSong()
|
||||
path, err := m.PreviousSong(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, first, path)
|
||||
path, err = m.PreviousSong()
|
||||
path, err = m.PreviousSong(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, first, path, "previous stays on the first song")
|
||||
|
||||
path, err = m.NextSong()
|
||||
path, err = m.NextSong(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, second, path)
|
||||
path, err = m.NextSong()
|
||||
path, err = m.NextSong(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, second, path, "next stays on the last song")
|
||||
|
||||
path, err = m.Song(99)
|
||||
path, err = m.Song(ctx, q, 99)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, second, path, "song number is clamped")
|
||||
|
||||
played := m.PlayedSongs(ctx)
|
||||
played, err := m.PlayedSongs(ctx, q)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, played, 2)
|
||||
assert.False(t, played[0].CurrentlyPlaying)
|
||||
assert.True(t, played[1].CurrentlyPlaying)
|
||||
}
|
||||
|
||||
require.NoError(t, m.Reset(ctx))
|
||||
_, err = m.NextSong()
|
||||
func TestMusicServiceQueuesAreSeparate(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
q1, q2 := newQueue(t, m), newQueue(t, m)
|
||||
require.NotEqual(t, q1, q2)
|
||||
|
||||
queueSong(t, m, q1)
|
||||
queueSong(t, m, q1)
|
||||
queueSong(t, m, q2)
|
||||
|
||||
played, err := m.PlayedSongs(ctx, q1)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, played, 2)
|
||||
played, err = m.PlayedSongs(ctx, q2)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, played, 1)
|
||||
}
|
||||
|
||||
func TestMusicServiceQueueSurvivesRestart(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
q := newQueue(t, m)
|
||||
path := queueSong(t, m, q)
|
||||
|
||||
restarted := NewMusicService(store, t.TempDir())
|
||||
id, err := restarted.ResolveQueue(context.Background(), q.String())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, q, id)
|
||||
current, err := restarted.Song(context.Background(), q, 0)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, path, current)
|
||||
}
|
||||
|
||||
func TestMusicServiceNewQueue(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
old := newQueue(t, m)
|
||||
queueSong(t, m, old)
|
||||
|
||||
q, err := m.NewQueue(ctx, old)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, old, q)
|
||||
_, err = m.NextSong(ctx, q)
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
|
||||
resolved, err := m.ResolveQueue(ctx, old.String())
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, old, resolved, "an ended queue is not used again")
|
||||
played, err := m.PlayedSongs(ctx, old)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, played, 1, "ended queues are kept")
|
||||
}
|
||||
|
||||
func TestMusicServiceEndAllQueues(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
ctx := context.Background()
|
||||
q1, q2 := newQueue(t, m), newQueue(t, m)
|
||||
|
||||
require.NoError(t, m.EndAllQueues(ctx))
|
||||
for _, q := range []uuid.UUID{q1, q2} {
|
||||
id, err := m.ResolveQueue(ctx, q.String())
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, q, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMusicServiceResolveQueue(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
ctx := context.Background()
|
||||
q := newQueue(t, m)
|
||||
|
||||
id, err := m.ResolveQueue(ctx, q.String())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, q, id)
|
||||
for _, header := range []string{"", "not-a-uuid", uuid.NewString()} {
|
||||
id, err := m.ResolveQueue(ctx, header)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, uuid.Nil, id, header)
|
||||
assert.NotEqual(t, q, id, header)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMusicServiceRemovedSongInQueue(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
q := newQueue(t, m)
|
||||
queueSong(t, m, q)
|
||||
require.NoError(t, store.ClearSongs(ctx))
|
||||
|
||||
_, err := m.Song(ctx, q, 0)
|
||||
assert.ErrorIs(t, err, ErrNotFound)
|
||||
info, err := m.SongInfo(ctx, q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Game A", info.Soundtrack, "the stored names are kept")
|
||||
assert.Equal(t, "Song 1", info.Song)
|
||||
assert.NoError(t, m.AddLatestPlayed(ctx, q), "removed songs are not counted")
|
||||
}
|
||||
|
||||
func TestMusicServiceAddLatestToQueueWithoutFetch(t *testing.T) {
|
||||
m := NewMusicService(fakedb.New(), t.TempDir())
|
||||
m.AddLatestToQueue()
|
||||
_, err := m.NextSong()
|
||||
q := newQueue(t, m)
|
||||
require.NoError(t, m.AddLatestToQueue(context.Background(), q))
|
||||
_, err := m.NextSong(context.Background(), q)
|
||||
assert.ErrorIs(t, err, ErrQueueEmpty)
|
||||
}
|
||||
|
||||
func TestMusicServiceAddLatestToQueueOnlyOnce(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
q := newQueue(t, m)
|
||||
queueSong(t, m, q)
|
||||
require.NoError(t, m.AddLatestToQueue(context.Background(), q))
|
||||
|
||||
played, err := m.PlayedSongs(context.Background(), q)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, played, 1)
|
||||
}
|
||||
|
||||
func TestMusicServiceMarkPlayed(t *testing.T) {
|
||||
store := fakedb.New()
|
||||
st := addSoundtrackWithSongs(t, store, t.TempDir(), "Game A", 0, "Song 1")
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
q := newQueue(t, m)
|
||||
queueSong(t, m, q)
|
||||
|
||||
_, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
|
||||
require.NoError(t, m.AddLatestPlayed(ctx))
|
||||
require.NoError(t, m.SetPlayed(ctx, 0))
|
||||
require.NoError(t, m.SetPlayed(ctx, 5), "out of range is ignored")
|
||||
require.NoError(t, m.AddLatestPlayed(ctx, q))
|
||||
require.NoError(t, m.SetPlayed(ctx, q, 0))
|
||||
require.NoError(t, m.SetPlayed(ctx, q, 5), "out of range is ignored")
|
||||
|
||||
updated, _ := store.Soundtrack("Game A")
|
||||
assert.Equal(t, int32(2), updated.TimesPlayed)
|
||||
@@ -155,9 +292,10 @@ func TestMusicServiceRemovesBrokenSongs(t *testing.T) {
|
||||
writeFile(t, st.Path+wav, "wav")
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: "Wave", Path: st.Path + wav, FileName: &wav})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
q := newQueue(t, m)
|
||||
|
||||
for range 20 {
|
||||
path, err := m.RandomSong(context.Background())
|
||||
path, err := m.RandomSong(context.Background(), q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, st.Path+"Good.mp3", path)
|
||||
}
|
||||
@@ -172,9 +310,10 @@ func TestMusicServiceRandomSongClassic(t *testing.T) {
|
||||
missing := "Missing.mp3"
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: "Missing", Path: st.Path + missing, FileName: &missing})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
q := newQueue(t, m)
|
||||
|
||||
for range 20 {
|
||||
path, err := m.RandomSongClassic(context.Background())
|
||||
path, err := m.RandomSongClassic(context.Background(), q)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, st.Path+"Good.mp3", path)
|
||||
}
|
||||
@@ -244,12 +383,9 @@ func TestMarkPlayedUsesSongUUID(t *testing.T) {
|
||||
writeFile(t, st.Path+fileName, "copy")
|
||||
store.AddTestSong(repository.Song{SoundtrackID: st.ID, SongName: "Song", Path: st.Path + fileName, FileName: &fileName})
|
||||
m := NewMusicService(store, t.TempDir())
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := m.RandomSong(ctx)
|
||||
require.NoError(t, err)
|
||||
m.AddLatestToQueue()
|
||||
require.NoError(t, m.AddLatestPlayed(ctx))
|
||||
q := newQueue(t, m)
|
||||
queueSong(t, m, q)
|
||||
require.NoError(t, m.AddLatestPlayed(context.Background(), q))
|
||||
|
||||
songs := store.SongsOf(st.ID)
|
||||
require.Len(t, songs, 2)
|
||||
|
||||
Reference in new issue
Block a user