diff --git a/config/reader_test.go b/config/reader_test.go index bb76abb956b..c24cad9927e 100644 --- a/config/reader_test.go +++ b/config/reader_test.go @@ -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{ @@ -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) diff --git a/config/testutils/fake_cloud.go b/config/testutils/fake_cloud.go index 08d220771e6..4df27fc2f0f 100644 --- a/config/testutils/fake_cloud.go +++ b/config/testutils/fake_cloud.go @@ -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" @@ -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{ @@ -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( @@ -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() @@ -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 diff --git a/config/watcher_test.go b/config/watcher_test.go index 2287c918b4f..2fbec70a0ac 100644 --- a/config/watcher_test.go +++ b/config/watcher_test.go @@ -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)