Skip to content

Commit

Permalink
Add tests for retry
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Nov 19, 2023
1 parent 58514fd commit f460268
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const (
DefaultDialTimeout = 60 * time.Second
DefaultRetries = 3
DefaultBackoff = 1 * time.Second
DefaultBackoffMultiplier = 2
DefaultBackoffMultiplier = 2.0
DefaultDisableBackoffCaps = false

// Pool constants.
Expand Down
5 changes: 0 additions & 5 deletions network/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
const (
BackoffMultiplierCap = 10
BackoffDurationCap = time.Minute
DefaultBackoff = 1 * time.Second
)

type IRetry interface {
Expand Down Expand Up @@ -121,10 +120,6 @@ func NewRetry(
logger: logger,
}

if retry.Backoff == 0 {
retry.Backoff = DefaultBackoff
}

if retry.Retries == 0 {
retry.Retries = 1
}
Expand Down
62 changes: 62 additions & 0 deletions network/retry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package network

import (
"context"
"testing"
"time"

"github.com/gatewayd-io/gatewayd/config"
"github.com/gatewayd-io/gatewayd/logging"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)

func TestRetry(t *testing.T) {
logger := logging.NewLogger(context.Background(), logging.LoggerConfig{
Output: []config.LogOutput{config.Console},
TimeFormat: zerolog.TimeFormatUnix,
ConsoleTimeFormat: time.RFC3339,
Level: zerolog.DebugLevel,
NoColor: true,
})

t.Run("DialTimeout", func(t *testing.T) {
t.Run("nil", func(t *testing.T) {
// Nil retry should just dial the connection once.
var retry *Retry
_, err := retry.DialTimeout("", "", 0)
assert.Error(t, err)
assert.ErrorContains(t, err, "dial: unknown network ")
})
t.Run("retry without timeout", func(t *testing.T) {
retry := NewRetry(0, 0, 0, false, logger)
assert.Equal(t, 1, retry.Retries)
assert.Equal(t, time.Duration(0), retry.Backoff)
assert.Equal(t, float64(0), retry.BackoffMultiplier)
assert.False(t, retry.DisableBackoffCaps)

conn, err := retry.DialTimeout("tcp", "localhost:5432", 0)
assert.NoError(t, err)
assert.NotNil(t, conn)
conn.Close()
})
t.Run("retry with timeout", func(t *testing.T) {
retry := NewRetry(
config.DefaultRetries,
config.DefaultBackoff,
config.DefaultBackoffMultiplier,
config.DefaultDisableBackoffCaps,
logger,
)
assert.Equal(t, config.DefaultRetries, retry.Retries)
assert.Equal(t, config.DefaultBackoff, retry.Backoff)
assert.Equal(t, config.DefaultBackoffMultiplier, retry.BackoffMultiplier)
assert.False(t, retry.DisableBackoffCaps)

conn, err := retry.DialTimeout("tcp", "localhost:5432", time.Second)
assert.NoError(t, err)
assert.NotNil(t, conn)
conn.Close()
})
})
}

0 comments on commit f460268

Please sign in to comment.