private static async Task RunSchedulerOnceAndWaitConnectionAttempts()

in DeviceBridgeTests/Services/SubscriptionSchedulerTests.cs [499:535]


        private static async Task RunSchedulerOnceAndWaitConnectionAttempts(SubscriptionScheduler subscriptionScheduler, int? taskCountToAssert = null, int? delayToAssert = null)
        {
            var connectionAttempTasks = new List<Task>();
            System.Threading.Tasks.Fakes.ShimTask.AllInstances.ContinueWithActionOfTaskTaskContinuationOptions = (task, action, options) =>
            {
                connectionAttempTasks.Add(task);
                return ShimsContext.ExecuteWithoutShims(() => task.ContinueWith(action, options));
            };

            // Stop the subscription scheduler at the first call to 'Delay' and capture the connection attempt tasks initialized by the scheduler.
            System.Threading.Tasks.Fakes.ShimTask.DelayInt32 = delay =>
            {
                if (delayToAssert.HasValue)
                {
                    Assert.AreEqual(delayToAssert.Value, delay);
                }

                if (taskCountToAssert.HasValue)
                {
                    Assert.AreEqual(taskCountToAssert.Value, connectionAttempTasks.Count);
                }

                return Task.FromException(new Exception("Cancelled at delay"));
            };

            try
            {
                await subscriptionScheduler.StartSubscriptionSchedulerAsync();
                throw new AssertionException("Expected StartSubscriptionSchedulerAsync to fail");
            }
            catch (Exception e)
            {
                Assert.AreEqual("Cancelled at delay", e.Message);
            }

            await Task.WhenAll(connectionAttempTasks);
        }