sonda/internal/library/autoscan_test.go
tgiachi 89f1b5275a feat: scan the libraries by themselves when their music changed
Every 15 minutes (library.auto_scan_minutes, SONDA_AUTO_SCAN_MINUTES, 0 turns it off)
Sonda takes the signature of each library, a hash of the names of everything in it folder
by folder, which costs only the names of the files and so suits a NAS that sleeps. A
library whose signature changed (a song or a folder came, went or was renamed) is
scanned the usual way; every library is also scanned at least once a day, to find a song
tagged again where it stands.

It does not scan while another scan runs, tries again at the next look when a scan failed,
leaves alone a folder that cannot be read (a NAS that is not mounted), and ignores the
housekeeping folders of a NAS.
2026-11-16 13:05:09 +01:00

328 lines
9.1 KiB
Go

package library
import (
"context"
"errors"
"os"
"path/filepath"
"sync"
"testing"
"time"
)
// ---- the signature of a folder ----
func tree(t *testing.T) string {
t.Helper()
root := t.TempDir()
for _, f := range []string{"a/one.mp3", "a/two.flac", "b/c/three.mp3"} {
touch(t, root, f)
}
settle(t, root)
return root
}
func touch(t *testing.T, root, rel string) {
t.Helper()
p := filepath.Join(root, filepath.FromSlash(rel))
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
}
// settle gives every folder of the tree a moment far in the past, so that a change made after it moves the time of the
// folder it touches, whatever the resolution of the file system.
func settle(t *testing.T, root string) {
t.Helper()
old := time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC) // always the same, so that two calls leave the same time
var dirs []string
_ = filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
if err == nil && d.IsDir() {
dirs = append(dirs, p)
}
return nil
})
for i := len(dirs) - 1; i >= 0; i-- {
if err := os.Chtimes(dirs[i], old, old); err != nil {
t.Fatal(err)
}
}
}
func sig(t *testing.T, root string) string {
t.Helper()
s, err := Signature(context.Background(), root)
if err != nil {
t.Fatal(err)
}
return s
}
func TestTheSignatureOfAFolderChangesWhenMusicComesOrGoes(t *testing.T) {
root := tree(t)
base := sig(t, root)
if sig(t, root) != base {
t.Fatal("the same folder, two signatures")
}
for name, change := range map[string]func(){
"a song comes": func() { touch(t, root, "a/new.mp3") },
"a song goes": func() { _ = os.Remove(filepath.Join(root, "a/one.mp3")) },
"a song is renamed": func() { _ = os.Rename(filepath.Join(root, "a/two.flac"), filepath.Join(root, "a/deux.flac")) },
"a folder comes": func() { touch(t, root, "d/new.mp3") },
"a folder goes": func() { _ = os.RemoveAll(filepath.Join(root, "b")) },
"something is dropped": func() { touch(t, root, "b/c/cover.jpg") },
"a deep folder changes": func() { touch(t, root, "b/c/more.flac") },
} {
fresh := tree(t)
before := sig(t, fresh)
root = fresh
change()
if sig(t, fresh) == before {
t.Errorf("%s: the signature did not change", name)
}
}
}
func TestTheSignatureLeavesOutTheHousekeepingFoldersOfANAS(t *testing.T) {
root := tree(t)
base := sig(t, root)
// A NAS writes thumbnails in @eaDir all the time: what happens inside it is not a change of the music.
if err := os.MkdirAll(filepath.Join(root, "a", "@eaDir"), 0o755); err != nil {
t.Fatal(err)
}
settle(t, root)
withDir := sig(t, root)
touch(t, root, "a/@eaDir/thumb.jpg")
touch(t, root, "a/@eaDir/deeper/thumb2.jpg")
settle(t, root)
if got := sig(t, root); got != withDir {
t.Errorf("a change inside @eaDir moved the signature")
}
_ = base
}
func TestTheSignatureOfAFolderThatCannotBeReadIsAnError(t *testing.T) {
if _, err := Signature(context.Background(), filepath.Join(t.TempDir(), "nothing")); err == nil {
t.Fatal("no error for a folder that is not there")
}
file := filepath.Join(t.TempDir(), "f")
_ = os.WriteFile(file, nil, 0o644)
if _, err := Signature(context.Background(), file); err == nil {
t.Fatal("no error for a file")
}
}
func TestTheSignatureFollowsTheLinkOfTheRootButNotOthers(t *testing.T) {
root := tree(t)
link := filepath.Join(t.TempDir(), "music")
if err := os.Symlink(root, link); err != nil {
t.Skip("no symlinks here")
}
if sig(t, link) != sig(t, root) {
t.Error("a library that is a link has another signature than its target")
}
outside := tree(t)
if err := os.Symlink(outside, filepath.Join(root, "outside")); err != nil {
t.Fatal(err)
}
settle(t, root)
before := sig(t, root)
touch(t, outside, "a/changed.mp3")
if sig(t, root) != before {
t.Error("a link inside the folder was followed")
}
}
func TestTheSignatureStopsWithTheContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := Signature(ctx, tree(t)); !errors.Is(err, context.Canceled) {
t.Fatalf("err = %v", err)
}
}
// ---- the watcher ----
type fakeScans struct {
mu sync.Mutex
started []int64
running bool
busy bool // StartLibrary answers ErrScanRunning
lastErr string // what the scan that finished says
finishes int // how many Status calls say "running" before it is over
left int
}
func (f *fakeScans) StartLibrary(_ context.Context, id int64, _ ...ScanOption) error {
f.mu.Lock()
defer f.mu.Unlock()
if f.busy {
return ErrScanRunning
}
f.started = append(f.started, id)
f.left = f.finishes
return nil
}
func (f *fakeScans) Status() Status {
f.mu.Lock()
defer f.mu.Unlock()
if f.left > 0 {
f.left--
return Status{Running: true}
}
return Status{LastError: f.lastErr}
}
func (f *fakeScans) count() int {
f.mu.Lock()
defer f.mu.Unlock()
return len(f.started)
}
type fakeLibs struct{ libs []Library }
func (f *fakeLibs) ListLibraries(context.Context) ([]Library, error) { return f.libs, nil }
func (f *fakeLibs) GetLibrary(_ context.Context, id int64) (Library, error) {
for _, l := range f.libs {
if l.ID == id {
return l, nil
}
}
return Library{}, ErrLibraryNotFound
}
func newWatcher(scans *fakeScans, libs ...Library) (*AutoScanner, *time.Time) {
now := time.Unix(1_700_000_000, 0)
a := NewAutoScanner(scans, &fakeLibs{libs: libs}, time.Minute)
a.now = func() time.Time { return now }
a.poll = time.Millisecond
return a, &now
}
func TestTheWatcherScansALibraryThatChangedAndLeavesOneThatDidNot(t *testing.T) {
ctx := context.Background()
rock, jazz := tree(t), tree(t)
scans := &fakeScans{}
a, _ := newWatcher(scans, Library{ID: 1, Path: rock}, Library{ID: 2, Path: jazz})
a.tick(ctx) // the first time nothing is known: each library is looked at
if scans.count() != 2 {
t.Fatalf("first tick: %v", scans.started)
}
a.tick(ctx)
if scans.count() != 2 {
t.Fatalf("nothing changed, yet %v", scans.started)
}
touch(t, jazz, "a/new.flac")
a.tick(ctx)
if got := scans.started; len(got) != 3 || got[2] != 2 {
t.Fatalf("only jazz changed: %v", got)
}
a.tick(ctx)
if scans.count() != 3 {
t.Fatalf("after the scan nothing changed: %v", scans.started)
}
}
func TestAScanThatCouldNotStartOrFailedIsTriedAgain(t *testing.T) {
ctx := context.Background()
root := tree(t)
scans := &fakeScans{busy: true}
a, _ := newWatcher(scans, Library{ID: 1, Path: root})
a.tick(ctx) // somebody else is scanning
scans.busy = false
a.tick(ctx)
if scans.count() != 1 {
t.Fatalf("a scan that did not start was forgotten: %v", scans.started)
}
scans.lastErr = "directory is empty but 10 indexed files exist"
touch(t, root, "a/new.mp3")
a.tick(ctx) // starts, and fails
a.tick(ctx) // the same change is still not scanned: try again
if scans.count() != 3 {
t.Fatalf("a failed scan was not tried again: %v", scans.started)
}
scans.lastErr = ""
a.tick(ctx)
a.tick(ctx)
if scans.count() != 4 {
t.Fatalf("after it worked it was scanned again: %v", scans.started)
}
}
func TestAnUnreadableLibraryIsLeftAlone(t *testing.T) {
scans := &fakeScans{}
a, _ := newWatcher(scans, Library{ID: 1, Path: filepath.Join(t.TempDir(), "unmounted")})
a.tick(context.Background())
if scans.count() != 0 {
t.Fatalf("a library that cannot be read was scanned: %v", scans.started)
}
}
func TestEveryLibraryIsScannedOnceADayEvenIfNothingMovedItsFolders(t *testing.T) {
// A song that is tagged again keeps its folder as it was: only the file knows. A scan once a day finds it.
ctx := context.Background()
scans := &fakeScans{}
a, now := newWatcher(scans, Library{ID: 1, Path: tree(t)})
a.tick(ctx)
*now = now.Add(23 * time.Hour)
a.tick(ctx)
if scans.count() != 1 {
t.Fatalf("after 23 hours: %v", scans.started)
}
*now = now.Add(2 * time.Hour)
a.tick(ctx)
if scans.count() != 2 {
t.Fatalf("after 25 hours: %v", scans.started)
}
}
func TestTheWatcherWaitsForTheScanToEndBeforeItGoesOn(t *testing.T) {
ctx := context.Background()
scans := &fakeScans{finishes: 3}
a, _ := newWatcher(scans, Library{ID: 1, Path: tree(t)}, Library{ID: 2, Path: tree(t)})
a.tick(ctx)
if scans.count() != 2 || scans.left != 0 {
t.Fatalf("%v, %d left", scans.started, scans.left)
}
}
func TestADeletedLibraryIsForgottenAndANewOneIsLookedAt(t *testing.T) {
ctx := context.Background()
scans := &fakeScans{}
libs := &fakeLibs{libs: []Library{{ID: 1, Path: tree(t)}}}
a := NewAutoScanner(scans, libs, time.Minute)
a.poll = time.Millisecond
a.tick(ctx)
libs.libs = nil
a.tick(ctx)
if len(a.seen) != 0 {
t.Fatalf("a library that is gone is still remembered: %v", a.seen)
}
libs.libs = []Library{{ID: 1, Path: tree(t)}} // another one with the same id, later
a.tick(ctx)
if scans.count() != 2 {
t.Fatalf("a new library with an old id: %v", scans.started)
}
}
func TestRunStopsWithItsContext(t *testing.T) {
scans := &fakeScans{}
a := NewAutoScanner(scans, &fakeLibs{}, time.Hour)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { a.Run(ctx); close(done) }()
cancel()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Run did not stop")
}
}