Browse Source

make wallet async functions truly async

hk-custom-address
Wei Lu 11 years ago
parent
commit
4d4388f6bf
  1. 14
      src/wallet.js
  2. 60
      test/wallet.js

14
src/wallet.js

@ -84,13 +84,14 @@ var Wallet = function (seed, options) {
} }
this.setUnspentOutputsAsync = function(utxo, callback) { this.setUnspentOutputsAsync = function(utxo, callback) {
var error = null
try { try {
this.setUnspentOutputs(utxo) this.setUnspentOutputs(utxo)
} catch(err) { } catch(err) {
return callback(err) error = err
} finally {
process.nextTick(function(){ callback(error) })
} }
return callback()
} }
function outputToUnspentOutput(output){ function outputToUnspentOutput(output){
@ -206,14 +207,15 @@ var Wallet = function (seed, options) {
fixedFee = undefined fixedFee = undefined
} }
var tx = null var tx = null
var error = null
try { try {
tx = this.createTx(to, value, fixedFee) tx = this.createTx(to, value, fixedFee)
} catch(err) { } catch(err) {
return callback(err) error = err
} finally {
process.nextTick(function(){ callback(error, tx) })
} }
callback(null, tx)
} }
this.dustThreshold = 5430 this.dustThreshold = 5430

60
test/wallet.js

@ -284,24 +284,25 @@ describe('Wallet', function() {
wallet.setUnspentOutputs.restore() wallet.setUnspentOutputs.restore()
}) })
it('calls setUnspentOutputs', function(){ it('calls setUnspentOutputs', function(done){
sinon.stub(wallet, "setUnspentOutputs") sinon.stub(wallet, "setUnspentOutputs")
var callback = sinon.spy() var callback = function(){
var tx = wallet.setUnspentOutputsAsync(utxo, callback)
assert(wallet.setUnspentOutputs.calledWith(utxo)) assert(wallet.setUnspentOutputs.calledWith(utxo))
assert(callback.called) done()
}
wallet.setUnspentOutputsAsync(utxo, callback)
}) })
it('when setUnspentOutputs throws an error, it invokes callback with error', function(){ it('when setUnspentOutputs throws an error, it invokes callback with error', function(done){
sinon.stub(wallet, "setUnspentOutputs").throws() sinon.stub(wallet, "setUnspentOutputs").throws()
var callback = sinon.spy() var callback = function(err){
var tx = wallet.setUnspentOutputsAsync(utxo, callback) assert(err instanceof Error)
done()
assert(callback.called) }
assert(callback.args[0][0] instanceof Error) wallet.setUnspentOutputsAsync(utxo, callback)
}) })
}) })
}) })
@ -549,34 +550,41 @@ describe('Wallet', function() {
wallet.createTx.restore() wallet.createTx.restore()
}) })
it('calls createTx', function(){ it('calls createTx', function(done){
sinon.stub(wallet, "createTx").returns("fakeTx") sinon.stub(wallet, "createTx").returns("fakeTx")
var callback = sinon.spy() var callback = function(err, tx){
var tx = wallet.createTxAsync(to, value, callback)
assert(wallet.createTx.calledWith(to, value)) assert(wallet.createTx.calledWith(to, value))
assert(callback.calledWith(null, "fakeTx")) assert.equal(err, null)
assert.equal(tx, "fakeTx")
done()
}
wallet.createTxAsync(to, value, callback)
}) })
it('calls createTx correctly when fee is specified', function(){ it('calls createTx correctly when fee is specified', function(done){
sinon.stub(wallet, "createTx").returns("fakeTx") sinon.stub(wallet, "createTx").returns("fakeTx")
var callback = sinon.spy() var callback = function(err, tx){
var tx = wallet.createTxAsync(to, value, fee, callback)
assert(wallet.createTx.calledWith(to, value, fee)) assert(wallet.createTx.calledWith(to, value, fee))
assert(callback.calledWith(null, "fakeTx")) assert.equal(err, null)
assert.equal(tx, "fakeTx")
done()
}
wallet.createTxAsync(to, value, fee, callback)
}) })
it('when createTx throws an error, it invokes callback with error', function(){ it('when createTx throws an error, it invokes callback with error', function(done){
sinon.stub(wallet, "createTx").throws() sinon.stub(wallet, "createTx").throws()
var callback = sinon.spy() var callback = function(err, tx){
var tx = wallet.createTxAsync(to, value, callback) assert(err instanceof Error)
done()
}
assert(callback.called) wallet.createTxAsync(to, value, callback)
assert(callback.args[0][0] instanceof Error)
}) })
}) })

Loading…
Cancel
Save