Skip to content

Commit

Permalink
Merge pull request #3565 from ethereum/fix-wallet-race-conditions
Browse files Browse the repository at this point in the history
Fix wallet test suite race conditions
  • Loading branch information
ryanio authored Jun 3, 2020
2 parents 8ee7053 + 9093051 commit 9e46628
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions test/eth.accounts.wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ describe("eth", function () {
assert.equal(ethAccounts.wallet.length, 1);
});

it("remove wallet using an index", function() {
it("remove wallet using an index", async function() {
var ethAccounts = new Accounts();
assert.equal(ethAccounts.wallet.length, 0);

var wallet = ethAccounts.wallet.add(test.privateKey);
assert.equal(ethAccounts.wallet.length, 1);

// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.remove(0);

assert.isUndefined(ethAccounts.wallet[0]);
Expand All @@ -130,13 +133,16 @@ describe("eth", function () {

});

it("remove wallet using an address", function() {
it("remove wallet using an address", async function() {
var ethAccounts = new Accounts();
assert.equal(ethAccounts.wallet.length, 0);

var wallet = ethAccounts.wallet.add(test.privateKey);
assert.equal(ethAccounts.wallet.length, 1);

// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.remove(test.address);

assert.isUndefined(ethAccounts.wallet[0]);
Expand All @@ -147,13 +153,16 @@ describe("eth", function () {

});

it("remove wallet using an lowercase address", function() {
it("remove wallet using an lowercase address", async function() {
var ethAccounts = new Accounts();
assert.equal(ethAccounts.wallet.length, 0);

var wallet = ethAccounts.wallet.add(test.privateKey);
assert.equal(ethAccounts.wallet.length, 1);

// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.remove(test.address.toLowerCase());

assert.isUndefined(ethAccounts.wallet[0]);
Expand All @@ -164,7 +173,7 @@ describe("eth", function () {

});

it("create 5 wallets, remove two, create two more and check for overwrites", function() {
it("create 5 wallets, remove two, create two more and check for overwrites", async function() {
var count = 5;
var ethAccounts = new Accounts();
assert.equal(ethAccounts.wallet.length, 0);
Expand All @@ -178,6 +187,9 @@ describe("eth", function () {
var remainingAddresses = [0,1,3];
var beforeRemoval = remainingAddresses.map(function(n) { return wallet[n].address } );

// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.remove(2);
ethAccounts.wallet.remove(4);

Expand Down Expand Up @@ -209,7 +221,9 @@ describe("eth", function () {
assert.equal(ethAccounts.wallet.length, count);
});

it("clear wallet", function(done) {
it("clear wallet", async function() {
this.timeout(10000);

var count = 10;
var ethAccounts = new Accounts();
assert.equal(ethAccounts.wallet.length, 0);
Expand All @@ -222,22 +236,23 @@ describe("eth", function () {
addresses.push(wallet[i].address);
}

setTimeout(function () {
ethAccounts.wallet.clear();
// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.clear();

for (var i = 0; i < count; i++) {
assert.isUndefined(ethAccounts.wallet[i]);
assert.isUndefined(ethAccounts.wallet[addresses[i]]);
assert.isUndefined(ethAccounts.wallet[addresses[i].toLowerCase()]);
}
for (var i = 0; i < count; i++) {
assert.isUndefined(ethAccounts.wallet[i]);
assert.isUndefined(ethAccounts.wallet[addresses[i]]);
assert.isUndefined(ethAccounts.wallet[addresses[i].toLowerCase()]);
}

assert.equal(ethAccounts.wallet.length, 0);

done();
}, 1000);
assert.equal(ethAccounts.wallet.length, 0);
});

it("remove accounts then clear wallet", function(done) {
it("remove accounts then clear wallet", async function() {
this.timeout(10000);

var count = 10;
var ethAccounts = new Accounts();
assert.equal(ethAccounts.wallet.length, 0);
Expand All @@ -250,28 +265,28 @@ describe("eth", function () {
addresses.push(wallet[i].address);
}

// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.remove(0);
assert.isUndefined(ethAccounts.wallet[0])
ethAccounts.wallet.remove(5);
assert.isUndefined(ethAccounts.wallet[5])

setTimeout(function () {

ethAccounts.wallet.clear();
ethAccounts.wallet.clear();

for (var i = 0; i < count; i++) {
assert.isUndefined(ethAccounts.wallet[i]);
assert.isUndefined(ethAccounts.wallet[addresses[i]]);
assert.isUndefined(ethAccounts.wallet[addresses[i].toLowerCase()]);
}
await new Promise(resolve => setImmediate(resolve));

assert.equal(ethAccounts.wallet.length, 0);
for (var i = 0; i < count; i++) {
assert.isUndefined(ethAccounts.wallet[i]);
assert.isUndefined(ethAccounts.wallet[addresses[i]]);
assert.isUndefined(ethAccounts.wallet[addresses[i].toLowerCase()]);
}

done();
}, 1000);
assert.equal(ethAccounts.wallet.length, 0);
});

it("encrypt then decrypt wallet", function() {
it("encrypt then decrypt wallet", async function() {
this.timeout(10000);

var ethAccounts = new Accounts();
Expand All @@ -283,6 +298,9 @@ describe("eth", function () {
var addressFromWallet = ethAccounts.wallet[0].address;
assert.equal(ethAccounts.wallet.length, 5);

// await setImmediate to fix wallet race condition when calling `wallet.remove` immediately after `wallet.add`
await new Promise(resolve => setImmediate(resolve));

ethAccounts.wallet.remove(2);
assert.equal(ethAccounts.wallet.length, 4);

Expand Down

0 comments on commit 9e46628

Please sign in to comment.