Skip to content
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

Data race fix for context slog.Logger #1193

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ jobs:
-
name: Test code
run: |
go test ./pkg/...
go test ./internal/...
go test -race ./pkg/...
go test -race ./internal/...
shell: bash

-
Expand Down
80 changes: 80 additions & 0 deletions pkg/handler/handler_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions pkg/handler/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

"golang.org/x/exp/slog"

httptestrecorder "github.com/Acconut/go-httptest-recorder"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -70,6 +72,69 @@ func TestPost(t *testing.T) {
a.Equal(int64(300), info.Size)
})

SubTest(t, "WithSlog", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)

gomock.InOrder(
store.EXPECT().NewUpload(gomock.Any(), FileInfo{
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
"empty": "",
},
}).Return(upload, nil),
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
ID: "foo",
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
"empty": "",
},
}, nil),
)

logHandler := NewMockSlogHandler(ctrl)
logger := slog.New(logHandler)
logHandler.EXPECT().WithAttrs(gomock.Any()).Return(logHandler).AnyTimes()
logHandler.EXPECT().Enabled(gomock.Any(), gomock.Any()).Return(true).AnyTimes()
logHandler.EXPECT().Handle(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()

handler, _ := NewHandler(Config{
StoreComposer: composer,
BasePath: "https://buy.art/files/",
NotifyCreatedUploads: true,
Logger: logger,
})

c := make(chan HookEvent, 1)
handler.CreatedUploads = c

(&httpTest{
Method: "POST",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Upload-Length": "300",
// Invalid Base64-encoded values should be ignored
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=, hah INVALID, empty",
},
Code: http.StatusCreated,
ResHeader: map[string]string{
"Location": "https://buy.art/files/foo",
},
}).Run(handler, t)

event := <-c
info := event.Upload

a := assert.New(t)
a.Equal("foo", info.ID)
a.Equal(int64(300), info.Size)
})

SubTest(t, "CreateEmptyUpload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down
5 changes: 5 additions & 0 deletions pkg/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
httptestrecorder "github.com/Acconut/go-httptest-recorder"
"github.com/golang/mock/gomock"
"github.com/tus/tusd/v2/pkg/handler"
"golang.org/x/exp/slog"
)

//go:generate mockgen -package handler_test -source utils_test.go -destination=handler_mock_test.go
Expand Down Expand Up @@ -43,6 +44,10 @@ type FullLock interface {
handler.Lock
}

type SlogHandler interface {
slog.Handler
}

type httpTest struct {
Name string
Context context.Context
Expand Down
Loading