Skip to content

Commit

Permalink
Test coverage for frame misses
Browse files Browse the repository at this point in the history
  • Loading branch information
DingoEatingFuzz committed Nov 2, 2018
1 parent b0a1a36 commit f224222
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ui/tests/unit/utils/allocation-stats-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sinon from 'sinon';
import Pretender from 'pretender';
import AllocationStatsTracker, { stats } from 'nomad-ui/utils/classes/allocation-stats-tracker';
import fetch from 'nomad-ui/utils/fetch';
import statsTrackerFrameMissingBehavior from './behaviors/stats-tracker-frame-missing';

module('Unit | Util | AllocationStatsTracker');

Expand Down Expand Up @@ -453,3 +454,27 @@ test('changing the value of the allocationProp constructs a new AllocationStatsT
'Changing the value of alloc results in creating a new AllocationStatsTracker instance'
);
});

statsTrackerFrameMissingBehavior({
resourceName: 'allocation',
ResourceConstructor: MockAllocation,
TrackerConstructor: AllocationStatsTracker,
mockFrame,
compileResources(frame) {
const timestamp = makeDate(frame.Timestamp);
const cpu = frame.ResourceUsage.CpuStats.TotalTicks;
const memory = frame.ResourceUsage.MemoryStats.RSS;
return [
{
timestamp,
used: cpu,
percent: cpu / 200,
},
{
timestamp,
used: memory,
percent: memory / 1024 / 1024 / 512,
},
];
},
});
106 changes: 106 additions & 0 deletions ui/tests/unit/utils/behaviors/stats-tracker-frame-missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { resolve } from 'rsvp';
import wait from 'ember-test-helpers/wait';
import { test } from 'ember-qunit';
import sinon from 'sinon';

const refDate = Date.now() * 1000000;
const makeDate = ts => new Date(ts / 1000000);

const MockResponse = json => ({
ok: true,
json() {
return resolve(json);
},
});

export default function statsTrackerFrameMissing({
resourceName,
TrackerConstructor,
ResourceConstructor,
mockFrame,
compileResources,
}) {
test('a bad response from a fetch request is handled gracefully', function(assert) {
const frame = mockFrame(1);
const [compiledCPU, compiledMemory] = compileResources(frame);
// const compiledCPU = { timestamp: makeDate(refDate + 1000), used: 101, percent: 101 / 200 };
// const compiledMemory = {
// timestamp: makeDate(refDate + 1000),
// used: 401 * 1024 * 1024,
// percent: 401 / 512,
// };

let shouldFail = false;
const fetch = () => {
return resolve(shouldFail ? { ok: false } : new MockResponse(frame));
};

const resource = ResourceConstructor();
const tracker = TrackerConstructor.create({ fetch, [resourceName]: resource });

tracker.get('poll').perform();

return wait()
.then(() => {
assert.deepEqual(tracker.get('cpu'), [compiledCPU], 'One frame of cpu');
assert.deepEqual(tracker.get('memory'), [compiledMemory], 'One frame of memory');

shouldFail = true;
tracker.get('poll').perform();
return wait();
})
.then(() => {
assert.deepEqual(tracker.get('cpu'), [compiledCPU], 'Still one frame of cpu');
assert.deepEqual(tracker.get('memory'), [compiledMemory], 'Still one frame of memory');
assert.equal(tracker.get('frameMisses'), 1, 'Frame miss is tracked');

shouldFail = false;
tracker.get('poll').perform();
return wait();
})
.then(() => {
assert.deepEqual(tracker.get('cpu'), [compiledCPU, compiledCPU], 'Still one frame of cpu');
assert.deepEqual(
tracker.get('memory'),
[compiledMemory, compiledMemory],
'Still one frame of memory'
);
assert.equal(tracker.get('frameMisses'), 0, 'Frame misses is reset');
});
});

test('enough bad responses from fetch consecutively (as set by maxFrameMisses) results in a pause', function(assert) {
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();
return wait()
.then(() => {
assert.equal(tracker.get('frameMisses'), 1, 'Tick misses');
assert.notOk(tracker.pause.called, 'Pause not called yet');

tracker.get('poll').perform();
return wait();
})
.then(() => {
assert.equal(tracker.get('frameMisses'), 2, 'Tick misses');
assert.notOk(tracker.pause.called, 'Pause still not called yet');

tracker.get('poll').perform();
return wait();
})
.then(() => {
assert.equal(tracker.get('frameMisses'), 0, 'Mises reset');
assert.ok(tracker.pause.called, 'Pause called now that frameMisses == maxFrameMisses');
});
});
}
23 changes: 23 additions & 0 deletions ui/tests/unit/utils/node-stats-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sinon from 'sinon';
import Pretender from 'pretender';
import NodeStatsTracker, { stats } from 'nomad-ui/utils/classes/node-stats-tracker';
import fetch from 'nomad-ui/utils/fetch';
import statsTrackerFrameMissingBehavior from './behaviors/stats-tracker-frame-missing';

module('Unit | Util | NodeStatsTracker');

Expand Down Expand Up @@ -221,3 +222,25 @@ test('changing the value of the nodeProp constructs a new NodeStatsTracker', fun
'Changing the value of the node results in creating a new NodeStatsTracker instance'
);
});

statsTrackerFrameMissingBehavior({
resourceName: 'node',
ResourceConstructor: MockNode,
TrackerConstructor: NodeStatsTracker,
mockFrame,
compileResources(frame) {
const timestamp = makeDate(frame.Timestamp);
return [
{
timestamp,
used: frame.CPUTicksConsumed,
percent: frame.CPUTicksConsumed / 2000,
},
{
timestamp,
used: frame.Memory.Used,
percent: frame.Memory.Used / 1024 / 1024 / 4096,
},
];
},
});

0 comments on commit f224222

Please sign in to comment.