Skip to content

Commit 4d60d44

Browse files
authored
Merge pull request element-hq#3956 from vector-im/rav/deflakify
Attempt to deflakify joining test
2 parents 53f0a6b + f16086b commit 4d60d44

2 files changed

Lines changed: 165 additions & 64 deletions

File tree

‎test/app-tests/joining.js‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ describe('joining a room', function () {
7474

7575
httpBackend.when('GET', '/pushrules').respond(200, {});
7676
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
77-
httpBackend.when('GET', '/sync').respond(200, {});
7877

7978
// note that we deliberately do *not* set an expectation for a
8079
// presence update - setting one makes the first httpBackend.flush
@@ -86,18 +85,36 @@ describe('joining a room', function () {
8685
localStorage.setItem("mx_access_token", ACCESS_TOKEN );
8786
localStorage.setItem("mx_user_id", USER_ID);
8887

89-
var mc = <MatrixChat config={{}}/>;
88+
var mc = (
89+
<MatrixChat config={{}}
90+
makeRegistrationUrl={()=>{throw new Error("unimplemented");}}
91+
/>
92+
);
9093
matrixChat = ReactDOM.render(mc, parentDiv);
9194

9295
// switch to the Directory
9396
matrixChat._setPage(PageTypes.RoomDirectory);
9497

9598
var roomView;
9699

97-
// wait for /sync to happen
98-
return q.delay(1).then(() => {
99-
return httpBackend.flush();
100-
}).then(() => {
100+
// wait for /sync to happen. This may take some time, as the client
101+
// has to initialise indexeddb.
102+
console.log("waiting for /sync");
103+
let syncDone = false;
104+
httpBackend.when('GET', '/sync')
105+
.check((r) => {syncDone = true;})
106+
.respond(200, {});
107+
function awaitSync(attempts) {
108+
if (syncDone) {
109+
return q();
110+
}
111+
if (!attempts) {
112+
throw new Error("Gave up waiting for /sync")
113+
}
114+
return httpBackend.flush().then(() => awaitSync(attempts-1));
115+
}
116+
117+
return awaitSync(10).then(() => {
101118
// wait for the directory requests
102119
httpBackend.when('POST', '/publicRooms').respond(200, {chunk: []});
103120
httpBackend.when('GET', '/thirdparty/protocols').respond(200, {});

‎test/mock-request.js‎

Lines changed: 142 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
2-
var q = require("q");
3-
var expect = require('expect');
2+
const q = require("q");
3+
import expect from 'expect';
44

55
/**
66
* Construct a mock HTTP backend, heavily inspired by Angular.js.
@@ -9,23 +9,25 @@ var expect = require('expect');
99
function HttpBackend() {
1010
this.requests = [];
1111
this.expectedRequests = [];
12-
var self = this;
12+
const self = this;
1313
// the request function dependency that the SDK needs.
1414
this.requestFn = function(opts, callback) {
15-
var realReq = new Request(opts.method, opts.uri, opts.body, opts.qs);
16-
realReq.callback = callback;
17-
console.log("HTTP backend received request: %s %s", opts.method, opts.uri);
18-
self.requests.push(realReq);
15+
const req = new Request(opts, callback);
16+
console.log("HTTP backend received request: " + req);
17+
self.requests.push(req);
1918

20-
var abort = function() {
21-
var idx = self.requests.indexOf(realReq);
19+
const abort = function() {
20+
const idx = self.requests.indexOf(req);
2221
if (idx >= 0) {
23-
console.log("Aborting HTTP request: %s %s", opts.method, opts.uri);
22+
console.log("Aborting HTTP request: %s %s", opts.method,
23+
opts.uri);
2424
self.requests.splice(idx, 1);
25+
req.callback("aborted");
2526
}
26-
}
27+
};
28+
2729
return {
28-
abort: abort
30+
abort: abort,
2931
};
3032
};
3133
}
@@ -34,43 +36,61 @@ HttpBackend.prototype = {
3436
* Respond to all of the requests (flush the queue).
3537
* @param {string} path The path to flush (optional) default: all.
3638
* @param {integer} numToFlush The number of things to flush (optional), default: all.
37-
* @return {Promise} resolved when there is nothing left to flush.
39+
* @param {integer=} waitTime The time (in ms) to wait for a request to happen.
40+
* default: 5
41+
*
42+
* @return {Promise} resolves when there is nothing left to flush, with the
43+
* number of requests flushed
3844
*/
39-
flush: function(path, numToFlush) {
40-
var defer = q.defer();
41-
var self = this;
42-
var flushed = 0;
43-
var triedWaiting = false;
45+
flush: function(path, numToFlush, waitTime) {
46+
const defer = q.defer();
47+
const self = this;
48+
let flushed = 0;
49+
let triedWaiting = false;
50+
if (waitTime === undefined) {
51+
waitTime = 5;
52+
}
4453
console.log(
45-
"HTTP backend flushing... (path=%s numToFlush=%s)", path, numToFlush
54+
"HTTP backend flushing... (path=" + path
55+
+ " numToFlush=" + numToFlush
56+
+ " waitTime=" + waitTime
57+
+ ")",
4658
);
47-
var tryFlush = function() {
59+
const tryFlush = function() {
4860
// if there's more real requests and more expected requests, flush 'em.
4961
console.log(
50-
" trying to flush queue => reqs=%s expected=%s [%s]",
51-
self.requests.length, self.expectedRequests.length, path
62+
" trying to flush queue => reqs=[" + self.requests
63+
+ "] expected=[" + self.expectedRequests
64+
+ "]",
5265
);
5366
if (self._takeFromQueue(path)) {
5467
// try again on the next tick.
55-
console.log(" flushed. Trying for more. [%s]", path);
5668
flushed += 1;
5769
if (numToFlush && flushed === numToFlush) {
58-
console.log(" [%s] Flushed assigned amount: %s", path, numToFlush);
59-
defer.resolve();
60-
}
61-
else {
70+
console.log(" Flushed assigned amount:", numToFlush);
71+
defer.resolve(flushed);
72+
} else {
73+
console.log(" flushed. Trying for more.");
6274
setTimeout(tryFlush, 0);
6375
}
64-
}
65-
else if (flushed === 0 && !triedWaiting) {
76+
} else if (flushed === 0 && !triedWaiting) {
6677
// we may not have made the request yet, wait a generous amount of
6778
// time before giving up.
68-
setTimeout(tryFlush, 5);
79+
console.log(
80+
" nothing to flush yet; waiting " + waitTime +
81+
"ms for requests.")
82+
setTimeout(tryFlush, waitTime);
6983
triedWaiting = true;
70-
}
71-
else {
72-
console.log(" no more flushes. [%s]", path);
73-
defer.resolve();
84+
} else {
85+
if (flushed === 0) {
86+
console.log(" nothing to flush; giving up");
87+
} else {
88+
console.log(
89+
" no more flushes after flushing", flushed,
90+
"requests",
91+
);
92+
}
93+
defer.resolve(flushed);
7494
}
7595
};
7696

@@ -85,14 +105,19 @@ HttpBackend.prototype = {
85105
* @return {boolean} true if something was resolved.
86106
*/
87107
_takeFromQueue: function(path) {
88-
var req = null;
89-
var i, j;
90-
var matchingReq, expectedReq, testResponse = null;
108+
let req = null;
109+
let i;
110+
let j;
111+
let matchingReq = null;
112+
let expectedReq = null;
113+
let testResponse = null;
91114
for (i = 0; i < this.requests.length; i++) {
92115
req = this.requests[i];
93116
for (j = 0; j < this.expectedRequests.length; j++) {
94117
expectedReq = this.expectedRequests[j];
95-
if (path && path !== expectedReq.path) { continue; }
118+
if (path && path !== expectedReq.path) {
119+
continue;
120+
}
96121
if (expectedReq.method === req.method &&
97122
req.path.indexOf(expectedReq.path) !== -1) {
98123
if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
@@ -114,12 +139,12 @@ HttpBackend.prototype = {
114139
}
115140
testResponse = matchingReq.response;
116141
console.log(" responding to %s", matchingReq.path);
117-
var body = testResponse.body;
142+
let body = testResponse.body;
118143
if (Object.prototype.toString.call(body) == "[object Function]") {
119144
body = body(req.path, req.data);
120145
}
121146
req.callback(
122-
testResponse.err, testResponse.response, body
147+
testResponse.err, testResponse.response, body,
123148
);
124149
matchingReq = null;
125150
}
@@ -134,23 +159,20 @@ HttpBackend.prototype = {
134159
* Makes sure that the SDK hasn't sent any more requests to the backend.
135160
*/
136161
verifyNoOutstandingRequests: function() {
137-
var firstOutstandingReq = this.requests[0] || {};
162+
const firstOutstandingReq = this.requests[0] || {};
138163
expect(this.requests.length).toEqual(0,
139164
"Expected no more HTTP requests but received request to " +
140-
firstOutstandingReq.path
165+
firstOutstandingReq.path,
141166
);
142167
},
143168

144169
/**
145170
* Makes sure that the test doesn't have any unresolved requests.
146171
*/
147172
verifyNoOutstandingExpectation: function() {
148-
var firstOutstandingExpectation = this.expectedRequests[0] || {};
149-
expect(this.expectedRequests.length).toEqual(
150-
0,
151-
"Expected to see HTTP request for "
152-
+ firstOutstandingExpectation.method
153-
+ " " + firstOutstandingExpectation.path
173+
const firstOutstandingExpectation = this.expectedRequests[0] || {};
174+
expect(this.expectedRequests.length).toEqual(0,
175+
"Expected to see HTTP request for " + firstOutstandingExpectation.path,
154176
);
155177
},
156178

@@ -162,22 +184,36 @@ HttpBackend.prototype = {
162184
* @return {Request} An expected request.
163185
*/
164186
when: function(method, path, data) {
165-
var pendingReq = new Request(method, path, data);
187+
const pendingReq = new ExpectedRequest(method, path, data);
166188
this.expectedRequests.push(pendingReq);
167189
return pendingReq;
168-
}
190+
},
169191
};
170192

171-
function Request(method, path, data, queryParams) {
193+
/**
194+
* Represents the expectation of a request.
195+
*
196+
* <p>Includes the conditions to be matched against, the checks to be made,
197+
* and the response to be returned.
198+
*
199+
* @constructor
200+
* @param {string} method
201+
* @param {string} path
202+
* @param {object?} data
203+
*/
204+
function ExpectedRequest(method, path, data) {
172205
this.method = method;
173206
this.path = path;
174207
this.data = data;
175-
this.queryParams = queryParams;
176-
this.callback = null;
177208
this.response = null;
178209
this.checks = [];
179210
}
180-
Request.prototype = {
211+
212+
ExpectedRequest.prototype = {
213+
toString: function() {
214+
return this.method + " " + this.path
215+
},
216+
181217
/**
182218
* Execute a check when this request has been satisfied.
183219
* @param {Function} fn The function to execute.
@@ -198,10 +234,10 @@ Request.prototype = {
198234
this.response = {
199235
response: {
200236
statusCode: code,
201-
headers: {}
237+
headers: {},
202238
},
203239
body: data,
204-
err: null
240+
err: null,
205241
};
206242
},
207243

@@ -214,14 +250,62 @@ Request.prototype = {
214250
this.response = {
215251
response: {
216252
statusCode: code,
217-
headers: {}
253+
headers: {},
218254
},
219255
body: null,
220-
err: err
256+
err: err,
221257
};
222258
},
223259
};
224260

261+
/**
262+
* Represents a request made by the app.
263+
*
264+
* @constructor
265+
* @param {object} opts opts passed to request()
266+
* @param {function} callback
267+
*/
268+
function Request(opts, callback) {
269+
this.opts = opts;
270+
this.callback = callback;
271+
272+
Object.defineProperty(this, 'method', {
273+
get: function() {
274+
return opts.method;
275+
},
276+
});
277+
278+
Object.defineProperty(this, 'path', {
279+
get: function() {
280+
return opts.uri;
281+
},
282+
});
283+
284+
Object.defineProperty(this, 'data', {
285+
get: function() {
286+
return opts.body;
287+
},
288+
});
289+
290+
Object.defineProperty(this, 'queryParams', {
291+
get: function() {
292+
return opts.qs;
293+
},
294+
});
295+
296+
Object.defineProperty(this, 'headers', {
297+
get: function() {
298+
return opts.headers || {};
299+
},
300+
});
301+
}
302+
303+
Request.prototype = {
304+
toString: function() {
305+
return this.method + " " + this.path;
306+
},
307+
};
308+
225309
/**
226310
* The HttpBackend class.
227311
*/

0 commit comments

Comments
 (0)