MCPcopy Index your code
hub / github.com/coder/coder / TestRelease

Function TestRelease

coderd/files/cache_test.go:258–338  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

256}
257
258func TestRelease(t *testing.T) {
259 t.Parallel()
260 ctx := dbauthz.AsFileReader(t.Context())
261
262 const fileSize = 10
263 reg := prometheus.NewRegistry()
264 dbM := dbmock.NewMockStore(gomock.NewController(t))
265 dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
266 return database.File{
267 Data: make([]byte, fileSize),
268 }, nil
269 }).AnyTimes()
270
271 c := files.New(reg, &coderdtest.FakeAuthorizer{})
272
273 batches := 100
274 ids := make([]uuid.UUID, 0, batches)
275 for range batches {
276 ids = append(ids, uuid.New())
277 }
278
279 releases := make(map[uuid.UUID][]func(), 0)
280 // Acquire a bunch of references
281 batchSize := 10
282 for openedIdx, id := range ids {
283 for batchIdx := range batchSize {
284 it, err := c.Acquire(ctx, dbM, id)
285 require.NoError(t, err)
286 releases[id] = append(releases[id], it.Close)
287
288 // Each time a new file is opened, the metrics should be updated as so:
289 opened := openedIdx + 1
290 // Number of unique files opened is equal to the idx of the ids.
291 require.Equal(t, opened, c.Count())
292 require.Equal(t, opened, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_current"), nil))
293 // Current file size is unique files * file size.
294 require.Equal(t, opened*fileSize, promhelp.GaugeValue(t, reg, cachePromMetricName("open_files_size_bytes_current"), nil))
295 // The number of refs is the current iteration of both loops.
296 require.Equal(t, ((opened-1)*batchSize)+(batchIdx+1), promhelp.GaugeValue(t, reg, cachePromMetricName("open_file_refs_current"), nil))
297 }
298 }
299
300 // Make sure cache is fully loaded
301 require.Equal(t, c.Count(), batches)
302
303 // Now release all of the references
304 for closedIdx, id := range ids {
305 stillOpen := len(ids) - closedIdx
306 for closingIdx := range batchSize {
307 releases[id][0]()
308 releases[id] = releases[id][1:]
309
310 // Each time a file is released, the metrics should decrement the file refs
311 require.Equal(t, (stillOpen*batchSize)-(closingIdx+1), promhelp.GaugeValue(t, reg, cachePromMetricName("open_file_refs_current"), nil))
312
313 closed := closingIdx+1 == batchSize
314 if closed {
315 continue

Callers

nothing calls this directly

Calls 13

EXPECTMethod · 0.95
AsFileReaderFunction · 0.92
NewMockStoreFunction · 0.92
NewFunction · 0.92
GaugeValueFunction · 0.92
CounterValueFunction · 0.92
cachePromMetricNameFunction · 0.85
CountMethod · 0.80
ContextMethod · 0.65
GetFileByIDMethod · 0.65
NewMethod · 0.65
AcquireMethod · 0.65

Tested by

no test coverage detected