11using Grpc . Core ;
22using Moq ;
33using Xunit ;
4- using Xunit . Abstractions ;
54using Ydb . Issue ;
65using Ydb . Sdk . Services . Topic ;
76using Ydb . Sdk . Services . Topic . Writer ;
@@ -15,13 +14,11 @@ namespace Ydb.Sdk.Tests.Topic;
1514
1615public class WriterUnitTests
1716{
18- private readonly ITestOutputHelper _testOutputHelper ;
1917 private readonly Mock < IDriver > _mockIDriver = new ( ) ;
2018 private readonly Mock < WriterStream > _mockStream = new ( ) ;
2119
22- public WriterUnitTests ( ITestOutputHelper testOutputHelper )
20+ public WriterUnitTests ( )
2321 {
24- _testOutputHelper = testOutputHelper ;
2522 _mockIDriver . Setup ( driver => driver . BidirectionalStreamCall (
2623 It . IsAny < Method < FromClient , StreamWriteMessage . Types . FromServer > > ( ) ,
2724 It . IsAny < GrpcRequestSettings > ( ) )
@@ -317,94 +314,6 @@ public async Task Initialize_WhenNotSupportedCodec_ThrowWriterExceptionOnWriteAs
317314 _mockStream . Verify ( stream => stream . MoveNextAsync ( ) , Times . Once ) ;
318315 }
319316
320- [ Fact ]
321- public async Task WriteAsyncStress_WhenBufferIsOverflow_ThrowWriterExceptionOnBufferOverflow ( )
322- {
323- const int countBatchSendingSize = 1000 ;
324- const int batchTasksSize = 100 ;
325- const int bufferSize = 100 ;
326- const int messageSize = sizeof ( int ) ;
327-
328- Assert . True ( batchTasksSize > bufferSize / 4 ) ;
329- Assert . True ( bufferSize % 4 == 0 ) ;
330-
331- var taskSource = new TaskCompletionSource < bool > ( ) ;
332- _mockStream . Setup ( stream => stream . Write ( It . IsAny < FromClient > ( ) ) )
333- . Returns ( Task . CompletedTask ) ;
334- var mockNextAsync = _mockStream . SetupSequence ( stream => stream . MoveNextAsync ( ) )
335- . Returns ( new ValueTask < bool > ( true ) )
336- . Returns ( new ValueTask < bool > ( taskSource . Task ) ) ;
337- var sequentialResult = _mockStream . SetupSequence ( stream => stream . Current )
338- . Returns ( new StreamWriteMessage . Types . FromServer
339- {
340- InitResponse = new StreamWriteMessage . Types . InitResponse
341- { LastSeqNo = 0 , PartitionId = 1 , SessionId = "SessionId" } ,
342- Status = StatusIds . Types . StatusCode . Success
343- } ) ;
344- using var writer = new WriterBuilder < int > ( _mockIDriver . Object , "/topic" )
345- {
346- ProducerId = "producerId" ,
347- BufferMaxSize = bufferSize /* bytes */ ,
348- BufferOverflowRetryTimeoutMs = 1_000
349- } . Build ( ) ;
350-
351- for ( var attempt = 0 ; attempt < countBatchSendingSize ; attempt ++ )
352- {
353- _testOutputHelper . WriteLine ( $ "Processing attempt { attempt } ") ;
354- var cts = new CancellationTokenSource ( ) ;
355-
356- var tasks = new List < Task < WriteResult > > ( ) ;
357- var serverAck = new StreamWriteMessage . Types . FromServer
358- {
359- WriteResponse = new StreamWriteMessage . Types . WriteResponse { PartitionId = 1 } ,
360- Status = StatusIds . Types . StatusCode . Success
361- } ;
362- for ( var i = 0 ; i < batchTasksSize ; i ++ )
363- {
364- tasks . Add ( writer . WriteAsync ( 100 , cts . Token ) ) ;
365- serverAck . WriteResponse . Acks . Add ( new StreamWriteMessage . Types . WriteResponse . Types . WriteAck
366- {
367- SeqNo = bufferSize / messageSize * attempt + i + 1 ,
368- Written = new StreamWriteMessage . Types . WriteResponse . Types . WriteAck . Types . Written
369- { Offset = i * messageSize + bufferSize * attempt }
370- } ) ;
371- }
372-
373- sequentialResult . Returns ( ( ) =>
374- {
375- // ReSharper disable once AccessToModifiedClosure
376- Volatile . Write ( ref taskSource , new TaskCompletionSource < bool > ( ) ) ;
377- mockNextAsync . Returns ( ( ) =>
378- {
379- cts . Cancel ( ) ;
380- return new ValueTask < bool > ( Volatile . Read ( ref taskSource ) . Task ) ;
381- } ) ;
382- return serverAck ;
383- } ) ;
384- taskSource . SetResult ( true ) ;
385-
386- var countSuccess = 0 ;
387- var countErrors = 0 ;
388- foreach ( var task in tasks )
389- {
390- try
391- {
392- var res = await task ;
393- countSuccess ++ ;
394- Assert . Equal ( PersistenceStatus . Written , res . Status ) ;
395- }
396- catch ( WriterException e )
397- {
398- countErrors ++ ;
399- Assert . Equal ( "Buffer overflow" , e . Message ) ;
400- }
401- }
402-
403- Assert . Equal ( bufferSize / messageSize , countSuccess ) ;
404- Assert . Equal ( batchTasksSize - bufferSize / messageSize , countErrors ) ;
405- }
406- }
407-
408317 /*
409318 * Performed invocations:
410319
0 commit comments