@@ -16,9 +16,11 @@ if (!hasQuic) {
1616
1717const { listen, connect } = await import ( '../common/quic.mjs' ) ;
1818const { bytes } = await import ( 'stream/iter' ) ;
19+ const { setTimeout : sleep } = await import ( 'timers/promises' ) ;
1920
2021const encoder = new TextEncoder ( ) ;
2122const allDone = Promise . withResolvers ( ) ;
23+ const twoDone = Promise . withResolvers ( ) ;
2224let serverStreamCount = 0 ;
2325
2426// Server allows only 1 bidi stream at a time.
@@ -27,11 +29,17 @@ const serverEndpoint = await listen(mustCall((serverSession) => {
2729 await bytes ( stream ) ;
2830 stream . writer . endSync ( ) ;
2931 await stream . closed ;
30- if ( ++ serverStreamCount === 2 ) {
31- serverSession . close ( ) ;
32+ ++ serverStreamCount ;
33+ if ( serverStreamCount === 2 ) {
34+ twoDone . resolve ( ) ;
35+ }
36+ if ( serverStreamCount === 3 ) {
3237 allDone . resolve ( ) ;
3338 }
34- } , 2 ) ;
39+ if ( serverStreamCount === 4 ) {
40+ serverSession . close ( ) ;
41+ }
42+ } , 3 ) ;
3543} ) , {
3644 transportParams : { initialMaxStreamsBidi : 1 } ,
3745} ) ;
@@ -51,27 +59,45 @@ s1.opened.then(() => {
5159 opened ++ ;
5260} ) ;
5361
54- // Second stream is created but queued as pending because the
55- // server only allows 1 concurrent bidi stream.
56- const s2 = await clientSession . createBidirectionalStream ( {
57- body : encoder . encode ( 'stream 2' ) ,
62+ let s2 ;
63+ await assert . rejects (
64+ async ( ) => {
65+ // Second stream should not open, but throw.
66+ s2 = await clientSession . createBidirectionalStream ( {
67+ body : encoder . encode ( 'stream 2a' ) ,
68+ waitUntilAvailable : false ,
69+ } ) ;
70+ // eslint-disable-next-line node-core/must-call-assert
71+ s2 . opened . then ( ( ) => {
72+ opened ++ ;
73+ } ) ;
74+ } ,
75+ {
76+ name : 'Error' ,
77+ message : 'No new stream available within flow control' ,
78+ } ,
79+ ) ;
80+ // Ok try again a second second stream, that patiently waits
81+ s2 = await clientSession . createBidirectionalStream ( {
82+ body : encoder . encode ( 'stream 2b' )
5883} ) ;
59-
6084// eslint-disable-next-line node-core/must-call-assert
6185s2 . opened . then ( ( ) => {
6286 opened ++ ;
6387} ) ;
6488
89+
6590// Third stream is created but queued as pending because the
6691// server only allows 1 concurrent bidi stream.
6792const s3 = await clientSession . createBidirectionalStream ( {
6893 body : encoder . encode ( 'stream 3' ) ,
6994} ) ;
7095
7196
72- // s2 should be pending until s1 closes and the server grants
97+ // s2 and s3 should be pending until s1 closes and the server grants
7398// more stream credits.
7499assert . strictEqual ( s2 . pending , true ) ;
100+ assert . strictEqual ( s3 . pending , true ) ;
75101assert . strictEqual ( opened , 1 ) ;
76102
77103await dump ( s1 ) ;
@@ -82,15 +108,20 @@ s3.destroy(err);
82108
83109await Promise . all ( [ assert . rejects ( s3 . opened , err ) , assert . rejects ( s3 . closed , err ) ] ) ;
84110
85-
86- // After s1 closes, the server sends MAX_STREAMS which opens s2.
111+ // After s1 closes, the server sends MAX_STREAMS which opens s3.
87112// Wait for the server to receive both streams.
88- await allDone . promise ;
89- assert . strictEqual ( opened , 2 ) ;
90-
113+ await twoDone . promise ;
91114// s2 should no longer be pending.
92115await dump ( s2 ) ;
93116await s2 . closed ;
94117
118+ await sleep ( 10 ) ; // We wait a bit, as we do not have a callback exposed to js
119+ // fourth stream should open immediately and not throw
120+ const s4 = await clientSession . createBidirectionalStream ( {
121+ body : encoder . encode ( 'stream 4' ) ,
122+ waitUntilAvailable : false
123+ } ) ;
124+ await Promise . all ( [ s4 . closed , allDone . promise ] ) ;
125+
95126await clientSession . close ( ) ;
96127await serverEndpoint . close ( ) ;
0 commit comments