Skip to content

fix jitter function to only return jitter value #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions internal/integration_test/e2e_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func Test_mirror_detect_race_clone(t *testing.T) {
t.Log("TEST-2: forward HEAD")
fileSHA2 := mustCommit(t, upstream, "file", testName+"-2")

time.Sleep(2 * time.Second)

t.Run("clone-test", func(t *testing.T) {
wg := &sync.WaitGroup{}
// all following assertions will always be true
Expand Down Expand Up @@ -172,7 +170,7 @@ func Test_mirror_detect_race_slow_fetch(t *testing.T) {

ctx := context.Background()

time.Sleep(2 * time.Second) // wait for repo.Mirror to grab lock
time.Sleep(time.Second) // wait for repo.Mirror to grab lock

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

go func() {
for {
time.Sleep(2 * time.Second)
time.Sleep(time.Second)
select {
case <-ctx.Done():
close(readStopped)
Expand Down Expand Up @@ -388,7 +386,7 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
// start loop to trigger read on repo pool
go func() {
for {
time.Sleep(2 * time.Second)
time.Sleep(time.Second)
select {
case <-ctx.Done():
close(readStopped)
Expand Down
10 changes: 5 additions & 5 deletions internal/integration_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,13 +1297,13 @@ func Test_mirror_loop(t *testing.T) {

go repo.StartLoop(txtCtx)

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

// wait for the mirror
time.Sleep(testInterval + time.Second)
time.Sleep(testInterval)

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

// wait for the mirror
time.Sleep(testInterval + time.Second)
time.Sleep(testInterval)

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

// wait for the mirror
time.Sleep(testInterval + time.Second)
time.Sleep(testInterval)

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

// wait for the mirror
time.Sleep(2 * time.Second)
time.Sleep(time.Second)

// verify Hash, commit msg and checked out files
if got, err := rp.Hash(txtCtx, remote1, "HEAD", ""); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions repository/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func updatedRefs(output string) []string {
return refs
}

// jitter returns a time.Duration between duration and duration + maxFactor * duration.
// jitter returns a time.Duration between duration and maxFactor * duration.
func jitter(duration time.Duration, maxFactor float64) time.Duration {
return duration + time.Duration(rand.Float64()*maxFactor*float64(duration))
return time.Duration(rand.Float64() * maxFactor * float64(duration))
}
19 changes: 9 additions & 10 deletions repository/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,24 +330,23 @@ func TestJitter(t *testing.T) {
tests := []struct {
name string
args args
minWant time.Duration
maxWant time.Duration
}{
{"1", args{10 * time.Second, 0.1}, 10 * time.Second, 11 * time.Second},
{"2", args{10 * time.Second, 0.5}, 10 * time.Second, 15 * time.Second},
{"3", args{10 * time.Second, 0.0}, 10 * time.Second, 10 * time.Second},
{"4", args{30 * time.Second, 0.1}, 30 * time.Second, 33 * time.Second},
{"5", args{30 * time.Second, 0.5}, 30 * time.Second, 45 * time.Second},
{"6", args{30 * time.Second, 0.0}, 30 * time.Second, 30 * time.Second},
{"1", args{10 * time.Second, 0.1}, 1 * time.Second},
{"2", args{10 * time.Second, 0.5}, 5 * time.Second},
{"3", args{10 * time.Second, 1.0}, 10 * time.Second},
{"4", args{30 * time.Second, 0.1}, 3 * time.Second},
{"5", args{30 * time.Second, 0.5}, 15 * time.Second},
{"6", args{30 * time.Second, 1.0}, 30 * time.Second},
{"4", args{15 * time.Minute, 0.1}, 90 * time.Second},
{"5", args{15 * time.Minute, 0.5}, 450 * time.Second},
{"6", args{15 * time.Minute, 1.0}, 15 * time.Minute},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// since we are using rand test values 10 times
for i := 0; i < 10; i++ {
got := jitter(tt.args.duration, tt.args.maxFactor)
if got < tt.minWant {
t.Errorf("jitter() = %v, min-want %v", got, tt.minWant)
}
if got > tt.maxWant {
t.Errorf("jitter() = %v, max-want %v", got, tt.maxWant)
}
Expand Down