2018-11-02 05:12:44 +00:00
|
|
|
import { resolve } from 'rsvp';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { test } from 'qunit';
|
2018-11-02 05:12:44 +00:00
|
|
|
import sinon from 'sinon';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { settled } from '@ember/test-helpers';
|
2018-11-02 05:12:44 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const MockResponse = (json) => ({
|
2018-11-02 05:12:44 +00:00
|
|
|
ok: true,
|
|
|
|
json() {
|
|
|
|
return resolve(json);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default function statsTrackerFrameMissing({
|
|
|
|
resourceName,
|
|
|
|
TrackerConstructor,
|
|
|
|
ResourceConstructor,
|
|
|
|
mockFrame,
|
|
|
|
compileResources,
|
|
|
|
}) {
|
2021-12-28 14:45:20 +00:00
|
|
|
test('a bad response from a fetch request is handled gracefully', async function (assert) {
|
2018-11-02 05:12:44 +00:00
|
|
|
const frame = mockFrame(1);
|
|
|
|
const [compiledCPU, compiledMemory] = compileResources(frame);
|
|
|
|
|
|
|
|
let shouldFail = false;
|
|
|
|
const fetch = () => {
|
2019-03-14 07:03:30 +00:00
|
|
|
return resolve(shouldFail ? { ok: false } : MockResponse(frame));
|
2018-11-02 05:12:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const resource = ResourceConstructor();
|
2021-12-28 16:08:12 +00:00
|
|
|
const tracker = TrackerConstructor.create({
|
|
|
|
fetch,
|
|
|
|
[resourceName]: resource,
|
|
|
|
});
|
2018-11-02 05:12:44 +00:00
|
|
|
|
|
|
|
tracker.get('poll').perform();
|
2019-03-14 18:35:46 +00:00
|
|
|
await settled();
|
2018-11-02 05:12:44 +00:00
|
|
|
|
2019-03-14 18:35:46 +00:00
|
|
|
assert.deepEqual(tracker.get('cpu'), [compiledCPU], 'One frame of cpu');
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
tracker.get('memory'),
|
|
|
|
[compiledMemory],
|
|
|
|
'One frame of memory'
|
|
|
|
);
|
2019-03-14 18:35:46 +00:00
|
|
|
|
|
|
|
shouldFail = true;
|
|
|
|
tracker.get('poll').perform();
|
|
|
|
await settled();
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
tracker.get('cpu'),
|
|
|
|
[compiledCPU],
|
|
|
|
'Still one frame of cpu'
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
tracker.get('memory'),
|
|
|
|
[compiledMemory],
|
|
|
|
'Still one frame of memory'
|
|
|
|
);
|
2019-03-14 18:35:46 +00:00
|
|
|
assert.equal(tracker.get('frameMisses'), 1, 'Frame miss is tracked');
|
|
|
|
|
|
|
|
shouldFail = false;
|
|
|
|
tracker.get('poll').perform();
|
|
|
|
await settled();
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
tracker.get('cpu'),
|
|
|
|
[compiledCPU, compiledCPU],
|
|
|
|
'Still one frame of cpu'
|
|
|
|
);
|
2019-03-14 18:35:46 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
tracker.get('memory'),
|
|
|
|
[compiledMemory, compiledMemory],
|
|
|
|
'Still one frame of memory'
|
|
|
|
);
|
|
|
|
assert.equal(tracker.get('frameMisses'), 0, 'Frame misses is reset');
|
2018-11-02 05:12:44 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('enough bad responses from fetch consecutively (as set by maxFrameMisses) results in a pause', async function (assert) {
|
2018-11-02 05:12:44 +00:00
|
|
|
const fetch = () => {
|
|
|
|
return resolve({ ok: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
const resource = ResourceConstructor();
|
|
|
|
const tracker = TrackerConstructor.create({
|
|
|
|
fetch,
|
|
|
|
[resourceName]: resource,
|
|
|
|
maxFrameMisses: 3,
|
|
|
|
pause: sinon.spy(),
|
|
|
|
});
|
|
|
|
|
|
|
|
tracker.get('poll').perform();
|
2019-03-14 18:35:46 +00:00
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.equal(tracker.get('frameMisses'), 1, 'Tick misses');
|
|
|
|
assert.notOk(tracker.pause.called, 'Pause not called yet');
|
|
|
|
|
|
|
|
tracker.get('poll').perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.equal(tracker.get('frameMisses'), 2, 'Tick misses');
|
|
|
|
assert.notOk(tracker.pause.called, 'Pause still not called yet');
|
|
|
|
|
|
|
|
tracker.get('poll').perform();
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.equal(tracker.get('frameMisses'), 0, 'Misses reset');
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.ok(
|
|
|
|
tracker.pause.called,
|
|
|
|
'Pause called now that frameMisses == maxFrameMisses'
|
|
|
|
);
|
2018-11-02 05:12:44 +00:00
|
|
|
});
|
|
|
|
}
|