-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] ReadableStream @@asyncIterator tests #13362
Closed
devsnek
wants to merge
4
commits into
web-platform-tests:master
from
devsnek:readablestream-asynciterator
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>async-iterator.js browser context wrapper file</title> | ||
|
||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
|
||
<script src="../resources/rs-utils.js"></script> | ||
<script src="../resources/recording-streams.js"></script> | ||
|
||
<script src="async-iterator.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
'use strict'; | ||
|
||
if (self.importScripts) { | ||
self.importScripts('../resources/rs-utils.js'); | ||
self.importScripts('/resources/testharness.js'); | ||
self.importScripts('../resources/recording-streams.js'); | ||
} | ||
|
||
test(() => { | ||
assert_equals(ReadableStream.prototype[Symbol.asyncIterator], ReadableStream.prototype.getIterator); | ||
}, '@@asyncIterator() method is === to getIterator() method'); | ||
|
||
promise_test(async () => { | ||
const s = new ReadableStream({ | ||
start(c) { | ||
c.enqueue(1); | ||
c.enqueue(2); | ||
c.enqueue(3); | ||
c.close(); | ||
}, | ||
}); | ||
|
||
const chunks = []; | ||
for await (const chunk of s) { | ||
chunks.push(chunk); | ||
} | ||
assert_array_equals(chunks, [1, 2, 3]); | ||
}, 'async iterator push source'); | ||
|
||
promise_test(async () => { | ||
let i = 1; | ||
const s = new ReadableStream({ | ||
pull(c) { | ||
c.enqueue(i); | ||
if (i >= 3) { | ||
c.close(); | ||
} | ||
i += 1; | ||
}, | ||
}); | ||
|
||
const chunks = []; | ||
for await (const chunk of s) { | ||
chunks.push(chunk); | ||
} | ||
assert_array_equals(chunks, [1, 2, 3]); | ||
}, 'async iterator pull source'); | ||
|
||
promise_test(async () => { | ||
const s = new ReadableStream({ | ||
start(c) { | ||
c.error('e'); | ||
}, | ||
}); | ||
|
||
try { | ||
for await (const chunk of s) {} | ||
assert_unreached(); | ||
} catch (e) { | ||
assert_equals(e, 'e'); | ||
} | ||
}, 'Async-iterating an errored stream throws'); | ||
|
||
promise_test(async () => { | ||
const s = new ReadableStream({ | ||
start(c) { | ||
c.close(); | ||
} | ||
}); | ||
|
||
for await (const chunk of s) { | ||
assert_unreached(); | ||
} | ||
}, 'Async-iterating a closed stream never executes the loop body, but works fine'); | ||
|
||
promise_test(async () => { | ||
|
||
}, 'Async-iterating an empty but not closed/errored stream never executes the loop body and stalls the async function'); | ||
|
||
promise_test(async () => { | ||
const test = async (type, preventCancel) => { | ||
const s = recordingReadableStream({ | ||
start(c) { | ||
c.enqueue(0); | ||
} | ||
}); | ||
|
||
await (async () => { | ||
for await (const c of s.getIterator({ preventCancel })) { | ||
if (type === 'throw') { | ||
throw new Error(); | ||
} else if (type === 'break') { | ||
break; | ||
} else if (type === 'return') { | ||
return; | ||
} | ||
} | ||
})().catch(() => 0); | ||
|
||
if (preventCancel) { | ||
assert_array_equals(s.events, ['pull'], `cancel() should not be called when type = '${type}' and preventCancel is true`); | ||
} else { | ||
assert_array_equals(s.events, ['pull', 'cancel', undefined], `cancel() should be called when type = '${type}' and preventCancel is false`); | ||
} | ||
}; | ||
|
||
for (const t of ['throw', 'break', 'return']) { | ||
await test(t, true); | ||
await test(t, false); | ||
} | ||
}, 'cancellation behavior'); | ||
|
||
promise_test(async () => { | ||
{ | ||
const s = new ReadableStream(); | ||
const it = s[Symbol.asyncIterator](); | ||
await it.return(); | ||
try { | ||
await it.return(); | ||
assert_unreached(); | ||
} catch (e) {} | ||
} | ||
|
||
{ | ||
const s = new ReadableStream({ | ||
start(c) { | ||
c.enqueue(0); | ||
c.close(); | ||
}, | ||
}); | ||
const it = s[Symbol.asyncIterator](); | ||
const next = await it.next(); | ||
assert_equals(Object.getPrototypeOf(next), Object.prototype); | ||
assert_array_equals(Object.keys(next), ['value', 'done']); | ||
} | ||
}, 'manual manipulation'); | ||
|
||
test(() => { | ||
const s = new ReadableStream({ | ||
start(c) { | ||
c.enqueue(0); | ||
c.close(); | ||
}, | ||
}); | ||
const it = s.getIterator(); | ||
try { | ||
s.getIterator(); | ||
assert_unreached(); | ||
} catch (e) {} | ||
}, 'getIterator throws if there\'s already a lock'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this immediately-executed-async-function structure hard to understand. Is there a reason why a simple try ... catch wouldn't work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
testing
return
. I suppose it can be reworked a little bit though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably if you just split the creation of the function from the execution of the function it will be enough to make it less confusing.