Skip to content

Commit

Permalink
[RSDK-7437] check if a pin is already in the names list (#3861)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnN193 authored and 10zingpd committed Apr 29, 2024
1 parent 212b14a commit 2684a8d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
9 changes: 7 additions & 2 deletions components/board/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package board
import (
"context"
"math"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -67,7 +68,9 @@ func NewClientFromConn(
}

func (c *client) AnalogByName(name string) (Analog, error) {
c.info.analogNames = append(c.info.analogNames, name)
if !slices.Contains(c.info.analogNames, name) {
c.info.analogNames = append(c.info.analogNames, name)
}
return &analogClient{
client: c,
boardName: c.info.name,
Expand All @@ -76,7 +79,9 @@ func (c *client) AnalogByName(name string) (Analog, error) {
}

func (c *client) DigitalInterruptByName(name string) (DigitalInterrupt, error) {
c.info.digitalInterruptNames = append(c.info.digitalInterruptNames, name)
if !slices.Contains(c.info.digitalInterruptNames, name) {
c.info.digitalInterruptNames = append(c.info.digitalInterruptNames, name)
}
return &digitalInterruptClient{
client: c,
boardName: c.info.name,
Expand Down
67 changes: 67 additions & 0 deletions components/board/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package board_test
import (
"context"
"net"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -219,3 +220,69 @@ func TestWorkingClient(t *testing.T) {
test.That(t, conn.Close(), test.ShouldBeNil)
})
}

// these tests validate that for modular boards(which rely on client.go's board interface)
// we will only add new pins to the cached name lists.
func TestClientNames(t *testing.T) {
logger := logging.NewTestLogger(t)
injectBoard := &inject.Board{}

listener, cleanup := setupService(t, injectBoard)
defer cleanup()
t.Run("test analog names are cached correctly in the client", func(t *testing.T) {
ctx := context.Background()
conn, err := viamgrpc.Dial(ctx, listener.Addr().String(), logger)
test.That(t, err, test.ShouldBeNil)
client, err := board.NewClientFromConn(ctx, conn, "", board.Named(testBoardName), logger)
test.That(t, err, test.ShouldBeNil)

nameFunc := func(name string) error {
_, err = client.AnalogByName(name)
return err
}
testNamesAPI(t, client.AnalogNames, nameFunc, "Analog")

test.That(t, conn.Close(), test.ShouldBeNil)
})

t.Run("test interrupt names are cached correctly in the client", func(t *testing.T) {
ctx := context.Background()
conn, err := viamgrpc.Dial(ctx, listener.Addr().String(), logger)
test.That(t, err, test.ShouldBeNil)
client, err := board.NewClientFromConn(ctx, conn, "", board.Named(testBoardName), logger)
test.That(t, err, test.ShouldBeNil)

nameFunc := func(name string) error {
_, err = client.DigitalInterruptByName(name)
return err
}
testNamesAPI(t, client.DigitalInterruptNames, nameFunc, "DigitalInterrupt")
test.That(t, conn.Close(), test.ShouldBeNil)
})
}

func testNamesAPI(t *testing.T, namesFunc func() []string, nameFunc func(string) error, name string) {
t.Helper()
names := namesFunc()
test.That(t, len(names), test.ShouldEqual, 0)
name1 := name + "1"
err := nameFunc(name1)
test.That(t, err, test.ShouldBeNil)
names = namesFunc()
test.That(t, len(names), test.ShouldEqual, 1)
test.That(t, slices.Contains(names, name1), test.ShouldBeTrue)

name2 := name + "2"
err = nameFunc(name2)
test.That(t, err, test.ShouldBeNil)

names = namesFunc()
test.That(t, len(names), test.ShouldEqual, 2)
test.That(t, slices.Contains(names, name2), test.ShouldBeTrue)

err = nameFunc(name1)
test.That(t, err, test.ShouldBeNil)
names = namesFunc()
test.That(t, len(names), test.ShouldEqual, 2)
test.That(t, slices.Contains(names, name1), test.ShouldBeTrue)
}

0 comments on commit 2684a8d

Please sign in to comment.