public void Shutdown(
     [CombinatorialValues(false, true)] bool hardStop)
 {
     var topicName = new TopicName("FakeProject", "FakeTopic");
     var scheduler = new TestScheduler();
     TaskHelper taskHelper = scheduler.TaskHelper;
     var client = new FakePublisher(scheduler, taskHelper, TimeSpan.FromSeconds(1));
     var settings = MakeSettings(scheduler, batchElementCountThreshold: 2, batchRequestByteThreshold: 1000);
     int shutdownCount = 0;
     var pub = new SimplePublisherImpl(topicName, new[] { client }, settings, () =>
     {
         Interlocked.Increment(ref shutdownCount);
         return Task.FromResult(0);
     }, taskHelper);
     scheduler.Run(async () =>
     {
         var pubTask = pub.PublishAsync("1");
         var isCancelled = await taskHelper.ConfigureAwaitHideCancellation(
             () => pub.ShutdownAsync(new CancellationToken(hardStop)));
         var pubResult = await taskHelper.ConfigureAwaitHideCancellation(() => pubTask, null);
         Assert.Equal(hardStop, pubTask.IsCanceled);
         Assert.Equal(hardStop, isCancelled);
         Assert.Equal(hardStop ? null : "1", pubResult);
         Assert.Equal(hardStop ? 0 : 1, client.HandledMessages.Count);
         Assert.Equal(1, shutdownCount);
     });
 }
 public void ManyMessages(
     [CombinatorialValues(false, true)] bool hardStop,
     [CombinatorialValues(1, 2, 5, 7)] int clientCount,
     [CombinatorialValues(1, 2, 6, 13)] int threadCount,
     [CombinatorialValues(101, 2000, 9999)] int messageCount,
     [CombinatorialValues(1, 5, 50)] int batchElementCountThreshold)
 {
     var topicName = new TopicName("FakeProject", "FakeTopic");
     var scheduler = new TestScheduler(threadCount);
     TaskHelper taskHelper = scheduler.TaskHelper;
     var clients = Enumerable.Range(0, clientCount).Select(_ => new FakePublisher(scheduler, taskHelper)).ToArray();
     var settings = MakeSettings(scheduler, batchElementCountThreshold: batchElementCountThreshold, batchRequestByteThreshold: 10000);
     int shutdownCount = 0;
     var pub = new SimplePublisherImpl(topicName, clients, settings, () =>
     {
         Interlocked.Increment(ref shutdownCount);
         return Task.FromResult(0);
     }, taskHelper);
     scheduler.Run(async () =>
     {
         var tasks = Enumerable.Range(0, messageCount).Select(i => pub.PublishAsync(i.ToString())).ToArray();
         var ids = new HashSet<string>(await taskHelper.ConfigureAwait(taskHelper.WhenAll(tasks)));
         var isCancelled = await taskHelper.ConfigureAwaitHideCancellation(
             () => pub.ShutdownAsync(new CancellationToken(hardStop)));
         Assert.Equal(hardStop, isCancelled);
         var expected = new HashSet<string>(Enumerable.Range(0, messageCount).Select(x => x.ToString()));
         Assert.Equal(expected, ids);
         Assert.Equal(1, shutdownCount);
     });
 }
 public void FlowState()
 {
     var topicName = new TopicName("FakeProject", "FakeTopic");
     var scheduler = new TestScheduler();
     TaskHelper taskHelper = scheduler.TaskHelper;
     var client = new FakePublisher(scheduler, taskHelper, TimeSpan.FromSeconds(1));
     var settings = MakeSettings(scheduler);
     var pub = new SimplePublisherImpl(topicName, new[] { client }, settings, null, taskHelper);
     var msgSize = new PubsubMessage { Data = ByteString.CopyFromUtf8("1") }.CalculateSize();
     scheduler.Run(async () =>
     {
         // Publish 2 msgs; 1st will be immediately sent, 2nd will stay in queue for 1 second.
         var pubTask = Task.WhenAll(pub.PublishAsync("1"), pub.PublishAsync("2"));
         Assert.Equal(1, pub.GetCurrentFlowState().ElementCount);
         Assert.Equal(msgSize, pub.GetCurrentFlowState().ByteCount);
         await taskHelper.ConfigureAwait(pubTask);
         Assert.Equal(0, pub.GetCurrentFlowState().ElementCount);
         Assert.Equal(0, pub.GetCurrentFlowState().ByteCount);
     });
 }
 public void OneMessage(
     [CombinatorialValues(false, true)] bool hardStop)
 {
     var topicName = new TopicName("FakeProject", "FakeTopic");
     var scheduler = new TestScheduler();
     TaskHelper taskHelper = scheduler.TaskHelper;
     var client = new FakePublisher(scheduler, taskHelper);
     var settings = MakeSettings(scheduler);
     int shutdownCount = 0;
     var pub = new SimplePublisherImpl(topicName, new[] { client }, settings, () =>
     {
         Interlocked.Increment(ref shutdownCount);
         return Task.FromResult(0);
     }, taskHelper);
     scheduler.Run(async () =>
     {
         var id = await taskHelper.ConfigureAwait(pub.PublishAsync("1"));
         bool isCancelled = await taskHelper.ConfigureAwaitHideCancellation(
             () => pub.ShutdownAsync(new CancellationToken(hardStop)));
         Assert.Equal(hardStop, isCancelled);
         Assert.Equal("1", id);
         Assert.Equal(1, shutdownCount);
     });
 }