Skip to content

Commit 17f8439

Browse files
committed
fix(app): bound LCU status refresh
1 parent d5b4b33 commit 17f8439

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

internal/app/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
var ErrUpdateUnavailable = errors.New("update check unavailable")
1515

16+
const lcuStateRefreshTimeout = 750 * time.Millisecond
17+
1618
type (
1719
ServiceFactory func(RuntimeConfig) (autobuild.Service, error)
1820
LCUStatusProvider func(context.Context, RuntimeConfig) LCUStatus
@@ -131,7 +133,9 @@ func (a *App) State(ctx context.Context) ViewState {
131133
if a.coachlessAuth != nil {
132134
state.CoachlessAuth = cloneCoachlessAuthState(a.coachlessAuth.Status(ctx))
133135
}
134-
state.LCU = a.lcuStatus(ctx, configSnapshot)
136+
statusCtx, cancel := context.WithTimeout(ctx, lcuStateRefreshTimeout)
137+
state.LCU = a.lcuStatus(statusCtx, configSnapshot)
138+
cancel()
135139

136140
return state
137141
}

internal/app/app_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,29 @@ func TestStateCopiesCoachlessAuthState(t *testing.T) {
384384
}
385385
}
386386

387+
func TestStateBoundsLCUStatusRefresh(t *testing.T) {
388+
app := newTestApp(t, testAppOptions{
389+
lcuStatus: func(ctx context.Context, _ RuntimeConfig) LCUStatus {
390+
<-ctx.Done()
391+
return LCUStatus{State: LCUConnectionStateNotConnected, Message: ctx.Err().Error()}
392+
},
393+
})
394+
395+
start := time.Now()
396+
state := app.State(context.Background())
397+
elapsed := time.Since(start)
398+
399+
if elapsed > lcuStateRefreshTimeout+500*time.Millisecond {
400+
t.Fatalf("State() took %v, want bounded by LCU state refresh timeout", elapsed)
401+
}
402+
if state.LCU.State != LCUConnectionStateNotConnected {
403+
t.Fatalf("LCU state = %q, want not_connected", state.LCU.State)
404+
}
405+
if state.LCU.Message != context.DeadlineExceeded.Error() {
406+
t.Fatalf("LCU message = %q, want deadline exceeded", state.LCU.Message)
407+
}
408+
}
409+
387410
func TestCoachlessAuthActionsUseSessionWithoutSavingConfig(t *testing.T) {
388411
store := &recordingConfigStore{}
389412
authSession := &stubCoachlessAuth{

0 commit comments

Comments
 (0)