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

Function TestCancelledConcurrentFetch

coderd/files/cache_test.go:57–86  ·  view source on GitHub ↗

TestCancelledConcurrentFetch runs 2 Acquire calls. The first has a canceled context and will get a ctx.Canceled error. The second call should get a warmfirst error and try to fetch the file again, which should succeed.

(t *testing.T)

Source from the content-addressed store, hash-verified

55// context and will get a ctx.Canceled error. The second call should get a warmfirst error and try to fetch the file
56// again, which should succeed.
57func TestCancelledConcurrentFetch(t *testing.T) {
58 t.Parallel()
59
60 fileID := uuid.New()
61 dbM := dbmock.NewMockStore(gomock.NewController(t))
62
63 // The file fetch should succeed.
64 dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
65 return database.File{
66 ID: fileID,
67 Data: make([]byte, 100),
68 }, nil
69 })
70
71 cache := files.LeakCache{Cache: files.New(prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{})}
72
73 ctx := dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort))
74
75 // Cancel the context for the first call; should fail.
76 canceledCtx, cancel := context.WithCancel(ctx)
77 cancel()
78 _, err := cache.Acquire(canceledCtx, dbM, fileID)
79 require.ErrorIs(t, err, context.Canceled)
80
81 // Second call, that should succeed without fetching from the database again
82 // since the cache should be populated by the fetch the first request started
83 // even if it doesn't wait for completion.
84 _, err = cache.Acquire(ctx, dbM, fileID)
85 require.NoError(t, err)
86}
87
88func TestConcurrentFetch(t *testing.T) {
89 t.Parallel()

Callers

nothing calls this directly

Calls 8

EXPECTMethod · 0.95
AcquireMethod · 0.95
NewMockStoreFunction · 0.92
NewFunction · 0.92
AsFileReaderFunction · 0.92
ContextFunction · 0.92
NewMethod · 0.65
GetFileByIDMethod · 0.65

Tested by

no test coverage detected