Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get pinger from checker #7

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
Loading