Skip to content

Commit 1c0e275

Browse files
test: run the batch spec upload tests under -race again
The upload tests were tagged //go:build !race in #1082 because testify fmt-prints every recorded mock argument, and printing a bare *io.PipeReader reflects into the pipe's internals while the producer goroutine writes to it. The Windows variant never got the tag, so 'go test -race' has been failing on Windows the whole time, hidden because the step runs under pwsh, which does not fail on a non-zero exit in the middle of a multi-line script; the second non-race pass then went green. The mock client now wraps the body in a RequestBody struct, so fmt prints the pointer address instead of walking the pipe. That removes the need for the !race tag and for the second test pass. uploadFile also starts the writer goroutine only after NewHTTPRequest succeeds; before, an error there leaked a goroutine blocked forever on a pipe nobody reads. Amp-Thread-ID: https://ampcode.com/threads/T-01a18b5c-c5bb-7497-82f1-3e9333c8b7e1 Co-authored-by: Amp <amp@ampcode.com>
1 parent 8069b6c commit 1c0e275

5 files changed

Lines changed: 27 additions & 21 deletions

File tree

‎.github/workflows/go-ci.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ jobs:
4141
key: ${{ github.job }}-${{ runner.os }}-${{ runner.arch }}-go${{ steps.mise.outputs.go-version }}-${{ hashFiles('**/go.sum') }}
4242
restore-keys: |
4343
${{ github.job }}-${{ runner.os }}-${{ runner.arch }}-go${{ steps.mise.outputs.go-version }}-
44-
# -race already runs every test; a second plain pass only recompiles
45-
# everything without race instrumentation and reruns the same tests.
44+
# bash on every OS: the default pwsh on Windows does not fail the step
45+
# when a command in a multi-line script exits non-zero.
4646
- run: go test -race -v ./...
47+
shell: bash
4748

4849
# This job provides a stable name for branch protection rules
4950
# It will only pass if all Go CI matrix tests above pass

‎internal/api/mock/api.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ func (m *Client) NewGzippedQuery(query string) api.Request {
3434
return args.Get(0).(api.Request)
3535
}
3636

37+
// RequestBody wraps the body handed to NewHTTPRequest. testify fmt-prints every
38+
// recorded argument, and printing a bare *io.PipeReader reflects into the
39+
// pipe's internals, racing with the goroutine writing to it. Behind a struct
40+
// field, fmt prints the pointer's address instead.
41+
type RequestBody struct{ io.Reader }
42+
3743
func (m *Client) NewHTTPRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
38-
args := m.Called(ctx, method, path, body)
44+
args := m.Called(ctx, method, path, RequestBody{body})
3945
var obj *http.Request
4046
if args.Get(0) != nil {
4147
obj = args.Get(0).(*http.Request)

‎internal/batches/service/remote.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,15 @@ func (svc *Service) uploadFile(ctx context.Context, workingDir, filePath, batchS
175175
pipeReader, pipeWriter := io.Pipe()
176176
multipartWriter := multipart.NewWriter(pipeWriter)
177177

178+
request, err := svc.client.NewHTTPRequest(ctx, http.MethodPost, fmt.Sprintf(".api/files/batch-changes/%s", batchSpecID), pipeReader)
179+
if err != nil {
180+
return err
181+
}
182+
request.Header.Set("Content-Type", multipartWriter.FormDataContentType())
183+
178184
// Write in a separate goroutine to properly chunk the file content. Writing to the pipe lets us not have
179-
// to put the whole file in memory.
185+
// to put the whole file in memory. Started only once the request exists, so a NewHTTPRequest error
186+
// doesn't leak a goroutine blocked on a pipe nobody reads.
180187
go func() {
181188
defer pipeWriter.Close()
182189
defer multipartWriter.Close()
@@ -186,12 +193,6 @@ func (svc *Service) uploadFile(ctx context.Context, workingDir, filePath, batchS
186193
}
187194
}()
188195

189-
request, err := svc.client.NewHTTPRequest(ctx, http.MethodPost, fmt.Sprintf(".api/files/batch-changes/%s", batchSpecID), pipeReader)
190-
if err != nil {
191-
return err
192-
}
193-
request.Header.Set("Content-Type", multipartWriter.FormDataContentType())
194-
195196
resp, err := svc.client.Do(request)
196197
if err != nil {
197198
// Errors passed to pipeWriter.CloseWithError come through here.

internal/batches/service/remote_norace_test.go renamed to internal/batches/service/remote_upload_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build !race
2-
31
package service_test
42

53
import (
@@ -48,7 +46,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
4846
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
4947
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
5048
Run(func(args mock.Arguments) {
51-
req.Body = args[3].(*io.PipeReader)
49+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
5250
}).
5351
Return(req, nil).
5452
Once()
@@ -90,7 +88,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
9088
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
9189
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
9290
Run(func(args mock.Arguments) {
93-
req.Body = args[3].(*io.PipeReader)
91+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
9492
}).
9593
Return(req, nil).
9694
Once()
@@ -132,7 +130,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
132130
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
133131
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
134132
Run(func(args mock.Arguments) {
135-
req.Body = args[3].(*io.PipeReader)
133+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
136134
}).
137135
Return(req, nil).
138136
Twice()
@@ -180,7 +178,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
180178
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
181179
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
182180
Run(func(args mock.Arguments) {
183-
req.Body = args[3].(*io.PipeReader)
181+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
184182
}).
185183
Return(req, nil).
186184
Twice()
@@ -229,7 +227,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
229227
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
230228
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
231229
Run(func(args mock.Arguments) {
232-
req.Body = args[3].(*io.PipeReader)
230+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
233231
}).
234232
Return(req, nil).
235233
Once()
@@ -281,7 +279,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
281279
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
282280
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
283281
Run(func(args mock.Arguments) {
284-
req.Body = args[3].(*io.PipeReader)
282+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
285283
}).
286284
Return(req, nil).
287285
Times(3)
@@ -362,7 +360,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
362360
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
363361
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
364362
Run(func(args mock.Arguments) {
365-
req.Body = args[3].(*io.PipeReader)
363+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
366364
}).
367365
Return(req, nil).
368366
Once()
@@ -400,7 +398,7 @@ func TestService_UploadBatchSpecWorkspaceFiles(t *testing.T) {
400398
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
401399
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
402400
Run(func(args mock.Arguments) {
403-
req.Body = args[3].(*io.PipeReader)
401+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
404402
}).
405403
Return(req, nil).
406404
Once()

‎internal/batches/service/remote_windows_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestService_UploadBatchSpecWorkspaceFiles_Windows_Path(t *testing.T) {
4747
req := httptest.NewRequest(http.MethodPost, "http://fake.com/.api/files/batch-changes/123", nil)
4848
client.On("NewHTTPRequest", mock.Anything, http.MethodPost, ".api/files/batch-changes/123", mock.Anything).
4949
Run(func(args mock.Arguments) {
50-
req.Body = args[3].(*io.PipeReader)
50+
req.Body = io.NopCloser(args[3].(mockclient.RequestBody))
5151
}).
5252
Return(req, nil).
5353
Once()

0 commit comments

Comments
 (0)