Skip to content

test: unit test to excercise polluted file cache with error #18491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions coderd/files/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package files_test

import (
"context"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/sync/errgroup"

"cdr.dev/slog/sloggers/slogtest"
Expand All @@ -18,13 +21,70 @@ import (
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/files"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/testutil"
)

// TestCancelledFetch runs 2 Acquire calls. The first fails with a ctx.Canceled
// error. The second call should ignore the first error and try to fetch the file
// again, which should succeed.
func TestCancelledFetch(t *testing.T) {
t.Parallel()

fileID := uuid.New()
rdy := make(chan struct{})
dbM := dbmock.NewMockStore(gomock.NewController(t))

// First call should fail
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
// Wait long enough for the second call to be queued up.
<-rdy
return database.File{}, context.Canceled
})

// Second call should succeed
dbM.EXPECT().GetFileByID(gomock.Any(), gomock.Any()).DoAndReturn(func(mTx context.Context, fileID uuid.UUID) (database.File, error) {
return database.File{
ID: fileID,
Data: make([]byte, 100),
}, nil
})

//nolint:gocritic // Unit testing
ctx := dbauthz.AsFileReader(testutil.Context(t, testutil.WaitShort))
cache := files.NewFromStore(dbM, prometheus.NewRegistry(), &coderdtest.FakeAuthorizer{})

var wg sync.WaitGroup
wg.Add(2)

// First call that will fail
go func() {
_, err := cache.Acquire(ctx, fileID)
assert.ErrorIs(t, err, context.Canceled)
wg.Done()
}()

// Second call, that should succeed
go func() {
fs, err := cache.Acquire(ctx, fileID)
assert.NoError(t, err)
if fs != nil {
fs.Close()
}
wg.Done()
}()
Comment on lines +65 to +79
Copy link
Member Author

@Emyrk Emyrk Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should check that 1 error comes back, and 1 succeeds.

Good comment from @aslilac


// We need that second Acquire call to be queued up
time.Sleep(testutil.IntervalFast)

close(rdy)
wg.Wait()
}

// nolint:paralleltest,tparallel // Serially testing is easier
func TestCacheRBAC(t *testing.T) {
t.Parallel()
Expand Down
Loading