Skip to content

Commit

Permalink
Use go-time instead of own duration implementation (#54)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Jun 12, 2024
1 parent 7ac9456 commit a94e975
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 189 deletions.
4 changes: 2 additions & 2 deletions checkers/dns_lookup/dns_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func New(s spec) (checkers.Checker, error) {
query: s.Query,
resolver: s.Resolver,
tries: s.Tries,
interval: time.Duration(s.Interval),
timeout: time.Duration(s.Timeout),
interval: s.Interval.TimeDuration(),
timeout: s.Timeout.TimeDuration(),

resolverMaker: mkResolver,
}, nil
Expand Down
11 changes: 5 additions & 6 deletions checkers/dns_lookup/dns_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

func TestMkResolver(t *testing.T) {
Expand Down Expand Up @@ -49,8 +48,8 @@ func (s *checkTestSuite) TestHappyPath() {
Query: "example.com",
Resolver: "127.0.0.1:53",
Tries: 3,
Interval: config.Duration(2 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(2 * time.Second),
Timeout: th.Duration(5 * time.Second),
})
s.Require().NoError(err)

Expand All @@ -68,8 +67,8 @@ func (s *checkTestSuite) TestSecondTry() {
Query: "example.com",
Resolver: "127.0.0.1:53",
Tries: 3,
Interval: config.Duration(2 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(2 * time.Second),
Timeout: th.Duration(5 * time.Second),
})
s.Require().NoError(err)

Expand Down
13 changes: 6 additions & 7 deletions checkers/dns_lookup/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package dns_lookup
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

type spec struct {
Query string `json:"query"`
Resolver string `json:"resolver"`
Tries uint8 `json:"tries"`
Interval config.Duration `json:"interval"`
Timeout config.Duration `json:"timeout"`
Query string `json:"query"`
Resolver string `json:"resolver"`
Tries uint8 `json:"tries"`
Interval th.Duration `json:"interval"`
Timeout th.Duration `json:"timeout"`
}

func (s spec) Validate() error {
Expand Down
15 changes: 7 additions & 8 deletions checkers/dns_lookup/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

func TestSpecValidation(t *testing.T) {
Expand All @@ -24,8 +23,8 @@ func TestSpecValidation(t *testing.T) {
Query: "example.com",
Resolver: "127.0.0.1:53",
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(5 * time.Second),
},
},
{
Expand All @@ -34,8 +33,8 @@ func TestSpecValidation(t *testing.T) {
Query: "blah!",
Resolver: "127.0.0.1:53",
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(5 * time.Second),
},
expError: errors.New("query: must be a valid DNS name."),
},
Expand All @@ -45,8 +44,8 @@ func TestSpecValidation(t *testing.T) {
Query: "example.com",
Resolver: "!!!",
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(5 * time.Second),
},
expError: errors.New("resolver: must be a valid dial string."),
},
Expand Down
4 changes: 2 additions & 2 deletions checkers/http_2xx/http_2xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type http_2xx struct {

func New(s spec) (checkers.Checker, error) {
client := &http.Client{
Timeout: time.Duration(s.Timeout),
Timeout: s.Timeout.TimeDuration(),
}

if err := s.Validate(); err != nil {
Expand All @@ -42,7 +42,7 @@ func New(s spec) (checkers.Checker, error) {
url: s.URL,
method: s.Method,
tries: s.Tries,
interval: time.Duration(s.Interval),
interval: s.Interval.TimeDuration(),
}, nil
}

Expand Down
19 changes: 9 additions & 10 deletions checkers/http_2xx/http_2xx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

func TestSpec(t *testing.T) {
Expand All @@ -40,8 +39,8 @@ func (s *http2xxTestSuite) TestTrivial() {
URL: s.srv.URL + "/ping",
Method: "GET",
Tries: 1,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(5 * time.Second),
})
s.Require().NoError(err)

Expand All @@ -56,8 +55,8 @@ func (s *http2xxTestSuite) TestFiveTries() {
URL: s.srv.URL + "/ping",
Method: "GET",
Tries: 5,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(5 * time.Second),
})
s.Require().NoError(err)

Expand All @@ -74,8 +73,8 @@ func (s *http2xxTestSuite) TestSuccessFromThirdTime() {
URL: s.srv.URL + "/ping",
Method: "GET",
Tries: 3,
Interval: config.Duration(1 * time.Millisecond),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Millisecond),
Timeout: th.Duration(5 * time.Second),
})
s.Require().NoError(err)

Expand All @@ -90,8 +89,8 @@ func (s *http2xxTestSuite) TestNegative() {
URL: s.srv.URL + "/ping",
Method: "GET",
Tries: 2,
Interval: config.Duration(1 * time.Millisecond),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Millisecond),
Timeout: th.Duration(5 * time.Second),
})
s.Require().NoError(err)

Expand Down
13 changes: 6 additions & 7 deletions checkers/http_2xx/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package http_2xx
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

type spec struct {
URL string `json:"url"`
Method string `json:"method"`
Tries uint8 `json:"tries"`
Interval config.Duration `json:"interval"`
Timeout config.Duration `json:"timeout"`
URL string `json:"url"`
Method string `json:"method"`
Tries uint8 `json:"tries"`
Interval th.Duration `json:"interval"`
Timeout th.Duration `json:"timeout"`
}

func (s spec) Validate() error {
Expand Down
7 changes: 3 additions & 4 deletions checkers/http_2xx/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

func TestSpecValidation(t *testing.T) {
Expand All @@ -24,8 +23,8 @@ func TestSpecValidation(t *testing.T) {
URL: "http://127.0.0.1:8080/",
Method: "GET",
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(5 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(5 * time.Second),
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions checkers/icmp_ping/icmp_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func newWithPinger(
return &icmp_ping{
host: s.Static.Host,
tries: s.Tries,
interval: time.Duration(s.Interval),
timeout: time.Duration(s.Timeout),
interval: s.Interval.TimeDuration(),
timeout: s.Timeout.TimeDuration(),

pingerFn: pingerFn,
}, nil
Expand Down
7 changes: 3 additions & 4 deletions checkers/icmp_ping/icmp_ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"time"

"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

func TestSpec(t *testing.T) {
Expand All @@ -35,8 +34,8 @@ func TestCheck(t *testing.T) {
spec{
Static: Static{Host: "127.0.0.1"},
Tries: 10,
Interval: config.Duration(10 * time.Second),
Timeout: config.Duration(30 * time.Second),
Interval: th.Duration(10 * time.Second),
Timeout: th.Duration(30 * time.Second),
},
func(host string, tries uint8, interval, timeout time.Duration) (*pingStats, error) {
r.Equal("127.0.0.1", host)
Expand Down
11 changes: 5 additions & 6 deletions checkers/icmp_ping/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package icmp_ping
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

type Static struct {
Expand All @@ -18,10 +17,10 @@ func (s Static) Validate() error {
}

type spec struct {
Static Static `json:"static"`
Tries uint8 `json:"tries"`
Interval config.Duration `json:"interval"`
Timeout config.Duration `json:"timeout"`
Static Static `json:"static"`
Tries uint8 `json:"tries"`
Interval th.Duration `json:"interval"`
Timeout th.Duration `json:"timeout"`
}

func (s spec) Validate() error {
Expand Down
11 changes: 5 additions & 6 deletions checkers/icmp_ping/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

func TestSpecValidation(t *testing.T) {
Expand All @@ -23,17 +22,17 @@ func TestSpecValidation(t *testing.T) {
in: spec{
Static: Static{Host: "test.example.org"},
Tries: 10,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(2 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(2 * time.Second),
},
},
{
name: "valid spec w/ IP address",
in: spec{
Static: Static{Host: "127.0.0.1"},
Tries: 10,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(2 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(2 * time.Second),
},
},
{
Expand Down
13 changes: 6 additions & 7 deletions checkers/tftp_rrq/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package tftp_rrq
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"

"github.com/teran/anycastd/config"
th "github.com/teran/go-time"
)

type spec struct {
URL string `json:"url"`
ExpectedSHA256 *string `json:"expected_sha256"`
Tries uint8 `json:"tries"`
Interval config.Duration `json:"interval"`
Timeout config.Duration `json:"timeout"`
URL string `json:"url"`
ExpectedSHA256 *string `json:"expected_sha256"`
Tries uint8 `json:"tries"`
Interval th.Duration `json:"interval"`
Timeout th.Duration `json:"timeout"`
}

func (s spec) Validate() error {
Expand Down
18 changes: 9 additions & 9 deletions checkers/tftp_rrq/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
th "github.com/teran/go-time"

"github.com/teran/anycastd/config"
"github.com/teran/go-ptr"
)

Expand All @@ -24,8 +24,8 @@ func TestSpecValidation(t *testing.T) {
in: spec{
URL: "tftp://127.0.0.1:69/lpxelinux.0",
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(2 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(2 * time.Second),
},
},
{
Expand All @@ -34,8 +34,8 @@ func TestSpecValidation(t *testing.T) {
URL: "tftp://127.0.0.1:69/lpxelinux.0",
ExpectedSHA256: ptr.String("09da9c01b6b2a8ccc5d3445c4f364243d8a063bd0bf520643737899e6ce0170f"),
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(2 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(2 * time.Second),
},
},
{
Expand All @@ -51,8 +51,8 @@ func TestSpecValidation(t *testing.T) {
URL: "tftp://127.0.0.1:69/lpxelinux.0",
ExpectedSHA256: ptr.String("09da9c01b6b2a8ccc5d3445c4f364243d8a063bd0bf520643737899e6ce0170s"),
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(2 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(2 * time.Second),
},
expError: errors.New("expected_sha256: must be a valid hexadecimal number."),
},
Expand All @@ -62,8 +62,8 @@ func TestSpecValidation(t *testing.T) {
URL: "tftp://127.0.0.1:69/lpxelinux.0",
ExpectedSHA256: ptr.String("deadbeef"),
Tries: 3,
Interval: config.Duration(1 * time.Second),
Timeout: config.Duration(2 * time.Second),
Interval: th.Duration(1 * time.Second),
Timeout: th.Duration(2 * time.Second),
},
expError: errors.New("expected_sha256: the length must be exactly 64."),
},
Expand Down
Loading

0 comments on commit a94e975

Please sign in to comment.