Skip to content

Commit

Permalink
Improve fake server interface (viamrobotics#3698)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpertsov authored Mar 15, 2024
1 parent c629c2d commit 3ec4395
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
18 changes: 2 additions & 16 deletions config/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,10 @@ func TestFromReader(t *testing.T) {
test.That(t, os.IsNotExist(err), test.ShouldBeTrue)
}

setupFakeServer := func(t *testing.T) (*testutils.FakeCloudServer, func()) {
t.Helper()

logger := logging.NewTestLogger(t)

fakeServer, err := testutils.NewFakeCloudServer(context.Background(), logger)
test.That(t, err, test.ShouldBeNil)
cleanup := func() {
test.That(t, fakeServer.Shutdown(), test.ShouldBeNil)
}

return fakeServer, cleanup
}

t.Run("online", func(t *testing.T) {
setupClearCache(t)

fakeServer, cleanup := setupFakeServer(t)
fakeServer, cleanup := testutils.NewFakeCloudServer(t, ctx, logger)
defer cleanup()

cloudResponse := &Cloud{
Expand Down Expand Up @@ -114,7 +100,7 @@ func TestFromReader(t *testing.T) {
test.That(t, err, test.ShouldBeNil)
defer clearCache(robotPartID)

fakeServer, cleanup := setupFakeServer(t)
fakeServer, cleanup := testutils.NewFakeCloudServer(t, ctx, logger)
defer cleanup()
fakeServer.FailOnConfigAndCertsWith(context.DeadlineExceeded)
fakeServer.StoreDeviceConfig(robotPartID, nil, nil)
Expand Down
19 changes: 12 additions & 7 deletions config/testutils/fake_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"net"
"net/http"
"sync"
"testing"

pb "go.viam.com/api/app/v1"
"go.viam.com/test"
"go.viam.com/utils"
"go.viam.com/utils/rpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -42,10 +44,12 @@ type configAndCerts struct {
}

// NewFakeCloudServer creates and starts a new grpc server for the Viam Cloud.
func NewFakeCloudServer(ctx context.Context, logger logging.Logger) (*FakeCloudServer, error) {
func NewFakeCloudServer(t *testing.T, ctx context.Context, logger logging.Logger) (*FakeCloudServer, func()) { //nolint:revive
t.Helper()

listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
if err != nil {
return nil, err
t.Fatalf("failed to start listener for fake cloud server")
}

server := &FakeCloudServer{
Expand All @@ -63,7 +67,7 @@ func NewFakeCloudServer(ctx context.Context, logger logging.Logger) (*FakeCloudS
)),
rpc.WithWebRTCServerOptions(rpc.WebRTCServerOptions{Enable: false}))
if err != nil {
return nil, err
t.Fatalf("failed to create new fake cloud server")
}

err = server.rpcServer.RegisterServiceServer(
Expand All @@ -73,9 +77,8 @@ func NewFakeCloudServer(ctx context.Context, logger logging.Logger) (*FakeCloudS
pb.RegisterRobotServiceHandlerFromEndpoint,
)
if err != nil {
return nil, err
t.Fatalf("failed to register robot service for new fake cloud server")
}

server.exitWg.Add(1)
utils.PanicCapturingGo(func() {
defer server.exitWg.Done()
Expand All @@ -85,8 +88,10 @@ func NewFakeCloudServer(ctx context.Context, logger logging.Logger) (*FakeCloudS
logger.Warnf("Error shutting down grpc server", "error", err)
}
})

return server, nil
shutdown := func() {
test.That(t, server.Shutdown(), test.ShouldBeNil)
}
return server, shutdown
}

// FailOnConfigAndCerts if `failure` is true the server will return an Internal error on
Expand Down
7 changes: 2 additions & 5 deletions config/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,8 @@ func TestNewWatcherCloud(t *testing.T) {

deviceID := primitive.NewObjectID().Hex()

fakeServer, err := testutils.NewFakeCloudServer(context.Background(), logger)
test.That(t, err, test.ShouldBeNil)
defer func() {
test.That(t, fakeServer.Shutdown(), test.ShouldBeNil)
}()
fakeServer, cleanup := testutils.NewFakeCloudServer(t, context.Background(), logger)
defer cleanup()

storeConfigInServer := func(cfg config.Config) {
cloudConfProto, err := config.CloudConfigToProto(cfg.Cloud)
Expand Down

0 comments on commit 3ec4395

Please sign in to comment.