mirror of
https://github.com/harness/drone.git
synced 2025-05-10 02:50:58 +08:00
fix failing unit test fetching yaml
This commit is contained in:
parent
c36bf0d5ee
commit
1ba438d9f3
@ -23,17 +23,27 @@ import (
|
|||||||
"github.com/drone/go-scm/scm"
|
"github.com/drone/go-scm/scm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// default number of backoff attempts.
|
||||||
|
var attempts = 3
|
||||||
|
|
||||||
|
// default time to wait after failed attempt.
|
||||||
|
var wait = time.Second * 15
|
||||||
|
|
||||||
// New returns a new FileService.
|
// New returns a new FileService.
|
||||||
func New(client *scm.Client, renewer core.Renewer) core.FileService {
|
func New(client *scm.Client, renewer core.Renewer) core.FileService {
|
||||||
return &service{
|
return &service{
|
||||||
client: client,
|
client: client,
|
||||||
renewer: renewer,
|
renewer: renewer,
|
||||||
|
attempts: attempts,
|
||||||
|
wait: wait,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
renewer core.Renewer
|
renewer core.Renewer
|
||||||
client *scm.Client
|
client *scm.Client
|
||||||
|
attempts int
|
||||||
|
wait time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Find(ctx context.Context, user *core.User, repo, commit, ref, path string) (*core.File, error) {
|
func (s *service) Find(ctx context.Context, user *core.User, repo, commit, ref, path string) (*core.File, error) {
|
||||||
@ -75,7 +85,7 @@ func (s *service) Find(ctx context.Context, user *core.User, repo, commit, ref,
|
|||||||
// with backoff on failure. This may be required due to eventual
|
// with backoff on failure. This may be required due to eventual
|
||||||
// consistency issues with the github datastore.
|
// consistency issues with the github datastore.
|
||||||
func (s *service) findRetry(ctx context.Context, repo, path, commit string) (content *scm.Content, err error) {
|
func (s *service) findRetry(ctx context.Context, repo, path, commit string) (content *scm.Content, err error) {
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < s.attempts; i++ {
|
||||||
content, _, err = s.client.Contents.Find(ctx, repo, path, commit)
|
content, _, err = s.client.Contents.Find(ctx, repo, path, commit)
|
||||||
// if no error is returned we can exit immediately.
|
// if no error is returned we can exit immediately.
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -84,7 +94,7 @@ func (s *service) findRetry(ctx context.Context, repo, path, commit string) (con
|
|||||||
// wait a few seconds before retry. according to github
|
// wait a few seconds before retry. according to github
|
||||||
// support 30 seconds total should be enough time. we
|
// support 30 seconds total should be enough time. we
|
||||||
// try 3 x 15 seconds, giving a total of 45 seconds.
|
// try 3 x 15 seconds, giving a total of 45 seconds.
|
||||||
time.Sleep(time.Second * 15)
|
time.Sleep(s.wait)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/drone/drone/core"
|
||||||
"github.com/drone/drone/mock"
|
"github.com/drone/drone/mock"
|
||||||
"github.com/drone/drone/mock/mockscm"
|
"github.com/drone/drone/mock/mockscm"
|
||||||
"github.com/drone/drone/core"
|
|
||||||
"github.com/drone/go-scm/scm"
|
"github.com/drone/go-scm/scm"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
||||||
@ -68,8 +68,10 @@ func TestFind_Error(t *testing.T) {
|
|||||||
client := new(scm.Client)
|
client := new(scm.Client)
|
||||||
client.Contents = mockContents
|
client.Contents = mockContents
|
||||||
|
|
||||||
service := New(client, mockRenewer)
|
s := New(client, mockRenewer)
|
||||||
_, err := service.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".core.yml")
|
s.(*service).attempts = 1
|
||||||
|
s.(*service).wait = 0
|
||||||
|
_, err := s.Find(noContext, mockUser, "octocat/hello-world", "a6586b3db244fb6b1198f2b25c213ded5b44f9fa", "master", ".core.yml")
|
||||||
if err != scm.ErrNotFound {
|
if err != scm.ErrNotFound {
|
||||||
t.Errorf("Expect not found error, got %s", err)
|
t.Errorf("Expect not found error, got %s", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user