- The covers that come from the Cover Art Archive now live in a folder of their own (images/album-remote). Taking the provider's data back deletes that folder, so nothing from the archive is left whatever became of its records: a rescan makes a record stale and a later search that finds nothing replaces it, and neither of those used to remove the files. A cover that moves between the user's files and the archive, or goes away, no longer leaves the old files behind. - A redirect to a place that is not allowed is an outage (502, not remembered, an older cover served), not "no cover" for a day for every album. A picture over the limit is still "no cover". - A track that moves to another album (a fixed title) leaves the first with no track: it gives up its release group id, so the album the track went to can take it. An album that has an id keeps it; an id another album owns is not given twice. - Waiting for a local slot is no longer reported as the provider being unreachable, and a build whose reads were purged three times is not served with files that were never written. - README and spec no longer say covers never contact anybody. - Tests: the new folder and the purge after a rescan, files left by a replaced cover, the local wait, repeated purges, a refused redirect, the update path with a taken id and the emptied album.
268 lines
9.1 KiB
Go
268 lines
9.1 KiB
Go
package images
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"image/color"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type fakeRemote struct {
|
|
mu sync.Mutex
|
|
found Found
|
|
err error
|
|
calls int32
|
|
gate chan struct{}
|
|
}
|
|
|
|
func (f *fakeRemote) AlbumImage(_ context.Context, id int64) (Found, error) {
|
|
atomic.AddInt32(&f.calls, 1)
|
|
f.mu.Lock()
|
|
gate, found, err := f.gate, f.found, f.err
|
|
f.mu.Unlock()
|
|
if gate != nil {
|
|
<-gate
|
|
}
|
|
return found, err
|
|
}
|
|
|
|
func (f *fakeRemote) set(found Found, err error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.found, f.err = found, err
|
|
}
|
|
|
|
type remoteRig struct {
|
|
*rig
|
|
remote *fakeRemote
|
|
}
|
|
|
|
func newRemoteRig(t *testing.T) *remoteRig {
|
|
t.Helper()
|
|
r := &remoteRig{rig: newRig(t), remote: &fakeRemote{}}
|
|
r.remote.found = Found{Data: jpegBytes(t, 1200, 1200, color.RGBA{10, 120, 10, 255})}
|
|
r.svc.SetAlbumRemote(r.remote)
|
|
return r
|
|
}
|
|
|
|
func TestTheFilesOfTheUserComeBeforeTheArchive(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.write(t, "Album One/cover.jpg", jpegBytes(t, 400, 400, color.RGBA{200, 0, 0, 255}))
|
|
r.rig.read(t, 1, Large)
|
|
if n := atomic.LoadInt32(&r.remote.calls); n != 0 {
|
|
t.Errorf("the archive was asked %d times although the files had a cover", n)
|
|
}
|
|
if got := r.store.recs["album/1"].Source; got != SourceFolder {
|
|
t.Errorf("source = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAnAlbumWithNoPictureInItsFilesGetsTheCoverOfTheArchiveAndKeepsIt(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
big := r.rig.read(t, 1, Large)
|
|
if len(big) == 0 {
|
|
t.Fatal("no cover")
|
|
}
|
|
rec := r.store.recs["album/1"]
|
|
if rec.Status != StatusOK || rec.Source != SourceCoverArt || rec.ETag == "" {
|
|
t.Errorf("record = %+v", rec)
|
|
}
|
|
for i := 0; i < 3; i++ {
|
|
r.rig.read(t, 1, Small)
|
|
}
|
|
if n := atomic.LoadInt32(&r.remote.calls); n != 1 {
|
|
t.Errorf("the archive was asked %d times, want 1: the cover is stored", n)
|
|
}
|
|
info, err := os.Stat(filepath.Join(r.data, "images", "album-remote", "1-large.jpg"))
|
|
if err != nil || info.Mode().Perm()&0o077 != 0 {
|
|
t.Errorf("file: %v %v", info, err)
|
|
}
|
|
}
|
|
|
|
func TestWhatTheArchiveSaysAboutNoCoverIsRememberedLikeAnyNone(t *testing.T) {
|
|
for _, why := range []error{ErrNoImage, ErrNoID, ErrProviderDisabled} {
|
|
r := newRemoteRig(t)
|
|
r.remote.set(Found{}, why)
|
|
if _, err := r.svc.AlbumCover(context.Background(), 1, Large); !errors.Is(err, ErrNoCover) {
|
|
t.Errorf("%v: err = %v, want ErrNoCover", why, err)
|
|
}
|
|
if rec := r.store.recs["album/1"]; rec.Status != StatusNone {
|
|
t.Errorf("%v: record = %+v", why, rec)
|
|
}
|
|
for i := 0; i < 3; i++ {
|
|
_, _ = r.svc.AlbumCover(context.Background(), 1, Large)
|
|
}
|
|
if n := atomic.LoadInt32(&r.remote.calls); n != 1 {
|
|
t.Errorf("%v: asked %d times within the day, want 1", why, n)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnOutageOfTheArchiveIsNotRememberedAndIsReportedAsSuch(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.remote.set(Found{}, ErrSourceUnavailable)
|
|
if _, err := r.svc.AlbumCover(context.Background(), 1, Large); !errors.Is(err, ErrSourceUnavailable) {
|
|
t.Errorf("err = %v, want ErrSourceUnavailable", err)
|
|
}
|
|
if len(r.store.recs) != 0 {
|
|
t.Errorf("records = %v: the next request must try again", r.store.recs)
|
|
}
|
|
r.remote.set(Found{Data: jpegBytes(t, 600, 600, color.White)}, nil)
|
|
if got := r.rig.read(t, 1, Large); len(got) == 0 {
|
|
t.Error("no cover once the archive is back")
|
|
}
|
|
}
|
|
|
|
func TestAnOldCoverOfTheArchiveIsServedWhenItCannotBeCheckedAgain(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
first := r.rig.read(t, 1, Large)
|
|
r.now = r.now.Add(CoverTTL + time.Hour)
|
|
r.remote.set(Found{}, ErrSourceUnavailable)
|
|
if again := r.rig.read(t, 1, Large); !bytes.Equal(first, again) {
|
|
t.Error("the stored cover was not served when the archive could not be reached")
|
|
}
|
|
// And a cover that the files now have wins over the archive's.
|
|
r.write(t, "Album One/cover.jpg", jpegBytes(t, 300, 300, color.RGBA{0, 0, 200, 255}))
|
|
r.remote.set(Found{Data: jpegBytes(t, 600, 600, color.White)}, nil)
|
|
if now := r.rig.read(t, 1, Large); bytes.Equal(first, now) {
|
|
t.Error("the cover from the files did not replace the archive's")
|
|
}
|
|
if got := r.store.recs["album/1"].Source; got != SourceFolder {
|
|
t.Errorf("source = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAPurgeDuringTheReadOfACoverIsNotUndoneByIt(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.remote.gate = make(chan struct{})
|
|
done := make(chan error, 1)
|
|
go func() { _, err := r.svc.AlbumCover(context.Background(), 1, Large); done <- err }()
|
|
time.Sleep(60 * time.Millisecond)
|
|
r.svc.BeginPurge()
|
|
r.remote.set(Found{}, ErrProviderDisabled)
|
|
close(r.remote.gate)
|
|
if err := <-done; !errors.Is(err, ErrNoCover) {
|
|
t.Fatalf("err = %v, want no cover: the provider is off after the purge", err)
|
|
}
|
|
if rec := r.store.recs["album/1"]; rec.Status == StatusOK {
|
|
t.Errorf("record = %+v: what the read found before the purge came back", rec)
|
|
}
|
|
if entries, _ := os.ReadDir(filepath.Join(r.data, "images", "album")); len(entries) != 0 {
|
|
t.Errorf("%d files left by a read that started before the purge", len(entries))
|
|
}
|
|
}
|
|
|
|
func TestPurgingTheArchiveCoversRemovesAllOfThemWhateverBecameOfTheirRecords(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.rig.read(t, 1, Large) // from the archive
|
|
r.src.files[2] = []File{{Root: r.root, RelPath: "Album One/01.mp3"}}
|
|
r.write(t, "Album One/folder.jpg", jpegBytes(t, 300, 300, color.White))
|
|
r.remote.set(Found{}, ErrNoImage)
|
|
r.rig.read(t, 2, Large) // from the folder
|
|
|
|
// The records of the archive cover change (a rescan makes it stale) and the files must still go.
|
|
r.store.clear("album", 1)
|
|
n, err := r.svc.RemoveRemoteAlbumFiles()
|
|
if err != nil || n != 2 {
|
|
t.Fatalf("removed %d files err = %v, want the two sizes of the archive cover", n, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.data, "images", "album-remote")); !os.IsNotExist(err) {
|
|
t.Error("the folder of the archive covers is still there")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.data, "images", "album", "2-large.jpg")); err != nil {
|
|
t.Errorf("the cover from the folder went too: %v", err)
|
|
}
|
|
if n, err := r.svc.RemoveRemoteAlbumFiles(); err != nil || n != 0 {
|
|
t.Errorf("again: %d %v", n, err)
|
|
}
|
|
}
|
|
|
|
func TestACoverFromTheArchiveLivesApartFromTheOnesFromTheUsersFiles(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.rig.read(t, 1, Large)
|
|
if _, err := os.Stat(filepath.Join(r.data, "images", "album-remote", "1-large.jpg")); err != nil {
|
|
t.Errorf("the archive cover is not in its own folder: %v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(r.data, "images", "album", "1-large.jpg")); !os.IsNotExist(err) {
|
|
t.Error("the archive cover is among the covers from the user's files")
|
|
}
|
|
}
|
|
|
|
func TestACoverFromTheFilesReplacingOneFromTheArchiveLeavesNoOrphan(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.rig.read(t, 1, Large)
|
|
r.write(t, "Album One/cover.jpg", jpegBytes(t, 300, 300, color.RGBA{0, 0, 200, 255}))
|
|
r.store.clear("album", 1) // a file of the album changed
|
|
r.rig.read(t, 1, Large)
|
|
if _, err := os.Stat(filepath.Join(r.data, "images", "album-remote", "1-large.jpg")); !os.IsNotExist(err) {
|
|
t.Error("the archive cover stayed on disk after the files got their own")
|
|
}
|
|
// And the other way round: no picture any more (the archive says so) removes the old file too.
|
|
r2 := newRemoteRig(t)
|
|
r2.rig.read(t, 1, Large)
|
|
r2.now = r2.now.Add(CoverTTL + time.Hour)
|
|
r2.remote.set(Found{}, ErrNoImage)
|
|
if _, err := r2.svc.AlbumCover(context.Background(), 1, Large); !errors.Is(err, ErrNoCover) {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
if entries, _ := os.ReadDir(filepath.Join(r2.data, "images", "album-remote")); len(entries) != 0 {
|
|
t.Errorf("%d files left for a cover that is gone", len(entries))
|
|
}
|
|
}
|
|
|
|
func TestAWaitForALocalSlotIsNotAnOutageOfTheProvider(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.svc.buildTimeout = 40 * time.Millisecond
|
|
for i := 0; i < MaxParallelBuilds; i++ {
|
|
r.svc.slots <- struct{}{} // every local slot is taken
|
|
}
|
|
_, err := r.svc.AlbumCover(context.Background(), 1, Large)
|
|
if errors.Is(err, ErrSourceUnavailable) {
|
|
t.Errorf("err = %v: waiting for a local slot says nothing about the provider", err)
|
|
}
|
|
if err == nil {
|
|
t.Error("no error although the build could not start")
|
|
}
|
|
if n := atomic.LoadInt32(&r.remote.calls); n != 0 {
|
|
t.Errorf("the provider was asked %d times", n)
|
|
}
|
|
}
|
|
|
|
func TestPurgesThatKeepHappeningDoNotLeaveARecordWithoutFiles(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.remote.gate = make(chan struct{})
|
|
done := make(chan error, 1)
|
|
go func() { _, err := r.svc.AlbumCover(context.Background(), 1, Large); done <- err }()
|
|
for i := 0; i < 3; i++ {
|
|
time.Sleep(30 * time.Millisecond)
|
|
r.svc.BeginPurge()
|
|
if i == 2 {
|
|
close(r.remote.gate)
|
|
}
|
|
}
|
|
err := <-done
|
|
if err == nil {
|
|
if _, serr := os.Stat(filepath.Join(r.data, "images", "album-remote", "1-large.jpg")); serr != nil {
|
|
t.Errorf("a cover is served but its file was never written: %v", serr)
|
|
}
|
|
}
|
|
if rec := r.store.recs["album/1"]; rec.Status == StatusOK {
|
|
if _, serr := os.Stat(filepath.Join(r.data, "images", "album-remote", "1-large.jpg")); serr != nil {
|
|
t.Errorf("an ok record without its file: %v", serr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSomethingThatIsNotAPictureFromTheArchiveIsNoCover(t *testing.T) {
|
|
r := newRemoteRig(t)
|
|
r.remote.set(Found{Data: []byte("<html>blocked</html>")}, nil)
|
|
if _, err := r.svc.AlbumCover(context.Background(), 1, Large); !errors.Is(err, ErrNoCover) {
|
|
t.Errorf("err = %v", err)
|
|
}
|
|
}
|