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 FakePublisherServiceApiClient(scheduler, taskHelper, TimeSpan.FromSeconds(1)); var settings = MakeSettings(scheduler, batchElementCountThreshold: 2, batchRequestByteThreshold: 1000); int shutdownCount = 0; var pub = new PublisherClientImpl(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 OneMessage( [CombinatorialValues(false, true)] bool hardStop) { var topicName = new TopicName("FakeProject", "FakeTopic"); var scheduler = new TestScheduler(); TaskHelper taskHelper = scheduler.TaskHelper; var client = new FakePublisherServiceApiClient(scheduler, taskHelper); var settings = MakeSettings(scheduler); int shutdownCount = 0; var pub = new PublisherClientImpl(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); }); }
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 FakePublisherServiceApiClient(scheduler, taskHelper)).ToArray(); var settings = MakeSettings(scheduler, batchElementCountThreshold: batchElementCountThreshold, batchRequestByteThreshold: 10000); int shutdownCount = 0; var pub = new PublisherClientImpl(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); }); }