From 350e834e1884173b3181b515857c6ada89339481 Mon Sep 17 00:00:00 2001 From: chrislu Date: Thu, 4 Dec 2025 23:08:19 -0800 Subject: [PATCH] filer: add lookup error test and document test limitations Add TestSingleChunkCacherLookupError to test error handling when lookup fails. Document that full HTTP integration tests for SingleChunkCacher require global HTTP client initialization which is complex in unit tests. The download path is tested via FUSE integration tests. --- weed/filer/reader_cache_test.go | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/weed/filer/reader_cache_test.go b/weed/filer/reader_cache_test.go index 2aa20aaa7..610db51c0 100644 --- a/weed/filer/reader_cache_test.go +++ b/weed/filer/reader_cache_test.go @@ -2,6 +2,7 @@ package filer import ( "context" + "fmt" "sync" "sync/atomic" "testing" @@ -300,3 +301,40 @@ func TestSingleChunkCacherDoneSignal(t *testing.T) { } } +// ============================================================================ +// Tests that exercise SingleChunkCacher logic +// ============================================================================ +// +// Note: Full HTTP-based integration tests for SingleChunkCacher would require +// initializing the global HTTP client (weed/util/http/client), which is complex +// in a unit test environment. The tests above use pre-populated caches to test +// the cache fallback path. The SingleChunkCacher download path is tested via +// integration tests (e.g., FUSE mount tests) which have full initialization. +// +// The key behaviors tested here via the cache path: +// - Context cancellation propagation +// - Multiple concurrent readers +// - Fallback to chunkCache when cacher returns no data +// - Partial reads at different offsets +// - Downloader cleanup when exceeding limit + +// TestSingleChunkCacherLookupError tests handling of lookup errors +func TestSingleChunkCacherLookupError(t *testing.T) { + cache := newMockChunkCacheForReaderCache() + + // Lookup function that returns an error + lookupFn := func(ctx context.Context, fileId string) ([]string, error) { + return nil, fmt.Errorf("lookup failed for %s", fileId) + } + + rc := NewReaderCache(10, cache, lookupFn) + defer rc.destroy() + + buffer := make([]byte, 100) + _, err := rc.ReadChunkAt(context.Background(), buffer, "error-test", nil, false, 0, 100, true) + + if err == nil { + t.Error("Expected an error, got nil") + } +} +