Skip to content

Commit 2684a8d

Browse files
JohnN19310zingpd
authored andcommitted
[RSDK-7437] check if a pin is already in the names list (#3861)
1 parent 212b14a commit 2684a8d

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

components/board/client.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package board
44
import (
55
"context"
66
"math"
7+
"slices"
78
"sync"
89
"time"
910

@@ -67,7 +68,9 @@ func NewClientFromConn(
6768
}
6869

6970
func (c *client) AnalogByName(name string) (Analog, error) {
70-
c.info.analogNames = append(c.info.analogNames, name)
71+
if !slices.Contains(c.info.analogNames, name) {
72+
c.info.analogNames = append(c.info.analogNames, name)
73+
}
7174
return &analogClient{
7275
client: c,
7376
boardName: c.info.name,
@@ -76,7 +79,9 @@ func (c *client) AnalogByName(name string) (Analog, error) {
7679
}
7780

7881
func (c *client) DigitalInterruptByName(name string) (DigitalInterrupt, error) {
79-
c.info.digitalInterruptNames = append(c.info.digitalInterruptNames, name)
82+
if !slices.Contains(c.info.digitalInterruptNames, name) {
83+
c.info.digitalInterruptNames = append(c.info.digitalInterruptNames, name)
84+
}
8085
return &digitalInterruptClient{
8186
client: c,
8287
boardName: c.info.name,

components/board/client_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package board_test
33
import (
44
"context"
55
"net"
6+
"slices"
67
"testing"
78
"time"
89

@@ -219,3 +220,69 @@ func TestWorkingClient(t *testing.T) {
219220
test.That(t, conn.Close(), test.ShouldBeNil)
220221
})
221222
}
223+
224+
// these tests validate that for modular boards(which rely on client.go's board interface)
225+
// we will only add new pins to the cached name lists.
226+
func TestClientNames(t *testing.T) {
227+
logger := logging.NewTestLogger(t)
228+
injectBoard := &inject.Board{}
229+
230+
listener, cleanup := setupService(t, injectBoard)
231+
defer cleanup()
232+
t.Run("test analog names are cached correctly in the client", func(t *testing.T) {
233+
ctx := context.Background()
234+
conn, err := viamgrpc.Dial(ctx, listener.Addr().String(), logger)
235+
test.That(t, err, test.ShouldBeNil)
236+
client, err := board.NewClientFromConn(ctx, conn, "", board.Named(testBoardName), logger)
237+
test.That(t, err, test.ShouldBeNil)
238+
239+
nameFunc := func(name string) error {
240+
_, err = client.AnalogByName(name)
241+
return err
242+
}
243+
testNamesAPI(t, client.AnalogNames, nameFunc, "Analog")
244+
245+
test.That(t, conn.Close(), test.ShouldBeNil)
246+
})
247+
248+
t.Run("test interrupt names are cached correctly in the client", func(t *testing.T) {
249+
ctx := context.Background()
250+
conn, err := viamgrpc.Dial(ctx, listener.Addr().String(), logger)
251+
test.That(t, err, test.ShouldBeNil)
252+
client, err := board.NewClientFromConn(ctx, conn, "", board.Named(testBoardName), logger)
253+
test.That(t, err, test.ShouldBeNil)
254+
255+
nameFunc := func(name string) error {
256+
_, err = client.DigitalInterruptByName(name)
257+
return err
258+
}
259+
testNamesAPI(t, client.DigitalInterruptNames, nameFunc, "DigitalInterrupt")
260+
test.That(t, conn.Close(), test.ShouldBeNil)
261+
})
262+
}
263+
264+
func testNamesAPI(t *testing.T, namesFunc func() []string, nameFunc func(string) error, name string) {
265+
t.Helper()
266+
names := namesFunc()
267+
test.That(t, len(names), test.ShouldEqual, 0)
268+
name1 := name + "1"
269+
err := nameFunc(name1)
270+
test.That(t, err, test.ShouldBeNil)
271+
names = namesFunc()
272+
test.That(t, len(names), test.ShouldEqual, 1)
273+
test.That(t, slices.Contains(names, name1), test.ShouldBeTrue)
274+
275+
name2 := name + "2"
276+
err = nameFunc(name2)
277+
test.That(t, err, test.ShouldBeNil)
278+
279+
names = namesFunc()
280+
test.That(t, len(names), test.ShouldEqual, 2)
281+
test.That(t, slices.Contains(names, name2), test.ShouldBeTrue)
282+
283+
err = nameFunc(name1)
284+
test.That(t, err, test.ShouldBeNil)
285+
names = namesFunc()
286+
test.That(t, len(names), test.ShouldEqual, 2)
287+
test.That(t, slices.Contains(names, name1), test.ShouldBeTrue)
288+
}

0 commit comments

Comments
 (0)