Skip to content

Commit

Permalink
pkg/services/srvctest: add helper for testing HealthReport names
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Nov 27, 2023
1 parent 6953c5a commit b2682a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/loop/internal/test/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ func (s staticRelayer) Start(ctx context.Context) error { return nil }

func (s staticRelayer) Close() error { return nil }

func (s staticRelayer) Ready() error { panic("unimplemented") }
func (s staticRelayer) Ready() error { return nil }

func (s staticRelayer) Name() string { panic("unimplemented") }

func (s staticRelayer) HealthReport() map[string]error { panic("unimplemented") }
func (s staticRelayer) Name() string { return "staticRelayer" }

func (s staticRelayer) HealthReport() map[string]error {
return map[string]error{s.Name(): s.Ready()}
}
func (s staticRelayer) NewConfigProvider(ctx context.Context, r types.RelayArgs) (types.ConfigProvider, error) {
if !equalRelayArgs(r, RelayArgs) {
return nil, fmt.Errorf("expected relay args:\n\t%v\nbut got:\n\t%v", RelayArgs, r)
Expand Down
21 changes: 21 additions & 0 deletions pkg/loop/relayer_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/loop"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal"
"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/test"
"github.com/smartcontractkit/chainlink-common/pkg/services/srvctest"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
)

Expand Down Expand Up @@ -63,3 +64,23 @@ func TestRelayerService_recovery(t *testing.T) {

test.RunRelayer(t, relayer)
}

func TestRelayerService_HealthReport(t *testing.T) {
lggr := logger.Named(logger.Test(t), "Foo")
s := loop.NewRelayerService(lggr, loop.GRPCOpts{}, func() *exec.Cmd {
return test.HelperProcessCommand{Command: loop.PluginRelayerName}.New()
}, test.ConfigTOML, test.StaticKeystore{})

srvctest.AssertHealthReportNames(t, s.HealthReport(),
"Foo.RelayerService")

require.NoError(t, s.Start(tests.Context(t)))
t.Cleanup(func() { require.NoError(t, s.Close()) })

require.Eventually(t, func() bool { return s.Ready() == nil }, tests.WaitTimeout(t)/2, time.Second, s.Ready())

srvctest.AssertHealthReportNames(t, s.HealthReport(),
"Foo.RelayerService",
"Foo.RelayerService.PluginRelayerClient.ChainRelayerClient",
"staticRelayer")
}
17 changes: 17 additions & 0 deletions pkg/services/srvctest/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package srvctest

import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

func AssertHealthReportNames(t *testing.T, hp map[string]error, names ...string) {
t.Helper()
keys := maps.Keys(hp)
slices.Sort(keys)
slices.Sort(names)
assert.EqualValues(t, names, keys)
}

0 comments on commit b2682a5

Please sign in to comment.