Skip to content

Commit

Permalink
hooks: Apply hooks suggestions (#146)
Browse files Browse the repository at this point in the history
* precommit: Add

* precommit: Run on whole repo

* precommit: Add Makefile for installing dependencies, fix issues

* streaming: Update backoff test after new random gen

* logs: Remove prints

* sonar: Exclude examples from coverage repot

* sonar: Add examples to exclusions

* sonar: Format file

* test: Add debug, set timeout

* precommit: Update comments after adding makefile

* hooks: Remove external config
  • Loading branch information
echarrod authored Nov 6, 2024
1 parent 6da6da6 commit 3f47a7d
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.idea/
.idea/
7 changes: 5 additions & 2 deletions _examples/readonly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

luno "github.com/luno/luno-go"
"github.com/luno/luno-go"
)

var (
Expand All @@ -21,7 +21,10 @@ func main() {

cl := luno.NewClient()
cl.SetDebug(*debug)
cl.SetAuth(*apiKeyID, *apiKeySecret)
err := cl.SetAuth(*apiKeyID, *apiKeySecret)
if err != nil {
log.Println(err)
}

ctx := context.Background()

Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ go 1.21

require (
github.com/gorilla/websocket v1.5.3
github.com/stretchr/testify v1.9.0
golang.org/x/time v0.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 2 additions & 3 deletions luno.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"runtime"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (cl *Client) do(ctx context.Context, method, path string,
if req != nil {
values := makeURLValues(req)
if strings.Contains(path, "{id}") {
url = strings.Replace(url, "{id}", values.Get("id"), -1)
url = strings.ReplaceAll(url, "{id}", values.Get("id"))
values.Del("id")
}
if method == http.MethodGet {
Expand Down Expand Up @@ -151,7 +150,7 @@ func (cl *Client) do(ctx context.Context, method, path string,

body = httpRes.Body
if cl.debug {
b, err := ioutil.ReadAll(body)
b, err := io.ReadAll(body)
if err != nil {
log.Printf("luno: Error reading response body: %v", err)
} else {
Expand Down
10 changes: 7 additions & 3 deletions luno_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestDoGet(t *testing.T) {
Expand Down Expand Up @@ -114,12 +116,14 @@ func TestDoAuth(t *testing.T) {

cl := NewClient()
cl.SetBaseURL(srv.URL)
cl.SetAuth(username, password)

err := cl.SetAuth(username, password)
require.Nil(t, err)
cl.SetDebug(true)
cl.SetTimeout(10 * time.Second)
var res testRes

// No auth provided:
err := cl.do(context.Background(), "POST", "/test", nil, &res, false)
err = cl.do(context.Background(), "POST", "/test", nil, &res, false)
if err != nil {
t.Errorf("Expected success, got %v", err)
}
Expand Down
23 changes: 11 additions & 12 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
sonar.organization = luno
sonar.projectKey = luno_luno-go
sonar.projectName = luno-go
sonar.links.scm = https://github.com/luno/luno-go

sonar.sources = .
sonar.exclusions=**/*_test.go, **/*pb.go
sonar.go.coverage.reportPaths = coverage.out
sonar.go.tests.reportPaths = sonar-report.json
sonar.tests = .
sonar.test.inclusions = **/*_test.go
sonar.test.exclusions=**/*pb.go
sonar.organization=luno
sonar.projectKey=luno_luno-go
sonar.projectName=luno-go
sonar.links.scm=https://github.com/luno/luno-go
sonar.sources=.
sonar.exclusions=**/*_test.go, **/*pb.go, _examples/**/*
sonar.go.coverage.reportPaths=coverage.out
sonar.go.tests.reportPaths=sonar-report.json
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/*pb.go, _examples/**/*
9 changes: 7 additions & 2 deletions streaming/backoff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package streaming

import (
"crypto/rand"
"math"
"math/rand"
"math/big"
"time"
)

Expand All @@ -12,7 +13,11 @@ type backoffParams struct {
}

func defaultBackoffHandler(attempts int) time.Duration {
jitter := time.Duration(rand.Intn(200)-100) * time.Millisecond // ±100ms
randInt, err := rand.Int(rand.Reader, big.NewInt(200))
if err != nil {
panic(err)
}
jitter := time.Duration(randInt.Int64()-100) * time.Millisecond // ±100ms
backoff := time.Duration(math.Min(math.Pow(2, float64(attempts)), 60)) * time.Second // Exponential backoff up to 60s
return backoff + jitter
}
9 changes: 6 additions & 3 deletions streaming/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,14 @@ func (c *Conn) processCreate(u CreateUpdate) error {
Volume: u.Volume,
}

if u.Type == string(luno.OrderTypeBid) {
switch u.Type {
case string(luno.OrderTypeBid):
c.bids[o.ID] = o
} else if u.Type == string(luno.OrderTypeAsk) {

case string(luno.OrderTypeAsk):
c.asks[o.ID] = o
} else {

default:
return errors.New("streaming: unknown order type")
}

Expand Down
48 changes: 25 additions & 23 deletions streaming/streaming_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package streaming

import (
"math/rand"
"testing"
"time"

"github.com/luno/luno-go"
"github.com/luno/luno-go/decimal"
"github.com/stretchr/testify/require"
)

func TestFlatten(t *testing.T) {
Expand Down Expand Up @@ -131,25 +131,24 @@ func TestFlatten(t *testing.T) {

func TestBackoff(t *testing.T) {
tcs := []struct {
name string
fn BackoffHandler
attemptReset time.Duration
p *backoffParams
seed func()
reqTS time.Time
expBackoff time.Duration
expParams backoffParams
name string
fn BackoffHandler
attemptReset time.Duration
p *backoffParams
seed func()
reqTS time.Time
expBackoff time.Duration
expBackoffLowerLimit *time.Duration
expBackoffUpperLimit *time.Duration // Used when randomly generating
expParams backoffParams
}{
{
name: "default",
fn: defaultBackoffHandler,
p: &backoffParams{},
seed: func() {
// Seed random generator to test default backoff handler
rand.Seed(123)
},
reqTS: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
expBackoff: 1935000000,
name: "default",
fn: defaultBackoffHandler,
p: &backoffParams{},
reqTS: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
expBackoffLowerLimit: ptr[time.Duration](1900000000),
expBackoffUpperLimit: ptr[time.Duration](2100000000),
expParams: backoffParams{
attempts: 1,
lastAttempt: time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC),
Expand Down Expand Up @@ -206,17 +205,16 @@ func TestBackoff(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
if tc.seed != nil {
tc.seed()
}

c := &Conn{
backoffHandler: tc.fn,
attemptReset: tc.attemptReset,
}

dt := c.calculateBackoff(tc.p, tc.reqTS)
if dt != tc.expBackoff {
if tc.expBackoffUpperLimit != nil {
require.LessOrEqual(t, dt, *tc.expBackoffUpperLimit)
require.GreaterOrEqual(t, dt, *tc.expBackoffLowerLimit)
} else if dt != tc.expBackoff {
t.Errorf("backoff %d doesn't match expect backoff %d", dt, tc.expBackoff)
}

Expand All @@ -226,3 +224,7 @@ func TestBackoff(t *testing.T) {
})
}
}

func ptr[T any](t T) *T {
return &t
}

0 comments on commit 3f47a7d

Please sign in to comment.