public async Task <double> RunAsync()
        {
            using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            var benchmarkWorker = new BenchmarkWorker();

            return(await _deadManSwitchRunner.RunAsync(benchmarkWorker, _deadManSwitchOptions, cts.Token).ConfigureAwait(false));
        }
        public async Task ShouldHandleNotificationsInParallel(int numberOfNotifications)
        {
            using (var cts = new CancellationTokenSource())
            {
                // Arrange
                var options = new DeadManSwitchOptions {
                    Timeout = TimeSpan.FromSeconds(5), NumberOfNotificationsToKeep = 3
                };
                var worker = Worker(
                    Work(
                        async(deadManSwitch, cancellationToken) =>
                {
                    var sendNotifications = Enumerable.Range(0, numberOfNotifications)
                                            .AsParallel()
                                            .WithDegreeOfParallelism(100)
                                            .Select(i => Task.Run(() => deadManSwitch.Notify("Notification " + i)));
                    await Task.WhenAll(sendNotifications).ConfigureAwait(false);
                }
                        ),
                    Result(Math.PI)
                    );

                // Act
                var result = await _runner.RunAsync(worker, options, cts.Token).ConfigureAwait(false);

                // Assert
                result.Should().Be(Math.PI);
                _sessionFactory.Session.Should().NotBeNull();
                var notifications = _sessionFactory.Session.DeadManSwitchContext.Notifications;
                notifications.Should().HaveCount(3);
            }
        }
示例#3
0
        public async Task ShouldLetTaskFinishIfItCompletesImmediately()
        {
            using (var cts = new CancellationTokenSource())
            {
                // Arrange
                var timeout = TimeSpan.FromSeconds(2);
                var worker  = Worker(Work(), Result(Math.PI));
                var options = new DeadManSwitchOptions {
                    Timeout = timeout
                };

                // Act
                var result = await _runner.RunAsync(worker, options, cts.Token).ConfigureAwait(false);

                // Assert
                result.Should().Be(Math.PI);
            }
        }