diff --git a/blockdag/accept_test.go b/blockdag/accept_test.go index 452f73083..8c7c2c513 100644 --- a/blockdag/accept_test.go +++ b/blockdag/accept_test.go @@ -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) } diff --git a/blockdag/blockindex_test.go b/blockdag/blockindex_test.go index 0ed7eb3a8..16c5d97cf 100644 --- a/blockdag/blockindex_test.go +++ b/blockdag/blockindex_test.go @@ -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) } diff --git a/blockdag/dag_test.go b/blockdag/dag_test.go index b0ec348ca..25338176c 100644 --- a/blockdag/dag_test.go +++ b/blockdag/dag_test.go @@ -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) } diff --git a/blockdag/test_utils_test.go b/blockdag/test_utils_test.go index 506d42385..629181b6c 100644 --- a/blockdag/test_utils_test.go +++ b/blockdag/test_utils_test.go @@ -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) }