Skip to content

Commit

Permalink
Merge pull request #2417 from particle-iot/fix/cloud-tests
Browse files Browse the repository at this point in the history
[test] communication/functions communication/variables fixes for large describes
  • Loading branch information
technobly authored Mar 31, 2022
2 parents cbe11c0 + 5617455 commit b63a8dd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
34 changes: 30 additions & 4 deletions user/tests/integration/communication/functions/functions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ let device = null;
let deviceId = null;
let limits = null;

async function delayMs(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function getDeviceFunctionsWithRetries({ deviceId, auth, expectedFuncNum = 0, retries = 10, delay = 1000 } = {}) {
let lastError;
for (let i = 0; i < retries; i++) {
try {
const resp = await api.getDevice({ deviceId, auth });
const funcs = resp.body.functions;
if (expectedFuncNum > 0 && funcs.length !== expectedFuncNum) {
throw new Error('Number of functions returned from device does not match expected');
}
return resp;
} catch (e) {
lastError = e;
}
await delayMs(i * delay);
}
if (lastError) {
throw lastError;
}
throw new Error('Error fetching functions from device');
}

before(function() {
api = this.particle.apiClient.instance;
auth = this.particle.apiClient.token;
Expand All @@ -16,9 +41,10 @@ before(function() {
});

test('01_register_functions', async function() {
const resp = await api.getDevice({ deviceId, auth });
const expectedFuncs = ['fn_1', 'fn_2'];
const resp = await getDeviceFunctionsWithRetries({ deviceId, auth, expectedFuncNum: expectedFuncs.length });
const funcs = resp.body.functions;
expect(funcs).to.include.members(['fn_1', 'fn_2']);
expect(funcs).to.include.members(expectedFuncs);
});

test('02_publish_function_limits', async function() {
Expand Down Expand Up @@ -51,10 +77,10 @@ test('05_check_current_thread', async function () {
});

test('06_register_many_functions', async function() {
const resp = await api.getDevice({ deviceId, auth });
const funcCount = device.platform.is('gen2') ? 50 : limits.max_num;
const resp = await getDeviceFunctionsWithRetries({ deviceId, auth, expectedFuncNum: funcCount });
const funcs = resp.body.functions;
const expectedFuncs = [];
const funcCount = device.platform.is('gen2') ? 50 : limits.max_num;
for (let i = 1; i <= funcCount; ++i) {
let name = 'fn_' + i.toString().padStart(3, '0') + '_';
name += 'x'.repeat(Math.max(limits.max_name_len - name.length, 0));
Expand Down
35 changes: 29 additions & 6 deletions user/tests/integration/communication/variables/variables.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ async function delayMs(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function getDeviceVariablesWithRerties({ deviceId, auth, expectedVarNum = 0, retries = 10, delay = 1000 } = {}) {
let lastError;
for (let i = 0; i < retries; i++) {
try {
const resp = await api.getDevice({ deviceId, auth });
const vars = resp.body.variables;
if (expectedVarNum > 0 && Object.keys(vars).length !== expectedVarNum) {
throw new Error('Number of variables returned from device does not match expected');
}
return resp;
} catch (e) {
lastError = e;
}
await delayMs(i * delay);
}
if (lastError) {
throw lastError;
}
throw new Error('Error fetching variables from device');
}


async function getVariableWithRetries({ deviceId, name, auth, retries = 10, delay = 1000 } = {}) {
let lastError;
for (let i = 0; i < retries; i++) {
Expand All @@ -37,9 +59,7 @@ async function getVariableWithRetries({ deviceId, name, auth, retries = 10, dela
}

test('01_register_variables', async function() {
const resp = await api.getDevice({ deviceId, auth });
const vars = resp.body.variables;
expect(vars).to.include({
const expectedVars = {
'var_b': 'bool',
'var_i': 'int32',
'var_d': 'double',
Expand All @@ -60,7 +80,10 @@ test('01_register_variables', async function() {
'std_fn_d': 'double',
'std_fn_c': 'string',
'std_fn_s': 'string'
});
};
const resp = await getDeviceVariablesWithRerties({ deviceId, auth, expectedVarNum: Object.keys(expectedVars).length });
const vars = resp.body.variables;
expect(vars).to.include(expectedVars);
});

test('02_check_variable_values', async function() {
Expand Down Expand Up @@ -121,10 +144,10 @@ test('06_check_current_thread', async function() {
});

test('07_register_many_variables', async function() {
const resp = await api.getDevice({ deviceId, auth });
const varCount = device.platform.is('gen2') ? 50 : limits.max_num;
const resp = await getDeviceVariablesWithRerties({ deviceId, auth, expectedVarNum: varCount });
const vars = resp.body.variables;
const expectedVars = {};
const varCount = device.platform.is('gen2') ? 50 : limits.max_num;
for (let i = 1; i <= varCount; ++i) {
let name = 'var_' + i.toString().padStart(3, '0') + '_';
name += 'x'.repeat(Math.max(limits.max_name_len - name.length, 0));
Expand Down

0 comments on commit b63a8dd

Please sign in to comment.