@@ -16,6 +16,9 @@ package txn
16
16
17
17
import (
18
18
"context"
19
+ "crypto/sha256"
20
+ "io"
21
+ "os"
19
22
"strings"
20
23
"testing"
21
24
"time"
@@ -336,9 +339,8 @@ func TestReadonlyTxnError(t *testing.T) {
336
339
}
337
340
}
338
341
339
- func TestWriteTxnPanic (t * testing.T ) {
340
- b , _ := betesting .NewDefaultTmpBackend (t )
341
- defer betesting .Close (t , b )
342
+ func TestWriteTxnPanicWithoutApply (t * testing.T ) {
343
+ b , bePath := betesting .NewDefaultTmpBackend (t )
342
344
s := mvcc .NewStore (zaptest .NewLogger (t ), b , & lease.FakeLessor {}, mvcc.StoreConfig {})
343
345
defer s .Close ()
344
346
@@ -367,7 +369,17 @@ func TestWriteTxnPanic(t *testing.T) {
367
369
},
368
370
}
369
371
372
+ // compute DB file hash before applying the txn
373
+ dbHashBefore , err := computeFileHash (bePath )
374
+ require .NoErrorf (t , err , "failed to compute DB file hash before txn" )
375
+
376
+ // we verify the following properties below:
377
+ // 1. server panics after a write txn aply fails (invariant: server should never try to move on from a failed write)
378
+ // 2. no writes from the txn are applied to the backend (invariant: failed write should have no side-effect on DB state besides panic)
370
379
assert .Panics (t , func () { Txn (ctx , zaptest .NewLogger (t ), txn , false , s , & lease.FakeLessor {}) }, "Expected panic in Txn with writes" )
380
+ dbHashAfter , err := computeFileHash (bePath )
381
+ require .NoErrorf (t , err , "failed to compute DB file hash after txn" )
382
+ require .Equalf (t , dbHashBefore , dbHashAfter , "mismatch in DB hash before and after failed write txn" )
371
383
}
372
384
373
385
func TestCheckTxnAuth (t * testing.T ) {
@@ -566,6 +578,20 @@ func setupAuth(t *testing.T, be backend.Backend) auth.AuthStore {
566
578
return as
567
579
}
568
580
581
+ func computeFileHash (filePath string ) (string , error ) {
582
+ file , err := os .Open (filePath )
583
+ if err != nil {
584
+ return "" , err
585
+ }
586
+ defer file .Close ()
587
+
588
+ h := sha256 .New ()
589
+ if _ , err := io .Copy (h , file ); err != nil {
590
+ return "" , err
591
+ }
592
+ return string (h .Sum (nil )), nil
593
+ }
594
+
569
595
// CheckTxnAuth variables setup.
570
596
var (
571
597
inRangeCompare = & pb.Compare {
0 commit comments