Skip to content
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

Add unit test for NoBackoff #3

Merged
merged 2 commits into from
May 13, 2022
Merged

Add unit test for NoBackoff #3

merged 2 commits into from
May 13, 2022

Conversation

ntpthinh
Copy link
Collaborator

No description provided.

retry_test.go Outdated
Comment on lines 41 to 45
s1 := time.Now()
defer func() {
to += time.Since(s1)
}()
time.Sleep(100 * time.Millisecond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to sleep and calculate to variable here.
Just keep it running without waiting.
So finally, the expected duration will be smaller than 100 milliseconds

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestNoBackoff(t *testing.T) {
	var counting int64

	start := time.Now()
	goretry.NoBackoff(func() error {
		counting++
		if counting > 5 {
			return nil
		}
		return errors.New("fake error")
	})

	duration := time.Since(start)

	expectedCounting := int64(6)
	if counting != expectedCounting {
		t.Errorf("NoBackoff() expected counting: %d, actual: %d", expectedCounting, counting)
	}

	expectedDuration := 100 * time.Millisecond
	if duration > expectedDuration {
		t.Errorf("NoBackoff() expected duration: %d, actual: %d", expectedDuration, duration)
	}
}

Do you mean that it would be like this? If yes, may I ask why it is 100 * time.Millisecond as I don't see the relevance between it and action function? Or is it just a magic number?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just a magic number that we do 6 actions without any sleeping can't reach to

@ledongthuc ledongthuc merged commit 44f6af9 into main May 13, 2022
@ledongthuc ledongthuc deleted the backoff-test branch May 13, 2022 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants