summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scheduler/tentative/yield/yield-priority-posttask.any.js
blob: 0700094dcf3679d18099204705c885005e070f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';

// Posts a postTask task with `yieldyTaskParams` that yields 3 times using
// `yieldParams`, then posts 2 tasks of each priority, in descending order.
//
// Returns {tasks, ids} where `tasks` is an array of promises returned by
// postTask and `ids` is an array of task ids appended to by the scheduled
// tasks.
function postTestTasks(yieldyTaskParams, yieldParams) {
  const tasks = [];
  const ids = [];

  tasks.push(scheduler.postTask(async () => {
    ids.push('y0');
    for (let i = 1; i < 4; i++) {
      await scheduler.yield(yieldParams);
      ids.push('y' + i);
    }
  }, yieldyTaskParams));

  tasks.push(
      scheduler.postTask(() => {ids.push('ub1')}, {priority: 'user-blocking'}));
  tasks.push(
      scheduler.postTask(() => {ids.push('ub2')}, {priority: 'user-blocking'}));
  tasks.push(
      scheduler.postTask(() => {ids.push('uv1')}, {priority: 'user-visible'}));
  tasks.push(
      scheduler.postTask(() => {ids.push('uv2')}, {priority: 'user-visible'}));
  tasks.push(
      scheduler.postTask(() => {ids.push('bg1')}, {priority: 'background'}));
  tasks.push(
      scheduler.postTask(() => {ids.push('bg2')}, {priority: 'background'}));
  return {tasks, ids};
}

// Expected task orders for `postTestTasks` tasks.
const taskOrders = {
  'user-blocking': 'y0,y1,y2,y3,ub1,ub2,uv1,uv2,bg1,bg2',
  'user-visible': 'ub1,ub2,y0,y1,y2,y3,uv1,uv2,bg1,bg2',
  'background': 'ub1,ub2,uv1,uv2,y0,y1,y2,y3,bg1,bg2',
};

const priorityConfigs = [
  {options: {}, expected: taskOrders['user-visible']},
  {options: {priority: 'user-visible'}, expected: taskOrders['user-visible']},
  {options: {priority: 'user-blocking'}, expected: taskOrders['user-blocking']},
  {options: {priority: 'background'}, expected: taskOrders['background']},
];

const fixedPrioritySignals = {
  'user-blocking': (new TaskController({priority: 'user-blocking'})).signal,
  'user-visible': (new TaskController({priority: 'user-visible'})).signal,
  'background': (new TaskController({priority: 'background'})).signal,
};

const signalConfigs = [
  {
    options: {signal: fixedPrioritySignals['user-visible']},
    expected: taskOrders['user-visible']
  },
  {
    options: {signal: fixedPrioritySignals['user-blocking']},
    expected: taskOrders['user-blocking']
  },
  {
    options: {signal: fixedPrioritySignals['background']},
    expected: taskOrders['background']
  },
];

promise_test(async t => {
  for (const config of priorityConfigs) {
    const {tasks, ids} = postTestTasks(config.options, config.options);
    await Promise.all(tasks);
    assert_equals(ids.join(), config.expected);
  }
}, 'yield() with postTask tasks (priority option)');

promise_test(async t => {
  for (const config of signalConfigs) {
    const {tasks, ids} = postTestTasks(config.options, config.options);
    await Promise.all(tasks);
    assert_equals(ids.join(), config.expected);
  }
}, 'yield() with postTask tasks (signal option)');

promise_test(async t => {
  for (const config of priorityConfigs) {
    const {tasks, ids} =
        postTestTasks(config.options, {priority: 'inherit'});
    await Promise.all(tasks);
    assert_equals(ids.join(), config.expected);
  }
}, 'yield() with postTask tasks (inherit priority)');

promise_test(async t => {
  for (const config of signalConfigs) {
    const {tasks, ids} =
        postTestTasks(config.options, {signal: 'inherit'});
    await Promise.all(tasks);
    assert_equals(ids.join(), config.expected);
  }
}, 'yield() with postTask tasks (inherit signal)');

promise_test(async t => {
  const expected = 'y0,ub1,ub2,uv1,uv2,y1,y2,y3,bg1,bg2';
  const {tasks, ids} = postTestTasks(
      {priority: 'user-blocking'}, {priority: 'background'});
  await Promise.all(tasks);
  assert_equals(ids.join(), expected);
}, 'yield() with different priority from task (priority)');

promise_test(async t => {
  const expected = 'y0,ub1,ub2,uv1,uv2,y1,y2,y3,bg1,bg2';
  const bgSignal = (new TaskController({priority: 'background'})).signal;
  const {tasks, ids} =
      postTestTasks({priority: 'user-blocking'}, {signal: bgSignal});
  await Promise.all(tasks);
  assert_equals(ids.join(), expected);
}, 'yield() with different priority from task (signal)');

promise_test(async t => {
  const bgSignal = (new TaskController({priority: 'background'})).signal;
  const {tasks, ids} = postTestTasks(
      {priority: 'user-blocking'},
      {signal: bgSignal, priority: 'user-blocking'});
  await Promise.all(tasks);
  assert_equals(ids.join(), taskOrders['user-blocking']);
}, 'yield() priority overrides signal');

promise_test(async t => {
  const ids = [];

  const controller = new TaskController();
  const signal = controller.signal;

  await scheduler.postTask(async () => {
    ids.push('y0');

    const subtasks = [];
    subtasks.push(scheduler.postTask(() => { ids.push('uv1'); }));
    subtasks.push(scheduler.postTask(() => { ids.push('uv2'); }));

    // 'user-visible' continuations.
    await scheduler.yield({signal: 'inherit'});
    ids.push('y1');
    await scheduler.yield({signal: 'inherit'});
    ids.push('y2');

    controller.setPriority('background');

    // 'background' continuations.
    await scheduler.yield({signal: 'inherit'});
    ids.push('y3');
    await scheduler.yield({signal: 'inherit'});
    ids.push('y4');

    await Promise.all(subtasks);
  }, {signal});

  assert_equals(ids.join(), 'y0,y1,y2,uv1,uv2,y3,y4');
}, 'yield() with TaskSignal has dynamic priority')

promise_test(async t => {
  const ids = [];

  await scheduler.postTask(async () => {
    ids.push('y0');

    const subtasks = [];
    subtasks.push(scheduler.postTask(() => { ids.push('ub1'); }, {priority: 'user-blocking'}));
    subtasks.push(scheduler.postTask(() => { ids.push('uv1'); }));

    // Ignore inherited signal (user-visible continuations).
    await scheduler.yield();
    ids.push('y1');
    await scheduler.yield();
    ids.push('y2');

    subtasks.push(scheduler.postTask(() => { ids.push('ub2'); }, {priority: 'user-blocking'}));
    subtasks.push(scheduler.postTask(() => { ids.push('uv2'); }));

    // Now use inherited signal (user-blocking continuations).
    await scheduler.yield({signal: 'inherit'});
    ids.push('y3');
    await scheduler.yield({signal: 'inherit'});
    ids.push('y4');

    await Promise.all(subtasks);
  }, {priority: 'user-blocking'});

  assert_equals(ids.join(), 'y0,ub1,y1,y2,y3,y4,ub2,uv1,uv2');
}, 'yield() mixed inheritance and default')