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

feat: Add ConstantBackoff: always return the same duration #12

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.16"]
go: ["1.18"]
steps:
- name: Set up Golang
uses: actions/setup-go@v2
Expand Down Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Set up Golang
uses: actions/setup-go@v2
with:
go-version: "1.16"
go-version: "1.18"
id: go
- uses: actions/setup-node@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
main:
name: Validate PR title
name: Semantic Pull Request
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v4
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/shiftleft-go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# yamllint disable rule:line-length
# Use template from /~https://github.com/honestbank/workflows/tree/main/examples/repository-workflows
---
name: "repository-shiftleft-go"
permissions: read-all

on: # yamllint disable-line rule:truthy
push:
branches:
- main
- master
pull_request:
types:
- opened
- edited
- synchronize
schedule:
# * is a special character in YAML so you have to quote this string
# schedule a job to run every day at 17:30 UTC
- cron: '30 17 * * *'

jobs:
repository-shiftleft-go:
name: repository-shiftleft-go
uses: honestbank/workflows/.github/workflows/shared-shiftleft-go.yaml@main
with:
repo_name: ${{ github.event.repository.name }}
secrets: inherit
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/honestbank/backoff-policy

go 1.16
go 1.18

require github.com/stretchr/testify v1.7.0

require (
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
4 changes: 1 addition & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
Expand Down
13 changes: 13 additions & 0 deletions policies/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package policies

import "time"

func GetConstantPolicy(duration time.Duration) Policy {
return func(count int) time.Duration {
if count == 0 {
return 0
}

return duration
}
}
27 changes: 27 additions & 0 deletions policies/constant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package policies_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/honestbank/backoff-policy/policies"
)

func TestGetConstantPolicy(t *testing.T) {
t.Run("returns correct duration based on count", func(t *testing.T) {
policy := policies.GetConstantPolicy(time.Millisecond * 100)
assert.Equal(t, time.Millisecond*0, policy(0))
assert.Equal(t, time.Millisecond*100, policy(1))
assert.Equal(t, time.Millisecond*100, policy(2))
assert.Equal(t, time.Millisecond*100, policy(3))
})
t.Run("value is capped at max count", func(t *testing.T) {
policy := policies.GetConstantPolicy(time.Millisecond * 100)
assert.Equal(t, policy(10), policy(10))
assert.Equal(t, policy(10), policy(11))
assert.Equal(t, policy(10), policy(12))
assert.Equal(t, policy(10), policy(13))
})
Comment on lines +20 to +26

Choose a reason for hiding this comment

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

We don't need this test

}