Skip to content

Commit 4bd6c8c

Browse files
authored
fix jitter function to only return jitter value (#60)
1 parent e7881fd commit 4bd6c8c

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

internal/integration_test/e2e_race_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ func Test_mirror_detect_race_clone(t *testing.T) {
5757
t.Log("TEST-2: forward HEAD")
5858
fileSHA2 := mustCommit(t, upstream, "file", testName+"-2")
5959

60-
time.Sleep(2 * time.Second)
61-
6260
t.Run("clone-test", func(t *testing.T) {
6361
wg := &sync.WaitGroup{}
6462
// all following assertions will always be true
@@ -172,7 +170,7 @@ func Test_mirror_detect_race_slow_fetch(t *testing.T) {
172170

173171
ctx := context.Background()
174172

175-
time.Sleep(2 * time.Second) // wait for repo.Mirror to grab lock
173+
time.Sleep(time.Second) // wait for repo.Mirror to grab lock
176174

177175
gotHash, err := repo.Hash(ctx, "HEAD", "")
178176
if err != nil {
@@ -317,7 +315,7 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
317315

318316
go func() {
319317
for {
320-
time.Sleep(2 * time.Second)
318+
time.Sleep(time.Second)
321319
select {
322320
case <-ctx.Done():
323321
close(readStopped)
@@ -388,7 +386,7 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
388386
// start loop to trigger read on repo pool
389387
go func() {
390388
for {
391-
time.Sleep(2 * time.Second)
389+
time.Sleep(time.Second)
392390
select {
393391
case <-ctx.Done():
394392
close(readStopped)

internal/integration_test/e2e_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,13 +1297,13 @@ func Test_mirror_loop(t *testing.T) {
12971297

12981298
go repo.StartLoop(txtCtx)
12991299

1300-
time.Sleep(testInterval + time.Second)
1300+
time.Sleep(testInterval)
13011301
if repo.IsRunning() != true {
13021302
t.Errorf("repo running state is still false after starting mirror loop")
13031303
}
13041304

13051305
// wait for the mirror
1306-
time.Sleep(testInterval + time.Second)
1306+
time.Sleep(testInterval)
13071307

13081308
// verify checkout files
13091309
assertLinkedFile(t, root, link1, "file", t.Name()+"-1")
@@ -1314,7 +1314,7 @@ func Test_mirror_loop(t *testing.T) {
13141314
mustCommit(t, upstream, "file", t.Name()+"-2")
13151315

13161316
// wait for the mirror
1317-
time.Sleep(testInterval + time.Second)
1317+
time.Sleep(testInterval)
13181318

13191319
assertLinkedFile(t, root, link1, "file", t.Name()+"-2")
13201320
assertLinkedFile(t, root, link2, "file", t.Name()+"-2")
@@ -1324,7 +1324,7 @@ func Test_mirror_loop(t *testing.T) {
13241324
mustExec(t, upstream, "git", "reset", "-q", "--hard", "HEAD^")
13251325

13261326
// wait for the mirror
1327-
time.Sleep(testInterval + time.Second)
1327+
time.Sleep(testInterval)
13281328

13291329
assertLinkedFile(t, root, link1, "file", t.Name()+"-1")
13301330
assertLinkedFile(t, root, link2, "file", t.Name()+"-1")
@@ -1437,7 +1437,7 @@ func Test_RepoPool_Success(t *testing.T) {
14371437
fileU2SHA2 := mustCommit(t, upstream2, "file", t.Name()+"-u2-main-2")
14381438

14391439
// wait for the mirror
1440-
time.Sleep(2 * time.Second)
1440+
time.Sleep(time.Second)
14411441

14421442
// verify Hash, commit msg and checked out files
14431443
if got, err := rp.Hash(txtCtx, remote1, "HEAD", ""); err != nil {

repository/helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func updatedRefs(output string) []string {
131131
return refs
132132
}
133133

134-
// jitter returns a time.Duration between duration and duration + maxFactor * duration.
134+
// jitter returns a time.Duration between duration and maxFactor * duration.
135135
func jitter(duration time.Duration, maxFactor float64) time.Duration {
136-
return duration + time.Duration(rand.Float64()*maxFactor*float64(duration))
136+
return time.Duration(rand.Float64() * maxFactor * float64(duration))
137137
}

repository/helper_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,24 +330,23 @@ func TestJitter(t *testing.T) {
330330
tests := []struct {
331331
name string
332332
args args
333-
minWant time.Duration
334333
maxWant time.Duration
335334
}{
336-
{"1", args{10 * time.Second, 0.1}, 10 * time.Second, 11 * time.Second},
337-
{"2", args{10 * time.Second, 0.5}, 10 * time.Second, 15 * time.Second},
338-
{"3", args{10 * time.Second, 0.0}, 10 * time.Second, 10 * time.Second},
339-
{"4", args{30 * time.Second, 0.1}, 30 * time.Second, 33 * time.Second},
340-
{"5", args{30 * time.Second, 0.5}, 30 * time.Second, 45 * time.Second},
341-
{"6", args{30 * time.Second, 0.0}, 30 * time.Second, 30 * time.Second},
335+
{"1", args{10 * time.Second, 0.1}, 1 * time.Second},
336+
{"2", args{10 * time.Second, 0.5}, 5 * time.Second},
337+
{"3", args{10 * time.Second, 1.0}, 10 * time.Second},
338+
{"4", args{30 * time.Second, 0.1}, 3 * time.Second},
339+
{"5", args{30 * time.Second, 0.5}, 15 * time.Second},
340+
{"6", args{30 * time.Second, 1.0}, 30 * time.Second},
341+
{"4", args{15 * time.Minute, 0.1}, 90 * time.Second},
342+
{"5", args{15 * time.Minute, 0.5}, 450 * time.Second},
343+
{"6", args{15 * time.Minute, 1.0}, 15 * time.Minute},
342344
}
343345
for _, tt := range tests {
344346
t.Run(tt.name, func(t *testing.T) {
345347
// since we are using rand test values 10 times
346348
for i := 0; i < 10; i++ {
347349
got := jitter(tt.args.duration, tt.args.maxFactor)
348-
if got < tt.minWant {
349-
t.Errorf("jitter() = %v, min-want %v", got, tt.minWant)
350-
}
351350
if got > tt.maxWant {
352351
t.Errorf("jitter() = %v, max-want %v", got, tt.maxWant)
353352
}

0 commit comments

Comments
 (0)