Skip to content

Commit b55cafa

Browse files
committed
fix: tests
1 parent 2ca286a commit b55cafa

4 files changed

Lines changed: 32 additions & 18 deletions

File tree

packages/api/internal/orchestrator/autoresume_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func TestHandleExistingSandboxAutoResume(t *testing.T) {
113113
require.NoError(t, err)
114114
assert.False(t, alreadyDone)
115115
require.NotNil(t, finish)
116+
116117
finish(t.Context(), nil)
117118

118119
pausingSandbox, err := o.GetSandbox(t.Context(), sbx.TeamID, sbx.SandboxID)
@@ -125,7 +126,7 @@ func TestHandleExistingSandboxAutoResume(t *testing.T) {
125126
assert.ErrorIs(t, err, ErrSandboxStillTransitioning)
126127
})
127128

128-
t.Run("pausing sandbox wait failure returns internal error", func(t *testing.T) {
129+
t.Run("concurrently pausing sandbox returns internal error", func(t *testing.T) {
129130
t.Parallel()
130131

131132
o := newTestAutoResumeOrchestrator()
@@ -136,10 +137,15 @@ func TestHandleExistingSandboxAutoResume(t *testing.T) {
136137
require.NoError(t, err)
137138
assert.False(t, alreadyDone)
138139
require.NotNil(t, finish)
139-
finish(t.Context(), errors.New("boom"))
140140

141141
pausingSandbox, err := o.GetSandbox(t.Context(), sbx.TeamID, sbx.SandboxID)
142142
require.NoError(t, err)
143+
assert.Equal(t, sandbox.StatePausing, pausingSandbox.State)
144+
145+
go func() {
146+
time.Sleep(50 * time.Millisecond)
147+
finish(t.Context(), errors.New("boom"))
148+
}()
143149

144150
_, handled, err := o.HandleExistingSandboxAutoResume(t.Context(), sbx.TeamID, sbx.SandboxID, pausingSandbox, time.Minute)
145151
require.Error(t, err)

packages/api/internal/orchestrator/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ func (o *Orchestrator) discoverClusterNode(ctx context.Context, clusterID uuid.U
212212
ctx, span := tracer.Start(ctx, "discover-cluster-node")
213213
defer span.End()
214214

215+
if o.clusters == nil {
216+
logger.L().Error(ctx, "Cluster pool not initialized during on-demand node discovery", logger.WithClusterID(clusterID))
217+
218+
return
219+
}
220+
215221
cluster, found := o.clusters.GetClusterById(clusterID)
216222
if !found {
217223
logger.L().Error(ctx, "Cluster not found during on-demand node discovery", logger.WithClusterID(clusterID))

packages/api/internal/sandbox/storage/memory/operations.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ func startRemoving(ctx context.Context, sbx *memorySandbox, opts sandbox.RemoveO
172172
}
173173
}
174174

175+
originalState := sbx._data.State
175176
newState := opts.Action.TargetState
176177

177178
if transition != nil {
178-
currentState := sbx._data.State
179179
sbx.mu.Unlock()
180180

181-
if currentState != newState && !sandbox.AllowedTransitions[currentState][newState] {
182-
return false, nil, &sandbox.InvalidStateTransitionError{CurrentState: currentState, TargetState: newState}
181+
if originalState != newState && !sandbox.AllowedTransitions[originalState][newState] {
182+
return false, nil, &sandbox.InvalidStateTransitionError{CurrentState: originalState, TargetState: newState}
183183
}
184184

185185
logger.L().Debug(ctx, "State transition already in progress to the same state, waiting", logger.WithSandboxID(sbx.SandboxID()), zap.String("state", string(newState)))
@@ -190,9 +190,9 @@ func startRemoving(ctx context.Context, sbx *memorySandbox, opts sandbox.RemoveO
190190

191191
// If the transition is to the same state just wait
192192
switch {
193-
case currentState == newState:
193+
case originalState == newState:
194194
return true, func(context.Context, error) {}, nil
195-
case sandbox.AllowedTransitions[currentState][newState]:
195+
case sandbox.AllowedTransitions[originalState][newState]:
196196
return startRemoving(ctx, sbx, sandbox.RemoveOpts{Action: opts.Action})
197197
default:
198198
return false, nil, fmt.Errorf("unexpected state transition")
@@ -238,11 +238,11 @@ func startRemoving(ctx context.Context, sbx *memorySandbox, opts sandbox.RemoveO
238238
}
239239

240240
if err != nil {
241-
// Keep the transition in place so the error stays
242-
return
241+
// Revert the state change if the transition failed and it's not a transient transition
242+
sbx._data.State = originalState
243243
}
244244

245-
// The transition is completed and the next transition can be started
245+
// Remove the transition so the next transition can be started
246246
sbx.transition = nil
247247
}
248248

packages/api/internal/sandbox/storage/memory/operations_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,21 @@ func TestStartRemoving_Error(t *testing.T) {
229229
assert.False(t, alreadyDone2)
230230
assert.Nil(t, finish2)
231231

232-
// From Failed state, no transitions are allowed
232+
// Failed transition should be cleared so subsequent transitions can proceed.
233233
alreadyDone3, finish3, err3 := startRemoving(ctx, sbx, sandbox.RemoveOpts{Action: sandbox.StateActionPause})
234-
require.Error(t, err3)
235-
require.ErrorIs(t, err3, failureErr)
234+
require.NoError(t, err3)
236235
assert.False(t, alreadyDone3)
237-
assert.Nil(t, finish3)
236+
require.NotNil(t, finish3)
237+
finish3(ctx, nil)
238+
assert.Equal(t, sandbox.StatePausing, sbx.State())
238239

239-
// Trying to transition to Killed should also fail
240+
// Follow-up transition should also work.
240241
alreadyDone4, finish4, err4 := startRemoving(ctx, sbx, sandbox.RemoveOpts{Action: sandbox.StateActionKill})
241-
require.Error(t, err4)
242-
require.ErrorIs(t, err4, failureErr)
242+
require.NoError(t, err4)
243243
assert.False(t, alreadyDone4)
244-
assert.Nil(t, finish4)
244+
require.NotNil(t, finish4)
245+
finish4(ctx, nil)
246+
assert.Equal(t, sandbox.StateKilling, sbx.State())
245247
}
246248

247249
// Test context timeout during wait

0 commit comments

Comments
 (0)