@@ -84,6 +84,61 @@ func TestOpenAIGatewayService_SelectAccountForModelWithExclusions_SkipsFreshlyRa
8484 require .Equal (t , int64 (32002 ), account .ID )
8585}
8686
87+ func TestOpenAIGatewayService_SelectAccountWithScheduler_SessionStickyDBRuntimeRecheckSkipsStaleCachedAccount (t * testing.T ) {
88+ ctx := context .Background ()
89+ groupID := int64 (10103 )
90+ rateLimitedUntil := time .Now ().Add (30 * time .Minute )
91+ staleSticky := & Account {ID : 33001 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 0 }
92+ staleBackup := & Account {ID : 33002 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 5 }
93+ dbSticky := Account {ID : 33001 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 0 , RateLimitResetAt : & rateLimitedUntil }
94+ dbBackup := Account {ID : 33002 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 5 }
95+ cache := & stubGatewayCache {sessionBindings : map [string ]int64 {"openai:session_hash_db_runtime_recheck" : 33001 }}
96+ snapshotCache := & openAISnapshotCacheStub {
97+ snapshotAccounts : []* Account {staleSticky , staleBackup },
98+ accountsByID : map [int64 ]* Account {33001 : staleSticky , 33002 : staleBackup },
99+ }
100+ snapshotService := & SchedulerSnapshotService {cache : snapshotCache }
101+ svc := & OpenAIGatewayService {
102+ accountRepo : stubOpenAIAccountRepo {accounts : []Account {dbSticky , dbBackup }},
103+ cache : cache ,
104+ cfg : & config.Config {},
105+ schedulerSnapshot : snapshotService ,
106+ concurrencyService : NewConcurrencyService (stubConcurrencyCache {}),
107+ }
108+
109+ selection , decision , err := svc .SelectAccountWithScheduler (ctx , & groupID , "" , "session_hash_db_runtime_recheck" , "gpt-5.1" , nil , OpenAIUpstreamTransportAny )
110+ require .NoError (t , err )
111+ require .NotNil (t , selection )
112+ require .NotNil (t , selection .Account )
113+ require .Equal (t , int64 (33002 ), selection .Account .ID )
114+ require .Equal (t , openAIAccountScheduleLayerLoadBalance , decision .Layer )
115+ }
116+
117+ func TestOpenAIGatewayService_SelectAccountForModelWithExclusions_DBRuntimeRecheckSkipsStaleCachedCandidate (t * testing.T ) {
118+ ctx := context .Background ()
119+ groupID := int64 (10104 )
120+ rateLimitedUntil := time .Now ().Add (30 * time .Minute )
121+ stalePrimary := & Account {ID : 34001 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 0 }
122+ staleSecondary := & Account {ID : 34002 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 5 }
123+ dbPrimary := Account {ID : 34001 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 0 , RateLimitResetAt : & rateLimitedUntil }
124+ dbSecondary := Account {ID : 34002 , Platform : PlatformOpenAI , Type : AccountTypeOAuth , Status : StatusActive , Schedulable : true , Concurrency : 1 , Priority : 5 }
125+ snapshotCache := & openAISnapshotCacheStub {
126+ snapshotAccounts : []* Account {stalePrimary , staleSecondary },
127+ accountsByID : map [int64 ]* Account {34001 : stalePrimary , 34002 : staleSecondary },
128+ }
129+ snapshotService := & SchedulerSnapshotService {cache : snapshotCache }
130+ svc := & OpenAIGatewayService {
131+ accountRepo : stubOpenAIAccountRepo {accounts : []Account {dbPrimary , dbSecondary }},
132+ cfg : & config.Config {},
133+ schedulerSnapshot : snapshotService ,
134+ }
135+
136+ account , err := svc .SelectAccountForModelWithExclusions (ctx , & groupID , "" , "gpt-5.1" , nil )
137+ require .NoError (t , err )
138+ require .NotNil (t , account )
139+ require .Equal (t , int64 (34002 ), account .ID )
140+ }
141+
87142func TestOpenAIGatewayService_SelectAccountWithScheduler_PreviousResponseSticky (t * testing.T ) {
88143 ctx := context .Background ()
89144 groupID := int64 (9 )
0 commit comments