Some checks failed
PR Check / check-and-test (pull_request) Failing after 2m11s
- Added Prometheus client library for metrics collection. - Refactored garbage collection strategy from random deletion to LRU (Least Recently Used) deletion. - Introduced per-key locking in cache to prevent race conditions. - Enhanced logging with structured log messages for cache hits and misses. - Implemented a retry mechanism for upstream requests with exponential backoff. - Updated Go modules and indirect dependencies for better compatibility and performance. - Removed unused sync filesystem implementation. - Added version initialization to ensure a default version string.
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package steamcache
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCaching(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
td := t.TempDir()
|
|
|
|
os.WriteFile(filepath.Join(td, "key2"), []byte("value2"), 0644)
|
|
|
|
sc := New("localhost:8080", "1GB", 10, "1GB", 100, td, "", false)
|
|
|
|
if err := sc.vfs.Set("key", []byte("value")); err != nil {
|
|
t.Errorf("Set failed: %v", err)
|
|
}
|
|
if err := sc.vfs.Set("key1", []byte("value1")); err != nil {
|
|
t.Errorf("Set failed: %v", err)
|
|
}
|
|
|
|
if sc.diskgc.Size() != 17 {
|
|
t.Errorf("Size failed: got %d, want %d", sc.diskgc.Size(), 17)
|
|
}
|
|
|
|
if sc.vfs.Size() != 17 {
|
|
t.Errorf("Size failed: got %d, want %d", sc.vfs.Size(), 17)
|
|
}
|
|
|
|
if d, err := sc.vfs.Get("key"); err != nil {
|
|
t.Errorf("Get failed: %v", err)
|
|
} else if string(d) != "value" {
|
|
t.Errorf("Get failed: got %s, want %s", d, "value")
|
|
}
|
|
|
|
if d, err := sc.vfs.Get("key1"); err != nil {
|
|
t.Errorf("Get failed: %v", err)
|
|
} else if string(d) != "value1" {
|
|
t.Errorf("Get failed: got %s, want %s", d, "value1")
|
|
}
|
|
|
|
if d, err := sc.vfs.Get("key2"); err != nil {
|
|
t.Errorf("Get failed: %v", err)
|
|
} else if string(d) != "value2" {
|
|
t.Errorf("Get failed: got %s, want %s", d, "value2")
|
|
}
|
|
|
|
if sc.diskgc.Size() != 17 {
|
|
t.Errorf("Size failed: got %d, want %d", sc.diskgc.Size(), 17)
|
|
}
|
|
|
|
if sc.vfs.Size() != 17 {
|
|
t.Errorf("Size failed: got %d, want %d", sc.vfs.Size(), 17)
|
|
}
|
|
|
|
sc.memory.Delete("key2")
|
|
os.Remove(filepath.Join(td, "key2"))
|
|
|
|
if _, err := sc.vfs.Get("key2"); err == nil {
|
|
t.Errorf("Get failed: got nil, want error")
|
|
}
|
|
}
|