github.com/scottcagno/storage@v1.8.0/pkg/mmap/transaction/transaction_test.go (about) 1 package transaction 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 // testBuffer is the non-zero test data. 9 var testBuffer = []byte{'H', 'E', 'L', 'L', 'O'} 10 11 // testBufferLength is the length of testBuffer. 12 var testBufferLength = len(testBuffer) 13 14 // zeroBuffer is the zero test data of the same length as testBuffer. 15 var zeroBuffer = make([]byte, testBufferLength) 16 17 // TestSnapshot tests the snapshot. 18 // CASE: The data read from the snapshot MUST NOT be affected by the previous modification of the original data. 19 func TestSnapshot(t *testing.T) { 20 data := make([]byte, testBufferLength) 21 tx, err := Begin(data, 0, uintptr(testBufferLength)) 22 if err != nil { 23 t.Fatal(err) 24 } 25 copy(data, testBuffer) 26 snapshot := make([]byte, testBufferLength) 27 if _, err := tx.ReadAt(snapshot, 0); err != nil { 28 t.Fatal(err) 29 } 30 if bytes.Compare(snapshot, zeroBuffer) != 0 { 31 t.Fatalf("snapshot must be %q, %v found", zeroBuffer, data) 32 } 33 } 34 35 // TestRollback tests the transaction rollback. 36 // CASE: The original data MUST NOT be affected by the previous write through the transaction. 37 func TestRollback(t *testing.T) { 38 data := make([]byte, testBufferLength) 39 tx, err := Begin(data, 0, uintptr(testBufferLength)) 40 if err != nil { 41 t.Fatal(err) 42 } 43 if _, err := tx.WriteAt(testBuffer, 0); err != nil { 44 t.Fatal(err) 45 } 46 if err := tx.Rollback(); err != nil { 47 t.Fatal(err) 48 } 49 if bytes.Compare(data, zeroBuffer) != 0 { 50 t.Fatalf("original must be %q, %v found", zeroBuffer, data) 51 } 52 } 53 54 // TestCommit tests the transaction commit. 55 // CASE: The original data MUST be exactly the same as the previously written through the transaction. 56 func TestCommit(t *testing.T) { 57 data := make([]byte, testBufferLength) 58 tx, err := Begin(data, 0, uintptr(testBufferLength)) 59 if err != nil { 60 t.Fatal(err) 61 } 62 if _, err := tx.WriteAt(testBuffer, 0); err != nil { 63 t.Fatal(err) 64 } 65 if err := tx.Commit(); err != nil { 66 t.Fatal(err) 67 } 68 if bytes.Compare(data, testBuffer) != 0 { 69 t.Fatalf("original must be %q, %v found", testBuffer, data) 70 } 71 } 72 73 // TestPartialRead tests the reading beyond the transaction data. 74 // CASE 1: The ErrOutOfBounds MUST be returned. 75 // CASE 2: The reading buffer MUST NOT be modified. 76 func TestPartialRead(t *testing.T) { 77 partLen := uintptr(testBufferLength - 1) 78 data := make([]byte, partLen) 79 tx, err := Begin(data, 0, partLen) 80 if err != nil { 81 t.Fatal(err) 82 } 83 if _, err := tx.WriteAt(testBuffer[:partLen], 0); err != nil { 84 t.Fatal(err) 85 } 86 buf := make([]byte, testBufferLength) 87 if _, err := tx.ReadAt(buf, 0); err == nil { 88 t.Fatal("expected ErrOutOfBounds, no error found") 89 } else if err != ErrOutOfBounds { 90 t.Fatalf("expected ErrOutOfBounds, [%v] error found", err) 91 } 92 if bytes.Compare(buf, zeroBuffer) != 0 { 93 t.Fatalf("data must be %q, %v found", zeroBuffer, buf) 94 } 95 } 96 97 // TestPartialWrite tests the writing beyond the transaction data. 98 // CASE 1: The ErrOutOfBounds MUST be returned. 99 // CASE 2: The transaction data MUST NOT be modified. 100 func TestPartialWrite(t *testing.T) { 101 partLen := uintptr(testBufferLength - 1) 102 data := make([]byte, partLen) 103 tx, err := Begin(data, 0, partLen) 104 if err != nil { 105 t.Fatal(err) 106 } 107 if _, err := tx.WriteAt(testBuffer, 0); err == nil { 108 t.Fatal("expected ErrOutOfBounds, no error found") 109 } else if err != ErrOutOfBounds { 110 t.Fatalf("expected ErrOutOfBounds, [%v] error found", err) 111 } 112 partBuf := make([]byte, partLen) 113 if _, err := tx.ReadAt(partBuf, 0); err != nil { 114 t.Fatal(err) 115 } 116 if bytes.Compare(partBuf, zeroBuffer[:partLen]) != 0 { 117 t.Fatalf("data must be %q, %v found", zeroBuffer, partBuf) 118 } 119 }