[DEV-254] Defer any monkey unpatching (#109)

This commit is contained in:
Ori Newman 2018-10-29 12:16:58 +02:00 committed by stasatdaglabs
parent 3ace16ad23
commit 3ff2ef19e4
4 changed files with 11 additions and 11 deletions

View File

@ -101,9 +101,10 @@ func TestMaybeAcceptBlockErrors(t *testing.T) {
// Test rejecting the node due to 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)
})
defer guard.Unpatch()
err = dag.maybeAcceptBlock(block2, BFNone)
if err == nil {
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: "+
"Unexpected error. Want: %s, got: %s", databaseErrorMessage, err)
}
monkey.Unpatch(dbStoreBlock)
guard.Unpatch()
// Test rejecting the node due to index error
indexErrorMessage := "index error"
monkey.Patch((*blockIndex).flushToDB, func(_ *blockIndex) error {
guard = monkey.Patch((*blockIndex).flushToDB, func(_ *blockIndex) error {
return errors.New(indexErrorMessage)
})
defer guard.Unpatch()
err = dag.maybeAcceptBlock(block2, BFNone)
if err == nil {
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: "+
"Unexpected error. Want: %s, got: %s", indexErrorMessage, err)
}
monkey.Unpatch((*blockIndex).flushToDB)
}

View File

@ -40,9 +40,10 @@ func TestFlushToDBErrors(t *testing.T) {
// Test flushToDB failure due to 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)
})
defer guard.Unpatch()
err = dag.index.flushToDB()
if err == nil {
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: "+
"Unexpected flushToDB error. Expected: %s, got: %s", databaseErrorMessage, err)
}
monkey.Unpatch(dbStoreBlockNode)
}

View File

@ -821,7 +821,8 @@ func testErrorThroughPatching(t *testing.T, expectedErrorMessage string, targetF
// maturity to 1.
dag.TstSetCoinbaseMaturity(1)
monkey.Patch(targetFunction, replacementFunction)
guard := monkey.Patch(targetFunction, replacementFunction)
defer guard.Unpatch()
err = nil
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. "+
"Want: %s, got: %s", expectedErrorMessage, err)
}
monkey.Unpatch(targetFunction)
}

View File

@ -42,7 +42,8 @@ func TestDAGSetupErrors(t *testing.T) {
}
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)
if tearDown != nil {
defer tearDown()
@ -50,5 +51,4 @@ func testDAGSetupErrorThroughPatching(t *testing.T, expectedErrorMessage string,
if err == nil || !strings.HasPrefix(err.Error(), expectedErrorMessage) {
t.Errorf("DAGSetup: expected error to have prefix '%s' but got error '%v'", expectedErrorMessage, err)
}
monkey.Unpatch(targetFunction)
}