fix: go back to synchronous image processing

This commit is contained in:
Gabe Farrell 2025-06-12 00:30:01 -04:00
parent aba2b76def
commit 1a5a6acc95
10 changed files with 58 additions and 168 deletions

View file

@ -2,13 +2,11 @@ package catalog_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/gabehf/koito/internal/catalog"
"github.com/gabehf/koito/internal/cfg"
@ -19,8 +17,6 @@ import (
func TestImageLifecycle(t *testing.T) {
ip := catalog.NewImageProcessor(1)
// serve yuu.jpg as test image
imageBytes, err := os.ReadFile(filepath.Join("static", "yuu.jpg"))
require.NoError(t, err)
@ -33,59 +29,46 @@ func TestImageLifecycle(t *testing.T) {
imgID := uuid.New()
err = ip.EnqueueDownloadAndCache(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
require.NoError(t, err)
err = ip.EnqueueDownloadAndCache(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
require.NoError(t, err)
ip.WaitForIdle(5 * time.Second)
// ensure download is correct
imagePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
assert.NoError(t, waitForFile(imagePath, 1*time.Second))
_, err = os.Stat(imagePath)
assert.NoError(t, err)
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
assert.NoError(t, waitForFile(imagePath, 1*time.Second))
_, err = os.Stat(imagePath)
assert.NoError(t, err)
assert.NoError(t, catalog.DeleteImage(imgID))
// ensure delete works
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
assert.Error(t, waitForFile(imagePath, 1*time.Second))
_, err = os.Stat(imagePath)
assert.Error(t, err)
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
assert.Error(t, waitForFile(imagePath, 1*time.Second))
_, err = os.Stat(imagePath)
assert.Error(t, err)
// re-download for prune
err = ip.EnqueueDownloadAndCache(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
require.NoError(t, err)
err = ip.EnqueueDownloadAndCache(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
require.NoError(t, err)
ip.WaitForIdle(5 * time.Second)
assert.NoError(t, catalog.PruneOrphanedImages(context.Background(), store))
// ensure prune works
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
assert.Error(t, waitForFile(imagePath, 1*time.Second))
_, err = os.Stat(imagePath)
assert.Error(t, err)
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
assert.Error(t, waitForFile(imagePath, 1*time.Second))
}
func waitForFile(path string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for {
if _, err := os.Stat(path); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
}
if time.Now().After(deadline) {
return fmt.Errorf("timed out waiting for %s", path)
}
time.Sleep(20 * time.Millisecond)
}
_, err = os.Stat(imagePath)
assert.Error(t, err)
}