yield-priority-posttask.any.js (3687B)
1 'use strict'; 2 3 // Posts a postTask task with `yieldyTaskParams` that yields 3 times using 4 // `yieldParams`, then posts 2 tasks of each priority, in descending order. 5 // 6 // Returns {tasks, ids} where `tasks` is an array of promises returned by 7 // postTask and `ids` is an array of task ids appended to by the scheduled 8 // tasks. 9 function postTestTasks(yieldyTaskParams) { 10 const tasks = []; 11 const ids = []; 12 13 tasks.push(scheduler.postTask(async () => { 14 ids.push('y0'); 15 for (let i = 1; i < 4; i++) { 16 await scheduler.yield(); 17 ids.push('y' + i); 18 } 19 }, yieldyTaskParams)); 20 21 tasks.push( 22 scheduler.postTask(() => {ids.push('ub1')}, {priority: 'user-blocking'})); 23 tasks.push( 24 scheduler.postTask(() => {ids.push('ub2')}, {priority: 'user-blocking'})); 25 tasks.push( 26 scheduler.postTask(() => {ids.push('uv1')}, {priority: 'user-visible'})); 27 tasks.push( 28 scheduler.postTask(() => {ids.push('uv2')}, {priority: 'user-visible'})); 29 tasks.push( 30 scheduler.postTask(() => {ids.push('bg1')}, {priority: 'background'})); 31 tasks.push( 32 scheduler.postTask(() => {ids.push('bg2')}, {priority: 'background'})); 33 return {tasks, ids}; 34 } 35 36 // Expected task orders for `postTestTasks` tasks. 37 const taskOrders = { 38 'user-blocking': 'y0,y1,y2,y3,ub1,ub2,uv1,uv2,bg1,bg2', 39 'user-visible': 'ub1,ub2,y0,y1,y2,y3,uv1,uv2,bg1,bg2', 40 'background': 'ub1,ub2,uv1,uv2,y0,y1,y2,y3,bg1,bg2', 41 }; 42 43 const priorityConfigs = [ 44 {options: {}, expected: taskOrders['user-visible']}, 45 {options: {priority: 'user-visible'}, expected: taskOrders['user-visible']}, 46 {options: {priority: 'user-blocking'}, expected: taskOrders['user-blocking']}, 47 {options: {priority: 'background'}, expected: taskOrders['background']}, 48 ]; 49 50 const fixedPrioritySignals = { 51 'user-blocking': (new TaskController({priority: 'user-blocking'})).signal, 52 'user-visible': (new TaskController({priority: 'user-visible'})).signal, 53 'background': (new TaskController({priority: 'background'})).signal, 54 }; 55 56 const signalConfigs = [ 57 { 58 options: {signal: fixedPrioritySignals['user-visible']}, 59 expected: taskOrders['user-visible'] 60 }, 61 { 62 options: {signal: fixedPrioritySignals['user-blocking']}, 63 expected: taskOrders['user-blocking'] 64 }, 65 { 66 options: {signal: fixedPrioritySignals['background']}, 67 expected: taskOrders['background'] 68 }, 69 ]; 70 71 promise_test(async t => { 72 for (const config of priorityConfigs) { 73 const {tasks, ids} = postTestTasks(config.options); 74 await Promise.all(tasks); 75 assert_equals(ids.join(), config.expected); 76 } 77 }, 'yield() with postTask tasks (priority)'); 78 79 promise_test(async t => { 80 for (const config of signalConfigs) { 81 const {tasks, ids} = postTestTasks(config.options); 82 await Promise.all(tasks); 83 assert_equals(ids.join(), config.expected); 84 } 85 }, 'yield() with postTask tasks (signal)'); 86 87 promise_test(async t => { 88 const ids = []; 89 90 const controller = new TaskController(); 91 const signal = controller.signal; 92 93 await scheduler.postTask(async () => { 94 ids.push('y0'); 95 96 const subtasks = []; 97 subtasks.push(scheduler.postTask(() => { ids.push('uv1'); })); 98 subtasks.push(scheduler.postTask(() => { ids.push('uv2'); })); 99 100 // 'user-visible' continuations. 101 await scheduler.yield(); 102 ids.push('y1'); 103 await scheduler.yield(); 104 ids.push('y2'); 105 106 controller.setPriority('background'); 107 108 // 'background' continuations. 109 await scheduler.yield(); 110 ids.push('y3'); 111 await scheduler.yield(); 112 ids.push('y4'); 113 114 await Promise.all(subtasks); 115 }, {signal}); 116 117 assert_equals(ids.join(), 'y0,y1,y2,uv1,uv2,y3,y4'); 118 }, 'yield() with TaskSignal has dynamic priority')