From 56a178f54587f05c5569338a93d858b4f4c29bdf Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Tue, 5 Jul 2022 22:08:22 -0700 Subject: [PATCH] test: fix race conditions in tests --- waddrmgr/db_test.go | 4 ++-- waddrmgr/manager_test.go | 27 ++++++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/waddrmgr/db_test.go b/waddrmgr/db_test.go index 0683860..3735c11 100644 --- a/waddrmgr/db_test.go +++ b/waddrmgr/db_test.go @@ -12,11 +12,11 @@ import ( // TestStoreMaxReorgDepth ensures that we can only store up to MaxReorgDepth // blocks at any given time. func TestStoreMaxReorgDepth(t *testing.T) { - t.Parallel() - teardown, db, _ := setupManager(t) defer teardown() + t.Parallel() + // We'll start the test by simulating a synced chain where we start from // 1000 and end at 109999. const ( diff --git a/waddrmgr/manager_test.go b/waddrmgr/manager_test.go index af97f0c..faa2b0c 100644 --- a/waddrmgr/manager_test.go +++ b/waddrmgr/manager_test.go @@ -2129,11 +2129,12 @@ func deriveTestAccountKey(t *testing.T) *hdkeychain.ExtendedKey { // TestManagerIncorrectVersion ensures that that the manager cannot be accessed // if its version does not match the latest version. func TestManagerHigherVersion(t *testing.T) { - t.Parallel() teardown, db, _ := setupManager(t) defer teardown() + t.Parallel() + // We'll update our manager's version to be one higher than the latest. latestVersion := getLatestVersion() err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { @@ -2186,11 +2187,12 @@ func TestManagerHigherVersion(t *testing.T) { // TestEncryptDecryptErrors ensures that errors which occur while encrypting and // decrypting data return the expected errors. func TestEncryptDecryptErrors(t *testing.T) { - t.Parallel() teardown, db, mgr := setupManager(t) defer teardown() + t.Parallel() + invalidKeyType := CryptoKeyType(0xff) if _, err := mgr.Encrypt(invalidKeyType, []byte{}); err == nil { t.Fatalf("Encrypt accepted an invalid key type!") @@ -2239,11 +2241,12 @@ func TestEncryptDecryptErrors(t *testing.T) { // TestEncryptDecrypt ensures that encrypting and decrypting data with the // the various crypto key types works as expected. func TestEncryptDecrypt(t *testing.T) { - t.Parallel() teardown, db, mgr := setupManager(t) defer teardown() + t.Parallel() + plainText := []byte("this is a plaintext") // Make sure address manager is unlocked @@ -2282,11 +2285,12 @@ func TestEncryptDecrypt(t *testing.T) { // create, retrieve, and utilize new scoped managers outside the set of default // created scopes. func TestScopedKeyManagerManagement(t *testing.T) { - t.Parallel() teardown, db := emptyDB(t) defer teardown() + t.Parallel() + // We'll start the test by creating a new root manager that will be // used for the duration of the test. var mgr *Manager @@ -2531,11 +2535,12 @@ func TestScopedKeyManagerManagement(t *testing.T) { // TestRootHDKeyNeutering tests that callers are unable to create new scoped // managers once the root HD key has been deleted from the database. func TestRootHDKeyNeutering(t *testing.T) { - t.Parallel() teardown, db := emptyDB(t) defer teardown() + t.Parallel() + // We'll start the test by creating a new root manager that will be // used for the duration of the test. var mgr *Manager @@ -2623,11 +2628,12 @@ func TestRootHDKeyNeutering(t *testing.T) { // raw accounts created with only an account number, and not a string which is // eventually mapped to an account number. func TestNewRawAccount(t *testing.T) { - t.Parallel() teardown, db := emptyDB(t) defer teardown() + t.Parallel() + // We'll start the test by creating a new root manager that will be // used for the duration of the test. var mgr *Manager @@ -2682,11 +2688,12 @@ func TestNewRawAccount(t *testing.T) { // only an account number, and not a string which is eventually mapped // to an account number. func TestNewRawAccountWatchingOnly(t *testing.T) { - t.Parallel() teardown, db := emptyDB(t) defer teardown() + t.Parallel() + // We'll start the test by creating a new root manager that will be // used for the duration of the test. var mgr *Manager @@ -2749,11 +2756,12 @@ func TestNewRawAccountWatchingOnly(t *testing.T) { // shows that watch-only accounts can be added to managers with // non-watch-only accounts. func TestNewRawAccountHybrid(t *testing.T) { - t.Parallel() teardown, db := emptyDB(t) defer teardown() + t.Parallel() + // We'll start the test by creating a new root manager that will be // used for the duration of the test. var mgr *Manager @@ -2869,11 +2877,12 @@ func testNewRawAccount(t *testing.T, _ *Manager, db walletdb.DB, // properly cache items in the cache, and return corresponding errors if the // account isn't properly cached. func TestDeriveFromKeyPathCache(t *testing.T) { - t.Parallel() teardown, db := emptyDB(t) defer teardown() + t.Parallel() + // We'll start the test by creating a new root manager that will be // used for the duration of the test. var mgr *Manager