-
Notifications
You must be signed in to change notification settings - Fork 110
/
robot_utils.go
43 lines (38 loc) · 1.16 KB
/
robot_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Package robottestutils provides helper functions in testing
package robottestutils
import (
"context"
"net"
"testing"
"time"
"go.uber.org/zap"
"go.viam.com/test"
"go.viam.com/utils/testutils"
"go.viam.com/rdk/robot/client"
weboptions "go.viam.com/rdk/robot/web/options"
)
// CreateBaseOptionsAndListener creates a new web options with random port as listener.
func CreateBaseOptionsAndListener(tb testing.TB) (weboptions.Options, net.Listener, string) {
tb.Helper()
var listener net.Listener = testutils.ReserveRandomListener(tb)
options := weboptions.New()
options.Network.BindAddress = ""
options.Network.Listener = listener
addr := listener.Addr().String()
return options, listener, addr
}
// NewRobotClient creates a new robot client with a certain address.
func NewRobotClient(tb testing.TB, logger *zap.SugaredLogger, addr string, dur time.Duration) *client.RobotClient {
tb.Helper()
// start robot client
robotClient, err := client.New(
context.Background(),
addr,
logger,
client.WithRefreshEvery(dur),
client.WithCheckConnectedEvery(5*dur),
client.WithReconnectEvery(dur),
)
test.That(tb, err, test.ShouldBeNil)
return robotClient
}