@@ -156,81 +156,84 @@ describe('Prepare Execute Statement', function() {
156156 } ) ;
157157 } ) ;
158158
159- it ( 'should not persist error state between executions of prepared statement (GH#1712)' , async function ( ) {
159+ it ( 'should not persist error state between executions of prepared statement (GH#1712)' , function ( done ) {
160160 const config = getConfig ( ) ;
161161
162162 const connection = new Connection ( config ) ;
163163 if ( process . env . TEDIOUS_DEBUG ) {
164164 connection . on ( 'debug' , console . log ) ;
165165 }
166166
167- // Helper to execute the prepared statement and return the result
168- const execute = ( divisor : number ) => {
169- return new Promise < number > ( ( resolve , reject ) => {
170- request . once ( 'requestCompleted' , ( ) => {
171- if ( request . error ) {
172- reject ( request . error ) ;
173- } else {
174- resolve ( results [ results . length - 1 ] ! ) ;
175- }
176- } ) ;
177- connection . execute ( request , { divisor } ) ;
178- } ) ;
179- } ;
180-
181- // Connect to the database
182- await new Promise < void > ( ( resolve , reject ) => {
183- connection . connect ( ( err ) => {
184- if ( err ) {
185- return reject ( err ) ;
186- }
187- resolve ( ) ;
188- } ) ;
189- } ) ;
190-
191167 // Prepare a statement that can cause a divide by zero error depending on the parameter
192- const request = new Request ( 'select 1 / @divisor as result' , ( ) => { } ) ;
168+ const request = new Request ( 'select 1 / @divisor as result' , function ( ) { } ) ;
193169 request . addParameter ( 'divisor' , TYPES . Int ) ;
194170
171+ let executionCount = 0 ;
195172 const results : ( number | null ) [ ] = [ ] ;
196- request . on ( 'row' , ( columns ) => {
173+ const errors : ( Error | undefined ) [ ] = [ ] ;
174+
175+ request . on ( 'row' , function ( columns ) {
197176 results . push ( columns [ 0 ] . value ) ;
198177 } ) ;
199178
200- // Prepare the statement
201- await new Promise < void > ( ( resolve ) => {
202- request . once ( 'prepared' , ( ) => {
203- assert . ok ( request . handle ) ;
204- resolve ( ) ;
179+ request . on ( 'prepared' , function ( ) {
180+ assert . ok ( request . handle ) ;
181+
182+ // First execution: should succeed with divisor = 1
183+ request . once ( 'requestCompleted' , function ( ) {
184+ executionCount ++ ;
185+ errors . push ( request . error ) ;
186+
187+ // Second execution: should fail with divisor = 0 (divide by zero)
188+ request . once ( 'requestCompleted' , function ( ) {
189+ executionCount ++ ;
190+ errors . push ( request . error ) ;
191+
192+ // Third execution: should succeed with divisor = 2
193+ // Before the fix, this would report the error from the second execution
194+ request . once ( 'requestCompleted' , function ( ) {
195+ executionCount ++ ;
196+ errors . push ( request . error ) ;
197+
198+ // Unprepare and close
199+ connection . unprepare ( request ) ;
200+ connection . close ( ) ;
201+ } ) ;
202+
203+ connection . execute ( request , { divisor : 2 } ) ;
204+ } ) ;
205+
206+ connection . execute ( request , { divisor : 0 } ) ;
205207 } ) ;
208+
209+ connection . execute ( request , { divisor : 1 } ) ;
210+ } ) ;
211+
212+ connection . connect ( function ( err ) {
213+ if ( err ) {
214+ return done ( err ) ;
215+ }
216+
206217 connection . prepare ( request ) ;
207218 } ) ;
208219
209- // First execution: should succeed with divisor = 1
210- const result1 = await execute ( 1 ) ;
211- assert . strictEqual ( result1 , 1 , 'First execution should return 1 ' ) ;
220+ connection . on ( 'end' , function ( ) {
221+ // Verify the behavior
222+ assert . strictEqual ( executionCount , 3 , 'Should have completed 3 executions ' ) ;
212223
213- // Second execution: should fail with divisor = 0 (divide by zero)
214- try {
215- await execute ( 0 ) ;
216- assert . fail ( 'Second execution should have thrown an error' ) ;
217- } catch ( err ) {
218- assert . include ( ( err as Error ) . message , 'Divide by zero' , 'Error should be divide by zero' ) ;
219- }
224+ // First execution succeeded
225+ assert . isUndefined ( errors [ 0 ] , 'First execution should have no error' ) ;
226+ assert . strictEqual ( results [ 0 ] , 1 , 'First execution should return 1' ) ;
220227
221- // Third execution: should succeed with divisor = 2
222- // Before the fix, this would throw the error from the second execution
223- // This is the key assertion for GH#1712
224- const result3 = await execute ( 2 ) ;
225- assert . strictEqual ( result3 , 0 , 'Third execution should return 0 (1/2 truncated to int)' ) ;
228+ // Second execution failed with divide by zero
229+ assert . isDefined ( errors [ 1 ] , 'Second execution should have an error' ) ;
230+ assert . include ( errors [ 1 ] ! . message , 'Divide by zero' , 'Error should be divide by zero' ) ;
226231
227- // Unprepare and close
228- await new Promise < void > ( ( resolve ) => {
229- connection . on ( 'end' , ( ) => {
230- resolve ( ) ;
231- } ) ;
232- connection . unprepare ( request ) ;
233- connection . close ( ) ;
232+ // Third execution succeeded - this is the key assertion for GH#1712
233+ assert . isUndefined ( errors [ 2 ] , 'Third execution should have no error (error state should be cleared)' ) ;
234+ assert . strictEqual ( results [ 1 ] , 0 , 'Third execution should return 0 (1/2 truncated to int)' ) ;
235+
236+ done ( ) ;
234237 } ) ;
235238 } ) ;
236239
0 commit comments