Skip to content

Commit

Permalink
beaconmock: remove backoff dependency (#2089)
Browse files Browse the repository at this point in the history
Remove `gopkg.in/cenkalti/backoff.v1` direct dependency by removing its usage in `headproducer_internal_test.go`. Though it is still present as an indirect dependency since `sse/v2` uses it.

category: refactor
ticket: #2083
  • Loading branch information
xenowits authored Apr 12, 2023
1 parent d8c3e1c commit e755677
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ require (
golang.org/x/time v0.3.0
golang.org/x/tools v0.8.0
google.golang.org/protobuf v1.30.0
gopkg.in/cenkalti/backoff.v1 v1.1.0
)

require (
Expand Down Expand Up @@ -177,6 +176,7 @@ require (
golang.org/x/text v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
46 changes: 33 additions & 13 deletions testutil/beaconmock/headproducer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/r3labs/sse/v2"
"github.com/stretchr/testify/require"
"gopkg.in/cenkalti/backoff.v1"

"github.com/obolnetwork/charon/app/errors"
)
Expand Down Expand Up @@ -72,21 +71,26 @@ func TestHeadProducer(t *testing.T) {
requiredTopics[topic] = true
}

client := sse.NewClient(addr, func(c *sse.Client) {
c.ResponseValidator = func(c *sse.Client, resp *http.Response) error {
require.Equal(t, test.statusCode, resp.StatusCode)
client := sse.NewClient(addr,
func(c *sse.Client) {
c.ResponseValidator = func(c *sse.Client, resp *http.Response) error {
require.Equal(t, test.statusCode, resp.StatusCode)

if resp.StatusCode == http.StatusInternalServerError {
data, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(data), "unknown topic")
if resp.StatusCode == http.StatusInternalServerError {
data, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(data), "unknown topic")

return backoff.Permanent(unsupportedTopicErr)
}
return unsupportedTopicErr
}

return nil
}
})
return nil
}
},
func(c *sse.Client) {
c.ReconnectStrategy = StopBackOff{}
},
)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -117,3 +121,19 @@ func TestHeadProducer(t *testing.T) {
})
}
}

// Refer https://github.com/cenkalti/backoff/blob/v4/backoff.go#L46 for the following snippet.
// We don't need the full dependency since we don't want these tests to support exponential backoff.
// We want simple, fast tests where a single event is sent by the server and is intercepted by the client, or
// it produces an error.

// Stop indicates that no more retries should be made for use in NextBackOff().
const Stop time.Duration = -1

// StopBackOff is a fixed backoff policy that always returns backoff.Stop for
// NextBackOff(), meaning that the operation should never be retried.
type StopBackOff struct{}

func (b StopBackOff) Reset() {}

func (b StopBackOff) NextBackOff() time.Duration { return Stop }

0 comments on commit e755677

Please sign in to comment.