Skip to content

Commit

Permalink
Merge pull request #7 from music-tribe/alex/get-pinger-from-checker
Browse files Browse the repository at this point in the history
Get pinger from checker
  • Loading branch information
Herbievorious authored Apr 10, 2024
2 parents 9a33d51 + 4f8e338 commit f696416
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const defaultName = "healthcheck"
type Checker interface {
Check() error
Name() string
Pinger() Pinger
}

func NewChecker(name string, pinger Pinger) Checker {
Expand Down Expand Up @@ -36,3 +37,7 @@ func (c *checker) Check() error {
func (c *checker) Name() string {
return c.name
}

func (c *checker) Pinger() Pinger {
return c.pinger
}
24 changes: 23 additions & 1 deletion checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/music-tribe/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewChecker(t *testing.T) {
Expand Down Expand Up @@ -40,7 +42,7 @@ func TestNewChecker(t *testing.T) {
name: "when the pinger is missing, it should default to the stub pinger",
args: args{
name: "hello",
pinger: &stubPinger{},
pinger: nil,
},
wantName: "hello",
wantPingErrMsg: stubPingerErrorMessage,
Expand Down Expand Up @@ -77,3 +79,23 @@ func TestNewChecker(t *testing.T) {
})
}
}

func TestGetPinger(t *testing.T) {
t.Run("We should get back the correct pinger when requested", func(t *testing.T) {
checker := NewChecker("hello", &ShutdownPinger{})
pinger := checker.Pinger()
require.NotNil(t, pinger)
shutdownPinger, ok := pinger.(*ShutdownPinger)
assert.True(t, ok)
assert.NotNil(t, shutdownPinger)
})

t.Run("We should get back the stub pinger when no pinger is set", func(t *testing.T) {
checker := NewChecker("hello", nil)
pinger := checker.Pinger()
require.NotNil(t, pinger)
stubPinger, ok := pinger.(*stubPinger)
assert.True(t, ok)
assert.NotNil(t, stubPinger)
})
}

0 comments on commit f696416

Please sign in to comment.