@@ -24,6 +24,8 @@ type mockStore struct {
2424 SessionHandler
2525 //DetailNotFound is used to make mocked SessionDetail return (x,false), as if it doesn't find the session in the mocked storage.
2626 DetailNotFound bool
27+ // Delay simulates a slow RPC call for timeout testing
28+ Delay time.Duration
2729}
2830
2931var sess = user.SessionState {
@@ -32,6 +34,9 @@ var sess = user.SessionState{
3234}
3335
3436func (m mockStore ) SessionDetail (orgID string , keyName string , hashed bool ) (user.SessionState , bool ) {
37+ if m .Delay > 0 {
38+ time .Sleep (m .Delay )
39+ }
3540 return sess .Clone (), ! m .DetailNotFound
3641}
3742
@@ -642,4 +647,41 @@ func TestBaseMiddleware_OrgSession(t *testing.T) {
642647 assert .NotEmpty (t , session .OrgID )
643648 }
644649 })
650+
651+ t .Run ("should timeout when RPC call takes too long" , func (t * testing.T ) {
652+ timeoutOrgID := "timeout-org-" + uuid .New ()
653+
654+ slowMockStore := mockStore {
655+ DetailNotFound : false ,
656+ Delay : 3 * time .Second ,
657+ }
658+
659+ specSlow := & APISpec {
660+ GlobalConfig : config.Config {
661+ EnforceOrgDataAge : true ,
662+ LocalSessionCache : config.LocalSessionCacheConf {
663+ DisableCacheSessionState : false ,
664+ },
665+ },
666+ OrgSessionManager : slowMockStore ,
667+ }
668+
669+ baseMidSlow := & BaseMiddleware {
670+ Spec : specSlow ,
671+ Gw : ts .Gw ,
672+ logger : mainLog ,
673+ }
674+
675+ start := time .Now ()
676+ session , found := baseMidSlow .fetchOrgSessionWithTimeout (timeoutOrgID )
677+ elapsed := time .Since (start )
678+
679+ // Should timeout and return false
680+ assert .False (t , found , "Should timeout and return false" )
681+ assert .Empty (t , session .OrgID , "Session should be empty on timeout" )
682+
683+ // Should timeout around 2 seconds, not wait for the full 3 second delay
684+ assert .Less (t , elapsed , 3 * time .Second , "Should timeout before slow RPC completes" )
685+ assert .GreaterOrEqual (t , elapsed , 2 * time .Second , "Should wait for timeout duration" )
686+ })
645687}
0 commit comments