|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestArtifactCache_GetSet(t *testing.T) { |
| 8 | + cache, err := newArtifactCache(10) |
| 9 | + if err != nil { |
| 10 | + t.Fatalf("failed to create cache: %v", err) |
| 11 | + } |
| 12 | + |
| 13 | + bucket := "test-bucket" |
| 14 | + key := "test-key" |
| 15 | + content := []byte("test content") |
| 16 | + |
| 17 | + // Cache miss on first get |
| 18 | + _, found := cache.Get(bucket, key) |
| 19 | + if found { |
| 20 | + t.Error("expected cache miss, got hit") |
| 21 | + } |
| 22 | + |
| 23 | + // Set content |
| 24 | + cache.Set(bucket, key, content) |
| 25 | + |
| 26 | + // Cache hit on second get |
| 27 | + retrieved, found := cache.Get(bucket, key) |
| 28 | + if !found { |
| 29 | + t.Error("expected cache hit, got miss") |
| 30 | + } |
| 31 | + if string(retrieved) != string(content) { |
| 32 | + t.Errorf("expected content %q, got %q", content, retrieved) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestArtifactCache_MultipleEntries(t *testing.T) { |
| 37 | + cache, err := newArtifactCache(10) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("failed to create cache: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Add multiple entries |
| 43 | + entries := map[string][]byte{ |
| 44 | + "bucket1/key1": []byte("content1"), |
| 45 | + "bucket1/key2": []byte("content2"), |
| 46 | + "bucket2/key1": []byte("content3"), |
| 47 | + } |
| 48 | + |
| 49 | + for key, content := range entries { |
| 50 | + cache.Set("bucket", key, content) |
| 51 | + } |
| 52 | + |
| 53 | + // Verify all entries are cached |
| 54 | + for key, expectedContent := range entries { |
| 55 | + retrieved, found := cache.Get("bucket", key) |
| 56 | + if !found { |
| 57 | + t.Errorf("expected cache hit for key %q", key) |
| 58 | + } |
| 59 | + if string(retrieved) != string(expectedContent) { |
| 60 | + t.Errorf("for key %q, expected %q, got %q", key, expectedContent, retrieved) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestArtifactCache_LRUEviction(t *testing.T) { |
| 66 | + cache, err := newArtifactCache(2) // Small cache for testing eviction |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("failed to create cache: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Fill cache to capacity |
| 72 | + cache.Set("bucket", "key1", []byte("content1")) |
| 73 | + cache.Set("bucket", "key2", []byte("content2")) |
| 74 | + |
| 75 | + // Add one more entry, should evict oldest |
| 76 | + cache.Set("bucket", "key3", []byte("content3")) |
| 77 | + |
| 78 | + // key1 should have been evicted |
| 79 | + _, found := cache.Get("bucket", "key1") |
| 80 | + if found { |
| 81 | + t.Error("expected key1 to be evicted") |
| 82 | + } |
| 83 | + |
| 84 | + // key2 and key3 should still be present |
| 85 | + _, found = cache.Get("bucket", "key2") |
| 86 | + if !found { |
| 87 | + t.Error("expected key2 to be present") |
| 88 | + } |
| 89 | + |
| 90 | + _, found = cache.Get("bucket", "key3") |
| 91 | + if !found { |
| 92 | + t.Error("expected key3 to be present") |
| 93 | + } |
| 94 | +} |
0 commit comments