From 4dc2cf8a6be0434f95e980ed98b43e783f1a17e8 Mon Sep 17 00:00:00 2001 From: Nicholas Dudfield Date: Sat, 17 Jan 2015 12:40:11 +0700 Subject: [PATCH] Update tests to support latest ripple-lib: * Update ripple-lib api usage * Use latest npm ripple-lib * Tested with bignumber.js branch and tip of develop * Use new version of coffee-script * Better source maps * Update mocha * Add assert-diff for better error reporting * Add rconsole, enabled via USE_RCONSOLE env var * For use with manual installation only --- package.json | 17 +++---- test/batmans-belt.coffee | 7 ++- test/freeze-test.coffee | 5 ++- test/ledger-state.coffee | 14 ++---- test/mocha-loader-patch.js | 5 +++ test/mocha.opts | 2 +- test/orderbook-test.js | 79 +++++++++++++++------------------ test/robust-transaction-test.js | 19 ++++---- test/testutils.js | 2 +- 9 files changed, 72 insertions(+), 78 deletions(-) diff --git a/package.json b/package.json index 329eb8713a..c0c91e5b9d 100644 --- a/package.json +++ b/package.json @@ -2,33 +2,28 @@ "name": "rippled", "version": "0.0.1", "description": "Rippled Server", - "private": true, - "directories": { "test": "test" }, - "dependencies": { - "ripple-lib": "0.8.2", + "assert-diff": "0.0.4", "async": "~0.2.9", + "deep-equal": "0.0.0", "extend": "~1.2.0", - "simple-jsonrpc": "~0.0.2", - "deep-equal": "0.0.0" + "ripple-lib": "0.10.0", + "simple-jsonrpc": "~0.0.2" }, "devDependencies": { - "coffee-script": "~1.6.3", - "mocha": "~1.13.0" + "coffee-script": "^1.8.0", + "mocha": "^2.1.0" }, - "scripts": { "test": "mocha test/websocket-test.js test/server-test.js test/*-test.{js,coffee}" }, - "repository": { "type": "git", "url": "git://github.com/ripple/rippled.git" }, - "readmeFilename": "README.md" } diff --git a/test/batmans-belt.coffee b/test/batmans-belt.coffee index 863230a76a..84ad535113 100644 --- a/test/batmans-belt.coffee +++ b/test/batmans-belt.coffee @@ -101,6 +101,9 @@ exports.submit_for_final = (tx, done) -> accept so the transaction will finalize. ''' - tx.on 'proposed', -> tx.remote.ledger_accept() + tx.on 'submitted', (m) -> + ter = (m.engine_result ? '').slice(0, 3) + if ter in ['tes', 'tec'] + tx.remote.ledger_accept() tx.on 'final', (m) -> done(m) - tx.submit() \ No newline at end of file + tx.submit() diff --git a/test/freeze-test.coffee b/test/freeze-test.coffee index 1b8824f30e..fdd69f24a0 100644 --- a/test/freeze-test.coffee +++ b/test/freeze-test.coffee @@ -188,7 +188,8 @@ config = testutils.init_config() #################################### HELPERS ################################### get_lines = (remote, acc, done) -> - remote.request_account_lines acc, null, 'validated', (err, lines) -> + args = {account: acc, ledger: 'validated'} + remote.request_account_lines args, (err, lines) -> done(lines) account_set_factory = (remote, ledger, alias_for) -> @@ -844,7 +845,7 @@ execute_if_enabled (suite, enforced) -> test_if enforced, 'offer was removed by offer_create', (done) -> args = { account: 'A4', ledger: 'validated' } - + remote.request_account_offers args, (err, res) -> assert res.offers.length == 0 done() \ No newline at end of file diff --git a/test/ledger-state.coffee b/test/ledger-state.coffee index b57f6f9d88..30645634cb 100644 --- a/test/ledger-state.coffee +++ b/test/ledger-state.coffee @@ -357,12 +357,12 @@ exports.LedgerState = class LedgerState incr_reserve_amt = @incr_reserve() base_reserve = @parse_amount base_reserve_amt - inc_reserve = @parse_amount incr_reserve_amt + incr_reserve = @parse_amount incr_reserve_amt @assert base_reserve != null, "Base reserve amount #{base_reserve_amt} is invalid" - @assert base_reserve != null, + @assert incr_reserve != null, "incremental amount #{incr_reserve_amt} is invalid" for account_id, account of @declaration.accounts @@ -380,10 +380,10 @@ exports.LedgerState = class LedgerState owner_count += Object.keys(@trusts_by_ci[account_id]).length owner_count_amount = Amount.from_json(String(owner_count)) - inc_reserve_n = owner_count_amount.multiply(inc_reserve) + inc_reserve_n = owner_count_amount.multiply(incr_reserve) total_needed = total_needed.add(inc_reserve_n) - @assert @accounts[account_id].compareTo total_needed != - 1, + @assert @accounts[account_id].compareTo(total_needed) != - 1, "Account #{account_id} needs more XRP for reserve" @reserves[account_id] = total_needed @@ -481,12 +481,6 @@ exports.LedgerState = class LedgerState tx.offer_create(src, pays, gets) tx.set_flags(flags) - # console.log tx.tx_json - # process.exit() - - # tx_json = make_tx_json(src, 'OfferCreate') - # tx_json.TakerPays = pays.to_json() - # tx_json.TakerGets = gets.to_json() lines.push submit_line(src, tx.tx_json) ledger_accept() diff --git a/test/mocha-loader-patch.js b/test/mocha-loader-patch.js index 66da463246..127448ed11 100644 --- a/test/mocha-loader-patch.js +++ b/test/mocha-loader-patch.js @@ -2,6 +2,11 @@ mocha = require("mocha") // Stash a reference away to this old_loader = mocha.prototype.loadFiles +// Optionally use a more useful (but noisy) logger +if (process.env.USE_RCONSOLE) { + require('rconsole'); +}; + if (!old_loader.monkey_patched) { // Gee thanks Mocha ... mocha.prototype.loadFiles = function() { diff --git a/test/mocha.opts b/test/mocha.opts index d975235c01..ddee0baa96 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,6 +1,6 @@ --require ./test/mocha-loader-patch.js --reporter spec ---compilers coffee:coffee-script +--compilers coffee:coffee-script/register --ui tdd --timeout 10000 --slow 600 \ No newline at end of file diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 4e62eadcc9..13cb634ed5 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -1,25 +1,25 @@ var async = require('async'); -var assert = require('assert'); +var assert = require('assert-diff')({strict:true}); var Account = require('ripple-lib').UInt160; var Remote = require('ripple-lib').Remote; var Transaction = require('ripple-lib').Transaction; var testutils = require('./testutils'); var config = testutils.init_config(); - + suite('Order Book', function() { var $ = { }; - + setup(function(done) { testutils.build_setup().call($, done); }); - + teardown(function(done) { testutils.build_teardown().call($, done); }); - + test('Track offers', function (done) { var self = this; - + var steps = [ function(callback) { self.what = 'Create accounts'; @@ -32,17 +32,17 @@ suite('Order Book', function() { callback ); }, - + function waitLedgers(callback) { self.what = 'Wait ledger'; $.remote.once('ledger_closed', function() { callback(); }); - + $.remote.ledger_accept(); }, - + function verifyBalance(callback) { self.what = 'Verify balance'; @@ -53,63 +53,63 @@ suite('Order Book', function() { callback ); }, - + function (callback) { self.what = 'Set transfer rate'; - + var tx = $.remote.transaction('AccountSet', { account: 'mtgox' }); - + tx.transferRate(1.1 * 1e9); - + tx.submit(function(err, m) { assert.ifError(err); assert.strictEqual(m.engine_result, 'tesSUCCESS'); callback(); }); - + testutils.ledger_wait($.remote, tx); }, - + function (callback) { self.what = 'Set limits'; - + testutils.credit_limits($.remote, { 'alice' : '1000/USD/mtgox', 'bob' : '1000/USD/mtgox' }, callback); }, - + function (callback) { self.what = 'Distribute funds'; - + testutils.payments($.remote, { 'mtgox' : [ '100/USD/alice', '50/USD/bob' ] }, callback); }, - + function (callback) { self.what = 'Create offer'; - + // get 4000/XRP pay 10/USD : offer pays 10 USD for 4000 XRP var tx = $.remote.transaction('OfferCreate', { account: 'alice', taker_pays: '4000', taker_gets: '10/USD/mtgox' }); - + tx.submit(function(err, m) { assert.ifError(err); assert.strictEqual(m.engine_result, 'tesSUCCESS'); callback(); }); - + testutils.ledger_wait($.remote, tx); }, - + function (callback) { self.what = 'Create order book'; @@ -118,13 +118,13 @@ suite('Order Book', function() { issuer_gets: Account.json_rewrite('mtgox'), currency_gets: 'USD' }); - + ob.on('model', function(){}); ob.getOffers(function(err, offers) { assert.ifError(err); - //console.log('OFFERS', offers); - + // console.log('OFFERS', offers); + var expected = [ { Account: 'rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn', BookDirectory: 'AE0A97F385FFE42E3096BA3F98A0173090FE66A3C2482FE0570E35FA931A0000', @@ -132,8 +132,6 @@ suite('Order Book', function() { Flags: 0, LedgerEntryType: 'Offer', OwnerNode: '0000000000000000', - PreviousTxnID: offers[0].PreviousTxnID, - PreviousTxnLgrSeq: offers[0].PreviousTxnLgrSeq, Sequence: 2, TakerGets: { currency: 'USD', issuer: 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v', @@ -142,38 +140,38 @@ suite('Order Book', function() { TakerPays: '4000', index: 'CD6AE78EE0A5438978501A0404D9093597F57B705D566B5070D58BD48F98468C', owner_funds: '100', - quality: '400', is_fully_funded: true, taker_gets_funded: '10', taker_pays_funded: '4000' } ] + assert.deepEqual(offers, expected); - + callback(null, ob); }); }, function (ob, callback) { self.what = 'Create offer'; - - // get 5/USD pay 2000/XRP: offer pays 2000 XRP for 5 USD + + // get 5/USD pay 2000/XRP: offer pays 2000 XRP for 5 USD var tx = $.remote.transaction('OfferCreate', { account: 'bob', taker_pays: '5/USD/mtgox', taker_gets: '2000', }); - + tx.submit(function(err, m) { assert.ifError(err); assert.strictEqual(m.engine_result, 'tesSUCCESS'); callback(null, ob); }); - + testutils.ledger_wait($.remote, tx); - }, - + }, + function (ob, callback) { - self.what = 'Check order book tracking'; + self.what = 'Check order book tracking'; ob.getOffers(function(err, offers) { assert.ifError(err); @@ -186,17 +184,14 @@ suite('Order Book', function() { Flags: 0, LedgerEntryType: 'Offer', OwnerNode: '0000000000000000', - PreviousTxnID: offers[0].PreviousTxnID, - PreviousTxnLgrSeq: offers[0].PreviousTxnLgrSeq, Sequence: 2, - TakerGets: + TakerGets: { currency: 'USD', issuer: 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v', value: '5' }, TakerPays: '2000', index: 'CD6AE78EE0A5438978501A0404D9093597F57B705D566B5070D58BD48F98468C', owner_funds: '94.5', - quality: '400', is_fully_funded: true, taker_gets_funded: '5', taker_pays_funded: '2000' } @@ -207,7 +202,7 @@ suite('Order Book', function() { }); }, ]; - + async.waterfall(steps, function (error) { assert(!error, self.what + ': ' + error); done(); diff --git a/test/robust-transaction-test.js b/test/robust-transaction-test.js index 454e7c3d06..7604d6c028 100644 --- a/test/robust-transaction-test.js +++ b/test/robust-transaction-test.js @@ -59,7 +59,7 @@ make_suite('Robust transaction submission', function() { var tx = $.remote.transaction().payment({ from: 'root', to: 'alice', - amount: Amount.from_human('1XRP') + amount: Amount.from_human('1 XRP') }); tx.once('submitted', function(m) { @@ -72,16 +72,16 @@ make_suite('Robust transaction submission', function() { tx.submit(); //Invoke callback immediately - callback(); + callback(null, tx); }, - function sendValidTransaction(callback) { + function sendValidTransaction(previousTx, callback) { self.what = 'Send normal transaction which should succeed'; var tx = $.remote.transaction().payment({ from: 'root', to: 'bob', - amount: Amount.from_human('1XRP') + amount: Amount.from_human('1 XRP') }); tx.on('submitted', function(m) { @@ -99,7 +99,8 @@ make_suite('Robust transaction submission', function() { }); tx.once('final', function() { - callback(); + previousTx.once('final', function(){ callback(); }); + testutils.ledger_wait($.remote, previousTx); }); tx.submit(); @@ -121,7 +122,7 @@ make_suite('Robust transaction submission', function() { ] - async.series(steps, function(err) { + async.waterfall(steps, function(err) { assert(!err, self.what + ': ' + err); assert(self.resubmitted, 'Transaction failed to resubmit'); done(); @@ -161,7 +162,7 @@ make_suite('Robust transaction submission', function() { var tx = $.remote.transaction().payment({ from: 'root', to: 'alice', - amount: Amount.from_human('1XRP') + amount: Amount.from_human('1 XRP') }); tx.submit(); @@ -241,7 +242,7 @@ make_suite('Robust transaction submission', function() { var tx = $.remote.transaction().payment({ from: 'root', to: 'alice', - amount: Amount.from_human('1XRP') + amount: Amount.from_human('1 XRP') }); tx.once('submitted', function(m) { @@ -334,7 +335,7 @@ make_suite('Robust transaction submission', function() { var tx = $.remote.transaction().payment({ from: 'root', to: 'alice', - amount: Amount.from_human('1XRP') + amount: Amount.from_human('1 XRP') }); var timed_out = false; diff --git a/test/testutils.js b/test/testutils.js index 671ace017c..21ef77ddd2 100644 --- a/test/testutils.js +++ b/test/testutils.js @@ -245,7 +245,7 @@ function(remote, src, account, amount, callback) { // Before creating the account, check if it exists in the ledger. // If it does, regardless of the balance, fail the test, because // the ledger is not in the expected state. - var info = remote.requestAccountInfo(account); + var info = remote.requestAccountInfo({account: account}); info.once('success', function(result) { // The account exists. Fail by returning an error to callback.