Skip to content

Commit

Permalink
reconfigure in goroutine and check for state running in client new
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis committed Dec 13, 2024
1 parent 70a5096 commit a4d585d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion components/movementsensor/replay/replay_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type mockDataServiceServer struct {
//
//nolint:deprecated,staticcheck
func (mDServer *mockDataServiceServer) TabularDataByFilter(ctx context.Context, req *datapb.TabularDataByFilterRequest,

//nolint:deprecated,staticcheck
) (*datapb.TabularDataByFilterResponse, error) {
) (*datapb.TabularDataByFilterResponse, error) {
filter := req.DataRequest.GetFilter()
last := req.DataRequest.GetLast()
Expand Down
25 changes: 25 additions & 0 deletions robot/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,31 @@ func New(ctx context.Context, address string, clientLogger logging.ZapCompatible
}, rc.activeBackgroundWorkers.Done)
}

// If running in a testing environment, wait for machine to report a state of
// running. We often establish connections in tests and expected resources to
// be immediately available once the web service has started; resources will
// not be available when the machine is still initializing.
//
// It is expected that golang SDK users will handle lack of resource
// availability due to the machine being in an initializing state themselves.
if testing.Testing() {
for {
if ctx.Err() != nil {
return nil, multierr.Combine(ctx.Err(), rc.conn.Close())
}

mStatus, err := rc.MachineStatus(ctx)
if err != nil {
return nil, multierr.Combine(err, rc.conn.Close())
}

if mStatus.State == robot.StateRunning {
break
}
time.Sleep(50 * time.Millisecond)
}
}

return rc, nil
}

Expand Down
14 changes: 7 additions & 7 deletions web/server/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,11 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err
err = multierr.Combine(err, myRobot.Close(context.Background()))
}()

// Start web service with `minimalProcessedConfig`, then `Reconfigure` robot
// to have `fullProcessedConfig`.
// Create initial web options with `minimalProcessedConfig`.
options, err := s.createWebOptions(minimalProcessedConfig)
if err != nil {
return err
}
if err := web.RunWeb(ctx, myRobot, options, s.logger); err != nil {
return err
}
myRobot.Reconfigure(ctx, fullProcessedConfig)

// watch for and deliver changes to the robot
watcher, err := config.NewWatcher(ctx, cfg, s.logger.Sublogger("config"))
Expand All @@ -449,6 +444,10 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err
// config.
oldCfg := fullProcessedConfig
utils.ManagedGo(func() {
// Reconfigure robot to have full processed config before listening for any
// config changes.
myRobot.Reconfigure(ctx, fullProcessedConfig)

for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -508,7 +507,8 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err
<-onWatchDone
}()
defer cancel()
return nil

return web.RunWeb(ctx, myRobot, options, s.logger)
}

// dumpResourceRegistrations prints all builtin resource registrations as a json array
Expand Down
5 changes: 3 additions & 2 deletions web/server/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ func TestMachineState(t *testing.T) {
addr := "localhost:" + strconv.Itoa(port)
rc := robottestutils.NewRobotClient(t, logger, addr, time.Second)

// NewRobotClient will wait for machine state to be running. Assert that this
// is still the case.
machineStatus, err := rc.MachineStatus(context.Background())
test.That(t, err, test.ShouldBeNil)
test.That(t, machineStatus, test.ShouldNotBeNil)
test.That(t, machineStatus.State, test.ShouldBeIn,
[]robot.MachineState{robot.StateInitializing, robot.StateRunning})
test.That(t, machineStatus.State, test.ShouldEqual, robot.StateRunning)
}

0 comments on commit a4d585d

Please sign in to comment.