@@ -151,7 +151,7 @@ func (s *BestEffortUnitTestSuite) SetupTest() {
151151
152152func (s * BestEffortUnitTestSuite ) subtests () []TableDrivenTest {
153153 foos := []interface {}{test.Foo {ID : 28 }, test.Foo {ID : 1992 }, test.Foo {ID : 2 }, test.Foo {ID : 1111 }}
154- bars := []interface {}{test.Bar {ID : "ID" }, test.Bar {ID : "1992" }}
154+ bars := []interface {}{test.Bar {ID : "ID" }, test.Bar {ID : "1992" }, test. Bar { ID : "2022" } }
155155 fooType , barType := work .TypeNameOf (test.Foo {}), work .TypeNameOf (test.Bar {})
156156 return []TableDrivenTest {
157157 {
@@ -461,28 +461,51 @@ func (s *BestEffortUnitTestSuite) subtests() []TableDrivenTest {
461461 name : "DeleteAndRollbackError" ,
462462 additions : []interface {}{foos [0 ]},
463463 alters : []interface {}{foos [1 ], bars [1 ]},
464- removals : []interface {}{foos [2 ]},
464+ removals : []interface {}{foos [2 ], bars [ 2 ] },
465465 registers : []interface {}{foos [1 ], bars [1 ], foos [3 ]},
466466 expectations : func (ctx context.Context , registers , additions , alters , removals []interface {}) {
467467 // arrange - successfully apply inserts.
468- s .mappers [fooType ].EXPECT ().Insert (ctx , gomock .Any (), additions [0 ]).Return (nil ).Times (s .retryCount )
469- for i := 0 ; i < s .retryCount ; i ++ {
470- // arrange - successfully apply updates.
471- applyFooUpdate := s .mappers [fooType ].EXPECT ().Update (ctx , gomock .Any (), alters [0 ]).Return (nil )
472- applyBarUpdate := s .mappers [barType ].EXPECT ().Update (ctx , gomock .Any (), alters [1 ]).Return (nil )
473-
474- // arrange - successfully roll back updates.
475- s .mappers [fooType ].EXPECT ().
476- Update (ctx , gomock .Any (), []interface {}{registers [0 ], registers [2 ]}).Return (nil ).After (applyFooUpdate )
477- s .mappers [barType ].EXPECT ().
478- Update (ctx , gomock .Any (), registers [1 ]).Return (nil ).After (applyBarUpdate )
468+ s .mappers [fooType ].EXPECT ().Insert (ctx , gomock .Any (), additions [0 ]).Return (nil ).AnyTimes ()
479469
480- // arrange - encounter delete error.
481- applyDelete := s .mappers [fooType ].EXPECT ().Delete (ctx , gomock .Any (), removals [0 ]).Return (errors .New ("whoa" ))
470+ // arrange - successfully apply updates.
471+ s .mappers [fooType ].EXPECT ().Update (ctx , gomock .Any (), alters [0 ]).Return (nil ).AnyTimes ()
472+ s .mappers [barType ].EXPECT ().Update (ctx , gomock .Any (), alters [1 ]).Return (nil ).AnyTimes ()
473+
474+ // arrange - encounter delete error.
475+ // this looks a bit insane because it is. since internally
476+ // the units store a map of data mappers, and also because Golang
477+ // doesn't have deterministic ordering of map keys, the below solves
478+ // the edge case where the call to Delete that is suppose to fail is ran prior to
479+ // the call to Delete that should succeed. the call that should succeed MUST be ran first,
480+ // because the rollback error we simulate here can only occur if at least
481+ // one entity was successfully deleted.
482+ var a int
483+ var b int
484+ m := map [int ]bool {0 : false , 1 : false , 2 : false }
485+ s .mappers [fooType ].EXPECT ().Delete (ctx , gomock .Any (), removals [0 ]).DoAndReturn (
486+ func (_ctx context.Context , _mCtx work.UnitMapperContext , e ... interface {}) error {
487+ a += 1
488+ if ! m [a ] {
489+ m [a ] = true
490+ return nil
491+ }
492+ return errors .New ("whoa" )
493+ },
494+ ).AnyTimes ()
495+ s .mappers [barType ].EXPECT ().Delete (ctx , gomock .Any (), removals [1 ]).DoAndReturn (
496+ func (_ctx context.Context , _mCtx work.UnitMapperContext , e ... interface {}) error {
497+ b += 1
498+ if ! m [b ] {
499+ m [b ] = true
500+ return nil
501+ }
502+ return errors .New ("whoa" )
503+ },
504+ ).AnyTimes ()
482505
483- // arrange - encounter error when rolling back inserts .
484- s .mappers [fooType ].EXPECT ().Delete (ctx , gomock .Any (), additions [0 ]).Return (errors .New ("ouch" )).After ( applyDelete )
485- }
506+ // arrange - encounter error when rolling back deletes .
507+ s .mappers [fooType ].EXPECT ().Insert (ctx , gomock .Any (), removals [0 ]).Return (errors .New ("ouch" )).AnyTimes ( )
508+ s . mappers [ barType ]. EXPECT (). Insert ( ctx , gomock . Any (), removals [ 1 ]). Return ( errors . New ( "ouch" )). AnyTimes ()
486509 },
487510 ctx : context .Background (),
488511 err : errors .New ("whoa; ouch" ),
@@ -492,28 +515,51 @@ func (s *BestEffortUnitTestSuite) subtests() []TableDrivenTest {
492515 name : "DeleteAndRollbackError_MetricsEmitted" ,
493516 additions : []interface {}{foos [0 ]},
494517 alters : []interface {}{foos [1 ], bars [1 ]},
495- removals : []interface {}{foos [2 ]},
518+ removals : []interface {}{foos [2 ], bars [ 2 ] },
496519 registers : []interface {}{foos [1 ], bars [1 ], foos [3 ]},
497520 expectations : func (ctx context.Context , registers , additions , alters , removals []interface {}) {
498521 // arrange - successfully apply inserts.
499- s .mappers [fooType ].EXPECT ().Insert (ctx , gomock .Any (), additions [0 ]).Return (nil ).Times (s .retryCount )
500- for i := 0 ; i < s .retryCount ; i ++ {
501- // arrange - successfully apply updates.
502- applyFooUpdate := s .mappers [fooType ].EXPECT ().Update (ctx , gomock .Any (), alters [0 ]).Return (nil )
503- applyBarUpdate := s .mappers [barType ].EXPECT ().Update (ctx , gomock .Any (), alters [1 ]).Return (nil )
504-
505- // arrange - successfully roll back updates.
506- s .mappers [fooType ].EXPECT ().
507- Update (ctx , gomock .Any (), []interface {}{registers [0 ], registers [2 ]}).Return (nil ).After (applyFooUpdate )
508- s .mappers [barType ].EXPECT ().
509- Update (ctx , gomock .Any (), registers [1 ]).Return (nil ).After (applyBarUpdate )
522+ s .mappers [fooType ].EXPECT ().Insert (ctx , gomock .Any (), additions [0 ]).Return (nil ).AnyTimes ()
510523
511- // arrange - encounter delete error.
512- applyDelete := s .mappers [fooType ].EXPECT ().Delete (ctx , gomock .Any (), removals [0 ]).Return (errors .New ("whoa" ))
524+ // arrange - successfully apply updates.
525+ s .mappers [fooType ].EXPECT ().Update (ctx , gomock .Any (), alters [0 ]).Return (nil ).AnyTimes ()
526+ s .mappers [barType ].EXPECT ().Update (ctx , gomock .Any (), alters [1 ]).Return (nil ).AnyTimes ()
527+
528+ // arrange - encounter delete error.
529+ // this looks a bit insane because it is. since internally
530+ // the units store a map of data mappers, and also because Golang
531+ // doesn't have deterministic ordering of map keys, the below solves
532+ // the edge case where the call to Delete that is suppose to fail is ran prior to
533+ // the call to Delete that should succeed. the call that should succeed MUST be ran first,
534+ // because the rollback error we simulate here can only occur if at least
535+ // one entity was successfully deleted.
536+ var a int
537+ var b int
538+ m := map [int ]bool {0 : false , 1 : false , 2 : false }
539+ s .mappers [fooType ].EXPECT ().Delete (ctx , gomock .Any (), removals [0 ]).DoAndReturn (
540+ func (_ctx context.Context , _mCtx work.UnitMapperContext , e ... interface {}) error {
541+ a += 1
542+ if ! m [a ] {
543+ m [a ] = true
544+ return nil
545+ }
546+ return errors .New ("whoa" )
547+ },
548+ ).AnyTimes ()
549+ s .mappers [barType ].EXPECT ().Delete (ctx , gomock .Any (), removals [1 ]).DoAndReturn (
550+ func (_ctx context.Context , _mCtx work.UnitMapperContext , e ... interface {}) error {
551+ b += 1
552+ if ! m [b ] {
553+ m [b ] = true
554+ return nil
555+ }
556+ return errors .New ("whoa" )
557+ },
558+ ).AnyTimes ()
513559
514- // arrange - encounter error when rolling back inserts .
515- s .mappers [fooType ].EXPECT ().Delete (ctx , gomock .Any (), additions [0 ]).Return (errors .New ("ouch" )).After ( applyDelete )
516- }
560+ // arrange - encounter error when rolling back deletes .
561+ s .mappers [fooType ].EXPECT ().Insert (ctx , gomock .Any (), removals [0 ]).Return (errors .New ("ouch" )).AnyTimes ( )
562+ s . mappers [ barType ]. EXPECT (). Insert ( ctx , gomock . Any (), removals [ 1 ]). Return ( errors . New ( "ouch" )). AnyTimes ()
517563 },
518564 ctx : context .Background (),
519565 err : errors .New ("whoa; ouch" ),
0 commit comments