mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-17 03:06:42 -07:00
chore: push image processing to a dedicated thread
This commit is contained in:
parent
a35d1391a6
commit
aba2b76def
9 changed files with 168 additions and 54 deletions
|
|
@ -2,11 +2,13 @@ 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"
|
||||
|
|
@ -17,6 +19,8 @@ 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)
|
||||
|
|
@ -29,46 +33,59 @@ func TestImageLifecycle(t *testing.T) {
|
|||
|
||||
imgID := uuid.New()
|
||||
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
|
||||
err = ip.EnqueueDownloadAndCache(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
|
||||
require.NoError(t, err)
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
|
||||
err = ip.EnqueueDownloadAndCache(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())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, waitForFile(imagePath, 1*time.Second))
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, waitForFile(imagePath, 1*time.Second))
|
||||
|
||||
assert.NoError(t, catalog.DeleteImage(imgID))
|
||||
|
||||
// ensure delete works
|
||||
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
assert.Error(t, waitForFile(imagePath, 1*time.Second))
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
assert.Error(t, waitForFile(imagePath, 1*time.Second))
|
||||
|
||||
// re-download for prune
|
||||
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
|
||||
err = ip.EnqueueDownloadAndCache(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
|
||||
require.NoError(t, err)
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
|
||||
err = ip.EnqueueDownloadAndCache(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())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
assert.Error(t, waitForFile(imagePath, 1*time.Second))
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue