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)
| 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. |
| 57 | func 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 | |
| 88 | func TestConcurrentFetch(t *testing.T) { |
| 89 | t.Parallel() |
nothing calls this directly
no test coverage detected