(t *testing.T)
| 174 | } |
| 175 | |
| 176 | func TestCacheClean(t *testing.T) { |
| 177 | cfg := config.Cache{ |
| 178 | Name: "foobar", |
| 179 | FileSystem: config.FileSystemCacheConfig{ |
| 180 | Dir: testDir, |
| 181 | MaxSize: 8192, |
| 182 | }, |
| 183 | Expire: config.Duration(time.Minute), |
| 184 | } |
| 185 | c, err := newFilesSystemCache(cfg, time.Second) |
| 186 | if err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | defer c.Close() |
| 190 | |
| 191 | // populate the cache with a lot of entries |
| 192 | for i := 0; i < 1000; i++ { |
| 193 | key := &Key{ |
| 194 | Query: []byte(fmt.Sprintf("SELECT %d cache clean", i)), |
| 195 | } |
| 196 | trw := &testResponseWriter{} |
| 197 | crw, err := NewTmpFileResponseWriter(trw, testTmpWriterDir) |
| 198 | if err != nil { |
| 199 | t.Fatalf("create tmp cache: %s", err) |
| 200 | } |
| 201 | defer crw.Close() |
| 202 | |
| 203 | value := fmt.Sprintf("very big value %d", i) |
| 204 | bs := bytes.NewBufferString(value) |
| 205 | if _, err := io.Copy(crw, bs); err != nil { |
| 206 | t.Fatalf("cannot send response to cache: %s", err) |
| 207 | } |
| 208 | |
| 209 | reader, err := crw.Reader() |
| 210 | if err != nil { |
| 211 | t.Fatalf("failed to put it to cache: %s", err) |
| 212 | } |
| 213 | if _, err := c.Put(reader, ContentMetadata{}, key); err != nil { |
| 214 | t.Fatalf("failed to put it to cache: %s", err) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Forcibly clean the cache |
| 219 | c.clean() |
| 220 | |
| 221 | // Make sure the total cache size doesnt exceed MaxSize |
| 222 | stats := c.Stats() |
| 223 | if stats.Size <= 0 { |
| 224 | t.Fatalf("cache size must be greater than 0; got %d", stats.Size) |
| 225 | } |
| 226 | if stats.Size > c.maxSize { |
| 227 | t.Fatalf("cache size %d cannot exceed %d", stats.Size, c.maxSize) |
| 228 | } |
| 229 | |
| 230 | if stats.Items <= 0 { |
| 231 | t.Fatalf("cache items must be greater than 0; got %d", stats.Items) |
| 232 | } |
| 233 | if stats.Items > 1000 { |
nothing calls this directly
no test coverage detected