Merge pull request #3956 from vector-im/rav/deflakify
Attempt to deflakify joining test
This commit is contained in:
commit
4d60d444d4
2 changed files with 165 additions and 64 deletions
|
@ -74,7 +74,6 @@ describe('joining a room', function () {
|
||||||
|
|
||||||
httpBackend.when('GET', '/pushrules').respond(200, {});
|
httpBackend.when('GET', '/pushrules').respond(200, {});
|
||||||
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
|
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
|
||||||
httpBackend.when('GET', '/sync').respond(200, {});
|
|
||||||
|
|
||||||
// note that we deliberately do *not* set an expectation for a
|
// note that we deliberately do *not* set an expectation for a
|
||||||
// presence update - setting one makes the first httpBackend.flush
|
// presence update - setting one makes the first httpBackend.flush
|
||||||
|
@ -86,7 +85,11 @@ describe('joining a room', function () {
|
||||||
localStorage.setItem("mx_access_token", ACCESS_TOKEN );
|
localStorage.setItem("mx_access_token", ACCESS_TOKEN );
|
||||||
localStorage.setItem("mx_user_id", USER_ID);
|
localStorage.setItem("mx_user_id", USER_ID);
|
||||||
|
|
||||||
var mc = <MatrixChat config={{}}/>;
|
var mc = (
|
||||||
|
<MatrixChat config={{}}
|
||||||
|
makeRegistrationUrl={()=>{throw new Error("unimplemented");}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
matrixChat = ReactDOM.render(mc, parentDiv);
|
matrixChat = ReactDOM.render(mc, parentDiv);
|
||||||
|
|
||||||
// switch to the Directory
|
// switch to the Directory
|
||||||
|
@ -94,10 +97,24 @@ describe('joining a room', function () {
|
||||||
|
|
||||||
var roomView;
|
var roomView;
|
||||||
|
|
||||||
// wait for /sync to happen
|
// wait for /sync to happen. This may take some time, as the client
|
||||||
return q.delay(1).then(() => {
|
// has to initialise indexeddb.
|
||||||
return httpBackend.flush();
|
console.log("waiting for /sync");
|
||||||
}).then(() => {
|
let syncDone = false;
|
||||||
|
httpBackend.when('GET', '/sync')
|
||||||
|
.check((r) => {syncDone = true;})
|
||||||
|
.respond(200, {});
|
||||||
|
function awaitSync(attempts) {
|
||||||
|
if (syncDone) {
|
||||||
|
return q();
|
||||||
|
}
|
||||||
|
if (!attempts) {
|
||||||
|
throw new Error("Gave up waiting for /sync")
|
||||||
|
}
|
||||||
|
return httpBackend.flush().then(() => awaitSync(attempts-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return awaitSync(10).then(() => {
|
||||||
// wait for the directory requests
|
// wait for the directory requests
|
||||||
httpBackend.when('POST', '/publicRooms').respond(200, {chunk: []});
|
httpBackend.when('POST', '/publicRooms').respond(200, {chunk: []});
|
||||||
httpBackend.when('GET', '/thirdparty/protocols').respond(200, {});
|
httpBackend.when('GET', '/thirdparty/protocols').respond(200, {});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
var q = require("q");
|
const q = require("q");
|
||||||
var expect = require('expect');
|
import expect from 'expect';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a mock HTTP backend, heavily inspired by Angular.js.
|
* Construct a mock HTTP backend, heavily inspired by Angular.js.
|
||||||
|
@ -9,23 +9,25 @@ var expect = require('expect');
|
||||||
function HttpBackend() {
|
function HttpBackend() {
|
||||||
this.requests = [];
|
this.requests = [];
|
||||||
this.expectedRequests = [];
|
this.expectedRequests = [];
|
||||||
var self = this;
|
const self = this;
|
||||||
// the request function dependency that the SDK needs.
|
// the request function dependency that the SDK needs.
|
||||||
this.requestFn = function(opts, callback) {
|
this.requestFn = function(opts, callback) {
|
||||||
var realReq = new Request(opts.method, opts.uri, opts.body, opts.qs);
|
const req = new Request(opts, callback);
|
||||||
realReq.callback = callback;
|
console.log("HTTP backend received request: " + req);
|
||||||
console.log("HTTP backend received request: %s %s", opts.method, opts.uri);
|
self.requests.push(req);
|
||||||
self.requests.push(realReq);
|
|
||||||
|
|
||||||
var abort = function() {
|
const abort = function() {
|
||||||
var idx = self.requests.indexOf(realReq);
|
const idx = self.requests.indexOf(req);
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
console.log("Aborting HTTP request: %s %s", opts.method, opts.uri);
|
console.log("Aborting HTTP request: %s %s", opts.method,
|
||||||
|
opts.uri);
|
||||||
self.requests.splice(idx, 1);
|
self.requests.splice(idx, 1);
|
||||||
|
req.callback("aborted");
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
abort: abort
|
abort: abort,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -34,43 +36,61 @@ HttpBackend.prototype = {
|
||||||
* Respond to all of the requests (flush the queue).
|
* Respond to all of the requests (flush the queue).
|
||||||
* @param {string} path The path to flush (optional) default: all.
|
* @param {string} path The path to flush (optional) default: all.
|
||||||
* @param {integer} numToFlush The number of things to flush (optional), default: all.
|
* @param {integer} numToFlush The number of things to flush (optional), default: all.
|
||||||
* @return {Promise} resolved when there is nothing left to flush.
|
* @param {integer=} waitTime The time (in ms) to wait for a request to happen.
|
||||||
|
* default: 5
|
||||||
|
*
|
||||||
|
* @return {Promise} resolves when there is nothing left to flush, with the
|
||||||
|
* number of requests flushed
|
||||||
*/
|
*/
|
||||||
flush: function(path, numToFlush) {
|
flush: function(path, numToFlush, waitTime) {
|
||||||
var defer = q.defer();
|
const defer = q.defer();
|
||||||
var self = this;
|
const self = this;
|
||||||
var flushed = 0;
|
let flushed = 0;
|
||||||
var triedWaiting = false;
|
let triedWaiting = false;
|
||||||
|
if (waitTime === undefined) {
|
||||||
|
waitTime = 5;
|
||||||
|
}
|
||||||
console.log(
|
console.log(
|
||||||
"HTTP backend flushing... (path=%s numToFlush=%s)", path, numToFlush
|
"HTTP backend flushing... (path=" + path
|
||||||
|
+ " numToFlush=" + numToFlush
|
||||||
|
+ " waitTime=" + waitTime
|
||||||
|
+ ")",
|
||||||
);
|
);
|
||||||
var tryFlush = function() {
|
const tryFlush = function() {
|
||||||
// if there's more real requests and more expected requests, flush 'em.
|
// if there's more real requests and more expected requests, flush 'em.
|
||||||
console.log(
|
console.log(
|
||||||
" trying to flush queue => reqs=%s expected=%s [%s]",
|
" trying to flush queue => reqs=[" + self.requests
|
||||||
self.requests.length, self.expectedRequests.length, path
|
+ "] expected=[" + self.expectedRequests
|
||||||
|
+ "]",
|
||||||
);
|
);
|
||||||
if (self._takeFromQueue(path)) {
|
if (self._takeFromQueue(path)) {
|
||||||
// try again on the next tick.
|
// try again on the next tick.
|
||||||
console.log(" flushed. Trying for more. [%s]", path);
|
|
||||||
flushed += 1;
|
flushed += 1;
|
||||||
if (numToFlush && flushed === numToFlush) {
|
if (numToFlush && flushed === numToFlush) {
|
||||||
console.log(" [%s] Flushed assigned amount: %s", path, numToFlush);
|
console.log(" Flushed assigned amount:", numToFlush);
|
||||||
defer.resolve();
|
defer.resolve(flushed);
|
||||||
}
|
} else {
|
||||||
else {
|
console.log(" flushed. Trying for more.");
|
||||||
setTimeout(tryFlush, 0);
|
setTimeout(tryFlush, 0);
|
||||||
}
|
}
|
||||||
}
|
} else if (flushed === 0 && !triedWaiting) {
|
||||||
else if (flushed === 0 && !triedWaiting) {
|
|
||||||
// we may not have made the request yet, wait a generous amount of
|
// we may not have made the request yet, wait a generous amount of
|
||||||
// time before giving up.
|
// time before giving up.
|
||||||
setTimeout(tryFlush, 5);
|
console.log(
|
||||||
|
" nothing to flush yet; waiting " + waitTime +
|
||||||
|
"ms for requests.")
|
||||||
|
setTimeout(tryFlush, waitTime);
|
||||||
triedWaiting = true;
|
triedWaiting = true;
|
||||||
}
|
} else {
|
||||||
else {
|
if (flushed === 0) {
|
||||||
console.log(" no more flushes. [%s]", path);
|
console.log(" nothing to flush; giving up");
|
||||||
defer.resolve();
|
} else {
|
||||||
|
console.log(
|
||||||
|
" no more flushes after flushing", flushed,
|
||||||
|
"requests",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
defer.resolve(flushed);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -85,14 +105,19 @@ HttpBackend.prototype = {
|
||||||
* @return {boolean} true if something was resolved.
|
* @return {boolean} true if something was resolved.
|
||||||
*/
|
*/
|
||||||
_takeFromQueue: function(path) {
|
_takeFromQueue: function(path) {
|
||||||
var req = null;
|
let req = null;
|
||||||
var i, j;
|
let i;
|
||||||
var matchingReq, expectedReq, testResponse = null;
|
let j;
|
||||||
|
let matchingReq = null;
|
||||||
|
let expectedReq = null;
|
||||||
|
let testResponse = null;
|
||||||
for (i = 0; i < this.requests.length; i++) {
|
for (i = 0; i < this.requests.length; i++) {
|
||||||
req = this.requests[i];
|
req = this.requests[i];
|
||||||
for (j = 0; j < this.expectedRequests.length; j++) {
|
for (j = 0; j < this.expectedRequests.length; j++) {
|
||||||
expectedReq = this.expectedRequests[j];
|
expectedReq = this.expectedRequests[j];
|
||||||
if (path && path !== expectedReq.path) { continue; }
|
if (path && path !== expectedReq.path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (expectedReq.method === req.method &&
|
if (expectedReq.method === req.method &&
|
||||||
req.path.indexOf(expectedReq.path) !== -1) {
|
req.path.indexOf(expectedReq.path) !== -1) {
|
||||||
if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
|
if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
|
||||||
|
@ -114,12 +139,12 @@ HttpBackend.prototype = {
|
||||||
}
|
}
|
||||||
testResponse = matchingReq.response;
|
testResponse = matchingReq.response;
|
||||||
console.log(" responding to %s", matchingReq.path);
|
console.log(" responding to %s", matchingReq.path);
|
||||||
var body = testResponse.body;
|
let body = testResponse.body;
|
||||||
if (Object.prototype.toString.call(body) == "[object Function]") {
|
if (Object.prototype.toString.call(body) == "[object Function]") {
|
||||||
body = body(req.path, req.data);
|
body = body(req.path, req.data);
|
||||||
}
|
}
|
||||||
req.callback(
|
req.callback(
|
||||||
testResponse.err, testResponse.response, body
|
testResponse.err, testResponse.response, body,
|
||||||
);
|
);
|
||||||
matchingReq = null;
|
matchingReq = null;
|
||||||
}
|
}
|
||||||
|
@ -134,10 +159,10 @@ HttpBackend.prototype = {
|
||||||
* Makes sure that the SDK hasn't sent any more requests to the backend.
|
* Makes sure that the SDK hasn't sent any more requests to the backend.
|
||||||
*/
|
*/
|
||||||
verifyNoOutstandingRequests: function() {
|
verifyNoOutstandingRequests: function() {
|
||||||
var firstOutstandingReq = this.requests[0] || {};
|
const firstOutstandingReq = this.requests[0] || {};
|
||||||
expect(this.requests.length).toEqual(0,
|
expect(this.requests.length).toEqual(0,
|
||||||
"Expected no more HTTP requests but received request to " +
|
"Expected no more HTTP requests but received request to " +
|
||||||
firstOutstandingReq.path
|
firstOutstandingReq.path,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -145,12 +170,9 @@ HttpBackend.prototype = {
|
||||||
* Makes sure that the test doesn't have any unresolved requests.
|
* Makes sure that the test doesn't have any unresolved requests.
|
||||||
*/
|
*/
|
||||||
verifyNoOutstandingExpectation: function() {
|
verifyNoOutstandingExpectation: function() {
|
||||||
var firstOutstandingExpectation = this.expectedRequests[0] || {};
|
const firstOutstandingExpectation = this.expectedRequests[0] || {};
|
||||||
expect(this.expectedRequests.length).toEqual(
|
expect(this.expectedRequests.length).toEqual(0,
|
||||||
0,
|
"Expected to see HTTP request for " + firstOutstandingExpectation.path,
|
||||||
"Expected to see HTTP request for "
|
|
||||||
+ firstOutstandingExpectation.method
|
|
||||||
+ " " + firstOutstandingExpectation.path
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -162,22 +184,36 @@ HttpBackend.prototype = {
|
||||||
* @return {Request} An expected request.
|
* @return {Request} An expected request.
|
||||||
*/
|
*/
|
||||||
when: function(method, path, data) {
|
when: function(method, path, data) {
|
||||||
var pendingReq = new Request(method, path, data);
|
const pendingReq = new ExpectedRequest(method, path, data);
|
||||||
this.expectedRequests.push(pendingReq);
|
this.expectedRequests.push(pendingReq);
|
||||||
return pendingReq;
|
return pendingReq;
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function Request(method, path, data, queryParams) {
|
/**
|
||||||
|
* Represents the expectation of a request.
|
||||||
|
*
|
||||||
|
* <p>Includes the conditions to be matched against, the checks to be made,
|
||||||
|
* and the response to be returned.
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
* @param {string} method
|
||||||
|
* @param {string} path
|
||||||
|
* @param {object?} data
|
||||||
|
*/
|
||||||
|
function ExpectedRequest(method, path, data) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.queryParams = queryParams;
|
|
||||||
this.callback = null;
|
|
||||||
this.response = null;
|
this.response = null;
|
||||||
this.checks = [];
|
this.checks = [];
|
||||||
}
|
}
|
||||||
Request.prototype = {
|
|
||||||
|
ExpectedRequest.prototype = {
|
||||||
|
toString: function() {
|
||||||
|
return this.method + " " + this.path
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a check when this request has been satisfied.
|
* Execute a check when this request has been satisfied.
|
||||||
* @param {Function} fn The function to execute.
|
* @param {Function} fn The function to execute.
|
||||||
|
@ -198,10 +234,10 @@ Request.prototype = {
|
||||||
this.response = {
|
this.response = {
|
||||||
response: {
|
response: {
|
||||||
statusCode: code,
|
statusCode: code,
|
||||||
headers: {}
|
headers: {},
|
||||||
},
|
},
|
||||||
body: data,
|
body: data,
|
||||||
err: null
|
err: null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -214,14 +250,62 @@ Request.prototype = {
|
||||||
this.response = {
|
this.response = {
|
||||||
response: {
|
response: {
|
||||||
statusCode: code,
|
statusCode: code,
|
||||||
headers: {}
|
headers: {},
|
||||||
},
|
},
|
||||||
body: null,
|
body: null,
|
||||||
err: err
|
err: err,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a request made by the app.
|
||||||
|
*
|
||||||
|
* @constructor
|
||||||
|
* @param {object} opts opts passed to request()
|
||||||
|
* @param {function} callback
|
||||||
|
*/
|
||||||
|
function Request(opts, callback) {
|
||||||
|
this.opts = opts;
|
||||||
|
this.callback = callback;
|
||||||
|
|
||||||
|
Object.defineProperty(this, 'method', {
|
||||||
|
get: function() {
|
||||||
|
return opts.method;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(this, 'path', {
|
||||||
|
get: function() {
|
||||||
|
return opts.uri;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(this, 'data', {
|
||||||
|
get: function() {
|
||||||
|
return opts.body;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(this, 'queryParams', {
|
||||||
|
get: function() {
|
||||||
|
return opts.qs;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(this, 'headers', {
|
||||||
|
get: function() {
|
||||||
|
return opts.headers || {};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Request.prototype = {
|
||||||
|
toString: function() {
|
||||||
|
return this.method + " " + this.path;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The HttpBackend class.
|
* The HttpBackend class.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue