Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Fix intermittent failures in updateGrid test #172

Merged
merged 2 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions backend/certificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ export function saveCertsLocally({key, cert, subdomain}) {
}

export function fetchCertsFromCA() {
const {username, accessToken} = getSetting('USERS')[0];
Logger.log('Sending request to the CA:' + LOCAL_SETTINGS.CA_HOST_URL);
return fetch(
`${LOCAL_SETTINGS.CA_HOST_URL}`, {
method: 'POST',
body: JSON.stringify({
credentials: {
username,
access_token: accessToken,
plotly_api_domain: getSetting('PLOTLY_API_URL')
}
})
}
).then((res) => {
return new Promise(function(resolve) {
const {username, accessToken} = getSetting('USERS')[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice

Logger.log('Sending request to the CA:' + LOCAL_SETTINGS.CA_HOST_URL);
return resolve(fetch(
`${LOCAL_SETTINGS.CA_HOST_URL}`, {
method: 'POST',
body: JSON.stringify({
credentials: {
username,
access_token: accessToken,
plotly_api_domain: getSetting('PLOTLY_API_URL')
}
})
}
));
}).then((res) => {
if (res.status !== 201) {
let errorMessage = `An error occured requesting certificates from the CA, status ${res.status} was returned. `;
res.json().then(json => {
Expand Down
41 changes: 23 additions & 18 deletions test/backend/PlotlyAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,46 @@ import {
PlotlyAPIRequest,
updateGrid
} from '../../backend/persistent/PlotlyAPI.js';
import {names, createGrid, username, apiKey} from './utils.js';
import {wait, names, createGrid, username, apiKey} from './utils.js';
import {saveSetting} from '../../backend/settings.js';

describe('Grid API Functions', function () {
beforeEach(() => {
before(() => {
saveSetting('USERS', [{username, apiKey}]);
saveSetting('PLOTLY_API_DOMAIN', 'api.plot.ly');
saveSetting('PLOTLY_API_SSL_ENABLED', true);
});

xit('updateGrid overwrites a grid with new data', function (done) {
it('updateGrid overwrites a grid with new data', function () {
this.timeout(15 * 1000);
// First, create a new grid.
// Note that the app never actually does this,
// it works off of the assumption that a grid exists

let fid;
createGrid('Test updateGrid').then(res => res.json().then(json => {
return createGrid('Test updateGrid').then(res => res.json().then(json => {
// Update the grid's data
fid = json.file.fid;
const uids = json.file.cols.map(col => col.uid);

return updateGrid(
[
['x', 10, 40, 70, 100, 130],
['y', 20, 50, 80, 110, 140],
['z', 30, 60, 90, 120, 150]
],
fid,
uids,
username
);
return wait(1000).then(() => {
return updateGrid(
[
['x', 10, 40, 70, 100, 130],
['y', 20, 50, 80, 110, 140],
['z', 30, 60, 90, 120, 150]
],
fid,
uids,
username
);
});
})).then(() => {
const url = `grids/${fid}/content`;
// Retrieve the contents from the grid
return PlotlyAPIRequest(url, {username, apiKey, method: 'GET'});
return wait(1000).then(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this wait is needed (is it a plotly API issue?), but I'm OK with this 👍

const url = `grids/${fid}/content`;
return PlotlyAPIRequest(url, {username, apiKey, method: 'GET'});
});
}).then(res => res.json().then(json => {
// Test that the update worked
assert.deepEqual(
Expand All @@ -66,8 +72,7 @@ describe('Grid API Functions', function () {
json.cols[names[5]].data,
[130, 140, 150]
);
done();
})).catch(done);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Tthis pattern is everywhere in the tests, I'm not sure how I missed the fact that you can just return a promise. We're on an old-ish version of mocha (^2.4.5) but it seems like this was supported since 1.18.0 (https://github.com/mochajs/mocha/blob/master/CHANGELOG.md#1180--2014-03-13, mochajs/mocha#329)

}));

});
});
6 changes: 6 additions & 0 deletions test/backend/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import {
import {dissoc, merge} from 'ramda';
var restify = require('restify');

export function wait(milliseconds) {
return new Promise(function(resolve) {
setTimeout(resolve, milliseconds);
});
}

export const names = [
'country', 'month', 'year', 'lat', 'lon', 'value'
];
Expand Down