@@ -142,6 +142,11 @@ describe('EndToEnd', async () => {
142142 tracerProvider : tracerProvider ,
143143 enableExtendedTracing : false ,
144144 } ) ;
145+ let dbCounter = 1 ;
146+
147+ function newTestDatabase ( ) : Database {
148+ return instance . database ( `database-${ dbCounter ++ } ` , ) ;
149+ }
145150
146151 const server = setupResult . server ;
147152 const spannerMock = setupResult . spannerMock ;
@@ -305,6 +310,7 @@ describe('EndToEnd', async () => {
305310 } ) ;
306311
307312 it ( 'runTransactionAsync' , async ( ) => {
313+
308314 await database . runTransactionAsync ( async transaction => {
309315 await transaction ! . run ( 'SELECT 1' ) ;
310316 } ) ;
@@ -327,6 +333,105 @@ describe('EndToEnd', async () => {
327333 ) ;
328334 } ) ;
329335
336+ it . only ( 'runTransaction with abort' , done => {
337+ let attempts = 0 ;
338+ let rowCount = 0 ;
339+ const database = newTestDatabase ( ) ;
340+ database . runTransaction ( async ( err , transaction ) => {
341+ assert . ifError ( err ) ;
342+ if ( ! attempts ) {
343+ spannerMock . abortTransaction ( transaction ! ) ;
344+ }
345+ attempts ++ ;
346+ transaction ! . run ( selectSql , ( err , rows ) => {
347+ assert . ifError ( err ) ;
348+ rows . forEach ( ( ) => rowCount ++ ) ;
349+ transaction !
350+ . commit ( )
351+ . catch ( done )
352+ . then ( async ( ) => {
353+ const expectedSpanNames = [
354+ 'CloudSpanner.Database.batchCreateSessions' ,
355+ 'CloudSpanner.SessionPool.createSessions' ,
356+ 'CloudSpanner.Snapshot.runStream' ,
357+ 'CloudSpanner.Snapshot.run' ,
358+ 'CloudSpanner.Snapshot.runStream' ,
359+ 'CloudSpanner.Snapshot.run' ,
360+ 'CloudSpanner.Transaction.commit' ,
361+ 'CloudSpanner.Snapshot.begin' ,
362+ 'CloudSpanner.Database.runTransaction' ,
363+ ] ;
364+ const expectedEventNames = [
365+ ...waitingSessionsEvents ,
366+ 'Retrying Transaction' ,
367+ 'Starting stream' ,
368+ 'exception' ,
369+ 'Stream broken. Not safe to retry' ,
370+ 'Begin Transaction' ,
371+ 'Transaction Creation Done' ,
372+ 'Starting stream' ,
373+ 'Starting Commit' ,
374+ 'Commit Done' ,
375+ ] ;
376+ await verifySpansAndEvents ( traceExporter , expectedSpanNames , expectedEventNames )
377+ database
378+ . close ( )
379+ . catch ( done )
380+ . then ( ( ) => done ( ) ) ;
381+ } ) ;
382+ } ) ;
383+ } ) ;
384+ } ) ;
385+
386+ it ( 'runTransactionAsync with abort' , async ( ) => {
387+ let attempts = 0 ;
388+ const database = newTestDatabase ( ) ;
389+ await database . runTransactionAsync ( ( transaction ) : Promise < number > => {
390+ if ( ! attempts ) {
391+ spannerMock . abortTransaction ( transaction ) ;
392+ }
393+ attempts ++ ;
394+ return transaction . run ( selectSql ) . then ( ( [ rows ] ) => {
395+ let count = 0 ;
396+ rows . forEach ( ( ) => count ++ ) ;
397+ return transaction . commit ( ) . then ( ( ) => count ) ;
398+ } ) ;
399+ } ) ;
400+ assert . strictEqual ( attempts , 2 ) ;
401+ const expectedSpanNames = [
402+ 'CloudSpanner.Database.batchCreateSessions' ,
403+ 'CloudSpanner.SessionPool.createSessions' ,
404+ 'CloudSpanner.Snapshot.runStream' ,
405+ 'CloudSpanner.Snapshot.run' ,
406+ 'CloudSpanner.Snapshot.begin' ,
407+ 'CloudSpanner.Snapshot.runStream' ,
408+ 'CloudSpanner.Snapshot.run' ,
409+ 'CloudSpanner.Transaction.commit' ,
410+ 'CloudSpanner.Database.runTransactionAsync' ,
411+ ] ;
412+ const expectedEventNames = [
413+ 'Requesting 25 sessions' ,
414+ 'Creating 25 sessions' ,
415+ 'Requested for 25 sessions returned 25' ,
416+ 'Starting stream' ,
417+ 'exception' ,
418+ 'Stream broken. Not safe to retry' ,
419+ 'Begin Transaction' ,
420+ 'Transaction Creation Done' ,
421+ 'Starting stream' ,
422+ 'Starting Commit' ,
423+ 'Commit Done' ,
424+ ...waitingSessionsEvents ,
425+ 'Retrying transaction' ,
426+ ] ;
427+ await verifySpansAndEvents (
428+ traceExporter ,
429+ expectedSpanNames ,
430+ expectedEventNames
431+ ) ;
432+ await database . close ( ) ;
433+ } ) ;
434+
330435 it ( 'writeAtLeastOnce' , done => {
331436 const blankMutations = new MutationSet ( ) ;
332437 database . writeAtLeastOnce ( blankMutations , async ( err , response ) => {
0 commit comments