public async Task ValidatesCongestionControlAsync()
        {
            SemaphoreSlim      newLimiter         = new SemaphoreSlim(1, defaultMaxDegreeOfConcurrency);
            BatchAsyncStreamer batchAsyncStreamer = new BatchAsyncStreamer(2, MaxBatchByteSize, this.TimerWheel, newLimiter, defaultMaxDegreeOfConcurrency, MockCosmosUtil.Serializer, this.Executor, this.Retrier, this.GetMockClientContext());

            Assert.AreEqual(newLimiter.CurrentCount, 1);

            List <Task <TransactionalBatchOperationResult> > contexts = new List <Task <TransactionalBatchOperationResult> >(100);

            for (int i = 0; i < 600; i++)
            {
                ItemBatchOperation        operation = new ItemBatchOperation(OperationType.Create, i, Cosmos.PartitionKey.Null, i.ToString());
                ItemBatchOperationContext context   = AttachContext(operation);
                batchAsyncStreamer.Add(operation);
                contexts.Add(context.OperationTask);
            }

            // 300 batch request should atleast sum up to 1000 ms barrier with wait time of 20ms in executor
            await Task.WhenAll(contexts);

            await Task.Delay(2000);

            Assert.IsTrue(newLimiter.CurrentCount >= 2, "Count of threads that can enter into semaphore should increase atleast by 1");
        }
        public async Task DispatchesAsync()
        {
            // Expect all operations to complete as their batches get dispached
            BatchAsyncStreamer batchAsyncStreamer        = new BatchAsyncStreamer(2, MaxBatchByteSize, DispatchTimerInSeconds, this.TimerPool, new CosmosJsonDotNetSerializer(), this.Executor, this.Retrier);
            List <Task <BatchOperationResult> > contexts = new List <Task <BatchOperationResult> >(10);

            for (int i = 0; i < 10; i++)
            {
                ItemBatchOperation        operation = new ItemBatchOperation(OperationType.Create, i, i.ToString());
                ItemBatchOperationContext context   = AttachContext(operation);
                batchAsyncStreamer.Add(operation);
                contexts.Add(context.OperationTask);
            }

            await Task.WhenAll(contexts);

            for (int i = 0; i < 10; i++)
            {
                Task <BatchOperationResult> context = contexts[i];
                Assert.AreEqual(TaskStatus.RanToCompletion, context.Status);
                BatchOperationResult result = await context;
                Assert.AreEqual(i.ToString(), result.ETag);
            }
        }