mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-24 23:12:31 +00:00
[DEV-254] Defer any monkey unpatching (#109)
This commit is contained in:
parent
3ace16ad23
commit
3ff2ef19e4
@ -101,9 +101,10 @@ func TestMaybeAcceptBlockErrors(t *testing.T) {
|
|||||||
|
|
||||||
// Test rejecting the node due to database error
|
// Test rejecting the node due to database error
|
||||||
databaseErrorMessage := "database error"
|
databaseErrorMessage := "database error"
|
||||||
monkey.Patch(dbStoreBlock, func(dbTx database.Tx, block *util.Block) error {
|
guard := monkey.Patch(dbStoreBlock, func(dbTx database.Tx, block *util.Block) error {
|
||||||
return errors.New(databaseErrorMessage)
|
return errors.New(databaseErrorMessage)
|
||||||
})
|
})
|
||||||
|
defer guard.Unpatch()
|
||||||
err = dag.maybeAcceptBlock(block2, BFNone)
|
err = dag.maybeAcceptBlock(block2, BFNone)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to database error: "+
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to database error: "+
|
||||||
@ -113,13 +114,14 @@ func TestMaybeAcceptBlockErrors(t *testing.T) {
|
|||||||
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to database error: "+
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to database error: "+
|
||||||
"Unexpected error. Want: %s, got: %s", databaseErrorMessage, err)
|
"Unexpected error. Want: %s, got: %s", databaseErrorMessage, err)
|
||||||
}
|
}
|
||||||
monkey.Unpatch(dbStoreBlock)
|
guard.Unpatch()
|
||||||
|
|
||||||
// Test rejecting the node due to index error
|
// Test rejecting the node due to index error
|
||||||
indexErrorMessage := "index error"
|
indexErrorMessage := "index error"
|
||||||
monkey.Patch((*blockIndex).flushToDB, func(_ *blockIndex) error {
|
guard = monkey.Patch((*blockIndex).flushToDB, func(_ *blockIndex) error {
|
||||||
return errors.New(indexErrorMessage)
|
return errors.New(indexErrorMessage)
|
||||||
})
|
})
|
||||||
|
defer guard.Unpatch()
|
||||||
err = dag.maybeAcceptBlock(block2, BFNone)
|
err = dag.maybeAcceptBlock(block2, BFNone)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to index error: "+
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to index error: "+
|
||||||
@ -129,5 +131,4 @@ func TestMaybeAcceptBlockErrors(t *testing.T) {
|
|||||||
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to index error: "+
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the node due to index error: "+
|
||||||
"Unexpected error. Want: %s, got: %s", indexErrorMessage, err)
|
"Unexpected error. Want: %s, got: %s", indexErrorMessage, err)
|
||||||
}
|
}
|
||||||
monkey.Unpatch((*blockIndex).flushToDB)
|
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,10 @@ func TestFlushToDBErrors(t *testing.T) {
|
|||||||
|
|
||||||
// Test flushToDB failure due to database error
|
// Test flushToDB failure due to database error
|
||||||
databaseErrorMessage := "database error"
|
databaseErrorMessage := "database error"
|
||||||
monkey.Patch(dbStoreBlockNode, func(_ database.Tx, _ *blockNode) error {
|
guard := monkey.Patch(dbStoreBlockNode, func(_ database.Tx, _ *blockNode) error {
|
||||||
return errors.New(databaseErrorMessage)
|
return errors.New(databaseErrorMessage)
|
||||||
})
|
})
|
||||||
|
defer guard.Unpatch()
|
||||||
err = dag.index.flushToDB()
|
err = dag.index.flushToDB()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("TestFlushToDBErrors: flushToDB failure due to database error: "+
|
t.Errorf("TestFlushToDBErrors: flushToDB failure due to database error: "+
|
||||||
@ -52,5 +53,4 @@ func TestFlushToDBErrors(t *testing.T) {
|
|||||||
t.Errorf("TestFlushToDBErrors: flushToDB failure due to database error: "+
|
t.Errorf("TestFlushToDBErrors: flushToDB failure due to database error: "+
|
||||||
"Unexpected flushToDB error. Expected: %s, got: %s", databaseErrorMessage, err)
|
"Unexpected flushToDB error. Expected: %s, got: %s", databaseErrorMessage, err)
|
||||||
}
|
}
|
||||||
monkey.Unpatch(dbStoreBlockNode)
|
|
||||||
}
|
}
|
||||||
|
@ -821,7 +821,8 @@ func testErrorThroughPatching(t *testing.T, expectedErrorMessage string, targetF
|
|||||||
// maturity to 1.
|
// maturity to 1.
|
||||||
dag.TstSetCoinbaseMaturity(1)
|
dag.TstSetCoinbaseMaturity(1)
|
||||||
|
|
||||||
monkey.Patch(targetFunction, replacementFunction)
|
guard := monkey.Patch(targetFunction, replacementFunction)
|
||||||
|
defer guard.Unpatch()
|
||||||
|
|
||||||
err = nil
|
err = nil
|
||||||
for i := 1; i < len(blocks); i++ {
|
for i := 1; i < len(blocks); i++ {
|
||||||
@ -843,6 +844,4 @@ func testErrorThroughPatching(t *testing.T, expectedErrorMessage string, targetF
|
|||||||
t.Errorf("ProcessBlock returned wrong error. "+
|
t.Errorf("ProcessBlock returned wrong error. "+
|
||||||
"Want: %s, got: %s", expectedErrorMessage, err)
|
"Want: %s, got: %s", expectedErrorMessage, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
monkey.Unpatch(targetFunction)
|
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,8 @@ func TestDAGSetupErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testDAGSetupErrorThroughPatching(t *testing.T, expectedErrorMessage string, targetFunction interface{}, replacementFunction interface{}) {
|
func testDAGSetupErrorThroughPatching(t *testing.T, expectedErrorMessage string, targetFunction interface{}, replacementFunction interface{}) {
|
||||||
monkey.Patch(targetFunction, replacementFunction)
|
guard := monkey.Patch(targetFunction, replacementFunction)
|
||||||
|
defer guard.Unpatch()
|
||||||
_, tearDown, err := DAGSetup("TestDAGSetup", &dagconfig.MainNetParams)
|
_, tearDown, err := DAGSetup("TestDAGSetup", &dagconfig.MainNetParams)
|
||||||
if tearDown != nil {
|
if tearDown != nil {
|
||||||
defer tearDown()
|
defer tearDown()
|
||||||
@ -50,5 +51,4 @@ func testDAGSetupErrorThroughPatching(t *testing.T, expectedErrorMessage string,
|
|||||||
if err == nil || !strings.HasPrefix(err.Error(), expectedErrorMessage) {
|
if err == nil || !strings.HasPrefix(err.Error(), expectedErrorMessage) {
|
||||||
t.Errorf("DAGSetup: expected error to have prefix '%s' but got error '%v'", expectedErrorMessage, err)
|
t.Errorf("DAGSetup: expected error to have prefix '%s' but got error '%v'", expectedErrorMessage, err)
|
||||||
}
|
}
|
||||||
monkey.Unpatch(targetFunction)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user