@@ -124,21 +124,27 @@ func TestPBMemoryLeak(t *testing.T) {
124124 // read data
125125 runtime .GC ()
126126 allocatedBegin , inUseBegin := readMem ()
127- records , err := tk .Session ().Execute (context .Background (), "select * from t" )
128- require .NoError (t , err )
129- record := records [0 ]
130- rowCnt := 0
131- chk := record .NewChunk (nil )
132- for {
133- require .NoError (t , record .Next (context .Background (), chk ))
134- rowCnt += chk .NumRows ()
135- if chk .NumRows () == 0 {
136- break
127+ rowCnt := func () int {
128+ records , err := tk .Session ().Execute (context .Background (), "select * from t" )
129+ require .NoError (t , err )
130+ require .Len (t , records , 1 )
131+
132+ record := records [0 ]
133+ defer func () { require .NoError (t , record .Close ()) }()
134+
135+ rowCnt := 0
136+ chk := record .NewChunk (nil )
137+ for {
138+ require .NoError (t , record .Next (context .Background (), chk ))
139+ rowCnt += chk .NumRows ()
140+ if chk .NumRows () == 0 {
141+ return rowCnt
142+ }
137143 }
138- }
144+ }()
139145 require .Equal (t , int (numRows ), rowCnt )
140146
141- // check memory before close
147+ // check memory after the result set has been drained and closed
142148 runtime .GC ()
143149 allocatedAfter , inUseAfter := readMem ()
144150 require .GreaterOrEqual (t , allocatedAfter - allocatedBegin , totalSize )
0 commit comments