File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Open.ChannelExtensions.Tests Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -106,5 +106,50 @@ await c.Reader
106106 } ) ;
107107
108108 }
109+
110+
111+ [ Fact ]
112+ public static async Task ForceBatchTest ( )
113+ {
114+ var c = Channel . CreateUnbounded < int > ( new UnboundedChannelOptions { SingleReader = false , SingleWriter = false } ) ;
115+ #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
116+ Task . Run ( async ( ) =>
117+ {
118+ await Task . Delay ( 1000 ) ;
119+ c . Writer . TryWrite ( 1 ) ;
120+ c . Writer . TryWrite ( 2 ) ;
121+ c . Writer . TryWrite ( 3 ) ;
122+ c . Writer . TryWrite ( 4 ) ;
123+ c . Writer . TryWrite ( 5 ) ;
124+ } ) ;
125+ #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
126+
127+ using var tokenSource = new CancellationTokenSource ( ) ;
128+ var token = tokenSource . Token ;
129+ var reader = c . Reader . Batch ( 3 ) ;
130+ Assert . Equal ( 2 , await reader . ReadAllAsync ( async ( batch , i ) =>
131+ {
132+ switch ( i )
133+ {
134+ case 0 :
135+ Assert . Equal ( 1 , batch [ 0 ] ) ;
136+ Assert . Equal ( 2 , batch [ 1 ] ) ;
137+ Assert . Equal ( 3 , batch [ 2 ] ) ;
138+ await Task . Delay ( 500 ) ;
139+ reader . ForceBatch ( ) ;
140+ break ;
141+ case 1 :
142+ Assert . Equal ( 2 , batch . Count ) ;
143+ Assert . Equal ( 4 , batch [ 0 ] ) ;
144+ Assert . Equal ( 5 , batch [ 1 ] ) ;
145+ c . Writer . Complete ( ) ;
146+ break ;
147+ default :
148+ throw new Exception ( "Shouldn't arrive here." ) ;
149+ }
150+ await Task . Delay ( 500 ) ;
151+ } ) ) ;
152+
153+ }
109154 }
110155}
You can’t perform that action at this time.
0 commit comments