Arnaud Mandy | 8b1624b | 2022-09-22 11:58:45 | [diff] [blame] | 1 | // META: script=/resources/test-only-api.js |
| 2 | // META: script=resources/pressure-helpers.js |
Wei4 Wang | dd8db51dd | 2023-04-14 00:56:37 | [diff] [blame] | 3 | // META: global=window,dedicatedworker,sharedworker |
Arnaud Mandy | 8b1624b | 2022-09-22 11:58:45 | [diff] [blame] | 4 | |
| 5 | 'use strict'; |
| 6 | |
| 7 | pressure_test(async (t, mockPressureService) => { |
Arnaud Mandy | 1be0282 | 2022-09-22 13:40:23 | [diff] [blame] | 8 | const pressureChanges = await new Promise(async resolve => { |
| 9 | const observer_changes = []; |
Arnaud Mandy | 8b1624b | 2022-09-22 11:58:45 | [diff] [blame] | 10 | let n = 0; |
Arnaud Mandy | 1be0282 | 2022-09-22 13:40:23 | [diff] [blame] | 11 | const observer = new PressureObserver(changes => { |
| 12 | observer_changes.push(changes); |
Arnaud Mandy | 8b1624b | 2022-09-22 11:58:45 | [diff] [blame] | 13 | if (++n === 2) |
Arnaud Mandy | 1be0282 | 2022-09-22 13:40:23 | [diff] [blame] | 14 | resolve(observer_changes); |
Wei4 Wang | 1c72bb6 | 2023-01-09 01:44:09 | [diff] [blame] | 15 | }, {sampleRate: 5.0}); |
Arnaud Mandy | 9369bc1 | 2022-09-27 12:54:36 | [diff] [blame] | 16 | observer.observe('cpu'); |
| 17 | const updatesDelivered = mockPressureService.updatesDelivered(); |
Wei4 Wang | b5bd6a69 | 2023-04-13 01:13:45 | [diff] [blame] | 18 | mockPressureService.setPressureUpdate('cpu', 'critical'); |
Wei4 Wang | 1c72bb6 | 2023-01-09 01:44:09 | [diff] [blame] | 19 | mockPressureService.startPlatformCollector(/*sampleRate*/ 5.0); |
Arnaud Mandy | 6924d07 | 2022-10-03 11:04:08 | [diff] [blame] | 20 | // Deliver 2 updates. |
Arnaud Mandy | 9369bc1 | 2022-09-27 12:54:36 | [diff] [blame] | 21 | await t.step_wait( |
Arnaud Mandy | 6924d07 | 2022-10-03 11:04:08 | [diff] [blame] | 22 | () => mockPressureService.updatesDelivered() >= (updatesDelivered + 2), |
Arnaud Mandy | 9369bc1 | 2022-09-27 12:54:36 | [diff] [blame] | 23 | 'Wait for more than one update to be delivered to the observer'); |
Wei4 Wang | b5bd6a69 | 2023-04-13 01:13:45 | [diff] [blame] | 24 | mockPressureService.setPressureUpdate('cpu', 'nominal'); |
Arnaud Mandy | 9369bc1 | 2022-09-27 12:54:36 | [diff] [blame] | 25 | // Deliver more updates, |resolve()| will be called when the new pressure |
| 26 | // state reaches PressureObserver and its callback is invoked |
| 27 | // for the second time. |
Arnaud Mandy | 8b1624b | 2022-09-22 11:58:45 | [diff] [blame] | 28 | }); |
Arnaud Mandy | 1be0282 | 2022-09-22 13:40:23 | [diff] [blame] | 29 | assert_equals(pressureChanges.length, 2); |
| 30 | assert_equals(pressureChanges[0][0].state, 'critical'); |
| 31 | assert_equals(pressureChanges[1][0].state, 'nominal'); |
Arnaud Mandy | 9369bc1 | 2022-09-27 12:54:36 | [diff] [blame] | 32 | }, 'Changes that fail the "has change in data" test are discarded.'); |