TestNamespacedCacheIsolation verifies that namespaced caches are not affected by clearing the global cache.
(t *testing.T)
| 114 | |
| 115 | // TestNamespacedCacheIsolation verifies that namespaced caches are not affected by clearing the global cache. |
| 116 | func TestNamespacedCacheIsolation(t *testing.T) { |
| 117 | resetCacheState() |
| 118 | defer resetCacheState() |
| 119 | |
| 120 | cacheDir := t.TempDir() |
| 121 | |
| 122 | if err := InitGlobalCache(cacheDir); err != nil { |
| 123 | t.Fatalf("InitGlobalCache: %v", err) |
| 124 | } |
| 125 | |
| 126 | global := GetGlobalCache() |
| 127 | defer func() { |
| 128 | _ = global.Close() |
| 129 | }() |
| 130 | |
| 131 | nsCache := GetNamespacedCache("test-plugin") |
| 132 | defer func() { |
| 133 | _ = nsCache.Close() |
| 134 | }() |
| 135 | |
| 136 | type payload struct { |
| 137 | Value string |
| 138 | } |
| 139 | |
| 140 | item := payload{Value: "persist"} |
| 141 | |
| 142 | if err := nsCache.Set("ns-key", item, time.Hour); err != nil { |
| 143 | t.Fatalf("namespaced set: %v", err) |
| 144 | } |
| 145 | |
| 146 | if err := global.Clear(); err != nil { |
| 147 | t.Fatalf("global clear: %v", err) |
| 148 | } |
| 149 | |
| 150 | var got payload |
| 151 | found, err := nsCache.Get("ns-key", &got) |
| 152 | if err != nil { |
| 153 | t.Fatalf("namespaced get: %v", err) |
| 154 | } |
| 155 | |
| 156 | if !found { |
| 157 | t.Fatal("expected namespaced cache item to persist after global clear") |
| 158 | } |
| 159 | |
| 160 | if got != item { |
| 161 | t.Fatalf("expected %v, got %v", item, got) |
| 162 | } |
| 163 | } |
nothing calls this directly
no test coverage detected