@@ -3057,7 +3057,7 @@ func TestGarbageCollect(t *testing.T) {
30573057 resetTestDatabase (t , databaseURL )
30583058 dbosCtx := setupDBOS (t , false , true )
30593059 gcTestEvent := NewEvent ()
3060-
3060+
30613061 // Ensure the event is set at the end to unblock any remaining workflows
30623062 t .Cleanup (func () {
30633063 gcTestEvent .Set ()
@@ -3103,16 +3103,36 @@ func TestGarbageCollect(t *testing.T) {
31033103 require .NoError (t , err , "failed to list workflows after GC" )
31043104 require .Equal (t , 6 , len (workflows ), "expected exactly 6 workflows after GC (5 from threshold + 1 pending)" )
31053105
3106+ // Create a map of remaining workflow IDs for easy lookup
3107+ remainingIDs := make (map [string ]bool )
3108+ for _ , wf := range workflows {
3109+ remainingIDs [wf .ID ] = true
3110+ }
3111+
31063112 // Verify blocked workflow still exists (since it's pending)
3107- found := false
3113+ require .True (t , remainingIDs [blockedHandle .GetWorkflowID ()], "blocked workflow should still exist after GC" )
3114+
3115+ // Find status of blocked workflow
31083116 for _ , wf := range workflows {
31093117 if wf .ID == blockedHandle .GetWorkflowID () {
3110- found = true
31113118 require .Equal (t , WorkflowStatusPending , wf .Status , "blocked workflow should still be pending" )
31123119 break
31133120 }
31143121 }
3115- require .True (t , found , "blocked workflow should still exist after GC" )
3122+
3123+ // Verify that the 5 newest completed workflows are preserved
3124+ // The completedHandles slice is in order of creation (0 is oldest, 9 is newest)
3125+ // So indices 5-9 (the last 5) should be preserved
3126+ for i := range numWorkflows {
3127+ wfID := completedHandles [i ].GetWorkflowID ()
3128+ if i < numWorkflows - threshold {
3129+ // Older workflows (indices 0-4) should be deleted
3130+ require .False (t , remainingIDs [wfID ], "older workflow at index %d (ID: %s) should have been deleted" , i , wfID )
3131+ } else {
3132+ // Newer workflows (indices 5-9) should be preserved
3133+ require .True (t , remainingIDs [wfID ], "newer workflow at index %d (ID: %s) should have been preserved" , i , wfID )
3134+ }
3135+ }
31163136
31173137 // Complete the blocked workflow
31183138 gcTestEvent .Set ()
@@ -3126,7 +3146,7 @@ func TestGarbageCollect(t *testing.T) {
31263146 resetTestDatabase (t , databaseURL )
31273147 dbosCtx := setupDBOS (t , false , true )
31283148 gcTestEvent := NewEvent ()
3129-
3149+
31303150 // Ensure the event is set at the end to unblock any remaining workflows
31313151 t .Cleanup (func () {
31323152 gcTestEvent .Set ()
@@ -3228,7 +3248,7 @@ func TestGarbageCollect(t *testing.T) {
32283248 // Start with clean database for precise workflow counting
32293249 resetTestDatabase (t , databaseURL )
32303250 dbosCtx := setupDBOS (t , false , true )
3231-
3251+
32323252 RegisterWorkflow (dbosCtx , gcTestWorkflow )
32333253 RegisterWorkflow (dbosCtx , gcBlockedWorkflow )
32343254
@@ -3266,7 +3286,7 @@ func TestGarbageCollect(t *testing.T) {
32663286 resetTestDatabase (t , databaseURL )
32673287 dbosCtx := setupDBOS (t , false , true )
32683288 gcTestEvent := NewEvent ()
3269-
3289+
32703290 // Ensure the event is set at the end to unblock any remaining workflows
32713291 t .Cleanup (func () {
32723292 gcTestEvent .Set ()
0 commit comments