Skip to content

Commit 74de6c3

Browse files
authored
Merge pull request #89 from lwsanty/add-tests-with-race-detector
add tests with race detector + races fixes
2 parents e398e4f + 862c91c commit 74de6c3

File tree

14 files changed

+105
-71
lines changed

14 files changed

+105
-71
lines changed

.github/workflows/push_pull.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ jobs:
3333
bash <(curl -s https://codecov.io/bash) -f coverage.txt -cF $os,$go
3434
env:
3535
CODECOV_TOKEN: be731f61-6ca9-42b0-8f1a-59f4a0b28c0d
36+
37+
- name: Test races
38+
run: |
39+
make test-race
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
43+

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CI_REPOSITORY ?= https://github.com/src-d/ci.git
1010
CI_BRANCH ?= v1
1111
CI_PATH ?= .ci
1212
MAKEFILE := $(CI_PATH)/Makefile.main
13+
TEST_RACE ?= true
1314
$(MAKEFILE):
1415
git clone --quiet --depth 1 -b $(CI_BRANCH) $(CI_REPOSITORY) $(CI_PATH);
1516
-include $(MAKEFILE)

downloader/download.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
func Download(ctx context.Context, job *library.Job) error {
3232
logger := job.Logger.New(log.Fields{"job": "download", "id": job.ID})
3333
if job.Type != library.JobDownload ||
34-
len(job.Endpoints) == 0 ||
34+
len(job.Endpoints()) == 0 ||
3535
job.Lib == nil ||
3636
job.TempFS == nil {
3737
err := ErrNotDownloadJob.New()
@@ -46,7 +46,7 @@ func Download(ctx context.Context, job *library.Job) error {
4646
return err
4747
}
4848

49-
endpoint := job.Endpoints[0]
49+
endpoint := job.Endpoints()[0]
5050
logger = logger.New(log.Fields{"url": endpoint})
5151

5252
repoID, err := library.NewRepositoryID(endpoint)
@@ -113,8 +113,7 @@ func libHas(
113113
select {
114114
case <-done:
115115
case <-ctx.Done():
116-
ok = false
117-
err = ctx.Err()
116+
return false, "", ctx.Err()
118117
}
119118

120119
return ok, locID, err

downloader/download_test.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,16 @@ func testFSWithErrors(t *testing.T, h *testhelper.Helper, fsOpts testhelper.Brok
171171
require.NoError(t, err)
172172

173173
testRepo := tests[0].repoIDs[0]
174-
err = Download(context.Background(), &library.Job{
174+
job := &library.Job{
175175
Lib: lib,
176176
Type: library.JobDownload,
177-
Endpoints: []string{endPoint(gitProtocol, testRepo)},
178177
TempFS: h.TempFS,
179178
AuthToken: func(string) string { return "" },
180179
Logger: log.New(nil),
181-
})
180+
}
181+
job.SetEndpoints([]string{endPoint(gitProtocol, testRepo)})
182182

183+
err = Download(context.Background(), job)
183184
require.Error(t, err)
184185
require.Contains(t, err.Error(), errBrokenFS.Error())
185186
}
@@ -195,14 +196,16 @@ func testAuthSuccess(t *testing.T, h *testhelper.Helper) {
195196
t.Skip()
196197
}
197198

198-
require.NoError(t, Download(context.Background(), &library.Job{
199+
job := &library.Job{
199200
Lib: h.Lib,
200201
Type: library.JobDownload,
201-
Endpoints: []string{endPoint(httpsProtocol, testPrivateRepo.repoIDs[0])},
202202
TempFS: h.TempFS,
203203
AuthToken: func(string) string { return token },
204204
Logger: log.New(nil),
205-
}))
205+
}
206+
job.SetEndpoints([]string{endPoint(httpsProtocol, testPrivateRepo.repoIDs[0])})
207+
208+
require.NoError(t, Download(context.Background(), job))
206209
}
207210

208211
// testAuthErrors
@@ -212,14 +215,16 @@ func testAuthSuccess(t *testing.T, h *testhelper.Helper) {
212215
// <expected> error: authentication required
213216
func testAuthErrors(t *testing.T, h *testhelper.Helper) {
214217
getJob := func(p protocol) *library.Job {
215-
return &library.Job{
218+
job := &library.Job{
216219
Lib: h.Lib,
217220
Type: library.JobDownload,
218-
Endpoints: []string{endPoint(p, testPrivateRepo.repoIDs[0])},
219221
TempFS: h.TempFS,
220222
AuthToken: func(string) string { return "42" },
221223
Logger: log.New(nil),
222224
}
225+
job.SetEndpoints([]string{endPoint(p, testPrivateRepo.repoIDs[0])})
226+
227+
return job
223228
}
224229

225230
ctx := context.Background()
@@ -236,14 +241,16 @@ func testContextCancelledFail(t *testing.T, h *testhelper.Helper) {
236241
cancel()
237242

238243
testRepo := tests[0].repoIDs[0]
239-
require.Equal(t, fmt.Errorf("context canceled"), Download(ctx, &library.Job{
244+
job := &library.Job{
240245
Lib: h.Lib,
241246
Type: library.JobDownload,
242-
Endpoints: []string{endPoint(gitProtocol, testRepo)},
243247
TempFS: h.TempFS,
244248
AuthToken: func(string) string { return "" },
245249
Logger: log.New(nil),
246-
}))
250+
}
251+
job.SetEndpoints([]string{endPoint(gitProtocol, testRepo)})
252+
253+
require.Equal(t, fmt.Errorf("context canceled"), Download(ctx, job))
247254
}
248255

249256
// testWrongEndpointFail
@@ -253,14 +260,16 @@ func testContextCancelledFail(t *testing.T, h *testhelper.Helper) {
253260
func testWrongEndpointFail(t *testing.T, h *testhelper.Helper) {
254261
const corruptedEndpoint = "git://42.git"
255262

256-
err := Download(context.Background(), &library.Job{
263+
job := &library.Job{
257264
Lib: h.Lib,
258265
Type: library.JobDownload,
259-
Endpoints: []string{corruptedEndpoint},
260266
TempFS: h.TempFS,
261267
AuthToken: func(string) string { return "" },
262268
Logger: log.New(nil),
263-
})
269+
}
270+
job.SetEndpoints([]string{corruptedEndpoint})
271+
272+
err := Download(context.Background(), job)
264273
require.Error(t, err)
265274

266275
e, ok := err.(*net.OpError)
@@ -279,11 +288,11 @@ func testAlreadyDownloadedFail(t *testing.T, h *testhelper.Helper) {
279288
job := &library.Job{
280289
Lib: h.Lib,
281290
Type: library.JobDownload,
282-
Endpoints: []string{endPoint(gitProtocol, testRepo)},
283291
TempFS: h.TempFS,
284292
AuthToken: func(string) string { return "" },
285293
Logger: log.New(nil),
286294
}
295+
job.SetEndpoints([]string{endPoint(gitProtocol, testRepo)})
287296

288297
ctx := context.Background()
289298
require.NoError(t, Download(ctx, job))
@@ -401,11 +410,11 @@ func concurrentDownloads(h *testhelper.Helper, p protocol) chan error {
401410
job := &library.Job{
402411
Lib: h.Lib,
403412
Type: library.JobDownload,
404-
Endpoints: []string{endPoint(p, id)},
405413
TempFS: h.TempFS,
406414
AuthToken: func(string) string { return "" },
407415
Logger: log.New(nil),
408416
}
417+
job.SetEndpoints([]string{endPoint(p, id)})
409418

410419
jobs = append(jobs, job)
411420
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require (
2929
github.com/sirupsen/logrus v1.4.2 // indirect
3030
github.com/src-d/envconfig v1.0.0 // indirect
3131
github.com/src-d/go-borges v0.0.0-20190704083038-44867e8f2a2a
32-
github.com/stretchr/testify v1.3.0
32+
github.com/stretchr/testify v1.4.0
3333
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
3434
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
3535
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
@@ -41,5 +41,6 @@ require (
4141
gopkg.in/src-d/go-errors.v1 v1.0.0
4242
gopkg.in/src-d/go-git.v4 v4.12.0
4343
gopkg.in/src-d/go-log.v1 v1.0.2
44+
gopkg.in/yaml.v2 v2.2.4 // indirect
4445
gotest.tools v2.2.0+incompatible // indirect
4546
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
123123
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
124124
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
125125
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
126+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
127+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
126128
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
127129
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
128130
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=
@@ -214,5 +216,7 @@ gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRN
214216
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
215217
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
216218
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
219+
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
220+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
217221
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
218222
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

library/job.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package library
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/src-d/gitcollector"
78
"github.com/src-d/go-borges"
@@ -31,10 +32,11 @@ const (
3132

3233
// Job represents a gitcollector.Job to perform a task on a borges.Library.
3334
type Job struct {
35+
mu sync.Mutex
36+
endpoints []string
3437
ID string
3538
Type JobType
3639
Lib borges.Library
37-
Endpoints []string
3840
TempFS billy.Filesystem
3941
LocationID borges.LocationID
4042
AllowUpdate bool
@@ -48,6 +50,21 @@ var _ gitcollector.Job = (*Job)(nil)
4850
// JobFn represents the task to be performed by a Job.
4951
type JobFn func(context.Context, *Job) error
5052

53+
// TODO: we should probably secure other fiels
54+
func (j *Job) SetEndpoints(endpoints []string) {
55+
j.mu.Lock()
56+
defer j.mu.Unlock()
57+
58+
j.endpoints = endpoints
59+
}
60+
61+
func (j *Job) Endpoints() []string {
62+
j.mu.Lock()
63+
defer j.mu.Unlock()
64+
65+
return j.endpoints
66+
}
67+
5168
// Process implements the Job interface.
5269
func (j *Job) Process(ctx context.Context) error {
5370
if j.ProcessFn == nil {

library/job_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestJobScheduleFn(t *testing.T) {
2222
mu.Lock()
2323
defer mu.Unlock()
2424

25-
got = append(got, j.Endpoints[0])
25+
got = append(got, j.Endpoints()[0])
2626
return nil
2727
}
2828
)
@@ -56,7 +56,7 @@ func TestDownloadJobScheduleFn(t *testing.T) {
5656
mu.Lock()
5757
defer mu.Unlock()
5858

59-
got = append(got, j.Endpoints[0])
59+
got = append(got, j.Endpoints()[0])
6060
return nil
6161
}
6262
)
@@ -89,7 +89,7 @@ func TestUpdateJobScheduleFn(t *testing.T) {
8989
mu.Lock()
9090
defer mu.Unlock()
9191

92-
got = append(got, j.Endpoints[0])
92+
got = append(got, j.Endpoints()[0])
9393
return nil
9494
}
9595
)
@@ -127,7 +127,7 @@ func testScheduleFn(
127127

128128
queue <- &Job{
129129
Type: t,
130-
Endpoints: []string{e},
130+
endpoints: []string{e},
131131
}
132132
}
133133
}

metrics/metrics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ func (c *Collector) modifyMetrics(job *library.Job, kind int) error {
225225
break
226226
}
227227

228-
for range job.Endpoints {
228+
for range job.Endpoints() {
229229
c.successUpdateCount++
230230
}
231231
case failKind:
232-
for range job.Endpoints {
232+
for range job.Endpoints() {
233233
c.failCount++
234234
}
235235
case discoverKind:
@@ -351,16 +351,16 @@ func (c *CollectorByOrg) Discover(job gitcollector.Job) {
351351
func triageJob(job gitcollector.Job) map[string]*library.Job {
352352
organizations := map[string]*library.Job{}
353353
lj, _ := job.(*library.Job)
354-
for _, ep := range lj.Endpoints {
354+
for _, ep := range lj.Endpoints() {
355355
org := library.GetOrgFromEndpoint(ep)
356356
j, ok := organizations[org]
357357
if !ok {
358358
j = &(*lj)
359-
j.Endpoints = []string{}
359+
j.SetEndpoints([]string{})
360360
organizations[org] = j
361361
}
362362

363-
j.Endpoints = append(j.Endpoints, ep)
363+
j.SetEndpoints(append(j.Endpoints(), ep))
364364
}
365365

366366
return organizations

0 commit comments

Comments
 (0)