-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4833 from hashicorp/b-ui-gracefully-handle-stat-e…
…rrors UI: Gracefully handle stat errors
- Loading branch information
Showing
10 changed files
with
207 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { copy } from '@ember/object/internals'; | ||
|
||
// Used with fetch. | ||
// Fetch only goes into the promise catch if there is a network error. | ||
// This means that handling a 4xx or 5xx error is the responsibility | ||
// of the developer. | ||
const jsonWithDefault = defaultResponse => res => | ||
res.ok ? res.json() : copy(defaultResponse, true); | ||
|
||
export default jsonWithDefault; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
ui/tests/unit/utils/behaviors/stats-tracker-frame-missing.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { resolve } from 'rsvp'; | ||
import wait from 'ember-test-helpers/wait'; | ||
import { test } from 'ember-qunit'; | ||
import sinon from 'sinon'; | ||
|
||
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); | ||
|
||
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, 'Misses reset'); | ||
assert.ok(tracker.pause.called, 'Pause called now that frameMisses == maxFrameMisses'); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters