Ejemplo n.º 1
0
            public async Task ShouldNotThrowIfNotCancelled(
                Request request,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                Func <Task> func = () => service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await func.Should().NotThrowAsync <OperationCanceledException>();
            }
Ejemplo n.º 2
0
            public async Task ShouldFetchBucketFromTheRepository(
                Request request,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await repository.Received().FindByEndpointAndMajorParameters(Is(request.Endpoint), Is((MajorParameters)request.Parameters), Is(cancellationToken));
            }
Ejemplo n.º 3
0
            public async Task ShouldJitter(
                Request request,
                [Frozen, Substitute] ITimerFactory timerFactory,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await timerFactory.Received().CreateJitter(Is(cancellationToken));
            }
Ejemplo n.º 4
0
            public async Task ShouldThrowIfCancelled(
                Request request,
                [Target] DefaultBucketService service
                )
            {
                var cancellationToken = new CancellationToken(true);

                Func <Task> func = () => service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await func.Should().ThrowAsync <OperationCanceledException>();
            }
Ejemplo n.º 5
0
            public async Task ShouldReturnTheBucket(
                Request request,
                [Frozen] Bucket bucket,
                [Frozen, Substitute] ITimerFactory timerFactory,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                var result = await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                result.Should().Be(bucket);
            }
Ejemplo n.º 6
0
            public async Task ShouldNotDecreaseTheNumberOfHitsByOneIfHitsRemainingIsZero(
                Request request,
                [Frozen] Bucket bucket,
                [Frozen, Substitute] ITimerFactory timerFactory,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                bucket.HitsRemaining = 0;
                var result = await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                result.HitsRemaining.Should().Be(0);
            }
Ejemplo n.º 7
0
            public async Task ShouldNotDelayIfHitsRemainingIsNotZero(
                Request request,
                [Frozen] Bucket bucket,
                [Frozen, Substitute] ITimerFactory timerFactory,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                bucket.HitsRemaining = 1;
                await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await timerFactory.DidNotReceive().CreateDelay(Is((int)Math.Ceiling((bucket.ResetAfter - DateTimeOffset.Now).TotalMilliseconds)), Is(cancellationToken));
            }
Ejemplo n.º 8
0
            public async Task ShouldDelayUntilResetAfterIsReachedIfHitsRemainingIsZero(
                int secondsToWait,
                Request request,
                [Frozen] Bucket bucket,
                [Frozen, Substitute] ITimerFactory timerFactory,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                bucket.HitsRemaining = 0;
                bucket.ResetAfter    = DateTimeOffset.Now + TimeSpan.FromSeconds(secondsToWait);
                await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await timerFactory.Received().CreateDelay(Is <int>(ms => ms >= (secondsToWait * 1000) - 20), Is(cancellationToken));
            }
Ejemplo n.º 9
0
            public async Task ShouldNotCreateNewBucketIfOneWasFound(
                Request request,
                [Frozen] Bucket bucket,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await repository.DidNotReceive().Add(
                    Is <Bucket>(bucket =>
                                bucket.Endpoints == Convert.ToUInt64(request.Endpoint.Value) &&
                                bucket.ApiCategory == request.Endpoint.Category &&
                                bucket.MajorParameters == request.Parameters &&
                                bucket.HitsRemaining == 1
                                ),
                    Is(cancellationToken)
                    );
            }
Ejemplo n.º 10
0
            public async Task ShouldCreateNewBucketIfOneWasntFound(
                Request request,
                [Frozen, Substitute] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                var resetAfter = DateTimeOffset.Now;

                repository.FindByEndpointAndMajorParameters(Any <Endpoint>(), Any <MajorParameters>(), Any <CancellationToken>()).Returns((Bucket?)null);
                await service.GetBucketAndWaitForAvailability(request, cancellationToken);

                await repository.Received().Add(
                    Is <Bucket>(bucket =>
                                bucket.Endpoints == Convert.ToUInt64(request.Endpoint.Value) &&
                                bucket.ApiCategory == request.Endpoint.Category &&
                                bucket.MajorParameters == request.Parameters &&
                                bucket.HitsRemaining == 1 &&
                                bucket.ResetAfter >= resetAfter
                                ),
                    Is(cancellationToken)
                    );
            }