Skip to content

Commit

Permalink
Add failing test for race
Browse files Browse the repository at this point in the history
See tus#1192

This adds a mock slog Logger to create a data race issue.
Run go test -race to trigger
  • Loading branch information
wongak committed Sep 22, 2024
1 parent 9b86376 commit e9786ba
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
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

0 comments on commit e9786ba

Please sign in to comment.