diff --git a/components/button/button_test.go b/components/button/button_test.go index eb59dd16042..c891739b7ea 100644 --- a/components/button/button_test.go +++ b/components/button/button_test.go @@ -8,6 +8,7 @@ import ( "go.viam.com/rdk/components/button" "go.viam.com/rdk/components/button/fake" + "go.viam.com/rdk/logging" "go.viam.com/rdk/resource" ) @@ -19,11 +20,12 @@ const ( ) func TestPush(t *testing.T) { + logger := logging.NewTestLogger(t) cfg := resource.Config{ Name: "fakeButton", API: button.API, } - button, err := fake.NewButton(context.Background(), nil, cfg, nil) + button, err := fake.NewButton(context.Background(), nil, cfg, logger) test.That(t, err, test.ShouldBeNil) err = button.Push(context.Background(), nil) diff --git a/components/button/client_test.go b/components/button/client_test.go index fc9bbd96fc9..bad74ba562e 100644 --- a/components/button/client_test.go +++ b/components/button/client_test.go @@ -26,14 +26,14 @@ func TestClient(t *testing.T) { var buttonPushed string var extraOptions map[string]interface{} - injectButton := &inject.Button{} + injectButton := inject.NewButton(testButtonName) injectButton.PushFunc = func(ctx context.Context, extra map[string]interface{}) error { extraOptions = extra buttonPushed = testButtonName return nil } - injectButton2 := &inject.Button{} + injectButton2 := inject.NewButton(failButtonName) injectButton2.PushFunc = func(ctx context.Context, extra map[string]interface{}) error { buttonPushed = failButtonName return errCantPush diff --git a/components/button/fake/button.go b/components/button/fake/button.go index ee9776da5db..dbd6803e835 100644 --- a/components/button/fake/button.go +++ b/components/button/fake/button.go @@ -15,11 +15,11 @@ func init() { resource.RegisterComponent(button.API, model, resource.Registration[button.Button, *resource.NoNativeConfig]{Constructor: NewButton}) } -// Button is a fake button that logs when it is pressed +// Button is a fake button that logs when it is pressed. type Button struct { resource.Named resource.TriviallyCloseable - resource.AlwaysRebuild + resource.AlwaysRebuild logger logging.Logger } @@ -34,7 +34,7 @@ func NewButton( return b, nil } -// Push logs the push +// Push logs the push. func (b *Button) Push(ctx context.Context, extra map[string]interface{}) error { b.logger.Info("pushed button") return nil diff --git a/components/button/server.go b/components/button/server.go index 171cc289a62..89ab4c92092 100644 --- a/components/button/server.go +++ b/components/button/server.go @@ -23,7 +23,7 @@ func NewRPCServiceServer(coll resource.APIResourceCollection[Button]) interface{ return &serviceServer{coll: coll} } -// Pushes a button +// Pushes a button. func (s *serviceServer) Push(ctx context.Context, req *pb.PushRequest) (*pb.PushResponse, error) { button, err := s.coll.Resource(req.Name) if err != nil { diff --git a/testutils/inject/button.go b/testutils/inject/button.go index 0de487aa8a8..e3e83ddac36 100644 --- a/testutils/inject/button.go +++ b/testutils/inject/button.go @@ -10,12 +10,20 @@ import ( // Button implements button.Button for testing. type Button struct { button.Button + name resource.Name + PushFunc func(ctx context.Context, extra map[string]interface{}) error + DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) + CloseFunc func(ctx context.Context) error +} + +// NewButton returns a new injected button. +func NewButton(name string) *Button { + return &Button{name: button.Named(name)} +} - resource.Named - resource.TriviallyReconfigurable - resource.TriviallyCloseable - PushFunc func(ctx context.Context, extra map[string]interface{}) error - DoFunc func(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) +// Name returns the name of the resource. +func (b *Button) Name() resource.Name { + return b.name } // Push calls PushFunc. @@ -33,3 +41,11 @@ func (b *Button) DoCommand(ctx context.Context, cmd map[string]interface{}) (map } return b.DoFunc(ctx, cmd) } + +// Close calls CloseFunc. +func (b *Button) Close(ctx context.Context) error { + if b.CloseFunc == nil { + return b.Button.Close(ctx) + } + return b.CloseFunc(ctx) +}