| 1 |
// META: global=window,worker,shadowrealm |
| 2 |
// META: script=../resources/test-utils.js |
| 3 |
// META: script=/common/gc.js |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
promise_test(async () => { |
| 7 |
|
| 8 |
let controller; |
| 9 |
new ReadableStream({ |
| 10 |
start(c) { |
| 11 |
controller = c; |
| 12 |
} |
| 13 |
}); |
| 14 |
|
| 15 |
await garbageCollect(); |
| 16 |
|
| 17 |
return delay(50).then(() => { |
| 18 |
controller.close(); |
| 19 |
assert_throws_js(TypeError, () => controller.close(), 'close should throw a TypeError the second time'); |
| 20 |
controller.error(); |
| 21 |
}); |
| 22 |
|
| 23 |
}, 'ReadableStreamController methods should continue working properly when scripts lose their reference to the ' + |
| 24 |
'readable stream'); |
| 25 |
|
| 26 |
promise_test(async () => { |
| 27 |
|
| 28 |
let controller; |
| 29 |
|
| 30 |
const closedPromise = new ReadableStream({ |
| 31 |
start(c) { |
| 32 |
controller = c; |
| 33 |
} |
| 34 |
}).getReader().closed; |
| 35 |
|
| 36 |
await garbageCollect(); |
| 37 |
|
| 38 |
return delay(50).then(() => controller.close()).then(() => closedPromise); |
| 39 |
|
| 40 |
}, 'ReadableStream closed promise should fulfill even if the stream and reader JS references are lost'); |
| 41 |
|
| 42 |
promise_test(async t => { |
| 43 |
|
| 44 |
const theError = new Error('boo'); |
| 45 |
let controller; |
| 46 |
|
| 47 |
const closedPromise = new ReadableStream({ |
| 48 |
start(c) { |
| 49 |
controller = c; |
| 50 |
} |
| 51 |
}).getReader().closed; |
| 52 |
|
| 53 |
await garbageCollect(); |
| 54 |
|
| 55 |
return delay(50).then(() => controller.error(theError)) |
| 56 |
.then(() => promise_rejects_exactly(t, theError, closedPromise)); |
| 57 |
|
| 58 |
}, 'ReadableStream closed promise should reject even if stream and reader JS references are lost'); |
| 59 |
|
| 60 |
promise_test(async () => { |
| 61 |
|
| 62 |
const rs = new ReadableStream({}); |
| 63 |
|
| 64 |
rs.getReader(); |
| 65 |
|
| 66 |
await garbageCollect(); |
| 67 |
|
| 68 |
return delay(50).then(() => assert_throws_js(TypeError, () => rs.getReader(), |
| 69 |
'old reader should still be locking the stream even after garbage collection')); |
| 70 |
|
| 71 |
}, 'Garbage-collecting a ReadableStreamDefaultReader should not unlock its stream'); |
| 72 |
|
| 73 |
promise_test(async () => { |
| 74 |
|
| 75 |
const promise = (() => { |
| 76 |
const rs = new ReadableStream({ |
| 77 |
pull(controller) { |
| 78 |
controller.enqueue('words'); |
| 79 |
} |
| 80 |
}); |
| 81 |
const reader = rs.getReader(); |
| 82 |
return reader.read(); |
| 83 |
})(); |
| 84 |
await garbageCollect(); |
| 85 |
const {value, done} = await promise; |
| 86 |
// If we get here, the test passed. |
| 87 |
assert_equals(value, 'words', 'value should be words'); |
| 88 |
assert_false(done, 'we should not be done'); |
| 89 |
|
| 90 |
}, 'A ReadableStream and its reader should not be garbage collected while there is a read promise pending'); |