@@ -54,7 +54,7 @@ func TestRuntimeManagerSharesLazyInitialization(t *testing.T) {
5454 }
5555}
5656
57- func TestRuntimeManagerWaitsForExpiredRuntimeToCloseBeforeRecreating (t * testing.T ) {
57+ func TestRuntimeManagerReplacesExpiredRuntimeWhileOldReadersFinish (t * testing.T ) {
5858 var attempts atomic.Int32
5959 runtime := fakeServerRuntime ()
6060 manager := & RuntimeManager {
@@ -101,14 +101,24 @@ func TestRuntimeManagerWaitsForExpiredRuntimeToCloseBeforeRecreating(t *testing.
101101 }()
102102
103103 select {
104- case <- acquireDone :
104+ case err := <- acquireDone :
105+ if err != nil {
106+ runtime .RequestMu .RUnlock ()
107+ t .Fatal (err )
108+ }
109+ case <- time .After (time .Second ):
105110 runtime .RequestMu .RUnlock ()
106- t .Fatal ("new runtime was created before expired runtime closed" )
111+ t .Fatal ("new queries blocked behind retired runtime" )
112+ }
113+ select {
114+ case <- expired :
115+ runtime .RequestMu .RUnlock ()
116+ t .Fatal ("old runtime closed with active reader" )
107117 default :
108118 }
109- if got := attempts .Load (); got != 0 {
119+ if got := attempts .Load (); got != 1 {
110120 runtime .RequestMu .RUnlock ()
111- t .Fatalf ("runtime creation attempts before close = %d, want 0 " , got )
121+ t .Fatalf ("initializations = %d" , got )
112122 }
113123
114124 runtime .RequestMu .RUnlock ()
@@ -117,12 +127,16 @@ func TestRuntimeManagerWaitsForExpiredRuntimeToCloseBeforeRecreating(t *testing.
117127 case <- time .After (time .Second ):
118128 t .Fatal ("expired runtime did not close" )
119129 }
120- if err := <- acquireDone ; err != nil {
121- t .Fatalf ("runtime acquisition after close failed: %v" , err )
122- }
123130 if got := attempts .Load (); got != 1 {
124131 t .Fatalf ("runtime creation attempts after close = %d, want 1" , got )
125132 }
133+ manager .mu .Lock ()
134+ replacement := manager .runtime
135+ manager .mu .Unlock ()
136+ if replacement == nil || replacement == runtime {
137+ t .Fatal ("closing the retired runtime removed its replacement" )
138+ }
139+
126140}
127141
128142func TestRuntimeManagerRetriesAfterInitializationFailure (t * testing.T ) {
0 commit comments