示例#1
0
            public async Task ShouldUpdateTheNewBucketsRemoteIdIfNoExistingBucketWithRemoteId(
                string remoteId,
                Bucket bucket,
                Bucket existingBucket,
                [Frozen] IBucketRepository repository,
                [Target] DefaultBucketService service,
                CancellationToken cancellationToken
                )
            {
                bucket.RemoteId            = null;
                bucket.Endpoints           = (long)ChannelEndpoint.CreateMessage;
                bucket.ApiCategory         = 'c';
                existingBucket.Endpoints   = (long)ChannelEndpoint.DeleteMessage;
                existingBucket.ApiCategory = 'c';

                var httpResponse = new HttpResponseMessage();

                httpResponse.Headers.Add("x-ratelimit-bucket", new[] { remoteId });

                repository.FindByRemoteIdAndMajorParameters(Any <string>(), Any <MajorParameters>(), Any <CancellationToken>()).Returns((Bucket)null !);
                var result = await service.MergeBucketIds(bucket, httpResponse, cancellationToken);

                result.Should().Be(bucket);
                result.RemoteId.Should().Be(remoteId);
                await repository.Received().FindByRemoteIdAndMajorParameters(Is(remoteId), Is(bucket.MajorParameters), Is(cancellationToken));
            }
        /// <inheritdoc />
        public async Task <Bucket> MergeBucketIds(Bucket bucket, HttpResponseMessage response, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (!response.Headers.TryGetValues(RateLimitBucketHeader, out var headerValues))
            {
                logger.LogWarning($"Response did not have {RateLimitBucketHeader} header.");
                return(bucket);
            }

            var remoteId = headerValues.First();

            if (bucket.RemoteId != null)
            {
                bucket.RemoteId = remoteId;
                return(bucket);
            }

            var existingBucket = await repository.FindByRemoteIdAndMajorParameters(remoteId, bucket.MajorParameters, cancellationToken);

            if (existingBucket == null)
            {
                bucket.RemoteId = remoteId;
                return(bucket);
            }

            await repository.Remove(bucket, cancellationToken);

            existingBucket.Endpoints |= bucket.Endpoints;
            return(existingBucket);
        }