Skip to content

Commit

Permalink
[#17] - Disallow negative maxRetries and waitTime arguments
Browse files Browse the repository at this point in the history
Also defaults retries to 1 if given less - expected functionality in using a retrier anyway so not even a feature release required
  • Loading branch information
J7mbo committed Feb 23, 2019
1 parent 882cdfb commit 9203fbc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: go

go:
- "1.11.x"
- 1.11.x
- tip

os:
- linux
Expand All @@ -12,6 +13,12 @@ dist: trusty
sudo: false
install: true

before_install:
- go get -t -v ./...

script:
- env GO111MODULE=on go build
- env GO111MODULE=on go test
- env GO111MODULE=on go test -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
8 changes: 6 additions & 2 deletions MethodCallRetrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ func New(waitTime int64, maxRetries int64, exponent *int64) *MethodCallRetrier {
exponent = &defaultInt
}

if maxRetries <= 0 {
maxRetries = 0
if maxRetries < 1 {
maxRetries = 1
}

if waitTime <= 0 {
waitTime = 0
}

return &MethodCallRetrier{waitTime: waitTime, maxRetries: maxRetries, exponent: *exponent}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ MethodCallRetrier

[![Build Status](https://travis-ci.org/J7mbo/MethodCallRetrier.svg?branch=master)](https://travis-ci.org/J7mbo/MethodCallRetrier)
[![GoDoc](https://godoc.org/github.com/J7mbo/MethodCallRetrier?status.svg)](https://godoc.org/github.com/J7mbo/MethodCallRetrier)
[![codecov](https://img.shields.io/codecov/c/github.com/j7mbo/MethodCallRetrier/master.svg)](https://codecov.io/gh/J7mbo/MethodCallRetrier)

Features
-
Expand Down
31 changes: 30 additions & 1 deletion Retrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ func (s *RetrierTestSuite) TestRetrierRetriesCorrectNumberOfTimes() {
testObj.AssertExpectations(s.T())
}

func (s *RetrierTestSuite) TestRetrierWorksWithNegativeMaxRetries() {
arg := "testArg"

results, _ := New(-1, -1, nil).ExecuteWithRetry(RetryObject{}, "MethodReturningString", arg)

s.Assert().EqualValues(results[0].String(), arg)
}

func (s *RetrierTestSuite) TestRetrierDefaultsToOneRetryGivenZeroMaxRetries() {
testObj := RetryMockObject{}

New(0, 0, nil).ExecuteFuncWithRetry(testObj.MethodToBeCalledToReturnResultAndError)

s.Assert().Equal(1, testObj.timesCalled)
}

func (s *RetrierTestSuite) TestRetrierReturnsAllErrorsPlusOurError() {
testObj := RetryMockObject{}
methodName := "MethodReturningError"
Expand All @@ -96,7 +112,7 @@ func (s *RetrierTestSuite) TestRetrierWorksWhenErrorIsNotLastReturnParamOnObject
testObj := RetryObject{}
methodName := "MethodReturningErrorInRandomPosition"

_, errs := New(0, 5, nil).ExecuteWithRetry(&testObj, methodName, "")
_, errs := New(1, 1, nil).ExecuteWithRetry(&testObj, methodName, "")

s.Assert().IsType(errors.New(""), errs[0])
}
Expand Down Expand Up @@ -139,6 +155,19 @@ func (s *RetrierTestSuite) TestRetrierWorksWithUserFunctionCalledCorrectNumberOf
s.Assert().Equal(3, testObj.timesCalled)
}

/* This really only exists for coverage */
func (s *RetrierTestSuite) TestMaxRetriesError() {
methodName := "AMethod"
waitTime := "42"
maxRetries := "52"

err := MaxRetriesError{methodName: methodName, waitTime: 42, maxRetries: 52}

s.Assert().Contains(err.Error(), methodName)
s.Assert().Contains(err.Error(), waitTime)
s.Assert().Contains(err.Error(), maxRetries)
}

type RetryObject struct{}

func (m *RetryObject) MethodReturningNoValues() {}
Expand Down

0 comments on commit 9203fbc

Please sign in to comment.