@@ -654,6 +654,57 @@ func TestTx_CopyFile_Error_Normal(t *testing.T) {
654654 }
655655}
656656
657+ // TestTx_Rollback ensures there is no error when tx rollback whether we sync freelist or not.
658+ func TestTx_Rollback (t * testing.T ) {
659+ for _ , isSyncFreelist := range []bool {false , true } {
660+ // Open the database.
661+ db , err := bolt .Open (tempfile (), 0666 , nil )
662+ if err != nil {
663+ log .Fatal (err )
664+ }
665+ defer os .Remove (db .Path ())
666+ db .NoFreelistSync = isSyncFreelist
667+
668+ tx , err := db .Begin (true )
669+ if err != nil {
670+ t .Fatalf ("Error starting tx: %v" , err )
671+ }
672+ bucket := []byte ("mybucket" )
673+ if _ , err := tx .CreateBucket (bucket ); err != nil {
674+ t .Fatalf ("Error creating bucket: %v" , err )
675+ }
676+ if err := tx .Commit (); err != nil {
677+ t .Fatalf ("Error on commit: %v" , err )
678+ }
679+
680+ tx , err = db .Begin (true )
681+ if err != nil {
682+ t .Fatalf ("Error starting tx: %v" , err )
683+ }
684+ b := tx .Bucket (bucket )
685+ if err := b .Put ([]byte ("k" ), []byte ("v" )); err != nil {
686+ t .Fatalf ("Error on put: %v" , err )
687+ }
688+ // Imagine there is an error and tx needs to be rolled-back
689+ if err := tx .Rollback (); err != nil {
690+ t .Fatalf ("Error on rollback: %v" , err )
691+ }
692+
693+ tx , err = db .Begin (false )
694+ if err != nil {
695+ t .Fatalf ("Error starting tx: %v" , err )
696+ }
697+ b = tx .Bucket (bucket )
698+ if v := b .Get ([]byte ("k" )); v != nil {
699+ t .Fatalf ("Value for k should not have been stored" )
700+ }
701+ if err := tx .Rollback (); err != nil {
702+ t .Fatalf ("Error on rollback: %v" , err )
703+ }
704+
705+ }
706+ }
707+
657708// TestTx_releaseRange ensures db.freePages handles page releases
658709// correctly when there are transaction that are no longer reachable
659710// via any read/write transactions and are "between" ongoing read
0 commit comments