kaspad/database/ffldb/reconcile_test.go
Svarog 225f349e6a [DEV-81] 100% Database Coverage (#110)
* [DEV-81] Overwrite maxOpenFiles for testInterface to force tests to check the LRU-mechanism in openFile

* [DEV-81] Added database.UseLogger test

* [DEV-81] Completed coverage of reconcileDB()

* [DEV-81] Added some tests for dbcache

* [DEV-81] Moved init and UseLogger to separate file to make them more easily-testable + added tests

* [DEV-81] Added tests for deleteFile

* [DEV-81] Added tests to cursor.Delete + made sure it returns error when transaction is not writable

* [DEV-81] Moved database/error_test.go from database_test package to database package + added test for IsErrorCode

* [DEV-81] Added tests for handleRollback error-cases

* [DEV-81] Added tests for cursor.skipPendingUpdates

* [DEV-81] Added tests for various cursor edge-cases

* [DEV-81] tx.putKey no longer returns error, because there is no case when it does

* [DEV-81] Added tests to CreateBucket error cases

* [DEV-81] Added tests to bucket.Get and .Delete error cases + .Delete now returns error on empty key

* [DEV-81] Added test for ForEachBucket

* [DEV-81] Added tests to StoreBlock

* [DEV-81] Added test for deleting a double nested bucket

* [DEV-81] Removed log_test, as it is no longer necessary with the logging system re-design

* [DEV-81] Added test to some of writePendingAndCommit error-cases

* [DEV-81] Update references from btcutil to btcd/util

* [DEV-81] Add tests for dbCacheIterator{.Next(), .Prev(), .Key, .Value()} in cases when iterator is exhausted

* [DEV-81] Added tests for ldbIterator placeholder functions

* [DEV-81] Added test name to Error messsages in TestSkipPendingUpdates

* [DEV-81] Begin writing TestSkipPendingUpdatesCache

* [DEV-81] Added error-cases for DBCache.flush() and DBCache.commitTreaps()

* [DEV-81] Use monkey.patch from bou.ke and not from github

* [DEV-81] Rewrote IsErrorCode in both database and txscript packages to be more concise

* [DEV-81] Rename any database.Tx to dbTx instead of tx - to remove confusion with coin Tx

* [DEV-81] Fix typo

* [DEV-81] Use os.TempDir() instead of /tmp/ to be cross-platform

* [DEV-81] use SimNet for database tests + Error if testDB exists after deleting it

* [DEV-81] Removed useLogger - it's redundant

* [DEV-81] Added comment on how CRC32 checksums are calculated in reconcile_test.go

* [DEV-81] Added comment that explains what setWriteRow does

* [DEV-81] Use constant instead of hard-coded value

* [DEV-81] Fixed some typo's + better formatting
2018-11-06 18:18:42 +02:00

181 lines
5.7 KiB
Go

package ffldb
import (
"reflect"
"testing"
"bou.ke/monkey"
)
func TestSerializeWriteRow(t *testing.T) {
tests := []struct {
curBlockFileNum uint32
curFileOffset uint32
expectedWriteRow []byte
}{
// WriteRow format:
// First 4 bytes: curBlockFileNum
// Next 4 bytes: curFileOffset
// Next 4 bytes: Castagnoli CRC-32 checksum
// One can easily calculate checksums using the following code:
// https://play.golang.org/p/zoMKT-ORyF9
{0, 0, []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0xB2, 0x28, 0x8C}},
{10, 11, []byte{0x0A, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0xC1, 0xA6, 0x0D, 0xC8}},
}
for i, test := range tests {
actualWriteRow := serializeWriteRow(test.curBlockFileNum, test.curFileOffset)
if !reflect.DeepEqual(test.expectedWriteRow, actualWriteRow) {
t.Errorf("TestSerializeWriteRow: %d: Expected: %v, but got: %v",
i, test.expectedWriteRow, actualWriteRow)
}
}
}
func TestDeserializeWriteRow(t *testing.T) {
tests := []struct {
writeRow []byte
expectedCurBlockFileNum uint32
expectedCurFileOffset uint32
expectedError bool
}{
// WriteRow format:
// First 4 bytes: curBlockFileNum
// Next 4 bytes: curFileOffset
// Next 4 bytes: Castagnoli CRC-32 checksum
// One can easily calculate checksums using the following code:
// https://play.golang.org/p/zoMKT-ORyF9
{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0xB2, 0x28, 0x8C}, 0, 0, false},
{[]byte{0x0A, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0xC1, 0xA6, 0x0D, 0xC8}, 10, 11, false},
{[]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0xB2, 0x28, 0x8D}, 0, 0, true},
{[]byte{0x0A, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0, 0, true},
}
for i, test := range tests {
actualCurBlockFileNum, actualCurFileOffset, err := deserializeWriteRow(test.writeRow)
if (err != nil) != test.expectedError {
t.Errorf("TestDeserializeWriteRow: %d: Expected error status: %t, but got: %t",
i, test.expectedError, err != nil)
}
if test.expectedCurBlockFileNum != actualCurBlockFileNum {
t.Errorf("TestDeserializeWriteRow: %d: Expected curBlockFileNum: %d, but got: %d",
i, test.expectedCurBlockFileNum, actualCurBlockFileNum)
}
if test.expectedCurFileOffset != actualCurFileOffset {
t.Errorf("TestDeserializeWriteRow: %d: Expected curFileOffset: %d, but got: %d",
i, test.expectedCurFileOffset, actualCurFileOffset)
}
}
}
// setWriteRow is a low-level helper method to update the write row in the
// metadata bucket to enable certain test-cases in TestReconcileErrors
// if writeRow = nil deletes the write row altogether
func setWriteRow(pdb *db, writeRow []byte, t *testing.T) {
tx, err := pdb.begin(true)
if err != nil {
t.Fatalf("TestReconcileErrors: Error getting tx for setting "+
"writeLoc in metadata: %s", err)
}
if writeRow == nil {
tx.metaBucket.Delete(writeLocKeyName)
if err != nil {
t.Fatalf("TestReconcileErrors: Error deleting writeLoc from metadata: %s",
err)
}
} else {
tx.metaBucket.Put(writeLocKeyName, writeRow)
if err != nil {
t.Fatalf("TestReconcileErrors: Error updating writeLoc in metadata: %s",
err)
}
}
err = pdb.cache.commitTx(tx)
if err != nil {
t.Fatalf("TestReconcileErrors: Error commiting the update of "+
"writeLoc in metadata: %s", err)
}
pdb.writeLock.Unlock()
pdb.closeLock.RUnlock()
}
// TestReconcileErrors tests all error-cases in reconclieDB.
// The non-error-cases are tested in the more general tests.
func TestReconcileErrors(t *testing.T) {
// Set-up tests
pdb := newTestDb("TestReconcileErrors", t)
// Test without writeLoc
setWriteRow(pdb, nil, t)
_, err := reconcileDB(pdb, false)
if err == nil {
t.Errorf("TestReconcileErrors: ReconcileDB() didn't error out when " +
"running without a writeRowLoc")
}
// Test with writeLoc in metadata after the actual cursor position
setWriteRow(pdb, serializeWriteRow(1, 0), t)
_, err = reconcileDB(pdb, false)
if err == nil {
t.Errorf("TestReconcileErrors: ReconcileDB() didn't error out when " +
"curBlockFileNum after the actual cursor position")
}
setWriteRow(pdb, serializeWriteRow(0, 1), t)
_, err = reconcileDB(pdb, false)
if err == nil {
t.Errorf("TestReconcileErrors: ReconcileDB() didn't error out when " +
"curFileOffset after the actual cursor position")
}
// Restore previous writeRow
setWriteRow(pdb, serializeWriteRow(0, 0), t)
// Test with writeLoc in metadata before the actual cursor position
handleRollbackCalled := false
patch := monkey.Patch((*blockStore).handleRollback,
func(s *blockStore, oldBlockFileNum, oldBlockOffset uint32) {
handleRollbackCalled = true
})
defer patch.Unpatch()
pdb.store.writeCursor.curFileNum = 1
_, err = reconcileDB(pdb, false)
if err != nil {
t.Errorf("TestReconcileErrors: Error in ReconcileDB() when curFileNum before " +
"the actual cursor position ")
}
if !handleRollbackCalled {
t.Errorf("TestReconcileErrors: handleRollback was not called when curFileNum before " +
"the actual cursor position ")
}
pdb.store.writeCursor.curFileNum = 0
pdb.store.writeCursor.curOffset = 1
_, err = reconcileDB(pdb, false)
if err != nil {
t.Errorf("TestReconcileErrors: Error in ReconcileDB() when curOffset before " +
"the actual cursor position ")
}
if !handleRollbackCalled {
t.Errorf("TestReconcileErrors: handleRollback was not called when curOffset before " +
"the actual cursor position ")
}
// Restore previous writeCursor location
pdb.store.writeCursor.curFileNum = 0
pdb.store.writeCursor.curOffset = 0
// Test create with closed DB to force initDB to fail
pdb.Close()
_, err = reconcileDB(pdb, true)
if err == nil {
t.Errorf("ReconcileDB didn't error out when running with closed db and create = true")
}
}