fix broken tests

This commit is contained in:
Fred K. Schott
2019-10-25 10:18:46 -07:00
parent b8022610ca
commit 43ff824da1
4 changed files with 34 additions and 15 deletions

View File

@@ -39,6 +39,7 @@ class Connection extends EventEmitter {
private _availableLedgerVersions = new RangeSet() private _availableLedgerVersions = new RangeSet()
private _nextRequestID: number = 1 private _nextRequestID: number = 1
private _retry: number = 0 private _retry: number = 0
private _connectTimer: null|NodeJS.Timer = null
private _retryTimer: null|NodeJS.Timer = null private _retryTimer: null|NodeJS.Timer = null
private _onOpenErrorBound: null| null|((...args: any[]) => void) = null private _onOpenErrorBound: null| null|((...args: any[]) => void) = null
private _onUnexpectedCloseBound: null|((...args: any[]) => void) = null private _onUnexpectedCloseBound: null|((...args: any[]) => void) = null
@@ -182,6 +183,13 @@ class Connection extends EventEmitter {
} }
} }
_clearConnectTimer() {
if (this._connectTimer !== null) {
clearTimeout(this._connectTimer)
this._connectTimer = null
}
}
_onOpen() { _onOpen() {
if (!this._ws) { if (!this._ws) {
return Promise.reject(new DisconnectedError()) return Promise.reject(new DisconnectedError())
@@ -285,9 +293,10 @@ class Connection extends EventEmitter {
} }
connect(): Promise<void> { connect(): Promise<void> {
this._clearConnectTimer()
this._clearReconnectTimer() this._clearReconnectTimer()
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const connectTimeout = setTimeout(() => { this._connectTimer = setTimeout(() => {
reject(new ConnectionError(`Error: connect() timed out after ${this._connectionTimeout} ms. ` + reject(new ConnectionError(`Error: connect() timed out after ${this._connectionTimeout} ms. ` +
`If your internet connection is working, the rippled server may be blocked or inaccessible.`)) `If your internet connection is working, the rippled server may be blocked or inaccessible.`))
}, this._connectionTimeout) }, this._connectionTimeout)
@@ -298,7 +307,7 @@ class Connection extends EventEmitter {
if (this._state === WebSocket.OPEN) { if (this._state === WebSocket.OPEN) {
resolve() resolve()
} else if (this._state === WebSocket.CONNECTING) { } else if (this._state === WebSocket.CONNECTING) {
this._ws.once('open', resolve) this._ws.once('open', () => resolve)
} else { } else {
this._ws = this._createWebSocket() this._ws = this._createWebSocket()
// when an error causes the connection to close, the close event // when an error causes the connection to close, the close event
@@ -319,11 +328,19 @@ class Connection extends EventEmitter {
resolve, reject) resolve, reject)
this._ws.once('close', this._onUnexpectedCloseBound) this._ws.once('close', this._onUnexpectedCloseBound)
this._ws.once('open', () => { this._ws.once('open', () => {
clearTimeout(connectTimeout);
return this._onOpen().then(resolve, reject) return this._onOpen().then(resolve, reject)
}) })
} }
}) })
// Once we have a resolution or rejection, clear the timeout timer as no
// longer needed.
.then(() => {
this._clearConnectTimer()
})
.catch((err) => {
this._clearConnectTimer()
throw err;
})
} }
disconnect(): Promise<void> { disconnect(): Promise<void> {
@@ -332,6 +349,7 @@ class Connection extends EventEmitter {
_disconnect(calledByUser): Promise<void> { _disconnect(calledByUser): Promise<void> {
if (calledByUser) { if (calledByUser) {
this._clearConnectTimer()
this._clearReconnectTimer() this._clearReconnectTimer()
this._retry = 0 this._retry = 0
} }

View File

@@ -153,11 +153,11 @@ describe('RippleAPI', function () {
it('throws with an amount more than one decimal point', function () { it('throws with an amount more than one decimal point', function () {
assert.throws(() => { assert.throws(() => {
this.api.xrpToDrops('1.0.0') this.api.xrpToDrops('1.0.0')
}, /xrpToDrops: invalid value '1\.0\.0', should be a number matching \(\^-\?\[0-9\]\*\.\?\[0-9\]\*\$\)\./) }, /xrpToDrops: invalid value '1\.0\.0'/)
assert.throws(() => { assert.throws(() => {
this.api.xrpToDrops('...') this.api.xrpToDrops('...')
}, /xrpToDrops: invalid value '\.\.\.', should be a number matching \(\^-\?\[0-9\]\*\.\?\[0-9\]\*\$\)\./) }, /xrpToDrops: invalid value '\.\.\.'/)
}) })
}) })
@@ -267,11 +267,11 @@ describe('RippleAPI', function () {
it('throws with an amount more than one decimal point', function () { it('throws with an amount more than one decimal point', function () {
assert.throws(() => { assert.throws(() => {
this.api.dropsToXrp('1.0.0') this.api.dropsToXrp('1.0.0')
}, /dropsToXrp: invalid value '1\.0\.0', should be a number matching \(\^-\?\[0-9\]\*\.\?\[0-9\]\*\$\)\./) }, /dropsToXrp: invalid value '1\.0\.0'/)
assert.throws(() => { assert.throws(() => {
this.api.dropsToXrp('...') this.api.dropsToXrp('...')
}, /dropsToXrp: invalid value '\.\.\.', should be a number matching \(\^-\?\[0-9\]\*\.\?\[0-9\]\*\$\)\./) }, /dropsToXrp: invalid value '\.\.\.'/)
}) })
}) })
@@ -3130,14 +3130,14 @@ describe('RippleAPI', function () {
}); });
}); });
it('getTransaction - ledger_index not found', function () { it('getTransaction - transaction not validated', function () {
const hash = const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11'; '4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11';
return this.api.getTransaction(hash).then(() => { return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw NotFoundError'); assert(false, 'Should throw NotFoundError');
}).catch(error => { }).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
assert(error.message.indexOf('ledger_index') !== -1); assert(error.message.indexOf('Transaction has not been validated yet') !== -1);
}); });
}); });

View File

@@ -72,6 +72,7 @@ describe('Connection', function() {
const got = data.toString('ascii', 0, expect.length); const got = data.toString('ascii', 0, expect.length);
assert.strictEqual(got, expect); assert.strictEqual(got, expect);
server.close(); server.close();
connection.disconnect();
done(); done();
}); });
}); });
@@ -81,10 +82,10 @@ describe('Connection', function() {
authorization: 'authorization', authorization: 'authorization',
trustedCertificates: ['path/to/pem'] trustedCertificates: ['path/to/pem']
}; };
const connection = const connection = new utils.common.Connection(this.api.connection._url, options);
new utils.common.Connection(this.api.connection._url, options); connection.connect().catch((err) => {
connection.connect().catch(done); assert(err instanceof this.api.errors.NotConnectedError);
connection.connect().catch(done); });
}, done); }, done);
}); });

View File

@@ -2925,14 +2925,14 @@ describe('X-address Usage', function () {
}); });
}); });
it('getTransaction - ledger_index not found', function () { it('getTransaction - transaction not validated', function () {
const hash = const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11'; '4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11';
return this.api.getTransaction(hash).then(() => { return this.api.getTransaction(hash).then(() => {
assert(false, 'Should throw NotFoundError'); assert(false, 'Should throw NotFoundError');
}).catch(error => { }).catch(error => {
assert(error instanceof this.api.errors.NotFoundError); assert(error instanceof this.api.errors.NotFoundError);
assert(error.message.indexOf('ledger_index') !== -1); assert(error.message.indexOf('Transaction has not been validated yet') !== -1);
}); });
}); });