示例#1
0
        public async Task ExecuteShouldCallOnCancelledWhenHostIsTerminating()
        {
            //Arrange
            using var startedEvent = new ManualResetEvent(false);
            var attempt       = new ChallengeAttempt();
            var workerCreator = A.Fake <IWorkerCreator>();
            var worker        = A.Fake <IWorker>();
            var listener      = A.Fake <IChallengeAttemptListener>();

            using var completedEvent = new ManualResetEvent(false);
            A.CallTo(() => workerCreator.CreateWorkerAsync(A <CancellationToken> .Ignored)).Returns(worker);
            A.CallTo(() => worker.ExecuteAsync(attempt, listener, A <CancellationToken> .Ignored)).ReturnsLazily(async args =>
            {
                var cancellationToken = args.GetArgument <CancellationToken>(2);
                while (!cancellationToken.IsCancellationRequested)
                {
                    await Task.Delay(1000, cancellationToken);
                    startedEvent.Set();
                }
                cancellationToken.ThrowIfCancellationRequested();
            });
            using var foreman = new WorkersForeman(workerCreator);

            //Act
            await foreman.StartAsync(CancellationToken.None);

            await foreman.EnqueueWorkAsync(attempt, listener, CancellationToken.None);

            startedEvent.WaitOne(TimeSpan.FromSeconds(5));
            await foreman.StopAsync(CancellationToken.None);

            //Assert
            A.CallTo(() => listener.OnCancelledAsync()).MustHaveHappenedOnceExactly();
        }
示例#2
0
        public async Task ExecuteShouldCallOnFailedForFailedTask()
        {
            //Arrange
            var attempt       = new ChallengeAttempt();
            var workerCreator = A.Fake <IWorkerCreator>();
            var worker        = A.Fake <IWorker>();
            var listener      = A.Fake <IChallengeAttemptListener>();

            using var completedEvent = new ManualResetEvent(false);
            A.CallTo(() => listener.OnCompletedAsync()).ReturnsLazily(() => Task.FromResult(completedEvent.Set()));
            A.CallTo(() => workerCreator.CreateWorkerAsync(A <CancellationToken> .Ignored)).Returns(worker);
            A.CallTo(() => worker.ExecuteAsync(attempt, listener, A <CancellationToken> .Ignored)).Throws <Exception>();
            using var foreman = new WorkersForeman(workerCreator);

            //Act
            await foreman.StartAsync(CancellationToken.None);

            await foreman.EnqueueWorkAsync(attempt, listener, CancellationToken.None);

            completedEvent.WaitOne(TimeSpan.FromSeconds(5));
            await foreman.StopAsync(CancellationToken.None);

            //Assert
            A.CallTo(() => listener.OnFailedAsync(A <Exception> .Ignored)).MustHaveHappenedOnceExactly();
        }
示例#3
0
        public async Task <ChallengeAttempt> CreateAttemptAsync(ChallengeAttempt challengeAttempt)
        {
            challengeAttempt.Status = ChallengeAttemptStatus.Queued;
            var createdAttempt = await _attemptsRepository.CreateAttemptAsync(challengeAttempt);

            await _foreman.EnqueueWorkAsync(createdAttempt, null, CancellationToken.None);

            return(createdAttempt);
        }
示例#4
0
 public Task <ChallengeAttempt> CreateAttemptAsync(ChallengeAttempt challengeAttempt)
 {
     throw new NotImplementedException();
 }
示例#5
0
 public Task ExecuteAsync(ChallengeAttempt challengeAttempt, IChallengeAttemptListener listener, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }