@@ -143,15 +143,15 @@ func TestInMemoryStateStore_ListActiveWorkflows(t *testing.T) {
143143// TestInMemoryStateStore_Cleanup tests automatic cleanup of stale workflows.
144144func TestInMemoryStateStore_Cleanup (t * testing.T ) {
145145 t .Parallel ()
146- // Use very short intervals for testing but with sufficient margin
146+ // Use short intervals with enough margin to reduce CI timing flakiness.
147147 cleanupInterval := 50 * time .Millisecond
148- maxAge := 50 * time .Millisecond
148+ maxAge := 100 * time .Millisecond
149149
150150 store := NewInMemoryStateStore (cleanupInterval , maxAge ).(* inMemoryStateStore )
151151 defer store .Stop ()
152152
153153 // Create workflows directly in the store with specific timestamps
154- veryOldTime := time .Now ().Add (- 1 * time .Second ) // Way older than maxAge
154+ veryOldTime := time .Now ().Add (- 500 * time .Millisecond ) // Well older than maxAge
155155
156156 store .mu .Lock ()
157157 // Old completed workflow - should be cleaned up
@@ -169,20 +169,15 @@ func TestInMemoryStateStore_Cleanup(t *testing.T) {
169169 }
170170 store .mu .Unlock ()
171171
172- // Wait for at least 2 cleanup cycles
173- time .Sleep (150 * time .Millisecond )
172+ require .Eventually (t , func () bool {
173+ store .mu .RLock ()
174+ oldExists := store .states ["old-workflow" ]
175+ runningExists := store .states ["running-workflow" ]
176+ store .mu .RUnlock ()
174177
175- // Verify cleanup results
176- store .mu .RLock ()
177- oldExists := store .states ["old-workflow" ]
178- runningExists := store .states ["running-workflow" ]
179- store .mu .RUnlock ()
180-
181- // Old completed workflow should be cleaned up
182- assert .Nil (t , oldExists , "old completed workflow should be cleaned up" )
183-
184- // Running workflow should still exist (not a terminal state)
185- assert .NotNil (t , runningExists , "running workflow should not be cleaned up" )
178+ // Old completed workflow should be cleaned up, running workflow must remain.
179+ return oldExists == nil && runningExists != nil
180+ }, 1 * time .Second , 25 * time .Millisecond , "expected stale terminal workflow to be cleaned up" )
186181}
187182
188183// TestInMemoryStateStore_GetStats tests statistics retrieval.
0 commit comments