Skip to content

Commit 7da94c4

Browse files
committed
fix: integration tests
Also run them in CI.
1 parent c5f059c commit 7da94c4

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ _help:
1818

1919
# Run tests
2020
test:
21-
@gotestsum --hide-summary output,skipped --format-hide-empty-pkg ${CI:+--format github-actions} ./... -- -race -timeout 30s
21+
@gotestsum --hide-summary output,skipped --format-hide-empty-pkg ${CI:+--format=github-actions} ./... ${CI:+--tags=integration} -race -timeout 30s
2222

2323
# Lint code
2424
lint:

internal/strategy/git/integration_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/alecthomas/assert/v2"
2121

22+
"github.com/block/cachew/internal/gitclone"
2223
"github.com/block/cachew/internal/jobscheduler"
2324
"github.com/block/cachew/internal/logging"
2425
"github.com/block/cachew/internal/strategy/git"
@@ -56,11 +57,12 @@ func TestIntegrationGitCloneViaProxy(t *testing.T) {
5657
assert.NoError(t, err)
5758

5859
// Create the git strategy
59-
mux := http.NewServeMux()
60-
strategy, err := git.New(ctx, git.Config{
60+
gc := gitclone.NewManagerProvider(ctx, gitclone.Config{
6161
MirrorRoot: clonesDir,
6262
FetchInterval: 15,
63-
}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux)
63+
})
64+
mux := http.NewServeMux()
65+
strategy, err := git.New(ctx, git.Config{}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux, gc)
6466
assert.NoError(t, err)
6567
assert.NotZero(t, strategy)
6668

@@ -134,11 +136,13 @@ func TestIntegrationGitFetchViaProxy(t *testing.T) {
134136
err := os.MkdirAll(workDir, 0o750)
135137
assert.NoError(t, err)
136138

137-
mux := http.NewServeMux()
138-
_, err = git.New(ctx, git.Config{
139+
gc := gitclone.NewManagerProvider(ctx, gitclone.Config{
139140
MirrorRoot: clonesDir,
140141
FetchInterval: 15,
141-
}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux)
142+
})
143+
144+
mux := http.NewServeMux()
145+
_, err = git.New(ctx, git.Config{}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux, gc)
142146
assert.NoError(t, err)
143147

144148
server := testServerWithLogging(ctx, mux)
@@ -214,10 +218,11 @@ func TestIntegrationPushForwardsToUpstream(t *testing.T) {
214218
defer upstreamServer.Close()
215219

216220
mux := http.NewServeMux()
217-
_, err = git.New(ctx, git.Config{
221+
gc := gitclone.NewManagerProvider(ctx, gitclone.Config{
218222
MirrorRoot: clonesDir,
219223
FetchInterval: 15,
220-
}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux)
224+
})
225+
_, err = git.New(ctx, git.Config{}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux, gc)
221226
assert.NoError(t, err)
222227

223228
server := testServerWithLogging(ctx, mux)
@@ -307,10 +312,11 @@ func TestIntegrationSpoolReusesDuringClone(t *testing.T) {
307312
var upstreamUploadPackRequests atomic.Int32
308313

309314
mux := http.NewServeMux()
310-
strategy, err := git.New(ctx, git.Config{
315+
gc := gitclone.NewManagerProvider(ctx, gitclone.Config{
311316
MirrorRoot: clonesDir,
312317
FetchInterval: 15,
313-
}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux)
318+
})
319+
strategy, err := git.New(ctx, git.Config{}, jobscheduler.New(ctx, jobscheduler.Config{}), nil, mux, gc)
314320
assert.NoError(t, err)
315321

316322
strategy.SetHTTPTransport(&countingTransport{

internal/strategy/git/spool.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ var ErrSpoolFailed = errors.New("spool failed before response started")
2121
// allowing one writer and multiple concurrent readers. Readers follow the writer,
2222
// blocking when caught up until the write completes.
2323
type ResponseSpool struct {
24-
mu sync.Mutex
25-
cond *sync.Cond
26-
filePath string
27-
file *os.File
28-
status int
29-
headers http.Header
30-
written int64
31-
complete bool
32-
err error
33-
readers sync.WaitGroup
24+
mu sync.Mutex
25+
cond *sync.Cond
26+
filePath string
27+
file *os.File
28+
status int
29+
headers http.Header
30+
written int64
31+
complete bool
32+
err error
33+
readerCount int
3434
}
3535

3636
func NewResponseSpool(filePath string) (*ResponseSpool, error) {
@@ -102,8 +102,15 @@ func (rs *ResponseSpool) Failed() bool {
102102

103103
// ServeTo streams the spooled response to w, blocking when caught up to the writer.
104104
func (rs *ResponseSpool) ServeTo(w http.ResponseWriter) error {
105-
rs.readers.Add(1)
106-
defer rs.readers.Done()
105+
rs.mu.Lock()
106+
rs.readerCount++
107+
rs.mu.Unlock()
108+
defer func() {
109+
rs.mu.Lock()
110+
rs.readerCount--
111+
rs.cond.Broadcast()
112+
rs.mu.Unlock()
113+
}()
107114

108115
// Wait for headers to be available.
109116
rs.mu.Lock()
@@ -168,7 +175,11 @@ func (rs *ResponseSpool) ServeTo(w http.ResponseWriter) error {
168175

169176
// WaitForReaders blocks until all active spool readers have finished.
170177
func (rs *ResponseSpool) WaitForReaders() {
171-
rs.readers.Wait()
178+
rs.mu.Lock()
179+
defer rs.mu.Unlock()
180+
for rs.readerCount > 0 {
181+
rs.cond.Wait()
182+
}
172183
}
173184

174185
// SpoolTeeWriter wraps an http.ResponseWriter to capture the response into a spool

0 commit comments

Comments
 (0)