How to use create_worker method in wpt

Best JavaScript code snippet using wpt

worker_manager_test.js

Source:worker_manager_test.js Github

copy

Full Screen

...75 e.extra || { extra: true },76 );77 };78 const create_worker = async (db, w = {}) => {79 return (await db.fns.create_worker(80 w.worker_pool_id || 'wp/id',81 w.worker_group || 'w/group',82 w.worker_id || 'w/id',83 w.provider_id || 'provider',84 w.created || new Date(),85 w.expires || new Date(),86 w.state || 'state',87 w.provider_data || { providerdata: true },88 w.capacity || 1,89 w.last_modified || new Date(),90 w.last_checked || new Date(),91 ))[0].create_worker;92 };93 const update_worker = async (db, w = {}, etag) => {94 return await db.deprecatedFns.update_worker(95 w.worker_pool_id || 'wp/id',96 w.worker_group || 'w/group',97 w.worker_id || 'w/id',98 w.provider_id || 'provider',99 w.created || new Date(),100 w.expires || new Date(),101 w.state || 'state',102 w.provider_data || { providerdata: true },103 w.capacity || 1,104 w.last_modified || new Date(),105 w.last_checked || new Date(),106 etag,107 );108 };109 const update_worker_2 = async (db, w = {}, etag) => {110 return await db.fns.update_worker_2(111 w.worker_pool_id || 'wp/id',112 w.worker_group || 'w/group',113 w.worker_id || 'w/id',114 w.provider_id || 'provider',115 w.created || new Date(),116 w.expires || new Date(),117 w.state || 'state',118 w.provider_data || { providerdata: true },119 w.capacity || 1,120 w.last_modified || new Date(),121 w.last_checked || new Date(),122 etag,123 w.secret || null,124 );125 };126 const get_worker_pool = async (db, worker_pool_id) => {127 const with_cap = await db.fns.get_worker_pool_with_capacity_and_counts_by_state(worker_pool_id);128 for (const wp of with_cap) {129 assert(wp.requested_count === 0);130 assert(wp.running_count === 0);131 assert(wp.stopping_count === 0);132 assert(wp.stopped_count === 0);133 assert(wp.requested_capacity === 0);134 assert(wp.running_capacity === 0);135 assert(wp.stopping_capacity === 0);136 assert(wp.stopped_capacity === 0);137 delete wp.requested_count;138 delete wp.running_count;139 delete wp.stopping_count;140 delete wp.stopped_count;141 delete wp.requested_capacity;142 delete wp.running_capacity;143 delete wp.stopping_capacity;144 delete wp.stopped_capacity;145 }146 const old = await db.deprecatedFns.get_worker_pool_with_capacity(worker_pool_id);147 assert.deepEqual(with_cap, old);148 return with_cap;149 };150 const get_worker_pools = async (db, page_size, page_offset) => {151 const with_cap = await db.fns.get_worker_pools_with_capacity_and_counts_by_state(page_size, page_offset);152 for (const wp of with_cap) {153 assert(wp.requested_count === 0);154 assert(wp.running_count === 0);155 assert(wp.stopping_count === 0);156 assert(wp.stopped_count === 0);157 assert(wp.requested_capacity === 0);158 assert(wp.running_capacity === 0);159 assert(wp.stopping_capacity === 0);160 assert(wp.stopped_capacity === 0);161 delete wp.requested_count;162 delete wp.running_count;163 delete wp.stopping_count;164 delete wp.stopped_count;165 delete wp.requested_capacity;166 delete wp.running_capacity;167 delete wp.stopping_capacity;168 delete wp.stopped_capacity;169 }170 const old = await db.deprecatedFns.get_worker_pools_with_capacity(page_size, page_offset);171 assert.deepEqual(with_cap, old);172 return with_cap;173 };174 suite(`${testing.suiteName()} - worker_pools`, function() {175 helper.dbTest('create_worker_pool/get_worker_pool', async function(db) {176 const now = new Date();177 await create_worker_pool(db, { created: now, last_modified: now });178 const rows = await get_worker_pool(db, 'wp/id');179 assert.equal(rows[0].worker_pool_id, 'wp/id');180 assert.equal(rows[0].provider_id, 'provider');181 assert.deepEqual(rows[0].previous_provider_ids, []);182 assert.equal(rows[0].description, 'descr');183 assert.deepEqual(rows[0].config, { config: true });184 assert.deepEqual(rows[0].created, now);185 assert.deepEqual(rows[0].last_modified, now);186 assert.equal(rows[0].owner, 'me@me.com');187 assert.equal(rows[0].email_on_error, false);188 assert.deepEqual(rows[0].provider_data, { providerdata: true });189 });190 helper.dbTest('get_worker_pool not found', async function(db) {191 const rows = await get_worker_pool(db, 'wp/id');192 assert.deepEqual(rows, []);193 });194 helper.dbTest('get_worker_pools empty', async function(db) {195 const rows = await get_worker_pools(db, null, null);196 assert.deepEqual(rows, []);197 });198 helper.dbTest('get_worker_pools full, pagination', async function(db) {199 for (let i = 0; i < 10; i++) {200 await create_worker_pool(db, { worker_pool_id: `wp/${i}` });201 }202 let rows = await get_worker_pools(db, null, null);203 assert.deepEqual(rows.map(r => r.worker_pool_id), _.range(10).map(i => `wp/${i}`));204 assert.equal(rows[0].provider_id, 'provider');205 assert.deepEqual(rows[0].previous_provider_ids, []);206 assert.equal(rows[0].description, 'descr');207 assert.deepEqual(rows[0].config, { config: true });208 assert.equal(rows[0].owner, 'me@me.com');209 assert.equal(rows[0].email_on_error, false);210 assert.deepEqual(rows[0].provider_data, { providerdata: true });211 rows = await get_worker_pools(db, 2, 4);212 assert.deepEqual(rows.map(r => r.worker_pool_id), [4, 5].map(i => `wp/${i}`));213 });214 helper.dbTest('update_worker_pool, change to providerId', async function(db) {215 await create_worker_pool(db);216 const upd = await update_worker_pool(db, { provider_id: 'provider2' });217 assert.deepEqual(upd[0].previous_provider_id, 'provider');218 const rows = await get_worker_pool(db, 'wp/id');219 assert.deepEqual(rows[0].previous_provider_ids, ['provider']);220 });221 helper.dbTest('update_worker_pool, change to providerId, new provider already in previous', async function(db) {222 await create_worker_pool(db, { previous_provider_ids: JSON.stringify(['provider2']) });223 const upd = await update_worker_pool(db, { provider_id: 'provider2' });224 assert.deepEqual(upd[0].previous_provider_id, 'provider');225 const rows = await get_worker_pool(db, 'wp/id');226 assert.deepEqual(rows[0].previous_provider_ids, ['provider']);227 });228 helper.dbTest('update_worker_pool, change to providerId, old provider already in previous', async function(db) {229 await create_worker_pool(db, { previous_provider_ids: JSON.stringify(['provider']) });230 const upd = await update_worker_pool(db, { provider_id: 'provider2' });231 assert.deepEqual(upd[0].previous_provider_id, 'provider');232 const rows = await get_worker_pool(db, 'wp/id');233 assert.deepEqual(rows[0].previous_provider_ids, ['provider']);234 });235 helper.dbTest('expire_worker_pool', async function(db) {236 await create_worker_pool(db, {237 worker_pool_id: 'done',238 provider_id: 'null-provider',239 previous_provider_ids: JSON.stringify([]),240 });241 await create_worker_pool(db, {242 worker_pool_id: 'still-running',243 provider_id: 'azure',244 previous_provider_ids: JSON.stringify([]),245 });246 await create_worker_pool(db, {247 worker_pool_id: 'hanger-on',248 provider_id: 'null-provider',249 previous_provider_ids: JSON.stringify(['google']),250 });251 const rows = await db.fns.expire_worker_pools();252 assert.deepEqual(rows.map(r => r.worker_pool_id), ['done']);253 });254 helper.dbTest('delete_worker_pool', async function(db) {255 await create_worker_pool(db);256 await db.fns.delete_worker_pool('wp/id');257 const rows = await get_worker_pool(db, 'wp/id');258 assert.deepEqual(rows, []);259 });260 helper.dbTest('remove_worker_pool_previous_provider_id', async function(db) {261 await create_worker_pool(db, { previous_provider_ids: JSON.stringify(['old1', 'old2']) });262 await db.fns.remove_worker_pool_previous_provider_id('wp/id', 'old1');263 const rows = await get_worker_pool(db, 'wp/id');264 assert.deepEqual(rows[0].previous_provider_ids, ['old2']);265 });266 helper.dbTest('remove_worker_pool_previous_provider_id, no worker-pool', async function(db) {267 await db.fns.remove_worker_pool_previous_provider_id('wp/id', 'old1');268 const rows = await get_worker_pool(db, 'wp/id');269 assert.deepEqual(rows, []);270 });271 helper.dbTest('remove_worker_pool_previous_provider_id, no such provider', async function(db) {272 await create_worker_pool(db, { previous_provider_ids: JSON.stringify(['old1', 'old2']) });273 await db.fns.remove_worker_pool_previous_provider_id('wp/id', 'unknown');274 const rows = await get_worker_pool(db, 'wp/id');275 assert.deepEqual(rows[0].previous_provider_ids, ['old1', 'old2']);276 });277 helper.dbTest('update_worker_pool_provider_data', async function(db) {278 await create_worker_pool(db, { provider_data: { someprov: { somedata: true } } });279 await db.fns.update_worker_pool_provider_data('wp/id', 'another', { moredata: true });280 const rows = await get_worker_pool(db, 'wp/id');281 assert.deepEqual(rows[0].provider_data, {282 someprov: { somedata: true },283 another: { moredata: true },284 });285 });286 });287 suite(`${testing.suiteName()} - worker_pool_errors`, function() {288 helper.dbTest('create_worker_pool_error/get_worker_pool_error', async function(db, isFake) {289 const now = new Date();290 await create_worker_pool_error(db, { reported: now });291 const rows = await db.fns.get_worker_pool_error('e/id', 'wp/id');292 assert.equal(rows[0].error_id, 'e/id');293 assert.equal(rows[0].worker_pool_id, 'wp/id');294 assert.deepEqual(rows[0].reported, now);295 assert.equal(rows[0].kind, 'kind');296 assert.equal(rows[0].title, 'title');297 assert.equal(rows[0].description, 'descr');298 assert.deepEqual(rows[0].extra, { extra: true });299 });300 helper.dbTest('get_worker_pool_error not found', async function(db, isFake) {301 const rows = await db.fns.get_worker_pool_error('e/id', 'wp/id');302 assert.deepEqual(rows, []);303 });304 helper.dbTest('get_worker_pool_errors_for_worker_pool empty', async function(db, isFake) {305 const rows = await db.fns.get_worker_pool_errors_for_worker_pool(null, null, null, null);306 assert.deepEqual(rows, []);307 });308 helper.dbTest('get_worker_pool_errors_for_worker_pool empty query', async function(db, isFake) {309 const now = new Date();310 await create_worker_pool_error(db, { reported: now });311 const rows = await db.fns.get_worker_pool_errors_for_worker_pool(null, null, null, null);312 assert.equal(rows[0].error_id, 'e/id');313 assert.equal(rows[0].worker_pool_id, 'wp/id');314 assert.deepEqual(rows[0].reported, now);315 assert.equal(rows[0].kind, 'kind');316 assert.equal(rows[0].title, 'title');317 assert.equal(rows[0].description, 'descr');318 assert.deepEqual(rows[0].extra, { extra: true });319 });320 helper.dbTest('get_worker_pool_errors_for_worker_pool full, pagination', async function(db, isFake) {321 let newestDate;322 for (let i = 9; i >= 0; i--) {323 newestDate = fromNow(`- ${i} days`);324 await create_worker_pool_error(db, {325 error_id: `e/${i}`,326 worker_pool_id: `wp/${i}`,327 reported: newestDate,328 });329 }330 let rows = await db.fns.get_worker_pool_errors_for_worker_pool(null, null, null, null);331 assert.deepEqual(332 rows.map(r => ({ error_id: r.error_id, worker_pool_id: r.worker_pool_id })),333 _.range(10).map(i => ({ error_id: `e/${i}`, worker_pool_id: `wp/${i}` })));334 assert.deepEqual(rows[0].reported, newestDate);335 assert.equal(rows[0].kind, 'kind');336 assert.equal(rows[0].title, 'title');337 assert.equal(rows[0].description, 'descr');338 assert.deepEqual(rows[0].extra, { extra: true });339 rows = await db.fns.get_worker_pool_errors_for_worker_pool(null, null, 2, 4);340 assert.deepEqual(341 rows.map(r => ({ error_id: r.error_id, worker_pool_id: r.worker_pool_id })),342 [4, 5].map(i => ({ error_id: `e/${i}`, worker_pool_id: `wp/${i}` })));343 });344 helper.dbTest('expire_worker_pool_errors', async function(db, isFake) {345 await create_worker_pool_error(db, { error_id: 'done', reported: fromNow('- 1 day') });346 await create_worker_pool_error(db, { error_id: 'also-done', reported: fromNow('- 2 days') });347 await create_worker_pool_error(db, { error_id: 'still-running', reported: fromNow('1 day') });348 const count = (await db.fns.expire_worker_pool_errors(fromNow()))[0].expire_worker_pool_errors;349 assert.equal(count, 2);350 const rows = await db.fns.get_worker_pool_errors_for_worker_pool(null, null, null, null);351 assert.equal(rows.length, 1);352 });353 helper.dbTest('delete_worker_pool_errors', async function(db, isFake) {354 await create_worker_pool_error(db);355 await db.fns.delete_worker_pool_error('e/id', 'wp/id');356 const rows = await db.fns.get_worker_pool_error('e/id', 'wp/id');357 assert.deepEqual(rows, []);358 });359 });360 suite(`${testing.suiteName()} - workers`, function() {361 helper.dbTest('create_worker/get_worker', async function(db) {362 const now = new Date();363 await create_worker(db, { created: now, last_modified: now, last_checked: now, expires: now });364 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');365 assert.equal(rows[0].worker_pool_id, 'wp/id');366 assert.equal(rows[0].worker_group, 'w/group');367 assert.equal(rows[0].worker_id, 'w/id');368 assert.equal(rows[0].provider_id, 'provider');369 assert.deepEqual(rows[0].created, now);370 assert.deepEqual(rows[0].expires, now);371 assert.equal(rows[0].state, 'state');372 assert.deepEqual(rows[0].provider_data, { providerdata: true });373 assert.equal(rows[0].capacity, 1);374 assert.deepEqual(rows[0].last_modified, now);375 assert.deepEqual(rows[0].last_checked, now);376 });377 helper.dbTest('create_worker/get_worker_2', async function(db) {378 const now = new Date();379 await create_worker(db, {380 created: now,381 last_modified: now,382 last_checked: now,383 expires: now,384 });385 const rows = await db.fns.get_worker_2('wp/id', 'w/group', 'w/id');386 assert.equal(rows[0].worker_pool_id, 'wp/id');387 assert.equal(rows[0].worker_group, 'w/group');388 assert.equal(rows[0].worker_id, 'w/id');389 assert.equal(rows[0].provider_id, 'provider');390 assert.deepEqual(rows[0].created, now);391 assert.deepEqual(rows[0].expires, now);392 assert.equal(rows[0].state, 'state');393 assert.deepEqual(rows[0].provider_data, { providerdata: true });394 assert.equal(rows[0].capacity, 1);395 assert.deepEqual(rows[0].last_modified, now);396 assert.deepEqual(rows[0].last_checked, now);397 assert.equal(rows[0].secret, null);398 });399 helper.dbTest('get_worker not found', async function(db) {400 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');401 assert.deepEqual(rows, []);402 });403 helper.dbTest('get_worker_2 not found', async function(db) {404 const rows = await db.fns.get_worker_2('wp/id', 'w/group', 'w/id');405 assert.deepEqual(rows, []);406 });407 helper.dbTest('expire_workers', async function(db) {408 await create_worker(db, { worker_pool_id: 'done', provider_id: 'null-provider', expires: fromNow('- 1 day') });409 await create_worker(db, { worker_pool_id: 'also-done', expires: fromNow('- 2 days') });410 await create_worker(db, { worker_pool_id: 'still-running', expires: fromNow('1 day') });411 const count = (await db.fns.expire_workers(fromNow()))[0].expire_workers;412 assert.equal(count, 2);413 const rows = await db.fns.get_workers_without_provider_data(null, null, null, null, null, null);414 assert.equal(rows.length, 1);415 });416 helper.dbTest('get workers without provider_data', async function(db) {417 const now = new Date();418 for (let i = 0; i < 2; i++) {419 await create_worker(db, {420 worker_pool_id: `wp/${i}`,421 worker_group: `group${i}`,422 worker_id: `id${i}`,423 created: now,424 last_modified: now,425 last_checked: now,426 expires: now,427 });428 }429 let rows = await db.fns.get_workers_without_provider_data(null, null, null, null, null, null);430 assert.equal(rows.length, 2);431 for (let i = 0; i < rows.length; i++) {432 const row = rows[i];433 assert(!row.provider_data);434 assert.equal(row.worker_pool_id, `wp/${i}`);435 assert.equal(row.worker_group, `group${i}`);436 assert.equal(row.worker_id, `id${i}`);437 assert.equal(row.provider_id, 'provider');438 assert.equal(row.state, 'state');439 assert.equal(row.created.toJSON(), now.toJSON());440 assert.equal(row.expires.toJSON(), now.toJSON());441 assert.equal(row.last_modified.toJSON(), now.toJSON());442 assert.equal(row.last_checked.toJSON(), now.toJSON());443 assert.equal(row.capacity, 1);444 }445 });446 helper.dbTest('get non-stopped workers', async function(db) {447 const now = new Date();448 let i = 0;449 // we are randomly ordering the ids to make sure rows are actually coming back ordered accordingly450 const randomOrderIds = [4, 6, 5, 3, 2, 7, 0, 1];451 for (let state of ["requested", "running", "stopping", "stopped", "requested", "running", "stopping", "stopped"]) {452 await create_worker(db, {453 worker_pool_id: `wp/${randomOrderIds[i]}`,454 worker_group: `group${randomOrderIds[i]}`,455 worker_id: `id${randomOrderIds[i]}`,456 created: now,457 last_modified: now,458 last_checked: now,459 expires: now,460 state,461 });462 i++;463 }464 const rows = await db.deprecatedFns.get_non_stopped_workers_2(null, null, null, null, null);465 assert.equal(rows.length, 6);466 i = 0;467 const nonStoppedIds = [0, 2, 4, 5, 6, 7];468 for (let row of rows) {469 assert.equal(row.worker_pool_id, `wp/${nonStoppedIds[i]}`);470 assert.equal(row.worker_group, `group${nonStoppedIds[i]}`);471 assert.equal(row.worker_id, `id${nonStoppedIds[i]}`);472 assert.equal(row.provider_id, 'provider');473 assert(row.state !== 'stopped');474 assert.equal(row.created.toJSON(), now.toJSON());475 assert.equal(row.expires.toJSON(), now.toJSON());476 assert.equal(row.last_modified.toJSON(), now.toJSON());477 assert.equal(row.last_checked.toJSON(), now.toJSON());478 assert.equal(row.capacity, 1);479 assert.deepEqual(row.provider_data, { providerdata: true });480 assert(row.secret !== undefined);481 assert(row.etag !== undefined);482 i++;483 }484 });485 helper.dbTest('get non-stopped workers with quarantine_until', async function(db) {486 const now = new Date();487 let i = 0;488 // we are randomly ordering the ids to make sure rows are actually coming back ordered accordingly489 const randomOrderIds = [4, 6, 5, 3, 2, 7, 0, 1];490 for (let state of ["requested", "running", "stopping", "stopped", "requested", "running", "stopping", "stopped"]) {491 await create_worker(db, {492 worker_pool_id: `wp/${randomOrderIds[i]}`,493 worker_group: `group${randomOrderIds[i]}`,494 worker_id: `id${randomOrderIds[i]}`,495 created: now,496 last_modified: now,497 last_checked: now,498 expires: now,499 state,500 });501 i++;502 }503 const quarantineUntil = fromNow('1 hour');504 await helper.withDbClient(async client => {505 // worker 4 is quarantined, and worker 6 has the same workerGroup/workerId as a quarantined worker506 // in another pool, and thus should not appear as quarantined here507 for (const [workerPoolId, workerGroup, workerId] of [508 ['wp/4', 'group4', 'id4'],509 ['wp/1', 'group6', 'id6'],510 ]) {511 await client.query(`512 insert513 into queue_workers514 (task_queue_id, worker_group, worker_id, recent_tasks, quarantine_until, expires, first_claim, last_date_active) values515 ($1, $2, $3, jsonb_build_array(), $4, now() + interval '1 hour', now() - interval '1 hour', now())516 `, [workerPoolId, workerGroup, workerId, quarantineUntil]);517 }518 });519 const rows = await db.fns.get_non_stopped_workers_quntil_providers(null, null, null, null, null, null, null);520 assert.equal(rows.length, 6);521 i = 0;522 const nonStoppedIds = [0, 2, 4, 5, 6, 7];523 for (let row of rows) {524 assert.equal(row.worker_pool_id, `wp/${nonStoppedIds[i]}`);525 assert.equal(row.worker_group, `group${nonStoppedIds[i]}`);526 assert.equal(row.worker_id, `id${nonStoppedIds[i]}`);527 assert.equal(row.provider_id, 'provider');528 assert(row.state !== 'stopped');529 assert.equal(row.created.toJSON(), now.toJSON());530 assert.equal(row.expires.toJSON(), now.toJSON());531 assert.equal(row.last_modified.toJSON(), now.toJSON());532 assert.equal(row.last_checked.toJSON(), now.toJSON());533 assert.equal(row.capacity, 1);534 assert.deepEqual(row.provider_data, { providerdata: true });535 assert(row.secret !== undefined);536 assert(row.etag !== undefined);537 assert.deepEqual(row.quarantine_until, nonStoppedIds[i] === 4 ? quarantineUntil : null);538 i++;539 }540 });541 helper.dbTest('get non-stopped workers by provider', async function(db) {542 const now = new Date();543 let i = 0;544 for (const provider_id of ["azure", "static", "aws", "gcp"]) {545 await create_worker(db, {546 worker_id: `id${i++}`,547 state: 'running',548 provider_id,549 created: now,550 last_modified: now,551 last_checked: now,552 expires: now,553 });554 }555 const testRuns = [556 { providers_filter_cond: null, providers_filter_value: null, expected_count: 4 },557 { providers_filter_cond: '=', providers_filter_value: null, expected_count: 4 }, // ignoring partial condition558 { providers_filter_cond: null, providers_filter_value: 'a', expected_count: 4 }, // ignoring partial condition559 { providers_filter_cond: '=', providers_filter_value: 'aws', expected_count: 1 },560 { providers_filter_cond: '<>', providers_filter_value: 'aws', expected_count: 3 },561 { providers_filter_cond: '=', providers_filter_value: 'aws,static', expected_count: 2 },562 { providers_filter_cond: '<>', providers_filter_value: 'aws,static', expected_count: 2 },563 { providers_filter_cond: '=', providers_filter_value: 'static', expected_count: 1 },564 { providers_filter_cond: '<>', providers_filter_value: 'non-existent', expected_count: 4 },565 { providers_filter_cond: '=', providers_filter_value: 'non-existent', expected_count: 0 },566 { providers_filter_cond: '=', providers_filter_value: 'azure,static,aws,gcp', expected_count: 4 },567 ];568 for (const run of testRuns) {569 const rows = await db.fns.get_non_stopped_workers_quntil_providers(570 null, null, null, run.providers_filter_cond, run.providers_filter_value, null, null);571 assert.equal(rows.length, run.expected_count);572 }573 });574 helper.dbTest('update_worker, change to a single field', async function(db) {575 const etag = await create_worker(db);576 await db.deprecatedFns.update_worker(577 'wp/id',578 'w/group',579 'w/id',580 'provider2',581 null,582 null,583 null,584 null,585 null,586 null,587 null,588 etag,589 );590 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');591 assert.equal(rows[0].worker_pool_id, 'wp/id');592 assert.equal(rows[0].worker_group, 'w/group');593 assert.equal(rows[0].worker_id, 'w/id');594 assert.equal(rows[0].provider_id, 'provider2');595 assert(rows[0].created instanceof Date);596 assert(rows[0].expires instanceof Date);597 assert.equal(rows[0].state, 'state');598 assert.deepEqual(rows[0].provider_data, { providerdata: true });599 assert.equal(rows[0].capacity, 1);600 assert(rows[0].last_modified instanceof Date);601 assert(rows[0].last_checked instanceof Date);602 });603 helper.dbTest('update_worker_2, change to a single field', async function(db) {604 const etag = await create_worker(db);605 const secret = `${slug.v4()}${slug.v4()}`;606 const encryptedSecret = db.encrypt({ value: Buffer.from(secret, 'utf8') });607 await db.fns.update_worker_2(608 'wp/id',609 'w/group',610 'w/id',611 null,612 null,613 null,614 null,615 null,616 null,617 null,618 null,619 etag,620 encryptedSecret,621 );622 const rows = await db.fns.get_worker_2('wp/id', 'w/group', 'w/id');623 assert.equal(rows[0].worker_pool_id, 'wp/id');624 assert.equal(rows[0].worker_group, 'w/group');625 assert.equal(rows[0].worker_id, 'w/id');626 assert.equal(rows[0].provider_id, 'provider');627 assert(rows[0].created instanceof Date);628 assert(rows[0].expires instanceof Date);629 assert.equal(rows[0].state, 'state');630 assert.deepEqual(rows[0].provider_data, { providerdata: true });631 assert.equal(rows[0].capacity, 1);632 assert(rows[0].last_modified instanceof Date);633 assert(rows[0].last_checked instanceof Date);634 assert.deepEqual(rows[0].secret, encryptedSecret);635 });636 helper.dbTest('update_worker, change to a multiple fields', async function(db) {637 const etag = await create_worker(db);638 const updated = await db.deprecatedFns.update_worker(639 'wp/id',640 'w/group',641 'w/id',642 'provider2',643 null,644 null,645 'requested',646 null,647 null,648 null,649 null,650 etag,651 );652 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');653 assert.equal(rows[0].provider_id, 'provider2');654 assert.equal(rows[0].state, 'requested');655 assert.deepEqual(updated, rows);656 });657 helper.dbTest('update_worker_2, change to a multiple fields', async function(db) {658 const etag = await create_worker(db);659 const updated = await db.fns.update_worker_2(660 'wp/id',661 'w/group',662 'w/id',663 'provider2',664 null,665 null,666 'requested',667 null,668 null,669 null,670 null,671 etag,672 null,673 );674 const rows = await db.fns.get_worker_2('wp/id', 'w/group', 'w/id');675 assert.equal(rows[0].provider_id, 'provider2');676 assert.equal(rows[0].state, 'requested');677 assert.deepEqual(updated, rows);678 });679 helper.dbTest('update_worker, no changes', async function(db) {680 const etag = await create_worker(db);681 const updated = await db.deprecatedFns.update_worker(682 'wp/id',683 'w/group',684 'w/id',685 null,686 null,687 null,688 null,689 null,690 null,691 null,692 null,693 etag,694 );695 // this is not 0 because there was a row that matched even though there was no change696 assert.equal(updated.length, 1);697 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');698 assert.equal(rows[0].provider_id, 'provider');699 assert.equal(rows[0].state, 'state');700 });701 helper.dbTest('update_worker_2, no changes', async function(db) {702 const etag = await create_worker(db);703 const updated = await db.fns.update_worker_2(704 'wp/id',705 'w/group',706 'w/id',707 null,708 null,709 null,710 null,711 null,712 null,713 null,714 null,715 etag,716 null,717 );718 // this is not 0 because there was a row that matched even though there was no change719 assert.equal(updated.length, 1);720 const rows = await db.fns.get_worker_2('wp/id', 'w/group', 'w/id');721 assert.equal(rows[0].provider_id, 'provider');722 assert.equal(rows[0].state, 'state');723 });724 helper.dbTest('update_worker, worker doesn\'t exist', async function(db) {725 const etag = await create_worker(db);726 await assert.rejects(727 async () => {728 await update_worker(db, { worker_pool_id: 'does-not-exist' }, etag);729 },730 /no such row/,731 );732 });733 helper.dbTest('update_worker_2, worker doesn\'t exist', async function(db) {734 const etag = await create_worker(db);735 await assert.rejects(736 async () => {737 await update_worker_2(db, { worker_pool_id: 'does-not-exist' }, etag);738 },739 /no such row/,740 );741 });742 helper.dbTest('update_worker, override when etag not specified', async function(db) {743 await create_worker(db);744 await db.deprecatedFns.update_worker(745 'wp/id',746 'w/group',747 'w/id',748 null,749 null,750 null,751 null,752 null,753 2, /* capacity */754 null,755 null,756 null, /* etag */757 );758 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');759 assert.equal(rows[0].capacity, 2);760 });761 helper.dbTest('update_worker_2, override when etag not specified', async function(db) {762 await create_worker(db);763 await db.fns.update_worker_2(764 'wp/id',765 'w/group',766 'w/id',767 null,768 null,769 null,770 null,771 null,772 2, /* capacity */773 null,774 null,775 null, /* etag */776 null,777 );778 const rows = await db.fns.get_worker_2('wp/id', 'w/group', 'w/id');779 assert.equal(rows[0].capacity, 2);780 });781 helper.dbTest('update_worker, throws when etag is wrong', async function(db) {782 await create_worker(db);783 await assert.rejects(784 async () => {785 await db.deprecatedFns.update_worker(786 'wp/id',787 'w/group',788 'w/id',789 null,790 null,791 null,792 null,793 null,794 2, /* capacity */795 null,796 null,797 '915a609a-f3bb-42fa-b584-a1209e7d9a02', /* etag */798 );799 },800 /unsuccessful update/,801 );802 });803 helper.dbTest('update_worker_2, throws when etag is wrong', async function(db) {804 await create_worker(db);805 await assert.rejects(806 async () => {807 await db.fns.update_worker_2(808 'wp/id',809 'w/group',810 'w/id',811 null,812 null,813 null,814 null,815 null,816 2, /* capacity */817 null,818 null,819 '915a609a-f3bb-42fa-b584-a1209e7d9a02', /* etag */820 null,821 );822 },823 /unsuccessful update/,824 );825 });826 helper.dbTest('update_worker, throws when row does not exist', async function(db) {827 const etag = await create_worker(db);828 await assert.rejects(829 async () => {830 await db.deprecatedFns.update_worker(831 'does-not-exist',832 'w/group',833 'w/id',834 null,835 null,836 null,837 null,838 null,839 2, /* capacity */840 null,841 null,842 etag,843 );844 },845 /no such row/,846 );847 });848 helper.dbTest('update_worker_2, throws when row does not exist', async function(db) {849 const etag = await create_worker(db);850 await assert.rejects(851 async () => {852 await db.fns.update_worker_2(853 'does-not-exist',854 'w/group',855 'w/id',856 null,857 null,858 null,859 null,860 null,861 2, /* capacity */862 null,863 null,864 etag,865 null,866 );867 },868 /no such row/,869 );870 });871 helper.dbTest('delete_worker', async function(db) {872 await create_worker_pool(db);873 await db.fns.delete_worker('wp/id', 'w/group', 'w/id');874 const rows = await db.deprecatedFns.get_worker('wp/id', 'w/group', 'w/id');875 assert.deepEqual(rows, []);876 });877 });878 suite('existing capacity', function() {879 helper.dbTest('no workers', async function(db) {880 await create_worker_pool(db);881 const row = (await db.fns.get_worker_pool_with_capacity_and_counts_by_state('wp/id'))[0];882 assert.equal(row.current_capacity, 0);883 assert.equal(row.requested_count, 0);884 assert.equal(row.running_count, 0);885 assert.equal(row.stopping_count, 0);886 assert.equal(row.stopped_count, 0);887 assert.equal(row.requested_capacity, 0);888 assert.equal(row.running_capacity, 0);889 assert.equal(row.stopping_capacity, 0);890 assert.equal(row.stopped_capacity, 0);891 });892 helper.dbTest('single worker, capacity 1', async function(db) {893 await create_worker_pool(db);894 await create_worker(db, { capacity: 1, state: 'running' });895 const row = (await db.fns.get_worker_pool_with_capacity_and_counts_by_state('wp/id'))[0];896 assert.equal(row.current_capacity, 1);897 assert.equal(row.requested_count, 0);898 assert.equal(row.running_count, 1);899 assert.equal(row.stopping_count, 0);900 assert.equal(row.stopped_count, 0);901 assert.equal(row.requested_capacity, 0);902 assert.equal(row.running_capacity, 1);903 assert.equal(row.stopping_capacity, 0);904 assert.equal(row.stopped_capacity, 0);905 });906 helper.dbTest('single worker, capacity > 1', async function(db) {907 await create_worker_pool(db);908 await create_worker(db, { capacity: 64, state: 'running' });909 const row = (await db.fns.get_worker_pool_with_capacity_and_counts_by_state('wp/id'))[0];910 assert.equal(row.current_capacity, 64);911 assert.equal(row.requested_count, 0);912 assert.equal(row.running_count, 1);913 assert.equal(row.stopping_count, 0);914 assert.equal(row.stopped_count, 0);915 assert.equal(row.requested_capacity, 0);916 assert.equal(row.running_capacity, 64);917 assert.equal(row.stopping_capacity, 0);918 assert.equal(row.stopped_capacity, 0);919 });920 helper.dbTest('multiple workers, capacity 1', async function(db) {921 await create_worker_pool(db);922 await create_worker(db, { worker_id: 'foo1', capacity: 1, state: 'running' });923 await create_worker(db, { worker_id: 'foo2', capacity: 1, state: 'running' });924 await create_worker(db, { worker_id: 'foo3', capacity: 1, state: 'running' });925 await create_worker(db, { worker_id: 'foo4', capacity: 1, state: 'running' });926 const row = (await db.fns.get_worker_pool_with_capacity_and_counts_by_state('wp/id'))[0];927 assert.equal(row.current_capacity, 4);928 assert.equal(row.requested_count, 0);929 assert.equal(row.running_count, 4);930 assert.equal(row.stopping_count, 0);931 assert.equal(row.stopped_count, 0);932 assert.equal(row.requested_capacity, 0);933 assert.equal(row.running_capacity, 4);934 assert.equal(row.stopping_capacity, 0);935 assert.equal(row.stopped_capacity, 0);936 });937 helper.dbTest('multiple workers, capacity > 1', async function(db) {938 await create_worker_pool(db);939 await create_worker(db, { worker_id: 'foo1', capacity: 32, state: 'running' });940 await create_worker(db, { worker_id: 'foo2', capacity: 64, state: 'running' });941 await create_worker(db, { worker_id: 'foo3', capacity: 64, state: 'running' });942 await create_worker(db, { worker_id: 'foo4', capacity: 1, state: 'running' });943 const row = (await db.fns.get_worker_pool_with_capacity_and_counts_by_state('wp/id'))[0];944 assert.equal(row.current_capacity, 161);945 assert.equal(row.requested_count, 0);946 assert.equal(row.running_count, 4);947 assert.equal(row.stopping_count, 0);948 assert.equal(row.stopped_count, 0);949 assert.equal(row.requested_capacity, 0);950 assert.equal(row.running_capacity, 161);951 assert.equal(row.stopping_capacity, 0);952 assert.equal(row.stopped_capacity, 0);953 });954 helper.dbTest('multiple workers, multiple states', async function(db) {955 await create_worker_pool(db);956 await create_worker(db, { worker_id: 'foo1', capacity: 32, state: 'running' });957 await create_worker(db, { worker_id: 'foo2', capacity: 64, state: 'stopped' });958 await create_worker(db, { worker_id: 'foo3', capacity: 64, state: 'running' });959 await create_worker(db, { worker_id: 'foo4', capacity: 1, state: 'requested' });960 const row = (await db.fns.get_worker_pool_with_capacity_and_counts_by_state('wp/id'))[0];961 assert.equal(row.current_capacity, 97);962 assert.equal(row.requested_count, 1);963 assert.equal(row.running_count, 2);964 assert.equal(row.stopping_count, 0);965 assert.equal(row.stopped_count, 1);966 assert.equal(row.requested_capacity, 1);967 assert.equal(row.running_capacity, 96);968 assert.equal(row.stopping_capacity, 0);969 assert.equal(row.stopped_capacity, 64);970 });971 helper.dbTest('no workers (multiple pools)', async function(db) {972 await create_worker_pool(db);973 await create_worker_pool(db, { worker_pool_id: 'ff/tt' });974 const pools = (await db.fns.get_worker_pools_with_capacity_and_counts_by_state(null, null)).sort();975 assert.equal(pools[0].current_capacity, 0);976 assert.equal(pools[1].current_capacity, 0);977 });978 helper.dbTest('single worker (multiple pools)', async function(db) {979 await create_worker_pool(db);980 await create_worker_pool(db, { worker_pool_id: 'ff/tt' });981 await create_worker(db, { capacity: 4, state: 'running' });982 const pools = (await db.fns.get_worker_pools_with_capacity_and_counts_by_state(null, null)).sort();983 assert.equal(pools[0].worker_pool_id, 'ff/tt');984 assert.equal(pools[1].worker_pool_id, 'wp/id');985 assert.equal(pools[0].current_capacity, 0);986 assert.equal(pools[1].current_capacity, 4);987 });988 helper.dbTest('multiple workers (multiple pools)', async function(db) {989 await create_worker_pool(db);990 await create_worker_pool(db, { worker_pool_id: 'ff/tt' });991 await create_worker(db, { worker_id: 'foo1', capacity: 4, state: 'running' });992 await create_worker(db, { worker_id: 'foo2', capacity: 1, state: 'running' });993 await create_worker(db, { worker_id: 'foo3', capacity: 10, state: 'running', worker_pool_id: 'ff/tt' });994 await create_worker(db, { worker_id: 'foo4', capacity: 3, state: 'stopped' });995 await create_worker(db, { worker_id: 'foo5', capacity: 7, state: 'stopped', worker_pool_id: 'ff/tt' });996 const pools = (await db.fns.get_worker_pools_with_capacity_and_counts_by_state(null, null)).sort();997 assert.equal(pools[0].worker_pool_id, 'ff/tt');998 assert.equal(pools[1].worker_pool_id, 'wp/id');999 assert.equal(pools[0].current_capacity, 10);1000 assert.equal(pools[1].current_capacity, 5);1001 });1002 });1003 suite(`${testing.suiteName()} - TaskQueue`, function() {1004 helper.dbTest('no such task queue', async function(db) {1005 const res = await db.fns.get_task_queue_wm_2('prov/wt', new Date());1006 assert.deepEqual(res, []);1007 });1008 helper.dbTest('get_task_queues_wm empty', async function(db) {1009 const res = await db.fns.get_task_queues_wm(null, null, null, null);...

Full Screen

Full Screen

createParams.js

Source:createParams.js Github

copy

Full Screen

1/**2 * imports3 */4import Fs from 'fs';5import Path from 'path';6import Chalk from 'chalk';7import Clear from 'clear';8import _merge from '@onephrase/util/obj/merge.js';9import _before from '@onephrase/util/str/before.js';10import _after from '@onephrase/util/str/after.js';11import _isNumeric from '@onephrase/util/js/isNumeric.js';12import Promptx, { initialGetIndex, validateAs, transformAs } from '@onephrase/util/cli/Promptx.js';13import * as DotJson from '@onephrase/util/src/DotJson.js';14import printArgs from '@onephrase/util/cli/printArgs.js';15/**16 * Obtains parameters for initializing a server.17 * 18 * @param string ROOT19 * @param object flags20 * @param bool ellipsis21 * @param object pkg22 * 23 * @return Promise24 */25export default async function(ROOT, flags, ellipsis, pkg) {26 Clear();27 var _params = {}, _paramsFile;28 if (Fs.existsSync(_paramsFile = Path.join(ROOT, flags['CONFIG'] || './.navigator/client.config.json'))) {29 _params = DotJson.read(_paramsFile);30 }31 // Increment cache32 if (_params.WORKER_CACHE_NAME && _params.WORKER_CACHE_NAME.indexOf('_v') > -1 && _isNumeric(_after(_params.WORKER_CACHE_NAME, '_v'))) {33 _params.WORKER_CACHE_NAME = _before(_params.WORKER_CACHE_NAME, '_v') + '_v' + (parseInt(_after(_params.WORKER_CACHE_NAME, '_v')) + 1);34 }35 // -------------------36 // Create server parameters37 // -------------------38 var params = _merge({39 ROOT,40 PUBLIC_DIR: './public',41 CLIENT_DIR: './client',42 CLIENT_HOST_PATH: '',43 // -----------------44 // SERVICE WORKER45 // -----------------46 CREATE_WORKER: false,47 WORKER_DIR: './worker',48 WORKER_SHOW_LIFECYCLE_LOG: true,49 // -----------------50 WORKER_SCOPE: '/',51 WORKER_CACHE_NAME: 'cache_v0',52 WORKER_FETCHING_STRATEGY: 'cache_first',53 WORKER_CACHING_STRATEGY: 'dynamic',54 WORKER_CACHING_LIST: [],55 WORKER_SKIP_WAITING: false,56 // -----------------57 WORKER_SUPPORT_MESSAGING: true,58 WORKER_MESSAGE_ROUTING_URL_PROPERTY: '',59 WORKER_MESSAGE_SHOULD_RELAY_PROPERTY: '',60 // -----------------61 WORKER_SUPPORT_NOTIFICATION: false,62 WORKER_PUSH_REGISTRATION_URL: '',63 WORKER_PUSH_UNREGISTRATION_URL: '',64 WORKER_PUSH_PUBLIC_KEY: '',65 WORKER_NOTIFICATION_ROUTING_URL_PROPERTY: '',66 WORKER_NOTIFICATION_TARGET_URL_PROPERTY: '',67 }, _params, flags);68 // Choices hash...69 const choices = {70 worker_fetching_strategy: [71 {value: 'network_first',},72 {value: 'cache_first',},73 {value: 'auto',},74 ],75 worker_cahing_strategy:[76 {value: 'dynamic',},77 {value: 'static',},78 {value: 'none',},79 ],80 };81 82 if (ellipsis) {83 var questions = [84 {85 name: 'PUBLIC_DIR',86 type: 'text',87 message: 'Enter the application\'s public directory:',88 initial: params.PUBLIC_DIR,89 format: transformAs(['path']),90 validate: validateAs(['input', 'important']),91 },92 {93 name: 'CLIENT_DIR',94 type: 'text',95 message: 'Enter the application\'s client-side routing directory:',96 initial: params.CLIENT_DIR,97 format: transformAs(['path']),98 validate: validateAs(['input', 'important']),99 },100 // ------------- advanced --------------101 {102 name: '__client_advanced',103 type: 'toggle',104 message: 'Show advanced options?',105 active: 'YES',106 inactive: 'NO',107 initial: params.__client_advanced,108 },109 // ------------- advanced --------------110 {111 name: 'CLIENT_HOST_PATH',112 type: (rev, answers) => answers.__client_advanced ? 'text' : null,113 message: 'Enter the application\'s host path (in a multi-host setup):',114 initial: params.CLIENT_HOST_PATH,115 validate: validateAs(['input']),116 },117 // -----------------118 // SERVICE WORKER119 // -----------------120 {121 name: 'CREATE_WORKER',122 type: 'toggle',123 message: 'Create a Progressive Web App (PWA) Service Worker:',124 active: 'YES',125 inactive: 'NO',126 initial: params.CREATE_WORKER,127 validate: validateAs(['confirm']),128 },129 {130 name: 'WORKER_DIR',131 type: (prev, answers) => answers.CREATE_WORKER ? 'text' : null,132 message: 'Enter the application\'s worker-level routing directory:',133 initial: params.WORKER_DIR,134 format: transformAs(['path']),135 validate: validateAs(['input']),136 },137 {138 name: 'WORKER_SHOW_LIFECYCLE_LOG',139 type: (prev, answers) => answers.CREATE_WORKER ? 'toggle' : null,140 message: 'Choose whether to show logs:',141 active: 'YES',142 inactive: 'NO',143 initial: params.WORKER_SHOW_LIFECYCLE_LOG,144 },145 // ------------- advanced --------------146 {147 name: '__worker_advanced',148 type: (prev, answers) => answers.CREATE_WORKER ? 'toggle' : null,149 message: 'Show advanced Service Worker options?',150 active: 'YES',151 inactive: 'NO',152 initial: params.__worker_advanced,153 },154 // ------------- advanced --------------155 {156 name: 'WORKER_SCOPE',157 type: (prev, answers) => answers.CREATE_WORKER && answers.__worker_advanced ? 'text' : null,158 message: 'Specify the Service Worker scope:',159 initial: params.WORKER_SCOPE,160 validate: validateAs(['input']),161 },162 {163 name: 'WORKER_CACHE_NAME',164 type: (prev, answers) => answers.CREATE_WORKER && answers.__worker_advanced ? 'text' : null,165 message: 'Enter the Service Worker cache name:',166 initial: params.WORKER_CACHE_NAME,167 validate: validateAs(['input']),168 },169 {170 name: 'WORKER_FETCHING_STRATEGY',171 type: (prev, answers) => answers.CREATE_WORKER && answers.__worker_advanced ? 'select' : null,172 message: 'Select the Service Worker fetching strategy:',173 choices: choices.worker_fetching_strategy,174 initial: initialGetIndex(choices.worker_fetching_strategy, params.WORKER_FETCHING_STRATEGY),175 validate: validateAs(['input']),176 },177 {178 name: 'WORKER_CACHING_STRATEGY',179 type: (prev, answers) => answers.CREATE_WORKER && answers.__worker_advanced ? 'select' : null,180 message: 'Select the Service Worker caching strategy:',181 choices: choices.worker_cahing_strategy,182 initial: initialGetIndex(choices.worker_cahing_strategy, params.WORKER_CACHING_STRATEGY),183 validate: validateAs(['input']),184 },185 {186 name: 'WORKER_CACHING_LIST',187 type: (prev, answers) => answers.CREATE_WORKER && answers.__worker_advanced ? 'list' : null,188 message: 'Specify files to cache (comma-separated):',189 initial: (params.WORKER_CACHING_LIST || []).join(', '),190 validate: validateAs(['input']),191 },192 {193 name: 'WORKER_SKIP_WAITING',194 type: (prev, answers) => answers.CREATE_WORKER && answers.__worker_advanced ? 'toggle' : null,195 message: 'Choose whether to skip Service Worker waiting state:',196 active: 'YES',197 inactive: 'NO',198 initial: params.WORKER_SKIP_WAITING,199 },200 // ------------- messaging --------------201 {202 name: 'WORKER_SUPPORT_MESSAGING',203 type: (prev, answers) => answers.CREATE_WORKER ? 'toggle' : null,204 message: 'Support worker-clients post-messaging?',205 active: 'YES',206 inactive: 'NO',207 initial: params.WORKER_SUPPORT_MESSAGING,208 },209 // ------------- messaging --------------210 // ------------- messaging advanced --------------211 {212 name: '__messaging_advanced',213 type: (prev, answers) => answers.WORKER_SUPPORT_MESSAGING ? 'toggle' : null,214 message: 'Show advanced post-messaging options?',215 active: 'YES',216 inactive: 'NO',217 initial: params.__messaging_advanced,218 },219 // ------------- messaging advanced --------------220 {221 name: 'WORKER_MESSAGE_ROUTING_URL_PROPERTY',222 type: (prev, answers) => answers.__messaging_advanced ? 'text' : null,223 message: 'Enter the URL property name for message routing:',224 initial: params.WORKER_MESSAGE_ROUTING_URL_PROPERTY,225 validate: validateAs(['input']),226 },227 {228 name: 'WORKER_MESSAGE_SHOULD_RELAY_PROPERTY',229 type: (prev, answers) => answers.__messaging_advanced ? 'text' : null,230 message: 'Enter the property name for message relaying:',231 initial: params.WORKER_MESSAGE_SHOULD_RELAY_PROPERTY,232 validate: validateAs(['input']),233 },234 // ------------- notification --------------235 {236 name: 'WORKER_SUPPORT_NOTIFICATION',237 type: (prev, answers) => answers.CREATE_WORKER ? 'toggle' : null,238 message: 'Support push-notifications?',239 active: 'YES',240 inactive: 'NO',241 initial: params.WORKER_SUPPORT_NOTIFICATION,242 },243 // ------------- notification --------------244 {245 name: 'WORKER_PUSH_REGISTRATION_URL',246 type: (prev, answers) => answers.WORKER_SUPPORT_NOTIFICATION ? 'text' : null,247 message: 'Enter the URL for push notification subscription:',248 initial: params.WORKER_PUSH_REGISTRATION_URL,249 validate: validateAs(['input', 'important']),250 },251 {252 name: 'WORKER_PUSH_UNREGISTRATION_URL',253 type: (prev, answers) => answers.WORKER_SUPPORT_NOTIFICATION ? 'text' : null,254 message: 'Enter the URL for push notification unsubscription:',255 initial: params.WORKER_PUSH_UNREGISTRATION_URL,256 validate: validateAs(['input', 'important']),257 },258 {259 name: 'WORKER_PUSH_PUBLIC_KEY',260 type: (prev, answers) => answers.WORKER_SUPPORT_NOTIFICATION ? 'text' : null,261 message: 'Enter the Public Key for push notification subscription:',262 initial: params.WORKER_PUSH_PUBLIC_KEY,263 validate: validateAs(['input', 'important']),264 },265 // ------------- notification advanced --------------266 {267 name: '__notification_advanced',268 type: (prev, answers) => answers.WORKER_SUPPORT_NOTIFICATION ? 'toggle' : null,269 message: 'Show advanced push-notifications options?',270 active: 'YES',271 inactive: 'NO',272 initial: params.__notification_advanced,273 },274 // ------------- notification advanced --------------275 {276 name: 'WORKER_NOTIFICATION_ROUTING_URL_PROPERTY',277 type: (prev, answers) => answers.__notification_advanced ? 'text' : null,278 message: 'Enter the URL property name for notification routing:',279 initial: params.WORKER_NOTIFICATION_ROUTING_URL_PROPERTY,280 validate: validateAs(['input']),281 },282 {283 name: 'WORKER_NOTIFICATION_TARGET_URL_PROPERTY',284 type: (prev, answers) => answers.__notification_advanced ? 'text' : null,285 message: 'Enter the URL property name for notification targeting:',286 initial: params.WORKER_NOTIFICATION_TARGET_URL_PROPERTY,287 validate: validateAs(['input']),288 },289 ];290 console.log('');291 console.log(Chalk.whiteBright(`Enter parameters:`));292 _merge(params, await Promptx(questions));293 } else {294 console.log('');295 console.log(Chalk.whiteBright(`Creating a build with the following params:`));296 printArgs(params);297 }298 if (!flags['CONFIG']) {299 DotJson.write(params, _paramsFile);300 }301 return params;...

Full Screen

Full Screen

race.js

Source:race.js Github

copy

Full Screen

1// https://es6.ruanyifeng.com/#docs/promise#Promise-race2// 将多个promise封装成1个,只要其中1个完成,就会触发完成3// 只要其中有1个错误,就会触发错误4function create_worker(name) {5 return new Promise(function (resolve, reject) {6 const timeout = 100 + 500 * Math.random()7 setTimeout(() => {8 console.log(name, 'finished')9 resolve()10 }, timeout);11 })12}13const workers = [14 create_worker('w1'),15 create_worker('w2'),16 create_worker('w3'),17 create_worker('w4'),18 create_worker('w5'),19]20const p = Promise.race(workers)...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5wpt.createWorker(url, options, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11var options = {12};13wpt.runTest(url, options, function(err, data) {14 if (err) return console.error(err);15 console.log(data);16});17var wpt = require('webpagetest');18var wpt = new WebPageTest('www.webpagetest.org');19var testId = '170718_7Y_1d6f2e2c2d1e8d7d9e9d7b7f1b1f';20wpt.getTestResults(testId, function(err, data) {21 if (err) return console.error(err);22 console.log(data);23});24var wpt = require('webpagetest');25var wpt = new WebPageTest('www.webpagetest.org');26var testId = '170718_7Y_1d6f2e2c2d1e8d7d9e9d7b7f1b1f';27wpt.getTestStatus(testId, function(err, data) {28 if (err) return console.error(err);29 console.log(data);30});31var wpt = require('webpagetest');32var wpt = new WebPageTest('www.webpag

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest(options);5 if (err) return console.error(err);6 console.log('Test Created: %s', data);7});8var wpt = require('webpagetest');9var options = {10};11var wpt = new WebPageTest(options);12wpt.getLocations(function(err, data) {13 if (err) return console.error(err);14 console.log('Locations: %s', data);15});16var wpt = require('webpagetest');17var options = {18};19var wpt = new WebPageTest(options);20wpt.getTesters(function(err, data) {21 if (err) return console.error(err);22 console.log('Testers: %s', data);23});24var wpt = require('webpagetest');25var options = {26};27var wpt = new WebPageTest(options);28wpt.getTesters(function(err, data) {29 if (err) return console.error(err);30 console.log('Testers: %s', data);31});32var wpt = require('webpagetest');33var options = {34};35var wpt = new WebPageTest(options);36wpt.getTestStatus('140612_3S_2f1b8d2c7b9d9f9d7c6f8d6b7c6e1d1f', function(err, data) {37 if (err) return console.error(err);38 console.log('Test Status: %s', data);39});40var wpt = require('webpagetest');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var worker = wpt.createWorker('API_KEY');3worker.getLocations(function(err, locations) {4 console.log(locations);5});6var wpt = require('wpt');7var worker = wpt.createWorker('API_KEY');8worker.getLocations(function(err, locations) {9 console.log(locations);10});11var wpt = require('wpt');12var worker = wpt.createWorker('API_KEY');13worker.getLocations(function(err, locations) {14 console.log(locations);15});16var wpt = require('wpt');17var worker = wpt.createWorker('API_KEY');18worker.getLocations(function(err, locations) {19 console.log(locations);20});21var wpt = require('wpt');22var worker = wpt.createWorker('API_KEY');23worker.getLocations(function(err, locations) {24 console.log(locations);25});26var wpt = require('wpt');27var worker = wpt.createWorker('API_KEY');28worker.getLocations(function(err, locations) {29 console.log(locations);30});31var wpt = require('wpt');32var worker = wpt.createWorker('API_KEY');33worker.getLocations(function(err, locations) {34 console.log(locations);35});36var wpt = require('wpt');37var worker = wpt.createWorker('API_KEY');38worker.getLocations(function(err, locations) {39 console.log(locations);40});

Full Screen

Using AI Code Generation

copy

Full Screen

1worker.onmessage = function(event) {2 console.log(event.data);3}4worker.onerror = function(error) {5 console.log(error.message);6}7worker.postMessage("Hello World");8$worker->onmessage = function($event) {9 echo $event->data;10};11$worker->onerror = function($error) {12 echo $error->message;13};14$worker->postMessage("Hello World");15$worker->onmessage = function($event) {16 echo $event->data;17};18$worker->onerror = function($error) {19 echo $error->message;20};21$worker->postMessage("Hello World");22$worker->onmessage = function($event) {23 echo $event->data;24};25$worker->onerror = function($error) {26 echo $error->message;27};28$worker->postMessage("Hello World");29$worker->onmessage = function($event) {30 echo $event->data;31};32$worker->onerror = function($error) {33 echo $error->message;34};35$worker->postMessage("Hello World");

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('./wpt.js');2 if(err) {3 console.log(err);4 } else {5 console.log(data);6 }7});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Worker created: ' + data);7 }8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11self.onmessage = function(e) {12 var url = e.data;13 wpt.runTest(url, function(err, data) {14 if (err) {15 console.log('Error: ' + err);16 } else {17 console.log('Test submitted: ' + data);18 }19 });20}21var wpt = require('webpagetest');22var wpt = new WebPageTest('www.webpagetest.org');23 if (err) {24 console.log('Error: ' + err);25 } else {26 console.log('Worker created: ' + data);27 }28});29var wpt = require('webpagetest');30var wpt = new WebPageTest('www.webpagetest.org');31self.onmessage = function(e) {32 var url = e.data;33 wpt.runTest(url, function(err, data) {34 if (err) {35 console.log('Error: ' + err);36 } else {37 console.log('Test submitted: ' + data);38 }39 });40}

Full Screen

Using AI Code Generation

copy

Full Screen

1var worker = wpt.create_worker('testworker.js');2worker.postMessage('Hello from main thread');3worker.onmessage = function(e) {4 console.log(e.data);5}6onmessage = function(e) {7 console.log(e.data);8 postMessage('Hello from worker thread');9}

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.5f5a5e2d0e1b2c2f5f5a5e2d0e1b2c2f');3 console.log(data);4});5var wpt = require('webpagetest');6var wpt = new WebPageTest('www.webpagetest.org', 'A.5f5a5e2d0e1b2c2f5f5a5e2d0e1b2c2f');7 console.log(data);8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org', 'A.5f5a5e2d0e1b2c2f5f5a5e2d0e1b2c2f');11 console.log(data);12});13var wpt = require('webpagetest');14var wpt = new WebPageTest('www.webpagetest.org', 'A.5f5a5e2d0e1b2c2f5f5a5e2d0e1b2c2f');15 console.log(data);16});17var wpt = require('webpagetest');18var wpt = new WebPageTest('www.webpagetest.org', 'A.5f5a5e2d0e1b2c2f5f5a5e2d0e1b2c2f');19wpt.get_testers(function(err, data) {20 console.log(data);21});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful